|
ADS
Algorithms and Data Structures
|
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()). More...
#include "ADS/Contract.h"#include "ADS/concurrent/impl/Spin.tpp"#include <algorithm>#include <atomic>#include <concepts>#include <cstddef>#include <cstdint>#include <exception>#include <iterator>#include <memory>#include <mutex>#include <optional>#include <type_traits>#include <utility>#include <vector>#include "ADS/parallel/impl/Executor.tpp"Go to the source code of this file.
Classes | |
| class | ADS::Sequential |
| the executor of the calling thread alone, which makes the calls in their order More... | |
Namespaces | |
| namespace | ADS |
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) | |
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: 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()).
The parallel algorithms of ADS take one as their first argument, while the sequential ones, which take none, stay what they are. The executors are Sequential, which runs the calls in order on the calling thread, ThreadPool [see ThreadPool.h], the pool of ADS on std::thread with a deque of work stealing per thread, and, when their libraries are there, FastFlowExecutor, OpenMPExecutor and TBBExecutor [see FastFlowExecutor.h, OpenMPExecutor.h and TBBExecutor.h], the same calls on the threads of FastFlow, of OpenMP and of oneTBB. An executor may also have invoke( f , g ), which runs \( f \) and \( g \) in parallel; the others get it from bulk().
The calls of bulk() are tasks: a thread may run several of them, one after the other, so that a call that waits for another one may never see it start. A program of long-lived workers that talk to each other (the workers of a parallel search, each with its own queue and its inbox) needs instead run( p , f ), whose \( p \) calls \( f( 0 ) , \ldots , f( p - 1 ) \) all begin, each on a thread of the executor that holds no other one, and run at the same time on \( p \) distinct threads unless one returns before the others have begun, and a lock for the data that they share, lock_type (the concept WorkerExecutor), which is Lockable (try_lock() as well, for a worker that skips a busy section): Sequential runs one worker, the others as many as they have threads, and isRunCaller() tells a worker whether it runs on the thread that called run() (see Long-lived workers and their locks). The channels between the workers, the farm with feedback and the mailbox with the detection of the termination are built on these [see Channels.h, Farm.h and Mailbox.h].
On these the header builds the patterns that the algorithms share, each a function of the executor:
The blocks depend only on the number of indices, on the grain and on the most blocks \( \bar{B} \) of the call, never on the number of threads: at most \( \bar{B} \) of them (kBlocks = 64 by default), each of at least \( g \) indices. A block is a claim, a fetch-and-add on a shared line, and a partial result of a reduction, on a line of its own: 64 blocks keep the claims few (a sum of \( 10^6 \) integers of 64 bits takes blocks of 125 KB) and still leave 8 blocks to each of 8 threads; a loop whose calls cost very different times, e.g., one on the nodes of a frontier of a graph, passes a larger \( \bar{B} \) (and a smaller grain) for a finer balance. Hence a reduction gives the same result on every executor, Sequential included, also with an operation that is associative only up to rounding, e.g., the sum of doubles; that result is the sum by blocks, not the one of std::accumulate().
The prefix sums are those of Merrill and Garland, written for the processors instead of the graphics cards: the input is cut into tiles of about 64 KB, which the threads claim in increasing order from a counter. A thread reduces its tile \( c \) into its aggregate \( a_c \) (the tile then stays in its cache), publishes it, and looks back at the tiles before it: from \( c - 1 \) down, it adds on the left the aggregate of each tile that has published only that, and stops at the first one that has published its inclusive prefix \( P_{c'} = x_0 \oplus \cdots \oplus x_{\mathrm{end}( c' )} \), so that
\[ P_{c - 1} = P_{c'} \oplus a_{c' + 1} \oplus \cdots \oplus a_{c - 1} ; \]
then it publishes \( P_c = P_{c - 1} \oplus a_c \) and writes the prefixes of its tile, reading it again from the cache. The input is read from the memory once and the output written once, \( 2 n \) moves, where the scan in two passes of Blelloch (the reduction of the blocks, then their scan) reads the input twice, \( 3 n \). A tile waits only for tiles claimed before it, which threads that are running hold; the first tile not done has no tile to wait for, so that the scan always ends, on any executor. The look-back reads \( O( 1 ) \) tiles on average when the threads go at the same pace, and at most \( c \).
The algorithms take \( O( W / p + D ) \) time on \( p \) threads for work \( W \) and depth \( D \), with the dynamic schedule within a factor of 2 of the best when the blocks are many more than the threads (the greedy schedule of Graham); the reduction and the scans have \( W = O( n ) \) and \( D = O( n / B + B ) \) for \( B \) blocks.
The contracts [see Contract.h]: \( f \) may run on any thread, and its calls on distinct indices must not race; an exception thrown by a call stops the claim of new blocks and is thrown again by the algorithm, after the calls already started have returned; \( \oplus \) is associative. The patterns are defined in Executor.tpp.