1#ifndef BITSIM_UTILS_CONCURRENCY_PARALLEL_H
2#define BITSIM_UTILS_CONCURRENCY_PARALLEL_H
37template <
typename Index,
typename F>
41 static_assert(std::is_integral_v<Index>,
"parallel_for needs an integral index");
47 const auto n =
static_cast<std::size_t
>(end - begin);
48 const auto min_chunk =
static_cast<std::size_t
>(std::max<Index>(grain, 1));
49 const std::size_t max_chunks =
static_cast<std::size_t
>(pool.size()) + 1;
50 const std::size_t n_chunks = std::min(max_chunks, (n + min_chunk - 1) / min_chunk);
52 auto run_chunk = [&f, begin, n, n_chunks](std::size_t c)
55 const auto first = begin +
static_cast<Index
>(n * c / n_chunks);
56 const auto last = begin +
static_cast<Index
>(n * (c + 1) / n_chunks);
57 for (Index i = first; i < last; ++i)
63 if (n_chunks <= 1 || pool.is_worker_thread())
65 for (std::size_t c = 0; c < n_chunks; ++c)
72 std::vector<std::future<void>> pending;
73 pending.reserve(n_chunks - 1);
74 for (std::size_t c = 1; c < n_chunks; ++c)
76 pending.push_back(pool.submit(run_chunk, c));
80 std::exception_ptr error;
87 error = std::current_exception();
89 for (
auto &chunk : pending)
99 error = std::current_exception();
105 std::rethrow_exception(error);
120 -> std::vector<std::invoke_result_t<F &, std::size_t>>
122 using result_type = std::invoke_result_t<F &, std::size_t>;
123 static_assert(!std::is_void_v<result_type>,
"parallel_map needs f to return a value; "
124 "use parallel_for for work without a result");
127 std::vector<std::optional<result_type>> slots(n);
129 0, n, [&slots, &f](std::size_t i) { slots[i].emplace(f(i)); }, pool);
131 std::vector<result_type> results;
133 for (
auto &slot : slots)
135 results.push_back(std::move(*slot));
150 std::uint64_t z =
static_cast<std::uint64_t
>(base_seed) +
151 0x9E3779B97F4A7C15ULL * (
static_cast<std::uint64_t
>(task) + 1);
152 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
153 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
154 return static_cast<uint_t>(z ^ (z >> 31));
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
A fixed number of threads that run the tasks given to them, in the order given.
Definition thread_pool.h:33
Definition concurrent_queue.h:15
uint_t task_seed(uint_t base_seed, uint_t task) noexcept
A seed for task task, made from base_seed.
Definition parallel.h:148
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.
Definition parallel.h:119
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.
Definition parallel.h:38
ThreadPool & default_thread_pool()
The pool that parallel_for() and parallel_map() use when given none. It starts ThreadPool::default_n_...
Definition thread_pool.cpp:77
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40