subpar
Substitutable parallelization for C++ libraries
Loading...
Searching...
No Matches
simple.hpp
Go to the documentation of this file.
1#ifndef SUBPAR_SIMPLE_HPP
2#define SUBPAR_SIMPLE_HPP
3
4#include "range.hpp"
5
6#ifndef SUBPAR_CUSTOM_PARALLELIZE_SIMPLE
7#include <vector>
8#include <stdexcept>
9#include <exception>
10#include <thread>
11#include <type_traits>
12#endif
13
19namespace subpar {
20
59template<bool nothrow_ = false, typename Task_, class Run_>
60void parallelize_simple(const Task_ num_tasks, const Run_ run_task) {
61#ifdef SUBPAR_CUSTOM_PARALLELIZE_SIMPLE
62 if constexpr(nothrow_) {
63#ifdef SUBPAR_CUSTOM_PARALLELIZE_SIMPLE_NOTHROW
64 SUBPAR_CUSTOM_PARALLELIZE_SIMPLE_NOTHROW(num_tasks, run_task);
65#else
66 SUBPAR_CUSTOM_PARALLELIZE_SIMPLE(num_tasks, run_task);
67#endif
68 } else {
69 SUBPAR_CUSTOM_PARALLELIZE_SIMPLE(num_tasks, run_task);
70 }
71
72#else
73 if (num_tasks <= 0) {
74 return;
75 } else if (num_tasks == 1) {
76 run_task(0);
77 return;
78 }
79
80 // Avoid instantiating a vector if it is known that the function can't throw.
81 auto errors = [&]{
82 if constexpr(nothrow_) {
83 return true;
84 } else {
85 return sanisizer::create<std::vector<std::exception_ptr> >(num_tasks);
86 }
87 }();
88
89#if defined(_OPENMP) && !defined(SUBPAR_NO_OPENMP_SIMPLE)
90#define SUBPAR_USES_OPENMP_SIMPLE 1
91
92 // OpenMP doesn't guarantee that we'll actually start 'num_tasks' workers,
93 // so we need to do a loop here to ensure that each task is executed.
94 #pragma omp parallel for num_threads(num_tasks)
95 for (Task_ w = 0; w < num_tasks; ++w) {
96 if constexpr(nothrow_) {
97 run_task(w);
98 } else {
99 try {
100 run_task(w);
101 } catch (...) {
102 errors[w] = std::current_exception();
103 }
104 }
105 }
106
107#else
108// Wiping it out, just in case.
109#undef SUBPAR_USES_OPENMP_SIMPLE
110
111 // We run the first job on the current thread, to avoid having to spin up an unnecessary worker.
112 std::vector<std::thread> workers;
113 sanisizer::reserve(workers, num_tasks - 1); // make sure we don't get alloc errors during emplace_back().
114
115 for (Task_ w = 1; w < num_tasks; ++w) {
116 if constexpr(nothrow_) {
117 workers.emplace_back(run_task, w);
118 } else {
119 workers.emplace_back([&run_task,&errors](int w) -> void {
120 try {
121 run_task(w);
122 } catch (...) {
123 errors[w] = std::current_exception();
124 }
125 }, w);
126 }
127 }
128
129 {
130 if constexpr(nothrow_) {
131 run_task(0);
132 } else {
133 try {
134 run_task(0);
135 } catch (...) {
136 errors[0] = std::current_exception();
137 }
138 }
139 }
140
141 for (auto& wrk : workers) {
142 wrk.join();
143 }
144#endif
145
146 if constexpr(!nothrow_) {
147 for (const auto& e : errors) {
148 if (e) {
149 std::rethrow_exception(e);
150 }
151 }
152 }
153#endif
154}
155
156}
157
158#endif
Substitutable parallelization functions.
void parallelize_simple(const Task_ num_tasks, const Run_ run_task)
Parallelize individual tasks across workers.
Definition simple.hpp:60
Parallelize across a range of tasks.