Intel® Advisor User Guide

ID 766448
Date 6/24/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Parallelize Data - Intel® oneAPI Threading Building Blocks (oneTBB) Counted Loops

When tasks are loop iterations, and the iterations are over a range of values that are known before the loop starts, the loop is easily expressed in Intel® oneAPI Threading Building Blocks (oneTBB) .

Consider the following serial code and the need to add parallelism to this loop:

    ANNOTATE_SITE_BEGIN(sitename);
        for (int i = lo; i < hi; ++i) {
            ANNOTATE__ITERATION_TASK(taskname);
                statement;
        }
    ANNOTATE_SITE_END();

Here is the serial example converted to use oneTBB , after you remove the Intel Advisor annotations:

#include <tbb/tbb.h>
    ...
    tbb::parallel_for( lo, hi, 
        [&](int i) {statement;}
    );

The first two parameters are the loop bounds. As is typical in C++ (especially STL) programming, the lower bound is inclusive and the upper bound is exclusive. The third parameter is the loop body, wrapped in a lambda expression. The loop body will be called in parallel by threads created by oneTBB . As described before in Create the Tasks, Using C++ structs Instead of Lambda Expressions, the lambda expressions can be replaced with instances of explicitly defined class objects.