Tutorial : Generic Parallel Algorithms

Previous : Introduction Top : Tutorial    Next : Concurrent Containers

Generic Parallel Algorithms

The Structured Parallel Programming book by McCool, Robison and Reinders describes a number of useful parallel patterns. Intel TBB algorithms maps to those patterns as follows:

parallel_for: map
parallel_reduce, parallel_scan: reduce, scan
parallel_do: workpile
parallel_pipeline: pipeline
parallel_invoke, task_group: fork-join
flow_graph: plumbing for reactive and streaming apps

 

Consider map as an example.  The following serial function applies a function Foo to each element of an array a.

void SerialApplyFoo( float a[], size_t n ) {
    for( size_t i=0; i!=n; ++i )
        Foo(a[i]);
}

 

An Intel TBB parallel_for can be used to implement this same functionality, but with parallelism enabled.  In the code snippet below, the for loop is replaced by a call to the function parallel_for, which divides up the iterations in to tasks and provides them to the library’s task scheduler for parallel execution. The body of the loop in the code stays the same.

 

#include "tbb/tbb.h"

using namespace tbb;

void ParallelApplyFoo( float a[], size_t n ) {
    tbb::parallel_for( size_t(0), n, [&]( size_t i ) {
        Foo(a[i]);
    } );
}

 

There are a number of resources available to learn more about the generic algorithms in the Intel® Threading Building Blocks library.

 

 

 
Previous : Introduction Top : Tutorial    Next : Concurrent Containers