|
rexlib 0.1.0
Device-agnostic array computing library for CryoEM and CryoET image processing, with a pluggable backend architecture
|
A fixed set of threads a loop's iterations are spread over. More...
#include <rexlib/backends/cpu/thread_pool.hpp>
Public Types | |
| using | body_function = void(*)(const void *context, std::size_t begin, std::size_t end) |
| The type-erased body of a parallel loop. | |
Public Member Functions | |
| thread_pool (std::size_t worker_count) | |
| Construct a pool with a given number of worker threads. | |
| thread_pool (const thread_pool &other)=delete | |
| thread_pool (thread_pool &&other)=delete | |
| thread_pool & | operator= (const thread_pool &other)=delete |
| thread_pool & | operator= (thread_pool &&other)=delete |
| std::size_t | get_size () const noexcept |
| Get the number of threads a job is spread over. | |
| void | run (std::size_t count, std::size_t grain_size, body_function body, const void *context) |
| Run a loop, splitting it across this pool. | |
Static Public Member Functions | |
| static std::size_t | get_default_worker_count () |
| How many workers a pool spawns when nothing else says otherwise. | |
A fixed set of threads a loop's iterations are spread over.
The pool is fork-join and nothing else: run publishes one job, every participant claims chunks of it until there are none left, and the call returns only once every chunk has finished. There is no queue, no future and no way for work to outlive the call, which is what lets a program keep being handed raw pointers into buffers whose lifetime ends when command_queue::submit returns. See rexlib::command.
The calling thread participates. A pool of no workers therefore costs exactly one indirect call over running the loop inline, which is what makes it safe to route every loop of the backend through it rather than only the ones expected to be large.
Work below the grain size is not split at all. The grain is stated by the caller because only the caller knows what one iteration costs: an elementwise iteration is one arithmetic operation, a linalg one is a whole matrix product.
| using rexlib::cpu::thread_pool::body_function = void (*)(const void *context, std::size_t begin, std::size_t end) |
The type-erased body of a parallel loop.
| context | Opaque pointer, handed back untouched. |
| begin | The first iteration this call covers. |
| end | The past-the-end iteration this call covers. |
|
explicit |
Construct a pool with a given number of worker threads.
| worker_count | Threads to spawn, not counting the caller. Zero makes every run serial, which is the shape a test asks for when it wants the parallel path exercised on a single deterministic thread. |
|
static |
How many workers a pool spawns when nothing else says otherwise.
One participant per hardware thread ends up running a loop, and the thread that asked for it is one of them, so this is one fewer than that.
The number may be capped from the environment, through REXLIB_NUM_THREADS. The machine is a run time property, so a compile time knob could not answer this: a user under a batch scheduler or inside a process pool needs to cap the library from outside it, which is what every peer library offers a variable for. A value of one means no worker at all, and so a fully serial library.
|
noexcept |
Get the number of threads a job is spread over.
The workers plus the calling thread, so it is never zero.
| void rexlib::cpu::thread_pool::run | ( | std::size_t | count, |
| std::size_t | grain_size, | ||
| body_function | body, | ||
| const void * | context | ||
| ) |
Run a loop, splitting it across this pool.
The iteration space [0, count) is cut into as many contiguous chunks as there are participants, never into chunks shorter than grain_size, and body is invoked once per chunk. The partition depends only on count, grain_size and get_size, never on timing; which thread runs which chunk does depend on timing.
Runs the whole range inline, on the calling thread, when the pool has no worker, when the work is below one grain, and when a job is already running on this pool. That last case covers both a second host thread submitting at the same time and a body reaching this again: a job holds the pool for the whole of its duration, so a nested call finds it taken and runs itself rather than waiting for threads that are the very ones waiting on it. Nesting is therefore safe without a caller having to know it is nested.
| count | Number of iterations. |
| grain_size | Smallest number of iterations worth handing to a thread of its own. Treated as one when zero. |
| body | The loop body. Invoked concurrently on one and the same object, so any state a chunk needs to itself must be created inside it. |
| context | Passed to body untouched. |
| Whatever | body throws. Every chunk is guarded, the exception raised by the lowest numbered chunk is kept, the chunks not yet claimed are abandoned, and the exception is rethrown here, on the calling thread, once every chunk already running has finished. The pool is usable again afterwards. |