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