bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitsim::utils::concurrency Namespace Reference

Classes

class  ConcurrentQueue
 A first-in first-out queue that several threads can push to and pop from. More...
class  ThreadPool
 A fixed number of threads that run the tasks given to them, in the order given. More...

Functions

template<typename Index, typename F>
void parallel_for (Index begin, Index end, F &&f, ThreadPool &pool=default_thread_pool(), Index grain=1)
 Call f(i) for every i in [begin, end), spread over the threads of pool.
template<typename F>
auto parallel_map (std::size_t n, F &&f, ThreadPool &pool=default_thread_pool()) -> std::vector< std::invoke_result_t< F &, std::size_t > >
 Compute f(0), f(1), ..., f(n - 1) on the threads of pool.
uint_t task_seed (uint_t base_seed, uint_t task) noexcept
 A seed for task task, made from base_seed.
ThreadPool & default_thread_pool ()
 The pool that parallel_for() and parallel_map() use when given none. It starts ThreadPool::default_n_threads() threads the first time it is used.

Function Documentation

◆ default_thread_pool()

ThreadPool & bitsim::utils::concurrency::default_thread_pool ( )

The pool that parallel_for() and parallel_map() use when given none. It starts ThreadPool::default_n_threads() threads the first time it is used.

◆ parallel_for()

template<typename Index, typename F>
void bitsim::utils::concurrency::parallel_for ( Index begin,
Index end,
F && f,
ThreadPool & pool = default_thread_pool(),
Index grain = 1 )

Call f(i) for every i in [begin, end), spread over the threads of pool.

The range is cut into at most pool.size() + 1 chunks of consecutive indices, and the calling thread runs one of them, so a pool of one thread already uses two. f must be safe to call from several threads at once: each call should write only to what belongs to its index. The function returns when every call has returned; if calls throw, it rethrows the first exception after all chunks are done. Called from a task of pool, it runs the calls in order on the calling thread, since waiting for the pool from inside it could leave no thread to run them.

Parameters
beginthe first index
endone past the last index
fthe work for one index
poolthe threads to use
grainthe fewest indices in a chunk; raise it when f(i) is very cheap

◆ parallel_map()

template<typename F>
auto bitsim::utils::concurrency::parallel_map ( std::size_t n,
F && f,
ThreadPool & pool = default_thread_pool() )->std::vector< std::invoke_result_t< F &, std::size_t > >

Compute f(0), f(1), ..., f(n - 1) on the threads of pool.

The results come back in the order of their index, whatever order the threads finish in, so the output does not depend on the number of threads. A typical use is to run one simulation or one training per seed or per parameter value. The rules of parallel_for() apply.

Returns
the results, results[i] = f(i)

◆ task_seed()

uint_t bitsim::utils::concurrency::task_seed ( uint_t base_seed,
uint_t task )
inlinenoexcept

A seed for task task, made from base_seed.

Tasks that draw random numbers need a generator each, since sharing one between threads is a data race. Seeding them with base_seed + task gives generators whose first numbers are alike; this mixes the two numbers with SplitMix64, so that every task gets an unrelated seed that still depends only on base_seed and task.