ParallelAverage

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 );
}

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.