MCPcopy Create free account
hub / github.com/NVIDIA/stdexec / async_inclusive_scan

Function async_inclusive_scan

test/exec/asio/test_asio_thread_pool.cpp:39–81  ·  view source on GitHub ↗

Example adapted from https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2300r5.html#example-async-inclusive-scan

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 2

thenFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected