|
| | thread_pool (unsigned n) |
| | Construct a pool with n total logical workers (n-1 background threads + the caller).
|
| unsigned | size () const noexcept |
| | Total logical workers, including the calling thread (>= 1).
|
| template<typename Fn> |
| void | run (unsigned count, Fn &&fn) |
| | Run task(tid) for tid in [0, count); see the contract at the top of this header.
|
| template<typename Body> |
| void | parallel_for (std::size_t n, std::size_t grain, Body &&body) |
| | Partition [0, n) into contiguous chunks across the pool and run body(begin, end) per chunk.
|
| template<typename Body> |
| void | parallel_for (std::size_t n, std::size_t grain, unsigned max_threads, Body &&body) |
| | parallel_for with the team capped at max_threads (0: no cap, the whole pool), for a caller that runs at a thread count of its own below size().
|
| template<typename T, typename MapFn> |
| T | parallel_reduce (std::size_t n, std::size_t grain, MapFn &&map) |
| | Reduce over [0, n): map(begin, end) returns a partial T; partials are combined with operator+ in chunk (tid) order.
|
| template<typename T, typename MapFn> |
| T | parallel_reduce (std::size_t n, std::size_t grain, unsigned max_threads, MapFn &&map) |
| | parallel_reduce with the team capped at max_threads (0: no cap).
|
| template<typename Acc, typename InitFn, typename MapFn, typename CombineFn> |
| Acc | parallel_accumulate (std::size_t n, std::size_t grain, InitFn &&init, MapFn &&map, CombineFn &&combine) |
| | Reduce over [0, n) into accumulator partials that are combined by the caller's combine, not by + on a result type (#582).
|
| template<typename Acc, typename InitFn, typename MapFn, typename CombineFn> |
| Acc | parallel_accumulate (std::size_t n, std::size_t grain, unsigned max_threads, InitFn &&init, MapFn &&map, CombineFn &&combine) |
| | parallel_accumulate with the team capped at max_threads (0: no cap).
|
| | thread_pool (const thread_pool &)=delete |
| thread_pool & | operator= (const thread_pool &)=delete |
| | ~thread_pool () |
| mtl::parallel::pool_local_noexc::thread_pool::thread_pool |
( |
unsigned | n | ) |
|
|
inlineexplicit |
Construct a pool with n total logical workers (n-1 background threads + the caller).
n < 1 is treated as 1 (serial).
If a worker can't be created, the constructor stops and joins the workers it started and rethrows the std::system_error. It used to unwind with them still waiting on wake_, which was destroyed before workers_: pthread_cond_destroy blocked, and the process hung (thread_pool(2000) under ulimit -v 400000, issue 876). n is not capped: a pool may deliberately hold more threads than cores.
template<typename Acc, typename InitFn, typename MapFn, typename CombineFn>
| Acc mtl::parallel::pool_local_noexc::thread_pool::parallel_accumulate |
( |
std::size_t | n, |
|
|
std::size_t | grain, |
|
|
InitFn && | init, |
|
|
MapFn && | map, |
|
|
CombineFn && | combine ) |
|
inline |
Reduce over [0, n) into accumulator partials that are combined by the caller's combine, not by + on a result type (#582).
init() returns an empty accumulator; map(acc, begin, end) adds one chunk's terms to it; combine(into, from) folds a partial into another. The chunking is parallel_for's: contiguous, deterministic, capped so each chunk holds >= grain units. Partials are folded in tid order, starting from tid 0's.
Serially (one thread, or n too small to split) this is exactly acc = init(); map(acc, 0, n);, so a caller's single-threaded result is unchanged. Threaded, the result equals the serial one whenever combine and map are exact – a super-accumulator such as a quire – whatever the thread count. For a rounding accumulator it is deterministic for a fixed thread count but regrouped, as parallel_reduce is.
Allocates: a threaded call holds its partials in a std::vector<Acc>.
template<typename Body>
| void mtl::parallel::pool_local_noexc::thread_pool::parallel_for |
( |
std::size_t | n, |
|
|
std::size_t | grain, |
|
|
Body && | body ) |
|
inline |
Partition [0, n) into contiguous chunks across the pool and run body(begin, end) per chunk.
Runs serially (a single body(0, n)) when the pool is serial or n is too small to amortize the handoff; otherwise caps the team so each chunk holds >= grain iteration units. Contiguous, deterministic chunking -> element-wise callers stay bit-identical across thread counts (each output element is produced by exactly one chunk). Does not allocate.
template<typename T, typename MapFn>
| T mtl::parallel::pool_local_noexc::thread_pool::parallel_reduce |
( |
std::size_t | n, |
|
|
std::size_t | grain, |
|
|
MapFn && | map ) |
|
inline |
Reduce over [0, n): map(begin, end) returns a partial T; partials are combined with operator+ in chunk (tid) order.
Runs serially (a single map(0, n)) when the pool is serial or n is below the grain.
Deterministic for a fixed thread count, but NOT bit-identical to the serial reduction – chunking changes the summation grouping/associativity. Callers that need serial-exact results must not rely on this.
Allocates: a threaded call holds its partials in a std::vector<T>.
How long a waiting thread spins before it sleeps on the condition variable, in microseconds (#805): a worker waiting for the next region, and the caller waiting for the region's last tid.
It has to cover the serial work a kernel does between regions: on the Ventuno Q at 4 threads, 50 us left LU at 76 ms (22 ms on one thread), 200 us fixed LU, and QR, LQ and Hessenberg needed 1000 us, which made them 1.5-1.6x faster than one thread where they had been 1.5-2.2x slower. 3000 us gained nothing more. The cost is up to spin_us of a core per worker after a burst of regions ends. A region that starts or finishes after the spin costs what it did before: one condition-variable wake.