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

The pool of threads of ADS, beyond the book: the executor [see Executor.h] of the parallel algorithms on std::thread, built on the concurrent structures of ADS. More...

#include "ADS/Contract.h"
#include "ADS/concurrent/EventCount.h"
#include "ADS/concurrent/WorkStealingDeque.h"
#include "ADS/parallel/Executor.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "ADS/parallel/impl/ThreadPool.tpp"

Go to the source code of this file.

Classes

class  ADS::ThreadPool
 a pool of threads with work stealing, an executor More...

Namespaces

namespace  ADS

Detailed description

The pool of threads of ADS, beyond the book: the executor [see Executor.h] of the parallel algorithms on std::thread, built on the concurrent structures of ADS.

A pool of \( p \) threads starts \( p - 1 \) of them, the workers; the thread that calls it is the \( p \)-th, which works as they do until its call returns. Each of the \( p \) has a deque of work stealing of Chase and Lev [see WorkStealingDeque.h], on which it pushes the tasks that the others may take: it takes them back from the bottom, the others steal them from the top (the work-first principle of Cilk, which leaves the oldest, and usually largest, tasks to the thieves). A worker that finds no task in its deque tries to steal one from the others, starting at a random one; if all are empty it looks again for a while, its budget, and then sleeps on an event count [see EventCount.h], which wakes it when a task is pushed and costs the pusher a light fence and the read of a line when nobody sleeps. A worker woken from its sleep arrives 10 to 50 microseconds late, and on a core that the governor of the frequency may have slowed down meanwhile, so that the next parallel phase runs up to twice as slow; a worker that looks for long instead burns its core while the program runs sequentially. The budget therefore follows the gaps between the parallel phases that each worker sees, a gap being the time from the moment it found nothing to the one it found a task: it is twice the longest gap of the current window of \( 5 S \) and of the one before, kept between the parameters \( s \le S \) of the pool (2 ms and 200 ms by default, the second that of the runtimes of OpenMP that look for longest), and it goes back to \( s \) after a gap longer than \( S \). Thus the workers of a program that calls the pool at a steady pace stay awake across its sequential steps, while those of a program that went sequential sleep after at most \( S \), and after \( s \) from then on; all this is local to the worker, and costs the callers nothing. While it looks, a worker pauses ever longer between the looks, up to 64 pauses of the processor, and beyond \( s \) yields its core between them to the other threads of the machine.

bulk( n , f ) puts a job on the stack of the caller: a counter of the calls \( f( 0 ) , \ldots , f( n - 1 ) \) not yet claimed, and \( h = \min\{ p - 1 , n - 1 \} \) tokens of the job pushed on the deque of the caller. Every thread that runs a token (a thief, or the caller itself) claims calls from the counter, by a fetch-and-add, until none is left, and then gives the token back by decrementing a count of the tokens out; the caller claims calls as well, then takes back from the bottom of its deque the tokens nobody stole and waits for the count to reach 0, running in the meantime the tasks it steals, so that a call nested in \( f( i ) \) on another thread is helped. Since a thread pushes and takes back its tokens in the same call, the deques hold only the tokens of jobs still running, and a job never outlives the frame that holds it: no task is allocated. invoke( f , g ) pushes \( g \) as a task on the deque of the caller and runs \( f \); then it takes \( g \) back and runs it, unless a thief stole it, in which case it waits for \( g \) while stealing other tasks (the leapfrogging of Wagner and Calder). Calls of bulk() and invoke() nest to any depth, on any thread of the pool; a thread outside the pool takes the place of the \( p \)-th, one at a time (the others wait for it on a mutex).

run( p , f ) runs the \( p \) calls of long-lived workers (see Long-lived workers and their locks): the caller runs \( f( 0 ) \) (the call in which isRunCaller() holds), and the other calls are claimed one at a time, under a mutex, by the workers that are idle in their loop, never by a thread that waits inside a call and steals (a worker that waited for a nested job and took a call of a worker would hold it under its own, which may wait for it forever). No thread holds two calls at once and every call begins while the others wait, so that calls that do not return before all have begun (e.g., that meet at a barrier) run at the same time on \( p \) distinct threads; a call that returns earlier may leave its worker to claim another call after it. A run is the only one of the pool for its time; while it lasts the workers that are not among its \( p - 1 \) keep running the tasks, the nested jobs of the calls among them.

With work \( W \) and depth \( D \), the work stealing runs in expected time \( W / p + O( D ) \) (Blumofe and Leiserson), and a job of bulk() of \( n \) calls of equal cost \( c \) in at most \( n c / p + c + ( p - 1 ) \sigma / 2 \) after the wake-up, \( \sigma \) the time of a steal, since its \( h \) tokens are stolen one at a time from the top of one deque and each thief claims calls as soon as it holds one; a steal costs one compare-and-swap, a claim one fetch-and-add on the line of the job.

The contracts [see Contract.h]: \( p \ge 1 \); the pool outlives the calls; an exception thrown by \( f \) in bulk() stops the claims and is thrown again by bulk() once the calls already started have returned, the same for \( f \) and \( g \) in invoke(); run( p , f ) needs \( p - 1 \) workers idle besides the caller (all of them if it is called from outside the pool with \( p \) its threads), and throws again the first exception of its calls once all have returned. The operations are defined in ThreadPool.tpp.

See also
D. Chase, Y. Lev, Dynamic circular work-stealing deque, ACM Symposium on Parallelism in Algorithms and Architectures (SPAA), 21-28, 2005, doi:10.1145/1073970.1073974
R. D. Blumofe, C. E. Leiserson, Scheduling multithreaded computations by work stealing, Journal of the ACM 46(5), 720-748, 1999, doi:10.1145/324133.324234
M. Frigo, C. E. Leiserson, K. H. Randall, The implementation of the Cilk-5 multithreaded language, ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI), 212-223, 1998, doi:10.1145/277650.277725
D. B. Wagner, B. G. Calder, Leapfrogging: a portable technique for implementing efficient futures, ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming (PPoPP), 208-217, 1993, doi:10.1145/155332.155354
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa