July 2007
Intermediate to advanced
332 pages
10h 4m
English
ParallelAverage defines a routine named ParallelAverage that sets output[i] to the average of input[i-1], input[i], and input[i+1], for 0 ≤ i < n .
Example 11-1. ParallelAverage
#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"
using namespace tbb;
class Average {
public:
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, 1000 ), avg );
}