|
ADS
Algorithms and Data Structures
|
The layer of execution of the parallel algorithms of ADS, beyond the book. More...
Files | |
| file | ADS/parallel/Executor.h |
| The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls \( f( 0 ) , \ldots , f( n - 1 ) \) of a function \( f \) on its threads and returns when all have returned (bulk( n , f )), and tells how many threads it has (concurrency()). | |
Concepts | |
| concept | ADS::Executor |
| the executors, whose bulk( n , f ) runs \( f( 0 ) , \ldots , f( n - 1 ) \) on their threads and returns when they have returned, and whose concurrency() is the number of their threads | |
| concept | ADS::WorkerExecutor |
| the executors that also run long-lived workers: run( p , f ) runs \( f( 0 ) , \ldots , f( p - 1 ) \), at the same time on \( p \) distinct threads unless one returns before the others have begun, and returns when they have returned, and lock_type is a mutex of the executor with the semantics of the named requirement Lockable of the standard (lock(), unlock(), and try_lock(), which takes the lock only if it is free and never waits) | |
Classes | |
| class | ADS::Sequential |
| the executor of the calling thread alone, which makes the calls in their order More... | |
Functions | |
| bool | ADS::isRunCaller (void) |
| true if the running thread is the thread that called a run() of an executor (or a feedbackFarm() [see Farm.h]) which has not returned yet: asked in the call \( f( i ) \) of a worker, it tells whether that worker runs on the caller, e.g., \( f( 0 ) \) on ThreadPool, OpenMPExecutor and Sequential, any index or none on TBBExecutor, none on FastFlowExecutor with \( p > 1 \) (see Long-lived workers and their locks), so that a worker that pins itself to a processor can leave the caller as it found it | |
| template<Executor E, class F, class G> | |
| void | ADS::parallelInvoke (E &ex, F &&f, G &&g) |
| the calls f() and g() in parallel, returning when both have returned | |
| std::pair< std::size_t, std::size_t > | ADS::blocksOf (std::size_t first, std::size_t last, std::size_t grain=kGrain, std::size_t blocks=kBlocks) |
| the blocks of \( [ \mathit{first} , \mathit{last} ) \), with at least \( g \) indices each and at most \( \bar{B} \) of them: their number \( b \), and the size \( s \) of all but the last | |
| template<Executor E, class F> | |
| void | ADS::parallelForBlocks (E &ex, std::size_t first, std::size_t last, F &&f, std::size_t grain=kGrain, std::size_t blocks=kBlocks) |
| the calls f( lo , hi ) on the blocks \( [ \mathit{lo} , \mathit{hi} )
\) of \( [ \mathit{first} , \mathit{last} ) \), in parallel | |
| template<Executor E, class F> | |
| void | ADS::parallelFor (E &ex, std::size_t first, std::size_t last, F &&f, std::size_t grain=kGrain, std::size_t blocks=kBlocks) |
| the calls f( i ) for each \( i \in [ \mathit{first} , \mathit{last} )
\), in parallel | |
| template<Executor E, class T, class F, class Op> | |
| T | ADS::parallelReduce (E &ex, std::size_t first, std::size_t last, T id, F &&f, Op op, std::size_t grain=kGrain, std::size_t blocks=kBlocks) |
| the reduction of \( [ \mathit{first} , \mathit{last} ) \): the results of f( lo , hi ) on the blocks, combined by \( \oplus \) (op) in the order of the blocks, starting from id | |
| template<Executor E, std::random_access_iterator I, std::random_access_iterator O, class T, class Op = std::plus<>> | |
| T | ADS::parallelInclusiveScan (E &ex, I in, std::size_t n, O out, T init, Op op=Op()) |
| the inclusive prefixes \( y[ i ] = \mathit{init} \oplus x[ 0 ] \oplus \cdots \oplus x[ i ] \) of the \( n \) items \( x \) from in, written from out, which may be in; the last prefix (init if \( n = 0 \)) | |
| template<Executor E, std::random_access_iterator I, std::random_access_iterator O, class T, class Op = std::plus<>> | |
| T | ADS::parallelExclusiveScan (E &ex, I in, std::size_t n, O out, T init, Op op=Op()) |
| the exclusive prefixes \( y[ i ] = \mathit{init} \oplus x[ 0 ] \oplus \cdots \oplus x[ i - 1 ]
\) of the \( n \) items \( x \) from in, written from out, which may be in; the sum of all of them with init | |
Variables | |
| constexpr std::size_t | ADS::kBlocks = 64 |
| the largest number of blocks into which the loops and the reductions cut their indices, by default | |
| constexpr std::size_t | ADS::kGrain = 1024 |
| the least number of indices of a block of the loops, by default | |
The layer of execution of the parallel algorithms of ADS, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of parallelism.
|
inlinenodiscard |
the blocks of \( [ \mathit{first} , \mathit{last} ) \), with at least \( g \) indices each and at most \( \bar{B} \) of them: their number \( b \), and the size \( s \) of all but the last
In pseudocode (beyond the book), the block \( i \) being \( [ \mathit{first} + i s , \min\{ \mathit{last} , \mathit{first} + ( i + 1 ) s \} ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{blocksOf}( \mathit{first} , \mathit{last} , g , \bar{B} ) \cr 2 & \quad n \gets \max\{ \mathit{last} - \mathit{first} , 0 \} \cr 3 & \quad \textbf{if}\ n = 0\ \textbf{then return}\ ( 0 , 0 ) \cr 4 & \quad s \gets \max\{ g , 1 , \lceil n / \max\{ \bar{B} , 1 \} \rceil \} \cr 5 & \quad \textbf{return}\ ( \lceil n / s \rceil , s ) \end{array}} \]
|
inlinenodiscard |
true if the running thread is the thread that called a run() of an executor (or a feedbackFarm() [see Farm.h]) which has not returned yet: asked in the call \( f( i ) \) of a worker, it tells whether that worker runs on the caller, e.g., \( f( 0 ) \) on ThreadPool, OpenMPExecutor and Sequential, any index or none on TBBExecutor, none on FastFlowExecutor with \( p > 1 \) (see Long-lived workers and their locks), so that a worker that pins itself to a processor can leave the caller as it found it
| T ADS::parallelExclusiveScan | ( | E & | ex, |
| I | in, | ||
| std::size_t | n, | ||
| O | out, | ||
| T | init, | ||
| Op | op = Op() ) |
the exclusive prefixes \( y[ i ] = \mathit{init} \oplus x[ 0 ] \oplus \cdots \oplus x[ i - 1 ] \) of the \( n \) items \( x \) from in, written from out, which may be in; the sum of all of them with init
In pseudocode (beyond the book), that of parallelInclusiveScan() with its line 16 writing each prefix before it adds the item:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 16 & \qquad \textbf{for}\ i \gets \mathit{lo}_c\ \textbf{to} \ \mathit{hi}_c - 1\ \textbf{do}\ y[ i ] \gets e;\ e \gets e \oplus x[ i ] \end{array}} \]
| void ADS::parallelFor | ( | E & | ex, |
| std::size_t | first, | ||
| std::size_t | last, | ||
| F && | f, | ||
| std::size_t | grain = kGrain, | ||
| std::size_t | blocks = kBlocks ) |
the calls f( i ) for each \( i \in [ \mathit{first} , \mathit{last} ) \), in parallel
In pseudocode (beyond the book), a loop on each block of parallelForBlocks():
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{parallelFor}( \mathit{ex} , \mathit{first} , \mathit{last} , f , g , \bar{B} ) \cr 2 & \quad \mathrm{parallelForBlocks}( \mathit{ex} , \mathit{first} , \mathit{last} , ( \mathit{lo} , \mathit{hi} ) \mapsto \textbf{for}\ i \gets \mathit{lo}\ \textbf{to}\ \mathit{hi} - 1\ \textbf{do}\ f( i ) , g , \bar{B} ) \end{array}} \]
| void ADS::parallelForBlocks | ( | E & | ex, |
| std::size_t | first, | ||
| std::size_t | last, | ||
| F && | f, | ||
| std::size_t | grain = kGrain, | ||
| std::size_t | blocks = kBlocks ) |
the calls f( lo , hi ) on the blocks \( [ \mathit{lo} , \mathit{hi} ) \) of \( [ \mathit{first} , \mathit{last} ) \), in parallel
In pseudocode (the dynamic schedule, beyond the book), with \( [ \mathit{lo}_i , \mathit{hi}_i ) \) the block \( i \) of blocksOf():
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{parallelForBlocks}( \mathit{ex} , \mathit{first} , \mathit{last} , f , g , \bar{B} ) \cr 2 & \quad ( b , s ) \gets \mathrm{blocksOf}( \mathit{first} , \mathit{last} , g , \bar{B} ) \cr 3 & \quad \textbf{if}\ b = 1\ \textbf{or} \ \mathit{ex}.\mathrm{concurrency}() = 1\ \textbf{then} \cr 4 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ b - 1\ \textbf{do}\ f( \mathit{lo}_i , \mathit{hi}_i ) \cr 5 & \quad \textbf{else}\ \mathit{ex}.\mathrm{bulk}( b , i \mapsto \textbf{if}\ \text{no call has thrown }\ \textbf{then}\ f( \mathit{lo}_i , \mathit{hi}_i ) ) \cr 6 & \quad \text{throw again the first exception of the calls, if any} \end{array}} \]
| T ADS::parallelInclusiveScan | ( | E & | ex, |
| I | in, | ||
| std::size_t | n, | ||
| O | out, | ||
| T | init, | ||
| Op | op = Op() ) |
the inclusive prefixes \( y[ i ] = \mathit{init} \oplus x[ 0 ] \oplus \cdots \oplus x[ i ] \) of the \( n \) items \( x \) from in, written from out, which may be in; the last prefix (init if \( n = 0 \))
In pseudocode (the decoupled look-back of Merrill and Garland, beyond the book), with tiles of \( t \) items, the tile \( c \) being \( [ \mathit{lo}_c , \mathit{hi}_c ) \), and the flag of a tile 0, 1 when its aggregate \( A[ c ] \) is published, 2 when its prefix \( P[ c ] \) is:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{parallelInclusiveScan}( \mathit{ex} , x , n , y , \mathit{init} , \oplus ) \cr 2 & \quad t \gets \max\{ 256 , \lfloor 2^{16} / \text{the bytes of an } \text{item} \rfloor \};\ T \gets \lceil n / t \rceil \cr 3 & \quad \textbf{if}\ p = 1\ \textbf{or}\ T \le 1 \ \textbf{then return}\ \text{the scan of } x \text{ on one thread} \cr 4 & \quad \mathit{next} \gets 0;\ \mathit{flag}[ 0 .. T - 1 ] \gets 0 \cr 5 & \quad \text{on each of } \min\{ p , T \} \text{ threads} \ \textbf{loop} \cr 6 & \qquad c \gets \mathrm{fetchAndAdd}( \mathit{next} , 1 ); \ \textbf{if}\ c \ge T\ \textbf{then return} \cr 7 & \qquad a \gets x[ \mathit{lo}_c ] \oplus \cdots \oplus x[ \mathit{hi}_c - 1 ];\ e \gets \mathit{init} \cr 8 & \qquad \textbf{if}\ c > 0\ \textbf{then} \cr 9 & \qquad\quad A[ c ] \gets a;\ \mathit{flag}[ c ] \gets 1;\ e \gets \textbf{nil};\ j \gets c \cr 10 & \qquad\quad \textbf{repeat}\quad \triangleright\ \text{the } \text{look-back} \cr 11 & \qquad\qquad j \gets j - 1;\ \text{wait while } \mathit{flag}[ j ] = 0 \cr 12 & \qquad\qquad v \gets P[ j ]\ \textbf{if}\ \mathit{flag}[ j ] = 2 \ \textbf{else}\ A[ j ] \cr 13 & \qquad\qquad e \gets v \oplus e\quad \triangleright\ v \text{ if } e = \textbf{nil} \cr 14 & \qquad\quad \textbf{until}\ \mathit{flag}[ j ] = 2 \cr 15 & \qquad P[ c ] \gets e \oplus a;\ \mathit{flag}[ c ] \gets 2 \cr 16 & \qquad \textbf{for}\ i \gets \mathit{lo}_c\ \textbf{to} \ \mathit{hi}_c - 1\ \textbf{do}\ e \gets e \oplus x[ i ];\ y[ i ] \gets e \cr 17 & \quad \textbf{return}\ P[ T - 1 ] \end{array}} \]
| void ADS::parallelInvoke | ( | E & | ex, |
| F && | f, | ||
| G && | g ) |
the calls f() and g() in parallel, returning when both have returned
In pseudocode (the fork and join, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{parallelInvoke}( \mathit{ex} , f , g ) \cr 2 & \quad \textbf{if}\ \mathit{ex} \text{ has } \mathrm{invoke} \ \textbf{then}\ \mathit{ex}.\mathrm{invoke}( f , g ) \cr 3 & \quad \textbf{else}\ \mathit{ex}.\mathrm{bulk}( 2 , i \mapsto \textbf{if}\ i = 0\ \textbf{then}\ f()\ \textbf{else}\ g() ) \cr 4 & \quad \text{throw again the first exception of the calls, if any} \end{array}} \]
|
nodiscard |
the reduction of \( [ \mathit{first} , \mathit{last} ) \): the results of f( lo , hi ) on the blocks, combined by \( \oplus \) (op) in the order of the blocks, starting from id
In pseudocode (beyond the book), the same blocks on every executor:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{parallelReduce}( \mathit{ex} , \mathit{first} , \mathit{last} , \mathit{id} , f , \oplus , g , \bar{B} ) \cr 2 & \quad ( b , s ) \gets \mathrm{blocksOf}( \mathit{first} , \mathit{last} , g , \bar{B} ) \cr 3 & \quad \mathrm{parallelFor}( \mathit{ex} , 0 , b , i \mapsto \mathit{part}[ i ] \gets f( \mathit{lo}_i , \mathit{hi}_i ) , 1 , \bar{B} )\quad \triangleright\ \mathit{part}[ i ] \text{ on a line} \text{ of its own} \cr 4 & \quad r \gets \mathit{id} \cr 5 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ b - 1\ \textbf{do}\ r \gets r \oplus \mathit{part}[ i ] \cr 6 & \quad \textbf{return}\ r \end{array}} \]
|
inlineconstexpr |
the largest number of blocks into which the loops and the reductions cut their indices, by default
|
inlineconstexpr |
the least number of indices of a block of the loops, by default