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:

  1. Replace occurrences of 0 with the identity element for op.

  2. Replace occurrences of += with op= or its logical equivalent.

  3. Change the name Sum to something more appropriate for op.

The operation is allowed to be noncommutative. For example, op could be matrix multiplication.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.193.232