|
30 | 30 | #define MANIFOLD_PAR_NS omp
|
31 | 31 | #elif MANIFOLD_PAR == 'T'
|
32 | 32 | #include <thrust/system/tbb/execution_policy.h>
|
| 33 | + |
| 34 | +#include <algorithm> |
| 35 | +#include <execution> |
33 | 36 | #define MANIFOLD_PAR_NS tbb
|
34 | 37 | #else
|
35 | 38 | #define MANIFOLD_PAR_NS cpp
|
@@ -136,43 +139,105 @@ inline ExecutionPolicy autoPolicy(int size) {
|
136 | 139 | thrust::NAME(thrust::cpp::par, args...); \
|
137 | 140 | }
|
138 | 141 |
|
| 142 | +#if MANIFOLD_PAR == 'T' && !(__APPLE__) |
| 143 | +// sometimes stl variant is faster |
| 144 | +#define STL_DYNAMIC_BACKEND(NAME, RET) \ |
| 145 | + template <typename Ret = RET, typename... Args> \ |
| 146 | + Ret NAME(ExecutionPolicy policy, Args... args) { \ |
| 147 | + switch (policy) { \ |
| 148 | + case ExecutionPolicy::ParUnseq: \ |
| 149 | + case ExecutionPolicy::Par: \ |
| 150 | + return std::NAME(std::execution::par_unseq, args...); \ |
| 151 | + case ExecutionPolicy::Seq: \ |
| 152 | + break; \ |
| 153 | + } \ |
| 154 | + return std::NAME(args...); \ |
| 155 | + } |
| 156 | +#define STL_DYNAMIC_BACKEND_VOID(NAME) \ |
| 157 | + template <typename... Args> \ |
| 158 | + void NAME(ExecutionPolicy policy, Args... args) { \ |
| 159 | + switch (policy) { \ |
| 160 | + case ExecutionPolicy::ParUnseq: \ |
| 161 | + case ExecutionPolicy::Par: \ |
| 162 | + std::NAME(std::execution::par_unseq, args...); \ |
| 163 | + break; \ |
| 164 | + case ExecutionPolicy::Seq: \ |
| 165 | + std::NAME(args...); \ |
| 166 | + break; \ |
| 167 | + } \ |
| 168 | + } |
| 169 | + |
| 170 | +template <typename... Args> |
| 171 | +void exclusive_scan(ExecutionPolicy policy, Args... args) { |
| 172 | + // https://github.com/llvm/llvm-project/issues/59810 |
| 173 | + std::exclusive_scan(args...); |
| 174 | +} |
| 175 | +template <typename DerivedPolicy, typename InputIterator1, |
| 176 | + typename InputIterator2, typename OutputIterator, typename Predicate> |
| 177 | +OutputIterator copy_if(ExecutionPolicy policy, InputIterator1 first, |
| 178 | + InputIterator1 last, InputIterator2 stencil, |
| 179 | + OutputIterator result, Predicate pred) { |
| 180 | + if (policy == ExecutionPolicy::Seq) |
| 181 | + return thrust::copy_if(thrust::cpp::par, first, last, stencil, result, |
| 182 | + pred); |
| 183 | + else |
| 184 | + // note: this is not a typo, see |
| 185 | + // https://github.com/NVIDIA/thrust/issues/1977 |
| 186 | + return thrust::copy_if(first, last, stencil, result, pred); |
| 187 | +} |
| 188 | +template <typename DerivedPolicy, typename InputIterator1, |
| 189 | + typename OutputIterator, typename Predicate> |
| 190 | +OutputIterator copy_if(ExecutionPolicy policy, InputIterator1 first, |
| 191 | + InputIterator1 last, OutputIterator result, |
| 192 | + Predicate pred) { |
| 193 | + if (policy == ExecutionPolicy::Seq) |
| 194 | + return std::copy_if(first, last, result, pred); |
| 195 | + else |
| 196 | + return std::copy_if(std::execution::par_unseq, first, last, result, pred); |
| 197 | +} |
| 198 | +#else |
| 199 | +#define STL_DYNAMIC_BACKEND(NAME, RET) THRUST_DYNAMIC_BACKEND(NAME, RET) |
| 200 | +#define STL_DYNAMIC_BACKEND_VOID(NAME) THRUST_DYNAMIC_BACKEND_VOID(NAME) |
| 201 | + |
| 202 | +THRUST_DYNAMIC_BACKEND_VOID(exclusive_scan) |
| 203 | +THRUST_DYNAMIC_BACKEND(copy_if, void) |
| 204 | +#endif |
| 205 | + |
139 | 206 | THRUST_DYNAMIC_BACKEND_HOST_VOID(for_each)
|
140 | 207 | THRUST_DYNAMIC_BACKEND_HOST_VOID(for_each_n)
|
141 | 208 |
|
142 | 209 | THRUST_DYNAMIC_BACKEND_VOID(gather)
|
143 | 210 | THRUST_DYNAMIC_BACKEND_VOID(scatter)
|
144 | 211 | THRUST_DYNAMIC_BACKEND_VOID(for_each)
|
145 | 212 | THRUST_DYNAMIC_BACKEND_VOID(for_each_n)
|
146 |
| -THRUST_DYNAMIC_BACKEND_VOID(sort) |
147 |
| -THRUST_DYNAMIC_BACKEND_VOID(stable_sort) |
148 |
| -THRUST_DYNAMIC_BACKEND_VOID(fill) |
149 | 213 | THRUST_DYNAMIC_BACKEND_VOID(sequence)
|
150 | 214 | THRUST_DYNAMIC_BACKEND_VOID(sort_by_key)
|
151 | 215 | THRUST_DYNAMIC_BACKEND_VOID(stable_sort_by_key)
|
152 |
| -THRUST_DYNAMIC_BACKEND_VOID(copy) |
153 | 216 | THRUST_DYNAMIC_BACKEND_VOID(transform)
|
154 |
| -THRUST_DYNAMIC_BACKEND_VOID(inclusive_scan) |
155 |
| -THRUST_DYNAMIC_BACKEND_VOID(exclusive_scan) |
156 | 217 | THRUST_DYNAMIC_BACKEND_VOID(uninitialized_fill)
|
157 | 218 | THRUST_DYNAMIC_BACKEND_VOID(uninitialized_copy)
|
| 219 | +THRUST_DYNAMIC_BACKEND_VOID(stable_sort) |
| 220 | +THRUST_DYNAMIC_BACKEND_VOID(fill) |
| 221 | +THRUST_DYNAMIC_BACKEND_VOID(copy) |
| 222 | +THRUST_DYNAMIC_BACKEND_VOID(inclusive_scan) |
158 | 223 | THRUST_DYNAMIC_BACKEND_VOID(copy_n)
|
| 224 | +STL_DYNAMIC_BACKEND_VOID(sort) |
159 | 225 |
|
160 |
| -THRUST_DYNAMIC_BACKEND(all_of, bool) |
161 |
| -THRUST_DYNAMIC_BACKEND(is_sorted, bool) |
162 |
| -THRUST_DYNAMIC_BACKEND(reduce, void) |
163 |
| -THRUST_DYNAMIC_BACKEND(count_if, int) |
164 |
| -THRUST_DYNAMIC_BACKEND(binary_search, bool) |
165 | 226 | // void implies that the user have to specify the return type in the template
|
166 | 227 | // argument, as we are unable to deduce it
|
| 228 | +THRUST_DYNAMIC_BACKEND(transform_reduce, void) |
| 229 | +THRUST_DYNAMIC_BACKEND(gather_if, void) |
| 230 | +THRUST_DYNAMIC_BACKEND(reduce_by_key, void) |
| 231 | +THRUST_DYNAMIC_BACKEND(lower_bound, void) |
167 | 232 | THRUST_DYNAMIC_BACKEND(remove, void)
|
168 |
| -THRUST_DYNAMIC_BACKEND(copy_if, void) |
169 |
| -THRUST_DYNAMIC_BACKEND(remove_if, void) |
170 |
| -THRUST_DYNAMIC_BACKEND(unique, void) |
171 | 233 | THRUST_DYNAMIC_BACKEND(find, void)
|
172 | 234 | THRUST_DYNAMIC_BACKEND(find_if, void)
|
173 |
| -THRUST_DYNAMIC_BACKEND(reduce_by_key, void) |
174 |
| -THRUST_DYNAMIC_BACKEND(transform_reduce, void) |
175 |
| -THRUST_DYNAMIC_BACKEND(lower_bound, void) |
176 |
| -THRUST_DYNAMIC_BACKEND(gather_if, void) |
| 235 | +THRUST_DYNAMIC_BACKEND(all_of, bool) |
| 236 | +THRUST_DYNAMIC_BACKEND(is_sorted, bool) |
| 237 | +THRUST_DYNAMIC_BACKEND(reduce, void) |
| 238 | +THRUST_DYNAMIC_BACKEND(count_if, int) |
| 239 | +THRUST_DYNAMIC_BACKEND(binary_search, bool) |
| 240 | +STL_DYNAMIC_BACKEND(remove_if, void) |
| 241 | +STL_DYNAMIC_BACKEND(unique, void) |
177 | 242 |
|
178 | 243 | } // namespace manifold
|
0 commit comments