|
16 | 16 | */ |
17 | 17 | #include "sxt/execution/device/for_each.h" |
18 | 18 |
|
| 19 | +#include <algorithm> |
| 20 | +#include <numeric> |
| 21 | +#include <random> |
19 | 22 | #include <utility> |
20 | 23 | #include <vector> |
21 | 24 |
|
| 25 | +#include "sxt/base/error/assert.h" |
22 | 26 | #include "sxt/base/iterator/index_range.h" |
| 27 | +#include "sxt/base/iterator/index_range_iterator.h" |
23 | 28 | #include "sxt/base/test/unit_test.h" |
24 | 29 | #include "sxt/execution/async/future.h" |
25 | 30 |
|
@@ -67,3 +72,118 @@ TEST_CASE("we can concurrently invoke code on different GPUs") { |
67 | 72 | REQUIRE(t == 11); |
68 | 73 | } |
69 | 74 | } |
| 75 | + |
| 76 | +TEST_CASE("we can manage asynchronous chunked computations") { |
| 77 | + std::vector<std::pair<unsigned, unsigned>> ranges; |
| 78 | + std::vector<xena::promise<int>> promises(10); |
| 79 | + |
| 80 | + SECTION("we iterate over no chunks") { |
| 81 | + basit::index_range_iterator iter{basit::index_range{2, 2}, 1}; |
| 82 | + auto fut = for_each_device( |
| 83 | + iter, iter, [&](const chunk_context& ctx, basit::index_range rng) -> xena::future<> { |
| 84 | + return xena::future<int>{promises[0]}.then([](int /*val*/) noexcept {}); |
| 85 | + }); |
| 86 | + REQUIRE(fut.ready()); |
| 87 | + } |
| 88 | + |
| 89 | + SECTION("we can iterate over a single chunk") { |
| 90 | + basit::index_range_iterator first{basit::index_range{0, 1}, 1}; |
| 91 | + basit::index_range_iterator last{basit::index_range{1, 1}, 1}; |
| 92 | + auto fut = for_each_device( |
| 93 | + first, last, [&](const chunk_context& ctx, basit::index_range rng) -> xena::future<> { |
| 94 | + ranges.emplace_back(rng.a(), rng.b()); |
| 95 | + return xena::future<int>{promises[0]}.then( |
| 96 | + [&](int val) noexcept { SXT_RELEASE_ASSERT(val == 123); }); |
| 97 | + }); |
| 98 | + REQUIRE(!fut.ready()); |
| 99 | + promises[0].set_value(123); |
| 100 | + REQUIRE(fut.ready()); |
| 101 | + std::vector<std::pair<unsigned, unsigned>> expected = {{0, 1}}; |
| 102 | + REQUIRE(ranges == expected); |
| 103 | + } |
| 104 | + |
| 105 | + SECTION("we can iterate over two chunks") { |
| 106 | + basit::index_range_iterator first{basit::index_range{0, 2}, 1}; |
| 107 | + basit::index_range_iterator last{basit::index_range{2, 2}, 1}; |
| 108 | + auto fut = for_each_device( |
| 109 | + first, last, [&](const chunk_context& ctx, basit::index_range rng) -> xena::future<> { |
| 110 | + ranges.emplace_back(rng.a(), rng.b()); |
| 111 | + return xena::future<int>{promises[ctx.chunk_index]}.then( |
| 112 | + [chunk_index = ctx.chunk_index](int val) noexcept { |
| 113 | + if (chunk_index == 0) { |
| 114 | + SXT_RELEASE_ASSERT(val == 123); |
| 115 | + } else { |
| 116 | + SXT_RELEASE_ASSERT(val == 456); |
| 117 | + } |
| 118 | + }); |
| 119 | + }); |
| 120 | + REQUIRE(!fut.ready()); |
| 121 | + promises[0].set_value(123); |
| 122 | + REQUIRE(!fut.ready()); |
| 123 | + promises[1].set_value(456); |
| 124 | + REQUIRE(fut.ready()); |
| 125 | + std::vector<std::pair<unsigned, unsigned>> expected = {{0, 1}, {1, 2}}; |
| 126 | + REQUIRE(ranges == expected); |
| 127 | + } |
| 128 | + |
| 129 | + SECTION("we can iterate over different chunk sizes") { |
| 130 | + for (unsigned k = 3; k < 10; ++k) { |
| 131 | + promises.clear(); |
| 132 | + ranges.clear(); |
| 133 | + promises.resize(k); |
| 134 | + basit::index_range_iterator first{basit::index_range{0, k}, 1}; |
| 135 | + basit::index_range_iterator last{basit::index_range{k, k}, 1}; |
| 136 | + auto fut = for_each_device( |
| 137 | + first, last, [&](const chunk_context& ctx, basit::index_range rng) -> xena::future<> { |
| 138 | + ranges.emplace_back(rng.a(), rng.b()); |
| 139 | + return xena::future<int>{promises[ctx.chunk_index]}.then( |
| 140 | + [chunk_index = ctx.chunk_index](int val) noexcept { |
| 141 | + SXT_RELEASE_ASSERT(val == chunk_index); |
| 142 | + }); |
| 143 | + }); |
| 144 | + std::vector<std::pair<unsigned, unsigned>> expected; |
| 145 | + for (unsigned i = 0; i < k; ++i) { |
| 146 | + REQUIRE(!fut.ready()); |
| 147 | + promises[i].set_value(i); |
| 148 | + expected.emplace_back(i, i + 1); |
| 149 | + } |
| 150 | + REQUIRE(fut.ready()); |
| 151 | + REQUIRE(ranges == expected); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + SECTION("we can iterate over different chunks finished in an arbitrary order") { |
| 156 | + std::mt19937 rng{0}; |
| 157 | + |
| 158 | + for (unsigned k = 3; k < 10; ++k) { |
| 159 | + promises.clear(); |
| 160 | + promises.resize(k); |
| 161 | + std::vector<bool> finished(k); |
| 162 | + std::vector<xena::future<int>> futs; |
| 163 | + for (auto& ps : promises) { |
| 164 | + futs.emplace_back(ps); |
| 165 | + } |
| 166 | + basit::index_range_iterator first{basit::index_range{0, k}, 1}; |
| 167 | + basit::index_range_iterator last{basit::index_range{k, k}, 1}; |
| 168 | + auto fut = for_each_device( |
| 169 | + first, last, [&](const chunk_context& ctx, basit::index_range rng) -> xena::future<> { |
| 170 | + return futs[ctx.chunk_index].then( |
| 171 | + [&finished, chunk_index = ctx.chunk_index](int val) noexcept { |
| 172 | + finished[chunk_index] = true; |
| 173 | + SXT_RELEASE_ASSERT(val == chunk_index); |
| 174 | + }); |
| 175 | + }); |
| 176 | + std::vector<std::pair<unsigned, unsigned>> expected; |
| 177 | + std::vector<unsigned> ix(k); |
| 178 | + std::iota(ix.begin(), ix.end(), 0); |
| 179 | + std::shuffle(ix.begin(), ix.end(), rng); |
| 180 | + for (auto i : ix) { |
| 181 | + REQUIRE(!fut.ready()); |
| 182 | + promises[i].set_value(i); |
| 183 | + expected.emplace_back(i, i + 1); |
| 184 | + } |
| 185 | + REQUIRE(fut.ready()); |
| 186 | + REQUIRE(std::count(finished.begin(), finished.end(), true) == k); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments