127#ifdef SUBPAR_CUSTOM_PARALLELIZE_RANGE
128 if constexpr(nothrow_) {
129#ifdef SUBPAR_CUSTOM_PARALLELIZE_RANGE_NOTHROW
130 SUBPAR_CUSTOM_PARALLELIZE_RANGE_NOTHROW(num_workers, num_tasks, run_task_range);
132 SUBPAR_CUSTOM_PARALLELIZE_RANGE(num_workers, num_tasks, run_task_range);
135 SUBPAR_CUSTOM_PARALLELIZE_RANGE(num_workers, num_tasks, run_task_range);
139 if (num_tasks == 0) {
143 if (num_workers <= 1 || num_tasks == 1) {
144 run_task_range(0, 0, num_tasks);
149 Task_ tasks_per_worker;
151 if (internal::ge(num_workers, num_tasks)) {
152 num_workers = num_tasks;
153 tasks_per_worker = 1;
156 tasks_per_worker = num_tasks / num_workers;
157 remainder = num_tasks % num_workers;
161 typename std::conditional<nothrow_, int, std::vector<std::exception_ptr> >::type errors(num_workers);
163#if defined(_OPENMP) && !defined(SUBPAR_NO_OPENMP_RANGE) && !defined(SUBPAR_NO_OPENMP)
164#define SUBPAR_USES_OPENMP 1
165#define SUBPAR_USES_OPENMP_RANGE 1
169 #pragma omp parallel for num_threads(num_workers)
170 for (
int w = 0; w < num_workers; ++w) {
171 Task_ start = w * tasks_per_worker + (w < remainder ? w : remainder);
172 Task_ length = tasks_per_worker + (w < remainder);
174 if constexpr(nothrow_) {
175 run_task_range(w, start, length);
178 run_task_range(w, start, length);
180 errors[w] = std::current_exception();
187#undef SUBPAR_USES_OPENMP
188#undef SUBPAR_USES_OPENMP_RANGE
191 std::vector<std::thread> workers;
192 workers.reserve(num_workers);
194 for (
int w = 0; w < num_workers; ++w) {
195 Task_ length = tasks_per_worker + (w < remainder);
197 if constexpr(nothrow_) {
198 workers.emplace_back(run_task_range, w, start, length);
200 workers.emplace_back([&run_task_range,&errors](
int w, Task_ start, Task_ length) ->
void {
202 run_task_range(w, start, length);
204 errors[w] = std::current_exception();
206 }, w, start, length);
212 for (
auto& wrk : workers) {
217 if constexpr(!nothrow_) {
218 for (
const auto& e : errors) {
220 std::rethrow_exception(e);
void parallelize_range(int num_workers, Task_ num_tasks, Run_ run_task_range)
Parallelize a range of tasks across multiple workers.
Definition range.hpp:126