ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
TBBExecutor.h File Reference

The executor [see Executor.h] on the threads of oneTBB, beyond the book, compiled only with ADS_WITH_TBB (the CMake option of the same name, OFF by default) and oneTBB on the path: the executor runs in a task_arena, bulk( n , f ) is a parallel_for of oneTBB on the \( n \) indices with the simple partitioner (a task per index, since the patterns of ADS already give it blocks), and invoke( f , g ) is parallel_invoke, both run in the arena, so that the parallel algorithms of ADS run on the scheduler of oneTBB unchanged, and nest as its tasks do. More...

#include "ADS/Contract.h"
#include "ADS/parallel/Executor.h"
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_invoke.h>
#include <tbb/partitioner.h>
#include <tbb/spin_mutex.h>
#include <tbb/task_arena.h>
#include <algorithm>
#include <cstddef>
#include <optional>
#include <thread>
#include <utility>

Go to the source code of this file.

Classes

class  ADS::TBBExecutor
 the executor on an arena of oneTBB More...
struct  ADS::TBBExecutor::Current
 the tag of the executor on the arena of the calling thread More...

Namespaces

namespace  ADS

Detailed description

The executor [see Executor.h] on the threads of oneTBB, beyond the book, compiled only with ADS_WITH_TBB (the CMake option of the same name, OFF by default) and oneTBB on the path: the executor runs in a task_arena, bulk( n , f ) is a parallel_for of oneTBB on the \( n \) indices with the simple partitioner (a task per index, since the patterns of ADS already give it blocks), and invoke( f , g ) is parallel_invoke, both run in the arena, so that the parallel algorithms of ADS run on the scheduler of oneTBB unchanged, and nest as its tasks do.

The arena is one of three: its own, of \( p \) threads (TBBExecutor( p )); one of the program, which the executor refers to without owning it (TBBExecutor( arena )), so that the constraints of that arena (its NUMA node, its type of core, its priority, its number of threads) hold for the calls of ADS too; or the arena of the thread that makes each call (TBBExecutor( TBBExecutor::current )), so that an algorithm of ADS called from inside a loop of oneTBB of the program runs in the arena of that loop, whatever it is. Since the arenas of a process share the workers of oneTBB, none of the three starts threads of its own; the last two add nothing to the calls of oneTBB but execute(). The thread that calls joins the arena and works. run( p , f ) is the same parallel_for on the \( p \) calls of long-lived workers, each made inside this_task_arena::isolate(), so that a thread that waits inside a call (for a job nested in it) takes only the tasks of that job, never the call of another worker, which it would hold under its own; the \( p \) calls then run at the same time as soon as the arena has \( p \) threads free, i.e., when the threads of oneTBB are not fewer, which \( p \) not larger than the hardware threads ensures for an arena of its own (an arena of the program must have them free as well). The caller joins the arena and may make any of the calls or none of them, so that isRunCaller() holds in at most one call, whose index is not known in advance. Its lock is the spin mutex of oneTBB (tbb::spin_mutex), for the short sections of the workers.

The contracts [see Contract.h]: \( p \ge 1 \); an exception thrown by \( f \) is thrown again by bulk(), as oneTBB does, and by run() once all the calls have returned.

See also
J. Reinders, Intel Threading Building Blocks: Outfitting C++ for Multi-core Processor Parallelism, O'Reilly, 2007
M. Voss, R. Asenjo, J. Reinders, Pro TBB: C++ Parallel Programming with Threading Building Blocks, Apress, 2019, doi:10.1007/978-1-4842-4398-5
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa