|
8 | 8 | #define POOLSTL_NUMERIC_HPP |
9 | 9 |
|
10 | 10 | #include <functional> |
| 11 | +#include <tuple> |
11 | 12 |
|
12 | 13 | #include "execution" |
13 | 14 | #include "internal/ttp_impl.hpp" |
14 | 15 |
|
15 | 16 | namespace std { |
16 | 17 |
|
| 18 | +#if POOLSTL_HAVE_CXX17_LIB |
| 19 | + /** |
| 20 | + * NOTE: Iterators are expected to be random access. |
| 21 | + * See std::exclusive_scan https://en.cppreference.com/w/cpp/algorithm/exclusive_scan |
| 22 | + */ |
| 23 | + template <class ExecPolicy, class RandIt1, class RandIt2, class T, class BinaryOp> |
| 24 | + poolstl::internal::enable_if_par<ExecPolicy, RandIt2> |
| 25 | + exclusive_scan(ExecPolicy &&policy, RandIt1 first, RandIt1 last, RandIt2 dest, T init, BinaryOp binop) { |
| 26 | + if (first == last) { |
| 27 | + return dest; |
| 28 | + } |
| 29 | + |
| 30 | + // Pass 1: Chunk the input and find the sum of each chunk |
| 31 | + auto futures = poolstl::internal::parallel_chunk_for(std::forward<ExecPolicy>(policy), first, last, |
| 32 | + [binop](RandIt1 chunk_first, RandIt1 chunk_last) { |
| 33 | + auto sum = std::accumulate(chunk_first, chunk_last, T{}, binop); |
| 34 | + return std::make_tuple(std::make_pair(chunk_first, chunk_last), sum); |
| 35 | + }); |
| 36 | + |
| 37 | + std::vector<std::pair<RandIt1, RandIt1>> ranges; |
| 38 | + std::vector<T> sums; |
| 39 | + |
| 40 | + for (auto& future : futures) { |
| 41 | + auto res = future.get(); |
| 42 | + ranges.push_back(std::get<0>(res)); |
| 43 | + sums.push_back(std::get<1>(res)); |
| 44 | + } |
| 45 | + |
| 46 | + // find initial values for each range |
| 47 | + std::exclusive_scan(sums.begin(), sums.end(), sums.begin(), init, binop); |
| 48 | + |
| 49 | + // Pass 2: perform exclusive scan of each chunk, using the sum of previous chunks as init |
| 50 | + std::vector<std::tuple<RandIt1, RandIt1, RandIt2, T>> args; |
| 51 | + for (std::size_t i = 0; i < sums.size(); ++i) { |
| 52 | + auto chunk_first = std::get<0>(ranges[i]); |
| 53 | + args.emplace_back(std::make_tuple( |
| 54 | + chunk_first, std::get<1>(ranges[i]), |
| 55 | + dest + (chunk_first - first), |
| 56 | + sums[i])); |
| 57 | + } |
| 58 | + |
| 59 | + auto futures2 = poolstl::internal::parallel_apply(std::forward<ExecPolicy>(policy), |
| 60 | + [binop](RandIt1 chunk_first, RandIt1 chunk_last, RandIt2 chunk_dest, T chunk_init){ |
| 61 | + std::exclusive_scan(chunk_first, chunk_last, chunk_dest, chunk_init, binop); |
| 62 | + }, args); |
| 63 | + |
| 64 | + poolstl::internal::get_futures(futures2); |
| 65 | + return dest + (last - first); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * NOTE: Iterators are expected to be random access. |
| 70 | + * See std::exclusive_scan https://en.cppreference.com/w/cpp/algorithm/exclusive_scan |
| 71 | + */ |
| 72 | + template <class ExecPolicy, class RandIt1, class RandIt2, class T> |
| 73 | + poolstl::internal::enable_if_par<ExecPolicy, RandIt2> |
| 74 | + exclusive_scan(ExecPolicy &&policy, RandIt1 first, RandIt1 last, RandIt2 dest, T init) { |
| 75 | + return std::exclusive_scan(std::forward<ExecPolicy>(policy), first, last, dest, init, std::plus<T>()); |
| 76 | + } |
| 77 | +#endif |
| 78 | + |
17 | 79 | /** |
18 | 80 | * NOTE: Iterators are expected to be random access. |
19 | 81 | * See std::reduce https://en.cppreference.com/w/cpp/algorithm/reduce |
|
0 commit comments