Example adapted from https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2300r5.html#example-async-inclusive-scan
| 31 | // Example adapted from |
| 32 | // https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2300r5.html#example-async-inclusive-scan |
| 33 | [[nodiscard]] |
| 34 | auto async_inclusive_scan(STDEXEC::scheduler auto sch, // 2 |
| 35 | std::span<double const> input, // 1 |
| 36 | std::span<double> output, // 1 |
| 37 | double init, // 1 |
| 38 | std::size_t tile_count) -> STDEXEC::sender auto // 3 |
| 39 | { |
| 40 | using namespace STDEXEC; |
| 41 | std::size_t const tile_size = (input.size() + tile_count - 1) / tile_count; |
| 42 | |
| 43 | std::vector<double> partials(tile_count + 1); |
| 44 | partials[0] = init; |
| 45 | |
| 46 | return just(std::move(partials)) | continues_on(sch) |
| 47 | | bulk(ex::par, |
| 48 | tile_count, |
| 49 | [=](std::size_t i, std::span<double> partials) |
| 50 | { |
| 51 | auto start = i * tile_size; |
| 52 | auto end = (std::min) (input.size(), (i + 1) * tile_size); |
| 53 | partials[i + 1] = *--std::inclusive_scan(begin(input) + static_cast<long>(start), |
| 54 | begin(input) + static_cast<long>(end), |
| 55 | begin(output) |
| 56 | + static_cast<long>(start)); |
| 57 | }) // |
| 58 | | then( |
| 59 | [](std::vector<double>&& partials) |
| 60 | { |
| 61 | std::inclusive_scan(begin(partials), end(partials), begin(partials)); |
| 62 | return std::move(partials); |
| 63 | }) // |
| 64 | | bulk(ex::par, |
| 65 | tile_count, |
| 66 | [=](std::size_t i, std::span<double const> partials) |
| 67 | { |
| 68 | auto start = i * tile_size; |
| 69 | auto end = (std::min) (input.size(), (i + 1) * tile_size); |
| 70 | std::for_each(begin(output) + static_cast<long>(start), |
| 71 | begin(output) + static_cast<long>(end), |
| 72 | [&](double& e) { e = partials[i] + e; }); |
| 73 | }) // |
| 74 | | then([=](std::vector<double>&&) { return output; }); |
| 75 | } |
| 76 | |
| 77 | TEST_CASE("exec::taskflow::taskflow_thread_pool offers the parallel forward progress guarantee", |
| 78 | "[taskflow_thread_pool]") |
no test coverage detected