bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
parallel.h
Go to the documentation of this file.
1#ifndef BITSIM_UTILS_CONCURRENCY_PARALLEL_H
2#define BITSIM_UTILS_CONCURRENCY_PARALLEL_H
3
6
7#include <algorithm>
8#include <cstddef>
9#include <cstdint>
10#include <exception>
11#include <future>
12#include <optional>
13#include <type_traits>
14#include <vector>
15
16namespace bitsim
17{
18namespace utils::concurrency
19{
20
37template <typename Index, typename F>
38void parallel_for(Index begin, Index end, F &&f, ThreadPool &pool = default_thread_pool(),
39 Index grain = 1)
40{
41 static_assert(std::is_integral_v<Index>, "parallel_for needs an integral index");
42 if (end <= begin)
43 {
44 return;
45 }
46
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);
51
52 auto run_chunk = [&f, begin, n, n_chunks](std::size_t c)
53 {
54 // chunk c covers [n * c / n_chunks, n * (c + 1) / n_chunks)
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)
58 {
59 f(i);
60 }
61 };
62
63 if (n_chunks <= 1 || pool.is_worker_thread())
64 {
65 for (std::size_t c = 0; c < n_chunks; ++c)
66 {
67 run_chunk(c);
68 }
69 return;
70 }
71
72 std::vector<std::future<void>> pending;
73 pending.reserve(n_chunks - 1);
74 for (std::size_t c = 1; c < n_chunks; ++c)
75 {
76 pending.push_back(pool.submit(run_chunk, c));
77 }
78
79 // the chunks refer to f, so wait for all of them before leaving, even if one throws
80 std::exception_ptr error;
81 try
82 {
83 run_chunk(0);
84 }
85 catch (...)
86 {
87 error = std::current_exception();
88 }
89 for (auto &chunk : pending)
90 {
91 try
92 {
93 chunk.get();
94 }
95 catch (...)
96 {
97 if (!error)
98 {
99 error = std::current_exception();
100 }
101 }
102 }
103 if (error)
104 {
105 std::rethrow_exception(error);
106 }
107}
108
118template <typename F>
119auto parallel_map(std::size_t n, F &&f, ThreadPool &pool = default_thread_pool())
120 -> std::vector<std::invoke_result_t<F &, std::size_t>>
121{
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");
125
126 // optional, so that the result type need not be default constructible
127 std::vector<std::optional<result_type>> slots(n);
129 0, n, [&slots, &f](std::size_t i) { slots[i].emplace(f(i)); }, pool);
130
131 std::vector<result_type> results;
132 results.reserve(n);
133 for (auto &slot : slots)
134 {
135 results.push_back(std::move(*slot));
136 }
137 return results;
138}
139
148inline uint_t task_seed(uint_t base_seed, uint_t task) noexcept
149{
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));
155}
156
157} // namespace utils::concurrency
158} // namespace bitsim
159
160#endif // BITSIM_UTILS_CONCURRENCY_PARALLEL_H
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