ParallelSum
Example 11-19 sums the values in an array.
Example 11-19. ParallelSum
#include "tbb/parallel_reduce.h" #include "tbb/blocked_range.h" using namespace tbb; struct Sum { float value; Sum() : value(0) {} Sum( Sum& s, split ) {value = 0;} void operator()( const blocked_range<float*>& range ) { float temp = value; for( float* a=range.begin(); a!=range.end(); ++a ) { temp += *a; } value = temp; } void join( Sum& rhs ) {value += rhs.value;} }; float ParallelSum( float array[], size_t n ) { Sum total; parallel_reduce( blocked_range<float*>( array, array+n, 1000 ), total ); return total.value; }
This example is easily converted to do a reduction for any associative operation op
as follows:
Replace occurrences of 0 with the identity element for
op
.Replace occurrences of += with
op=
or its logical equivalent.Change the name
Sum
to something more appropriate forop
.
The operation is allowed to be noncommutative. For example, op
could be matrix multiplication.
Get Intel Threading Building Blocks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.