parallel_for with partitioner

parallel_for takes an optional third argument to specify a partitioner. See the earlier section “Automatic grain size” for more information.

This example shows a simple use of the partitioner concept with a parallel_for. The code shown in Example 3-8 is an extension of Example 3-6. An auto_partitioner is used to guide the splitting of the range.

Example 3-8. Parallel average with partitioner

#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"

using namespace tbb;

struct Average {
    float* input;
    float* output;
    void operator()( const blocked_range<int>& range ) const {
        for( int i=range.begin(); i!=range.end(); ++i )
            output[i] = (input[i-1]+input[i]+input[i+1])*(1/3.0f);
    }
};
// Note: The input must be padded such that input[-1] and input[n]
// can be used to calculate the first and last output values.
void ParallelAverage( float* output, float* input, size_t n ) {
    Average avg;
    avg.input = input;
    avg.output = output;parallel_for( blocked_range<int>( 0, n ), avg, auto_partitioner() );

Two important changes from Example 3-6 should be noted:

  1. The call to parallel_for takes a third argument, an auto_partitioner object.

  2. The blocked_range constructor is not provided with a grainsize parameter.

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

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