add original source
This commit is contained in:
31
source/listing_9.3.cpp
Normal file
31
source/listing_9.3.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
template<typename Iterator,typename T>
|
||||
T parallel_accumulate(Iterator first,Iterator last,T init)
|
||||
{
|
||||
unsigned long const length=std::distance(first,last);
|
||||
|
||||
if(!length)
|
||||
return init;
|
||||
|
||||
unsigned long const block_size=25;
|
||||
unsigned long const num_blocks=(length+block_size-1)/block_size;
|
||||
|
||||
std::vector<std::future<T> > futures(num_blocks-1);
|
||||
thread_pool pool;
|
||||
|
||||
Iterator block_start=first;
|
||||
for(unsigned long i=0;i<(num_threads-1);++i)
|
||||
{
|
||||
Iterator block_end=block_start;
|
||||
std::advance(block_end,block_size);
|
||||
futures[i]=pool.submit(accumulate_block<Iterator,T>());
|
||||
block_start=block_end;
|
||||
}
|
||||
T last_result=accumulate_block()(block_start,last);
|
||||
T result=init;
|
||||
for(unsigned long i=0;i<(num_blocks-1);++i)
|
||||
{
|
||||
result+=futures[i].get();
|
||||
}
|
||||
result += last_result;
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user