diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 17:11:38 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 17:11:38 -0500 |
| commit | ca9e45dbd136feae14936ffbbe300d18bcda48c7 (patch) | |
| tree | d6cad81523cd0cbdd774b9d6c1c597584055d1ec /runtime/test | |
| parent | b8c925002c6b4a6bfb5cfad6e7cc60ce2414586e (diff) | |
| parent | d3b8251bc390418cdbc1f2d30727b6b31d889cfa (diff) | |
Merge main into pe-inspect-linux-classes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test')
| -rw-r--r-- | runtime/test/big_compile_gate_test.cpp | 532 | ||||
| -rw-r--r-- | runtime/test/big_compute_module.h | 298 | ||||
| -rw-r--r-- | runtime/test/d3d12_big_compute_test.cpp | 978 | ||||
| -rw-r--r-- | runtime/test/gfx10_descriptors_test.cpp | 19 | ||||
| -rw-r--r-- | runtime/test/large_module_gate.h | 181 | ||||
| -rw-r--r-- | runtime/test/replay_pipeline_compiles_test.cpp | 306 |
6 files changed, 2124 insertions, 190 deletions
diff --git a/runtime/test/big_compile_gate_test.cpp b/runtime/test/big_compile_gate_test.cpp new file mode 100644 index 000000000..a012ac9f9 --- /dev/null +++ b/runtime/test/big_compile_gate_test.cpp | |||
| @@ -0,0 +1,532 @@ | |||
| 1 | // LargeModuleGate with a budget that is read (runtime/src/large_module_gate.h): who gets in, in which order and when, and | ||
| 2 | // the settings. The threads that wait for a place are real; every check that waits for one gives up after 10 s, so a | ||
| 3 | // missing wake-up fails the test and does not hang it. Fixed budgets (vk_pipeline_check's) are large_module_gate_test.cpp's. | ||
| 4 | // No Vulkan. | ||
| 5 | #include "../src/large_module_gate.h" | ||
| 6 | |||
| 7 | #include <atomic> | ||
| 8 | #include <chrono> | ||
| 9 | #include <condition_variable> | ||
| 10 | #include <cstdint> | ||
| 11 | #include <cstdio> | ||
| 12 | #include <cstdlib> | ||
| 13 | #include <functional> | ||
| 14 | #include <memory> | ||
| 15 | #include <mutex> | ||
| 16 | #include <thread> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | using namespace std::chrono_literals; | ||
| 22 | using leonida::LargeModuleGate; | ||
| 23 | using Work = LargeModuleGate::Work; | ||
| 24 | |||
| 25 | // Ends the process: the threads that the failed check left waiting would keep the rest from ending. | ||
| 26 | #define CHECK(condition) do { \ | ||
| 27 | if (!(condition)) { \ | ||
| 28 | std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #condition); \ | ||
| 29 | std::fflush(stderr); \ | ||
| 30 | std::_Exit(1); \ | ||
| 31 | } \ | ||
| 32 | } while (0) | ||
| 33 | |||
| 34 | bool eventually(const std::function<bool()>& what) { | ||
| 35 | const auto end = std::chrono::steady_clock::now() + 10s; | ||
| 36 | while (!what()) { | ||
| 37 | if (std::chrono::steady_clock::now() > end) return false; | ||
| 38 | std::this_thread::sleep_for(1ms); | ||
| 39 | } | ||
| 40 | return true; | ||
| 41 | } | ||
| 42 | |||
| 43 | // A thread in enter() for one estimate, which keeps its place until leave() is called. | ||
| 44 | class Entrant { | ||
| 45 | public: | ||
| 46 | Entrant(LargeModuleGate& gate, std::uint64_t estimate, Work work = Work::demand) { | ||
| 47 | thread_ = std::thread([&gate, estimate, work, this] { | ||
| 48 | LargeModuleGate::Ticket place(gate, estimate, work); | ||
| 49 | in_ = true; | ||
| 50 | std::unique_lock lock(mutex_); | ||
| 51 | go_.wait(lock, [this] { return leave_; }); | ||
| 52 | lock.unlock(); | ||
| 53 | }); | ||
| 54 | } | ||
| 55 | ~Entrant() { | ||
| 56 | if (thread_.joinable()) { | ||
| 57 | leave(); | ||
| 58 | join(); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | Entrant(const Entrant&) = delete; | ||
| 62 | Entrant& operator=(const Entrant&) = delete; | ||
| 63 | bool in() const { return in_; } | ||
| 64 | void leave() { | ||
| 65 | std::lock_guard lock(mutex_); | ||
| 66 | leave_ = true; | ||
| 67 | go_.notify_all(); | ||
| 68 | } | ||
| 69 | void join() { thread_.join(); } | ||
| 70 | |||
| 71 | private: | ||
| 72 | std::atomic<bool> in_{false}; | ||
| 73 | std::mutex mutex_; | ||
| 74 | std::condition_variable go_; | ||
| 75 | bool leave_ = false; | ||
| 76 | std::thread thread_; | ||
| 77 | }; | ||
| 78 | |||
| 79 | bool waiting(LargeModuleGate& gate, Work work, std::size_t count) { | ||
| 80 | return eventually([&] { return gate.waiting(work) == count; }); | ||
| 81 | } | ||
| 82 | bool in(const Entrant& e) { return eventually([&] { return e.in(); }); } | ||
| 83 | // The time a wrong admission would need to happen. | ||
| 84 | bool stays_out(const Entrant& e) { | ||
| 85 | std::this_thread::sleep_for(50ms); | ||
| 86 | return !e.in(); | ||
| 87 | } | ||
| 88 | |||
| 89 | LargeModuleGate fixed(std::uint64_t budget) { | ||
| 90 | return LargeModuleGate([budget] { return budget; }); | ||
| 91 | } | ||
| 92 | |||
| 93 | void bound_and_one_always() { | ||
| 94 | auto gate = fixed(10); | ||
| 95 | Entrant big(gate, 25); // more than the whole budget: it compiles alone | ||
| 96 | CHECK(in(big)); | ||
| 97 | Entrant first(gate, 6), second(gate, 4); | ||
| 98 | CHECK(stays_out(first) && stays_out(second)); | ||
| 99 | big.leave(); | ||
| 100 | big.join(); | ||
| 101 | CHECK(in(first) && in(second)); // 10 fits exactly | ||
| 102 | Entrant third(gate, 1); | ||
| 103 | CHECK(waiting(gate, Work::demand, 1) && stays_out(third)); | ||
| 104 | first.leave(); | ||
| 105 | CHECK(in(third)); | ||
| 106 | CHECK(gate.inflight() == 2); | ||
| 107 | second.leave(); | ||
| 108 | third.leave(); | ||
| 109 | second.join(); | ||
| 110 | third.join(); | ||
| 111 | first.join(); | ||
| 112 | CHECK(eventually([&] { return gate.inflight() == 0; })); | ||
| 113 | } | ||
| 114 | |||
| 115 | // Whatever the sizes, what is in fits, or is one alone; everything gets in, once. | ||
| 116 | void many_threads() { | ||
| 117 | for (const std::uint64_t budget : {0u, 1u, 10u, 40u}) { | ||
| 118 | auto gate = fixed(budget); | ||
| 119 | std::atomic<std::uint64_t> used{0}; | ||
| 120 | std::atomic<unsigned> inside{0}, most{0}; | ||
| 121 | std::atomic<int> over{0}, done{0}; | ||
| 122 | std::vector<std::thread> threads; | ||
| 123 | for (unsigned t = 0; t < 8; ++t) | ||
| 124 | threads.emplace_back([&, t] { | ||
| 125 | for (unsigned i = 0; i < 150; ++i) { | ||
| 126 | const std::uint64_t estimate = 1 + (t * 7 + i) % 13; | ||
| 127 | const LargeModuleGate::Ticket place(gate, estimate, | ||
| 128 | (t + i) % 3 ? Work::demand : Work::speculative); | ||
| 129 | const std::uint64_t sum = used += estimate; | ||
| 130 | const unsigned n = ++inside; | ||
| 131 | if (sum > budget && n > 1) ++over; | ||
| 132 | unsigned m = most; | ||
| 133 | while (n > m && !most.compare_exchange_weak(m, n)) {} | ||
| 134 | for (int spin = 0; spin < 20; ++spin) std::this_thread::yield(); | ||
| 135 | --inside; | ||
| 136 | used -= estimate; | ||
| 137 | ++done; | ||
| 138 | } | ||
| 139 | }); | ||
| 140 | for (auto& t : threads) t.join(); | ||
| 141 | CHECK(over == 0); | ||
| 142 | CHECK(done == 8 * 150); | ||
| 143 | CHECK(gate.inflight() == 0); | ||
| 144 | CHECK(budget < 40 || most > 1); // and where there is room, several are in | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | // Those that wait get in in the order they came, but for what fits now and also beside the first in line: that one finds | ||
| 149 | // its room when those that were in before it leave, whatever passed it. | ||
| 150 | void order() { | ||
| 151 | auto gate = fixed(10); | ||
| 152 | Entrant first(gate, 8); | ||
| 153 | CHECK(in(first)); | ||
| 154 | Entrant big(gate, 9); | ||
| 155 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 156 | Entrant in_the_way(gate, 2); // it fits beside the first, and not beside the big one | ||
| 157 | CHECK(waiting(gate, Work::demand, 2)); | ||
| 158 | Entrant little(gate, 1); | ||
| 159 | CHECK(in(little)); | ||
| 160 | Entrant next(gate, 1); // beside the big one there is room for one of these | ||
| 161 | CHECK(waiting(gate, Work::demand, 3)); | ||
| 162 | CHECK(stays_out(big) && stays_out(in_the_way) && stays_out(next)); | ||
| 163 | little.leave(); | ||
| 164 | little.join(); | ||
| 165 | CHECK(stays_out(big) && stays_out(in_the_way) && stays_out(next)); // nor when the one that passed has left | ||
| 166 | first.leave(); | ||
| 167 | CHECK(in(big)); | ||
| 168 | CHECK(in(next)); // the first in line is now in_the_way, which it can pass | ||
| 169 | CHECK(stays_out(in_the_way)); | ||
| 170 | big.leave(); | ||
| 171 | CHECK(in(in_the_way)); | ||
| 172 | in_the_way.leave(); | ||
| 173 | next.leave(); | ||
| 174 | } | ||
| 175 | |||
| 176 | // What passes the first in line is bounded: a stream of small ones does not keep it out. | ||
| 177 | void a_stream_of_small_ones_does_not_keep_out() { | ||
| 178 | auto gate = fixed(100); | ||
| 179 | Entrant first(gate, 50); | ||
| 180 | CHECK(in(first)); | ||
| 181 | Entrant big(gate, 70); | ||
| 182 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 183 | std::vector<std::unique_ptr<Entrant>> stream; | ||
| 184 | for (int i = 0; i < 3; ++i) { // 30 beside the big one's 70, one after another | ||
| 185 | stream.push_back(std::make_unique<Entrant>(gate, 10)); | ||
| 186 | CHECK(in(*stream.back())); | ||
| 187 | if (i < 2) { // one of them stays in | ||
| 188 | stream.back()->leave(); | ||
| 189 | stream.back()->join(); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | Entrant more(gate, 10); | ||
| 193 | CHECK(waiting(gate, Work::demand, 2)); | ||
| 194 | CHECK(stays_out(more)); // it fits (70 of 100 with the rest), but the first in line has been passed enough | ||
| 195 | first.leave(); | ||
| 196 | CHECK(in(big)); // 10 of the stream and 70 | ||
| 197 | CHECK(in(more)); | ||
| 198 | big.leave(); | ||
| 199 | more.leave(); | ||
| 200 | stream.back()->leave(); | ||
| 201 | } | ||
| 202 | |||
| 203 | // Of those that can pass, the one that came first does. | ||
| 204 | void those_that_pass_keep_their_order() { | ||
| 205 | for (int round = 0; round < 10; ++round) { | ||
| 206 | auto gate = fixed(20); | ||
| 207 | Entrant first(gate, 15), other(gate, 5); | ||
| 208 | CHECK(in(first) && in(other)); | ||
| 209 | Entrant big(gate, 12); | ||
| 210 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 211 | Entrant a(gate, 4); | ||
| 212 | CHECK(waiting(gate, Work::demand, 2)); | ||
| 213 | Entrant b(gate, 4); | ||
| 214 | CHECK(waiting(gate, Work::demand, 3)); | ||
| 215 | other.leave(); | ||
| 216 | CHECK(in(a)); | ||
| 217 | CHECK(stays_out(b) && stays_out(big)); // 19 of 20 | ||
| 218 | first.leave(); | ||
| 219 | CHECK(in(big) && in(b)); | ||
| 220 | big.leave(); | ||
| 221 | a.leave(); | ||
| 222 | b.leave(); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | // One that needs the machine for itself is not passed: what got in beside it would keep it out. | ||
| 227 | void larger_than_the_budget_is_not_passed() { | ||
| 228 | auto gate = fixed(10); | ||
| 229 | Entrant first(gate, 5); | ||
| 230 | CHECK(in(first)); | ||
| 231 | Entrant alone(gate, 25); | ||
| 232 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 233 | Entrant little(gate, 1); | ||
| 234 | CHECK(waiting(gate, Work::demand, 2)); | ||
| 235 | CHECK(stays_out(little)); | ||
| 236 | first.leave(); | ||
| 237 | CHECK(in(alone)); | ||
| 238 | CHECK(stays_out(little)); | ||
| 239 | alone.leave(); | ||
| 240 | CHECK(in(little)); | ||
| 241 | little.leave(); | ||
| 242 | } | ||
| 243 | |||
| 244 | // The same for the speculative ones, within their quarter. | ||
| 245 | void speculative_ones_pass_each_other() { | ||
| 246 | auto gate = fixed(100); | ||
| 247 | Entrant first(gate, 20, Work::speculative); | ||
| 248 | CHECK(in(first)); | ||
| 249 | Entrant big(gate, 18, Work::speculative); | ||
| 250 | CHECK(waiting(gate, Work::speculative, 1)); | ||
| 251 | Entrant little(gate, 5, Work::speculative); | ||
| 252 | CHECK(in(little)); // 25 of 25, and 23 with the big one | ||
| 253 | Entrant next(gate, 1, Work::speculative); | ||
| 254 | CHECK(waiting(gate, Work::speculative, 2)); | ||
| 255 | little.leave(); | ||
| 256 | little.join(); | ||
| 257 | CHECK(in(next)); // 21 of 25, and 24 of what may pass | ||
| 258 | Entrant over(gate, 2, Work::speculative); | ||
| 259 | CHECK(waiting(gate, Work::speculative, 2)); | ||
| 260 | CHECK(stays_out(over) && stays_out(big)); // it would be 26 | ||
| 261 | first.leave(); | ||
| 262 | CHECK(in(big) && in(over)); | ||
| 263 | big.leave(); | ||
| 264 | next.leave(); | ||
| 265 | over.leave(); | ||
| 266 | } | ||
| 267 | |||
| 268 | // The first in the queue leaves it, and the next one, which fits as well, gets in at once and not when something leaves. | ||
| 269 | void one_admission_wakes_the_next() { | ||
| 270 | auto gate = fixed(10); | ||
| 271 | Entrant first(gate, 10); | ||
| 272 | CHECK(in(first)); | ||
| 273 | Entrant a(gate, 4), b(gate, 4), c(gate, 2); | ||
| 274 | CHECK(waiting(gate, Work::demand, 3)); | ||
| 275 | first.leave(); | ||
| 276 | CHECK(in(a) && in(b) && in(c)); | ||
| 277 | CHECK(gate.inflight() == 3); | ||
| 278 | a.leave(); | ||
| 279 | b.leave(); | ||
| 280 | c.leave(); | ||
| 281 | } | ||
| 282 | |||
| 283 | void leaving_wakes_all_that_fit() { | ||
| 284 | auto gate = fixed(10); | ||
| 285 | Entrant a(gate, 5), b(gate, 5); | ||
| 286 | CHECK(in(a) && in(b)); | ||
| 287 | Entrant c(gate, 3), d(gate, 2); | ||
| 288 | CHECK(waiting(gate, Work::demand, 2) && stays_out(c)); | ||
| 289 | a.leave(); | ||
| 290 | CHECK(in(c) && in(d)); | ||
| 291 | b.leave(); | ||
| 292 | c.leave(); | ||
| 293 | d.leave(); | ||
| 294 | } | ||
| 295 | |||
| 296 | // What a submission waits for goes first, whenever it came. | ||
| 297 | void demand_before_speculative() { | ||
| 298 | auto gate = fixed(10); | ||
| 299 | Entrant first(gate, 10); | ||
| 300 | CHECK(in(first)); | ||
| 301 | Entrant speculative(gate, 1, Work::speculative); | ||
| 302 | CHECK(waiting(gate, Work::speculative, 1)); | ||
| 303 | Entrant demand(gate, 9); | ||
| 304 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 305 | first.leave(); | ||
| 306 | CHECK(in(demand)); // the speculative one came first and would have fitted | ||
| 307 | CHECK(stays_out(speculative)); | ||
| 308 | demand.leave(); | ||
| 309 | CHECK(in(speculative)); | ||
| 310 | speculative.leave(); | ||
| 311 | } | ||
| 312 | |||
| 313 | // Waiting demand keeps out speculative work that fits, also the one that is first in line. | ||
| 314 | void waiting_demand_keeps_speculative_out() { | ||
| 315 | auto gate = fixed(100); | ||
| 316 | Entrant first(gate, 40, Work::demand); | ||
| 317 | CHECK(in(first)); | ||
| 318 | Entrant blocked(gate, 100); // waits for the first | ||
| 319 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 320 | Entrant speculative(gate, 5, Work::speculative); // a quarter holds it | ||
| 321 | CHECK(waiting(gate, Work::speculative, 1)); | ||
| 322 | CHECK(stays_out(speculative)); | ||
| 323 | first.leave(); | ||
| 324 | CHECK(in(blocked)); | ||
| 325 | CHECK(stays_out(speculative)); | ||
| 326 | blocked.leave(); | ||
| 327 | CHECK(in(speculative)); | ||
| 328 | speculative.leave(); | ||
| 329 | } | ||
| 330 | |||
| 331 | // While a submission has more to build, the room between two of its modules is not a speculative one's. | ||
| 332 | void a_demand_keeps_speculative_out() { | ||
| 333 | auto gate = fixed(100); | ||
| 334 | auto one = std::make_unique<LargeModuleGate::Demand>(gate), other = std::make_unique<LargeModuleGate::Demand>(gate); | ||
| 335 | Entrant speculative(gate, 10, Work::speculative); | ||
| 336 | CHECK(waiting(gate, Work::speculative, 1)); | ||
| 337 | CHECK(stays_out(speculative)); // it fits, and nothing is in | ||
| 338 | for (int i = 0; i < 2; ++i) { | ||
| 339 | Entrant built(gate, 10); | ||
| 340 | CHECK(in(built)); | ||
| 341 | built.leave(); | ||
| 342 | built.join(); | ||
| 343 | CHECK(stays_out(speculative)); // between two of the submission's modules | ||
| 344 | } | ||
| 345 | one.reset(); | ||
| 346 | CHECK(stays_out(speculative)); // another submission has some | ||
| 347 | other.reset(); | ||
| 348 | CHECK(in(speculative)); // the end of the last one wakes it | ||
| 349 | speculative.leave(); | ||
| 350 | } | ||
| 351 | |||
| 352 | // A speculative one gets in within a quarter of the budget, and what is free of it is there for a demand. | ||
| 353 | void speculative_share() { | ||
| 354 | auto gate = fixed(100); | ||
| 355 | Entrant demand(gate, 30); | ||
| 356 | CHECK(in(demand)); | ||
| 357 | Entrant over(gate, 26, Work::speculative); // 56 of 100, but more than 25 with the rest | ||
| 358 | CHECK(waiting(gate, Work::speculative, 1)); | ||
| 359 | CHECK(stays_out(over)); | ||
| 360 | demand.leave(); | ||
| 361 | CHECK(in(over)); // alone, whatever its size | ||
| 362 | Entrant quick(gate, 70); | ||
| 363 | CHECK(in(quick)); // 96 of 100: the speculative one does not count against it | ||
| 364 | quick.leave(); | ||
| 365 | over.leave(); | ||
| 366 | quick.join(); | ||
| 367 | over.join(); | ||
| 368 | CHECK(eventually([&] { return gate.inflight() == 0; })); | ||
| 369 | Entrant share(gate, 25, Work::speculative); | ||
| 370 | CHECK(in(share)); | ||
| 371 | Entrant second(gate, 1, Work::speculative); | ||
| 372 | CHECK(waiting(gate, Work::speculative, 1)); // one more byte is past the quarter | ||
| 373 | CHECK(stays_out(second)); | ||
| 374 | Entrant next(gate, 75); | ||
| 375 | CHECK(in(next)); // the whole rest | ||
| 376 | CHECK(stays_out(second)); | ||
| 377 | next.leave(); | ||
| 378 | share.leave(); | ||
| 379 | CHECK(in(second)); | ||
| 380 | second.leave(); | ||
| 381 | } | ||
| 382 | |||
| 383 | // Speculative ones get in in the order they came, and alone when nothing is in. | ||
| 384 | void speculative_order_and_alone() { | ||
| 385 | auto gate = fixed(0); // unknown memory: one at a time | ||
| 386 | Entrant a(gate, 5, Work::speculative); | ||
| 387 | CHECK(in(a)); | ||
| 388 | Entrant b(gate, 5, Work::speculative); | ||
| 389 | CHECK(waiting(gate, Work::speculative, 1)); | ||
| 390 | Entrant c(gate, 5, Work::demand); | ||
| 391 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 392 | CHECK(stays_out(b) && stays_out(c)); | ||
| 393 | a.leave(); | ||
| 394 | CHECK(in(c)); | ||
| 395 | CHECK(stays_out(b)); | ||
| 396 | c.leave(); | ||
| 397 | CHECK(in(b)); | ||
| 398 | b.leave(); | ||
| 399 | } | ||
| 400 | |||
| 401 | // The budget is read when a place is asked for and when one is given back, not once. | ||
| 402 | void budget_follows_the_moment() { | ||
| 403 | std::atomic<std::uint64_t> free{100}; | ||
| 404 | LargeModuleGate gate([&] { return free.load(); }); | ||
| 405 | CHECK(gate.budget() == 100); | ||
| 406 | Entrant a(gate, 30), b(gate, 10); | ||
| 407 | CHECK(in(a) && in(b)); | ||
| 408 | free = 35; | ||
| 409 | Entrant c(gate, 20); // 60 of 100 a moment ago | ||
| 410 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 411 | CHECK(stays_out(c)); | ||
| 412 | free = 100; | ||
| 413 | b.leave(); | ||
| 414 | CHECK(in(c)); // 50 of 100 now; it would not be within 35 | ||
| 415 | CHECK(gate.inflight() == 2); | ||
| 416 | a.leave(); | ||
| 417 | c.leave(); | ||
| 418 | a.join(); | ||
| 419 | c.join(); | ||
| 420 | // A budget that shrinks while a module is in does not throw it out, and holds the next. | ||
| 421 | Entrant d(gate, 100); | ||
| 422 | CHECK(in(d)); | ||
| 423 | free = 1; | ||
| 424 | Entrant e(gate, 1); | ||
| 425 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 426 | CHECK(stays_out(e)); | ||
| 427 | CHECK(gate.inflight() == 1); | ||
| 428 | d.leave(); | ||
| 429 | CHECK(in(e)); | ||
| 430 | } | ||
| 431 | |||
| 432 | // An estimate counts for as long as its module is in, whatever the driver has taken of it already. | ||
| 433 | void estimates_stay_reserved() { | ||
| 434 | auto gate = fixed(100); // the driver has taken nothing yet: the memory is all there | ||
| 435 | Entrant a(gate, 60); | ||
| 436 | CHECK(in(a)); | ||
| 437 | Entrant b(gate, 60); | ||
| 438 | CHECK(waiting(gate, Work::demand, 1)); | ||
| 439 | CHECK(stays_out(b)); | ||
| 440 | a.leave(); | ||
| 441 | CHECK(in(b)); | ||
| 442 | b.leave(); | ||
| 443 | } | ||
| 444 | |||
| 445 | // A ticket gives its place back once, however it ends. | ||
| 446 | void tickets() { | ||
| 447 | auto gate = fixed(10); | ||
| 448 | { | ||
| 449 | LargeModuleGate::Ticket a(gate, 6, Work::speculative); | ||
| 450 | LargeModuleGate::Ticket moved = std::move(a); | ||
| 451 | CHECK(gate.inflight() == 1); | ||
| 452 | LargeModuleGate::Ticket b; | ||
| 453 | b = LargeModuleGate::Ticket(gate, 3); | ||
| 454 | CHECK(gate.inflight() == 2); | ||
| 455 | b.release(); | ||
| 456 | b.release(); | ||
| 457 | CHECK(gate.inflight() == 1); | ||
| 458 | } | ||
| 459 | CHECK(gate.inflight() == 0); | ||
| 460 | } | ||
| 461 | |||
| 462 | // The memory budget's setting: what it says, and nothing else. | ||
| 463 | void settings() { | ||
| 464 | using leonida::large_compile_budget; | ||
| 465 | const auto check = [](const char* text, bool valid, bool has, std::uint64_t mib) { | ||
| 466 | const auto s = large_compile_budget(text); | ||
| 467 | CHECK(s.valid == valid); | ||
| 468 | CHECK(s.fixed.has_value() == has); | ||
| 469 | if (has) CHECK(*s.fixed == mib << 20); | ||
| 470 | }; | ||
| 471 | check(nullptr, true, false, 0); | ||
| 472 | check("", true, false, 0); | ||
| 473 | check("0", true, true, 0); | ||
| 474 | check("1", true, true, 1); | ||
| 475 | check("512", true, true, 512); | ||
| 476 | check("0512", true, true, 512); | ||
| 477 | check("12a", false, false, 0); | ||
| 478 | check(" 5", false, false, 0); | ||
| 479 | check("-1", false, false, 0); | ||
| 480 | check("1.5", false, false, 0); | ||
| 481 | check("99999999999999999999999", true, true, UINT64_MAX >> 20); // saturates: no limit, not a small number | ||
| 482 | CHECK(*large_compile_budget("99999999999999999999999").fixed >= (UINT64_MAX >> 20) << 20); | ||
| 483 | } | ||
| 484 | |||
| 485 | // The share of the memory, and what the machine gives now: nothing unknown on Windows and Linux, and no more than three | ||
| 486 | // quarters of either limit. | ||
| 487 | void machine() { | ||
| 488 | using leonida::large_compile_share; | ||
| 489 | CHECK(large_compile_share(1000, 4000) == 750); | ||
| 490 | CHECK(large_compile_share(4000, 1000) == 750); // the commit limit has less room than the RAM | ||
| 491 | CHECK(large_compile_share(0, 4000) == 0); | ||
| 492 | const std::uint64_t budget = leonida::large_compile_budget_bytes(); | ||
| 493 | #if defined(_WIN32) || defined(__linux__) | ||
| 494 | CHECK(budget > 0); | ||
| 495 | #endif | ||
| 496 | // Read a moment apart on a running machine: the budget may not exceed either limit by more than that moment. | ||
| 497 | const std::uint64_t slack = std::uint64_t(256) << 20; | ||
| 498 | CHECK(budget <= leonida::available_memory_bytes() / 4 * 3 + slack); | ||
| 499 | CHECK(leonida::large_module_estimate(1 << 20) == (std::uint64_t(1) << 20) * leonida::kBytesPerSpirvByte); | ||
| 500 | #ifdef _WIN32 | ||
| 501 | MEMORYSTATUSEX status{}; | ||
| 502 | status.dwLength = sizeof(status); | ||
| 503 | CHECK(GlobalMemoryStatusEx(&status) != 0); | ||
| 504 | CHECK(budget <= status.ullAvailPageFile / 4 * 3 + slack); | ||
| 505 | #endif | ||
| 506 | } | ||
| 507 | |||
| 508 | } // namespace | ||
| 509 | |||
| 510 | int main() { | ||
| 511 | bound_and_one_always(); | ||
| 512 | many_threads(); | ||
| 513 | order(); | ||
| 514 | a_stream_of_small_ones_does_not_keep_out(); | ||
| 515 | those_that_pass_keep_their_order(); | ||
| 516 | larger_than_the_budget_is_not_passed(); | ||
| 517 | speculative_ones_pass_each_other(); | ||
| 518 | one_admission_wakes_the_next(); | ||
| 519 | leaving_wakes_all_that_fit(); | ||
| 520 | demand_before_speculative(); | ||
| 521 | waiting_demand_keeps_speculative_out(); | ||
| 522 | a_demand_keeps_speculative_out(); | ||
| 523 | speculative_share(); | ||
| 524 | speculative_order_and_alone(); | ||
| 525 | budget_follows_the_moment(); | ||
| 526 | estimates_stay_reserved(); | ||
| 527 | tickets(); | ||
| 528 | settings(); | ||
| 529 | machine(); | ||
| 530 | std::puts("big_compile_gate: ok"); | ||
| 531 | return 0; | ||
| 532 | } | ||
diff --git a/runtime/test/big_compute_module.h b/runtime/test/big_compute_module.h new file mode 100644 index 000000000..28dde996f --- /dev/null +++ b/runtime/test/big_compute_module.h | |||
| @@ -0,0 +1,298 @@ | |||
| 1 | // Large compute modules that are work for a driver's compiler, not padding: a register file in a Function array that every | ||
| 2 | // instruction loads from and stores to (as the translator's modules have it), integer instructions, selections, nested | ||
| 3 | // conditionals and loops, and loads from memory the compiler knows nothing about. The same instructions run on the CPU, so | ||
| 4 | // the value that a dispatch stores is known. Made from a seed; no game data. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstddef> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <initializer_list> | ||
| 10 | #include <random> | ||
| 11 | #include <utility> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace bigmodule { | ||
| 15 | |||
| 16 | using Words = std::vector<std::uint32_t>; | ||
| 17 | |||
| 18 | inline void op(Words& out, std::uint16_t code, std::initializer_list<std::uint32_t> operands) { | ||
| 19 | out.push_back((std::uint32_t(operands.size() + 1) << 16) | code); | ||
| 20 | out.insert(out.end(), operands.begin(), operands.end()); | ||
| 21 | } | ||
| 22 | |||
| 23 | enum class Kind : std::uint8_t { add, sub, mul, xor_, and_, or_, shl, shr, select, load, if_, end_if, loop, end_loop }; | ||
| 24 | |||
| 25 | struct Instruction { | ||
| 26 | Kind kind; | ||
| 27 | std::uint8_t d = 0, a = 0, b = 0, c = 0; // registers; c is a shift count or a loop count for those kinds | ||
| 28 | }; | ||
| 29 | |||
| 30 | constexpr unsigned kRegisters = 48, kConstants = 64, kDepth = 3; | ||
| 31 | constexpr unsigned kLoadBase = 256, kLoadWords = 64; // the words of the buffer the loads read (the CPU takes them as 0) | ||
| 32 | |||
| 33 | struct Program { | ||
| 34 | std::vector<Instruction> code; | ||
| 35 | std::uint32_t constants[kConstants]; | ||
| 36 | |||
| 37 | // What the module stores for the dispatch with this root constant. | ||
| 38 | std::uint32_t run(std::uint32_t root_constant) const { | ||
| 39 | std::uint32_t v[kRegisters]; | ||
| 40 | for (unsigned i = 0; i < kRegisters; ++i) v[i] = constants[i % kConstants] ^ (root_constant + i); | ||
| 41 | walk(0, v); | ||
| 42 | std::uint32_t out = 0; | ||
| 43 | for (unsigned i = 0; i < kRegisters; ++i) out ^= v[i]; | ||
| 44 | return out; | ||
| 45 | } | ||
| 46 | |||
| 47 | private: | ||
| 48 | // Runs from `at` to the end marker of its block and returns the index after it. | ||
| 49 | std::size_t walk(std::size_t at, std::uint32_t* v) const { | ||
| 50 | for (; at < code.size();) { | ||
| 51 | const Instruction& i = code[at]; | ||
| 52 | const std::uint32_t k = constants[i.c % kConstants]; | ||
| 53 | switch (i.kind) { | ||
| 54 | case Kind::add: v[i.d] = v[i.a] + v[i.b] + k; break; | ||
| 55 | case Kind::sub: v[i.d] = v[i.a] - v[i.b] - k; break; | ||
| 56 | case Kind::mul: v[i.d] = v[i.a] * v[i.b] * k; break; | ||
| 57 | case Kind::xor_: v[i.d] = v[i.a] ^ v[i.b] ^ k; break; | ||
| 58 | case Kind::and_: v[i.d] = v[i.a] & (v[i.b] | k); break; | ||
| 59 | case Kind::or_: v[i.d] = v[i.a] | (v[i.b] & k); break; | ||
| 60 | case Kind::shl: v[i.d] = (v[i.a] << (i.c & 31)) ^ v[i.b]; break; | ||
| 61 | case Kind::shr: v[i.d] = (v[i.a] >> (i.c & 31)) ^ v[i.b]; break; | ||
| 62 | case Kind::select: v[i.d] = v[i.a] < v[i.b] ? v[i.c % kRegisters] : v[i.a]; break; | ||
| 63 | case Kind::load: v[i.d] = v[i.b]; break; // + the loaded word, which is 0 | ||
| 64 | case Kind::if_: { | ||
| 65 | std::size_t after = at + 1; | ||
| 66 | if (v[i.a] < v[i.b]) | ||
| 67 | after = walk(at + 1, v); | ||
| 68 | else | ||
| 69 | after = skip(at + 1); | ||
| 70 | at = after; | ||
| 71 | continue; | ||
| 72 | } | ||
| 73 | case Kind::loop: { | ||
| 74 | std::size_t after = at + 1; | ||
| 75 | for (unsigned n = 0; n < i.c; ++n) after = walk(at + 1, v); | ||
| 76 | at = after; | ||
| 77 | continue; | ||
| 78 | } | ||
| 79 | case Kind::end_if: | ||
| 80 | case Kind::end_loop: return at + 1; | ||
| 81 | } | ||
| 82 | ++at; | ||
| 83 | } | ||
| 84 | return at; | ||
| 85 | } | ||
| 86 | std::size_t skip(std::size_t at) const { | ||
| 87 | for (int depth = 1; at < code.size(); ++at) { | ||
| 88 | const Kind k = code[at].kind; | ||
| 89 | if (k == Kind::if_ || k == Kind::loop) ++depth; | ||
| 90 | if ((k == Kind::end_if || k == Kind::end_loop) && !--depth) return at + 1; | ||
| 91 | } | ||
| 92 | return at; | ||
| 93 | } | ||
| 94 | }; | ||
| 95 | |||
| 96 | // A module of about `bytes` bytes of SPIR-V (a few KiB more), and the program it runs. `seed` decides everything. It has the | ||
| 97 | // translator's push-constant block: s[4]:s[5] is the console VA of a root UAV, s[6] a root constant. A dispatch stores | ||
| 98 | // Program::run(s[6]) at the UAV's word s[6], and reads its words kLoadBase to kLoadBase + kLoadWords - 1, which have to | ||
| 99 | // be 0. | ||
| 100 | inline Words make(std::size_t bytes, std::uint64_t seed, Program* out = nullptr) { | ||
| 101 | std::mt19937_64 random(seed); | ||
| 102 | Program program; | ||
| 103 | for (auto& c : program.constants) c = std::uint32_t(random()); | ||
| 104 | // Ids. | ||
| 105 | enum : std::uint32_t { | ||
| 106 | id_void = 1, id_fn, id_main, id_uint, id_ulong, id_bool, id_block, id_sarr, id_c32, id_ptr_push_block, | ||
| 107 | id_push, id_ptr_push_uint, id_ptr_push_ulong, id_data, id_ptr_data, id_ptr_phys_uint, id_regs, id_cregs, | ||
| 108 | id_ptr_regs, id_ptr_fn_uint, id_vregs, id_entry, id_c64_32, id_c64_2, id_mask, id_base, | ||
| 109 | id_index0 = 40, // uint constants 0..127 | ||
| 110 | id_const0 = id_index0 + 128, // the program's constants | ||
| 111 | id_counter0 = id_const0 + kConstants, // loop counters by depth | ||
| 112 | id_first = id_counter0 + kDepth + 1, | ||
| 113 | }; | ||
| 114 | std::uint32_t next = id_first; | ||
| 115 | Words w = {0x07230203u, 0x00010500u, 0u, 0u, 0u}; | ||
| 116 | op(w, 17, {1}); // Shader | ||
| 117 | op(w, 17, {11}); // Int64 | ||
| 118 | op(w, 17, {5347}); // PhysicalStorageBufferAddresses | ||
| 119 | op(w, 14, {5348, 1}); | ||
| 120 | op(w, 15, {5, id_main, 0x6e69616du, 0u, id_push}); | ||
| 121 | op(w, 16, {id_main, 17, 1, 1, 1}); | ||
| 122 | op(w, 71, {id_sarr, 6, 4}); | ||
| 123 | op(w, 71, {id_block, 2}); | ||
| 124 | op(w, 72, {id_block, 0, 35, 0}); | ||
| 125 | op(w, 72, {id_block, 1, 35, 128}); | ||
| 126 | op(w, 72, {id_block, 2, 35, 136}); | ||
| 127 | op(w, 71, {id_data, 2}); | ||
| 128 | op(w, 72, {id_data, 0, 35, 0}); | ||
| 129 | op(w, 19, {id_void}); | ||
| 130 | op(w, 33, {id_fn, id_void}); | ||
| 131 | op(w, 21, {id_uint, 32, 0}); | ||
| 132 | op(w, 21, {id_ulong, 64, 0}); | ||
| 133 | op(w, 20, {id_bool}); | ||
| 134 | op(w, 43, {id_uint, id_c32, 32}); | ||
| 135 | op(w, 28, {id_sarr, id_uint, id_c32}); | ||
| 136 | op(w, 30, {id_block, id_sarr, id_ulong, id_ulong}); | ||
| 137 | op(w, 32, {id_ptr_push_block, 9, id_block}); | ||
| 138 | op(w, 59, {id_ptr_push_block, id_push, 9}); | ||
| 139 | op(w, 32, {id_ptr_push_uint, 9, id_uint}); | ||
| 140 | op(w, 32, {id_ptr_push_ulong, 9, id_ulong}); | ||
| 141 | op(w, 30, {id_data, id_uint}); | ||
| 142 | op(w, 32, {id_ptr_data, 5349, id_data}); | ||
| 143 | op(w, 32, {id_ptr_phys_uint, 5349, id_uint}); | ||
| 144 | op(w, 43, {id_uint, id_cregs, kRegisters}); | ||
| 145 | op(w, 28, {id_regs, id_uint, id_cregs}); | ||
| 146 | op(w, 32, {id_ptr_regs, 7, id_regs}); | ||
| 147 | op(w, 32, {id_ptr_fn_uint, 7, id_uint}); | ||
| 148 | op(w, 43, {id_ulong, id_c64_32, 32, 0}); | ||
| 149 | op(w, 43, {id_ulong, id_c64_2, 2, 0}); | ||
| 150 | op(w, 43, {id_uint, id_mask, kLoadWords - 1}); | ||
| 151 | op(w, 43, {id_uint, id_base, kLoadBase}); | ||
| 152 | for (std::uint32_t i = 0; i < 128; ++i) op(w, 43, {id_uint, id_index0 + i, i}); | ||
| 153 | for (std::uint32_t i = 0; i < kConstants; ++i) op(w, 43, {id_uint, id_const0 + i, program.constants[i]}); | ||
| 154 | op(w, 54, {id_void, id_main, 0, id_fn}); | ||
| 155 | op(w, 248, {id_entry}); | ||
| 156 | op(w, 59, {id_ptr_regs, id_vregs, 7}); | ||
| 157 | for (std::uint32_t i = 0; i <= kDepth; ++i) op(w, 59, {id_ptr_fn_uint, id_counter0 + i, 7}); | ||
| 158 | const auto unary = [&](std::uint16_t code, std::uint32_t type, std::uint32_t a) { | ||
| 159 | op(w, code, {type, next, a}); | ||
| 160 | return next++; | ||
| 161 | }; | ||
| 162 | const auto binary = [&](std::uint16_t code, std::uint32_t type, std::uint32_t a, std::uint32_t b) { | ||
| 163 | op(w, code, {type, next, a, b}); | ||
| 164 | return next++; | ||
| 165 | }; | ||
| 166 | const auto push_word = [&](std::uint32_t index) { | ||
| 167 | op(w, 65, {id_ptr_push_uint, next, id_push, id_index0, id_index0 + index}); | ||
| 168 | return unary(61, id_uint, next++); | ||
| 169 | }; | ||
| 170 | const std::uint32_t lo = push_word(4), hi = push_word(5); | ||
| 171 | op(w, 65, {id_ptr_push_ulong, next, id_push, id_index0 + 1}); | ||
| 172 | const std::uint32_t heap = unary(61, id_ulong, next++); | ||
| 173 | const std::uint32_t lo64 = unary(113, id_ulong, lo), hi64 = unary(113, id_ulong, hi); | ||
| 174 | const std::uint32_t high = binary(196, id_ulong, hi64, id_c64_32); | ||
| 175 | const std::uint32_t va = binary(197, id_ulong, high, lo64); | ||
| 176 | const std::uint32_t base = binary(128, id_ulong, va, heap); | ||
| 177 | const std::uint32_t root = push_word(6); | ||
| 178 | const auto slot = [&](std::uint32_t r) { | ||
| 179 | op(w, 65, {id_ptr_fn_uint, next, id_vregs, id_index0 + r}); | ||
| 180 | return next++; | ||
| 181 | }; | ||
| 182 | const auto load_reg = [&](std::uint32_t r) { return unary(61, id_uint, slot(r)); }; | ||
| 183 | const auto store_reg = [&](std::uint32_t r, std::uint32_t value) { | ||
| 184 | const std::uint32_t p = slot(r); | ||
| 185 | op(w, 62, {p, value}); | ||
| 186 | }; | ||
| 187 | for (std::uint32_t i = 0; i < kRegisters; ++i) { | ||
| 188 | const std::uint32_t seeded = binary(128, id_uint, root, id_index0 + i); | ||
| 189 | store_reg(i, binary(198, id_uint, id_const0 + i % kConstants, seeded)); | ||
| 190 | } | ||
| 191 | struct Open { | ||
| 192 | Kind kind; | ||
| 193 | std::uint32_t merge, header, cont, counter; | ||
| 194 | unsigned left; | ||
| 195 | }; | ||
| 196 | std::vector<Open> open; | ||
| 197 | unsigned loops = 0; | ||
| 198 | const auto pick = [&](std::uint32_t n) { return std::uint8_t(random() % n); }; | ||
| 199 | const auto close = [&] { | ||
| 200 | const Open o = open.back(); | ||
| 201 | open.pop_back(); | ||
| 202 | if (o.kind == Kind::if_) { | ||
| 203 | op(w, 249, {o.merge}); | ||
| 204 | op(w, 248, {o.merge}); | ||
| 205 | program.code.push_back({Kind::end_if}); | ||
| 206 | } else { | ||
| 207 | op(w, 249, {o.cont}); | ||
| 208 | op(w, 248, {o.cont}); | ||
| 209 | const std::uint32_t n = unary(61, id_uint, o.counter); | ||
| 210 | op(w, 62, {o.counter, binary(128, id_uint, n, id_index0 + 1)}); | ||
| 211 | op(w, 249, {o.header}); | ||
| 212 | op(w, 248, {o.merge}); | ||
| 213 | program.code.push_back({Kind::end_loop}); | ||
| 214 | --loops; | ||
| 215 | } | ||
| 216 | }; | ||
| 217 | while (w.size() * 4 + 256 < bytes) { | ||
| 218 | const std::uint32_t roll = std::uint32_t(random() % 100); | ||
| 219 | if (roll < 4 && open.size() < kDepth) { | ||
| 220 | const std::uint8_t a = pick(kRegisters), b = pick(kRegisters); | ||
| 221 | const std::uint32_t condition = binary(176, id_bool, load_reg(a), load_reg(b)); // ULessThan | ||
| 222 | const std::uint32_t then = next++, merge = next++; | ||
| 223 | op(w, 247, {merge, 0}); | ||
| 224 | op(w, 250, {condition, then, merge}); | ||
| 225 | op(w, 248, {then}); | ||
| 226 | open.push_back({Kind::if_, merge, 0, 0, 0, 10 + unsigned(random() % 30)}); | ||
| 227 | program.code.push_back({Kind::if_, 0, a, b, 0}); | ||
| 228 | } else if (roll < 5 && open.size() < kDepth && loops < 1) { | ||
| 229 | const std::uint8_t count = std::uint8_t(2 + random() % 3); | ||
| 230 | const std::uint32_t counter = id_counter0 + loops++; | ||
| 231 | op(w, 62, {counter, id_index0}); | ||
| 232 | const std::uint32_t header = next++, check = next++, body = next++, cont = next++, merge = next++; | ||
| 233 | op(w, 249, {header}); | ||
| 234 | op(w, 248, {header}); | ||
| 235 | op(w, 246, {merge, cont, 0}); | ||
| 236 | op(w, 249, {check}); | ||
| 237 | op(w, 248, {check}); | ||
| 238 | const std::uint32_t n = unary(61, id_uint, counter); | ||
| 239 | const std::uint32_t more = binary(176, id_bool, n, id_index0 + count); | ||
| 240 | op(w, 250, {more, body, merge}); | ||
| 241 | op(w, 248, {body}); | ||
| 242 | open.push_back({Kind::loop, merge, header, cont, counter, 20 + unsigned(random() % 20)}); | ||
| 243 | program.code.push_back({Kind::loop, 0, 0, 0, count}); | ||
| 244 | } else { | ||
| 245 | Instruction i{Kind(random() % 10), pick(kRegisters), pick(kRegisters), pick(kRegisters), pick(kConstants)}; | ||
| 246 | if (i.kind == Kind::load && random() % 2) i.kind = Kind::add; | ||
| 247 | const std::uint32_t k = id_const0 + i.c % kConstants; | ||
| 248 | std::uint32_t value = 0; | ||
| 249 | const std::uint32_t a = load_reg(i.a), b = load_reg(i.b); | ||
| 250 | switch (i.kind) { | ||
| 251 | case Kind::add: value = binary(128, id_uint, binary(128, id_uint, a, b), k); break; | ||
| 252 | case Kind::sub: value = binary(130, id_uint, binary(130, id_uint, a, b), k); break; | ||
| 253 | case Kind::mul: value = binary(132, id_uint, binary(132, id_uint, a, b), k); break; | ||
| 254 | case Kind::xor_: value = binary(198, id_uint, binary(198, id_uint, a, b), k); break; | ||
| 255 | case Kind::and_: value = binary(199, id_uint, a, binary(197, id_uint, b, k)); break; | ||
| 256 | case Kind::or_: value = binary(197, id_uint, a, binary(199, id_uint, b, k)); break; | ||
| 257 | case Kind::shl: value = binary(198, id_uint, binary(196, id_uint, a, id_index0 + (i.c & 31)), b); break; | ||
| 258 | case Kind::shr: value = binary(198, id_uint, binary(194, id_uint, a, id_index0 + (i.c & 31)), b); break; | ||
| 259 | case Kind::select: { | ||
| 260 | const std::uint32_t c = load_reg(i.c % kRegisters); | ||
| 261 | const std::uint32_t less = binary(176, id_bool, a, b); | ||
| 262 | op(w, 169, {id_uint, next, less, c, a}); | ||
| 263 | value = next++; | ||
| 264 | break; | ||
| 265 | } | ||
| 266 | default: { // load | ||
| 267 | const std::uint32_t index = binary(128, id_uint, binary(199, id_uint, a, id_mask), id_base); | ||
| 268 | const std::uint32_t offset = binary(196, id_ulong, unary(113, id_ulong, index), id_c64_2); | ||
| 269 | const std::uint32_t address = binary(128, id_ulong, base, offset); | ||
| 270 | const std::uint32_t data = unary(120, id_ptr_data, address); | ||
| 271 | op(w, 65, {id_ptr_phys_uint, next, data, id_index0}); | ||
| 272 | const std::uint32_t pointer = next++; | ||
| 273 | op(w, 61, {id_uint, next, pointer, 2, 4}); | ||
| 274 | const std::uint32_t loaded = next++; | ||
| 275 | value = binary(128, id_uint, b, loaded); | ||
| 276 | break; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | store_reg(i.d, value); | ||
| 280 | program.code.push_back(i); | ||
| 281 | } | ||
| 282 | if (!open.empty() && !open.back().left--) close(); | ||
| 283 | } | ||
| 284 | while (!open.empty()) close(); | ||
| 285 | std::uint32_t result = load_reg(0); | ||
| 286 | for (std::uint32_t i = 1; i < kRegisters; ++i) result = binary(198, id_uint, result, load_reg(i)); | ||
| 287 | const std::uint32_t out_offset = binary(196, id_ulong, unary(113, id_ulong, root), id_c64_2); | ||
| 288 | const std::uint32_t out_data = unary(120, id_ptr_data, binary(128, id_ulong, base, out_offset)); | ||
| 289 | op(w, 65, {id_ptr_phys_uint, next, out_data, id_index0}); | ||
| 290 | op(w, 62, {next++, result, 2, 4}); | ||
| 291 | op(w, 253, {}); | ||
| 292 | op(w, 56, {}); | ||
| 293 | w[3] = next; | ||
| 294 | if (out) *out = std::move(program); | ||
| 295 | return w; | ||
| 296 | } | ||
| 297 | |||
| 298 | } // namespace bigmodule | ||
diff --git a/runtime/test/d3d12_big_compute_test.cpp b/runtime/test/d3d12_big_compute_test.cpp new file mode 100644 index 000000000..c6469719e --- /dev/null +++ b/runtime/test/d3d12_big_compute_test.cpp | |||
| @@ -0,0 +1,978 @@ | |||
| 1 | // Large pipelines that a submission compiles (LEONIDA_SERIAL_BIG_COMPILES, LEONIDA_BIG_COMPILE_BUDGET_MB), through the D3D12 | ||
| 2 | // layer on a GPU: real compute modules of more than 128 KiB (big_compute_module.h), small ones, and graphics pipelines of more than | ||
| 3 | // 1 MiB. Both settings are read once for each process, so the test runs once for each: with a fixed budget, one that holds | ||
| 4 | // nothing, the one read from the machine, one at a time and without a limit. Every run | ||
| 5 | // - stores what the CPU computes for each dispatch, and draws the graphics pipelines' cells; | ||
| 6 | // - compiles as many large ones at once as the setting allows and no more, and the small ones meanwhile; | ||
| 7 | // - gives a pipeline the driver refuses the result it has always had, and its place to the next; | ||
| 8 | // - compiles each pipeline once when two queues need the same ones; | ||
| 9 | // - ends when the device's last references go while its workers compile and wait for memory. | ||
| 10 | // `warmup`: a submission's compiles go before those of the warm-up thread, which waits without holding a pipeline state. | ||
| 11 | // `eager`: pipeline states compiled when they are created (LEONIDA_LAZY_COMPUTE=0), by several threads, keep the budget too. | ||
| 12 | // The calls that create pipelines are watched, and held back, through the device's vkGetDeviceProcAddr. | ||
| 13 | // Self-contained: builds its SPIR-V in memory. Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation. | ||
| 14 | #include "big_compute_module.h" | ||
| 15 | #include "vk_test_device.h" | ||
| 16 | #include "d3d12_test_shaders.h" | ||
| 17 | #include "test_temp_dir.h" | ||
| 18 | #include "../shims/d3d12_com.h" | ||
| 19 | #include "../shims/d3d12_pipeline_compile.h" | ||
| 20 | #include "../src/large_module_gate.h" | ||
| 21 | #include "../src/shader_library.h" | ||
| 22 | |||
| 23 | #include <algorithm> | ||
| 24 | #include <atomic> | ||
| 25 | #include <bit> | ||
| 26 | #include <chrono> | ||
| 27 | #include <condition_variable> | ||
| 28 | #include <cstdint> | ||
| 29 | #include <cstdio> | ||
| 30 | #include <cstdlib> | ||
| 31 | #include <cstring> | ||
| 32 | #include <fstream> | ||
| 33 | #include <map> | ||
| 34 | #include <memory> | ||
| 35 | #include <mutex> | ||
| 36 | #include <set> | ||
| 37 | #include <string> | ||
| 38 | #include <thread> | ||
| 39 | #include <type_traits> | ||
| 40 | #include <utility> | ||
| 41 | #include <vector> | ||
| 42 | |||
| 43 | namespace { | ||
| 44 | |||
| 45 | using namespace std::chrono_literals; | ||
| 46 | using d3d12test::Ref; | ||
| 47 | using leonida::ReplayResult; | ||
| 48 | using bigmodule::Words; | ||
| 49 | using bigmodule::op; | ||
| 50 | |||
| 51 | constexpr unsigned kBig = 6, kSmall = 4, kCompute = kBig + kSmall, kGraphics = 2; | ||
| 52 | constexpr std::size_t kBigBytes = 132 * 1024, kSmallBytes = 12 * 1024; | ||
| 53 | constexpr unsigned kExtent = 4; // the render target; each graphics pipeline draws one 2x2 cell | ||
| 54 | constexpr unsigned kWords = bigmodule::kLoadBase + bigmodule::kLoadWords; | ||
| 55 | |||
| 56 | #define CHECK(expr) \ | ||
| 57 | do { \ | ||
| 58 | if (!(expr)) { \ | ||
| 59 | std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #expr); \ | ||
| 60 | return false; \ | ||
| 61 | } \ | ||
| 62 | } while (0) | ||
| 63 | |||
| 64 | // ---- What the settings of this process say. ---- | ||
| 65 | |||
| 66 | std::string setting(const char* name) { | ||
| 67 | char text[64]{}; | ||
| 68 | std::size_t length = 0; | ||
| 69 | return getenv_s(&length, text, name) == 0 && length > 1 ? text : ""; | ||
| 70 | } | ||
| 71 | const bool serial = setting("LEONIDA_SERIAL_BIG_COMPILES") == "1"; | ||
| 72 | const bool unlimited = setting("LEONIDA_SERIAL_BIG_COMPILES") == "0"; | ||
| 73 | const bool budgeted = !serial && !unlimited; | ||
| 74 | const auto fixed_budget = leonida::large_compile_budget(setting("LEONIDA_BIG_COMPILE_BUDGET_MB").c_str()).fixed; | ||
| 75 | |||
| 76 | // ---- The driver's pipeline calls, watched. ---- | ||
| 77 | |||
| 78 | struct Start { | ||
| 79 | std::thread::id thread; | ||
| 80 | int program = -1; // compute: the index of its module | ||
| 81 | bool large = false; | ||
| 82 | }; | ||
| 83 | |||
| 84 | struct Hooks { | ||
| 85 | std::mutex mutex; | ||
| 86 | std::condition_variable changed; | ||
| 87 | std::map<std::uint64_t, int> programs; // the hash of a compute module's words: its index | ||
| 88 | std::map<VkShaderModule, std::pair<std::size_t, int>> modules; // bytes, program | ||
| 89 | bool closed = false; // large creations wait while it is | ||
| 90 | std::chrono::milliseconds linger{0}; // and each takes at least this long, as does what follows it on its thread | ||
| 91 | std::vector<Start> started; | ||
| 92 | int large_in = 0, most_large = 0, small_done = 0, large_compute_in = 0, most_large_compute = 0; | ||
| 93 | std::uint64_t reserved = 0; // the estimates of the large ones in the driver | ||
| 94 | int over = 0; // how often several were in past the budget, or two where one is allowed | ||
| 95 | int fail_large = -1; // the large compute create (counted from 0) the driver refuses | ||
| 96 | int large_compute = 0; | ||
| 97 | bool track = false; // threads that create a pipeline are counted until they end | ||
| 98 | int live = 0, in_flight = 0, cache_reads = 0; | ||
| 99 | bool cache_read_during_create = false, cache_read_with_workers = false; | ||
| 100 | |||
| 101 | void reset() { | ||
| 102 | std::lock_guard lock(mutex); | ||
| 103 | closed = track = cache_read_during_create = cache_read_with_workers = false; | ||
| 104 | started.clear(); | ||
| 105 | most_large = small_done = most_large_compute = over = large_compute = cache_reads = 0; | ||
| 106 | fail_large = -1; | ||
| 107 | linger = 0ms; | ||
| 108 | } | ||
| 109 | void open() { | ||
| 110 | std::lock_guard lock(mutex); | ||
| 111 | closed = false; | ||
| 112 | changed.notify_all(); | ||
| 113 | } | ||
| 114 | template<typename Until> | ||
| 115 | bool wait(Until&& until) { | ||
| 116 | std::unique_lock lock(mutex); | ||
| 117 | return changed.wait_for(lock, 20s, until); | ||
| 118 | } | ||
| 119 | bool wait_large(int count) { | ||
| 120 | return wait([&] { return large_in >= count; }); | ||
| 121 | } | ||
| 122 | std::size_t creates() { | ||
| 123 | std::lock_guard lock(mutex); | ||
| 124 | return started.size(); | ||
| 125 | } | ||
| 126 | } hooks; | ||
| 127 | |||
| 128 | struct Tracked { | ||
| 129 | bool on = false; | ||
| 130 | ~Tracked() { | ||
| 131 | if (!on) return; | ||
| 132 | std::lock_guard lock(hooks.mutex); | ||
| 133 | --hooks.live; | ||
| 134 | } | ||
| 135 | }; | ||
| 136 | thread_local Tracked tracked; | ||
| 137 | |||
| 138 | PFN_vkGetInstanceProcAddr real_instance_proc = nullptr; | ||
| 139 | PFN_vkGetDeviceProcAddr real_device_proc = nullptr; | ||
| 140 | PFN_vkCreateShaderModule real_create_module = nullptr; | ||
| 141 | PFN_vkDestroyShaderModule real_destroy_module = nullptr; | ||
| 142 | PFN_vkCreateGraphicsPipelines real_create_graphics = nullptr; | ||
| 143 | PFN_vkCreateComputePipelines real_create_compute = nullptr; | ||
| 144 | PFN_vkGetPipelineCacheData real_cache_data = nullptr; | ||
| 145 | |||
| 146 | std::uint64_t hash(const std::uint32_t* words, std::size_t count) { | ||
| 147 | std::uint64_t h = 0xcbf29ce484222325u; | ||
| 148 | for (std::size_t i = 0; i < count; ++i) h = (h ^ words[i]) * 0x100000001b3u; | ||
| 149 | return h; | ||
| 150 | } | ||
| 151 | |||
| 152 | struct Creation { | ||
| 153 | bool large = false, compute = false, refused = false; | ||
| 154 | std::uint64_t estimate = 0; | ||
| 155 | }; | ||
| 156 | |||
| 157 | Creation enter_create(bool compute, std::initializer_list<VkShaderModule> modules) { | ||
| 158 | std::unique_lock lock(hooks.mutex); | ||
| 159 | Creation c; | ||
| 160 | c.compute = compute; | ||
| 161 | std::size_t bytes = 0; | ||
| 162 | Start start{std::this_thread::get_id(), -1, false}; | ||
| 163 | for (const VkShaderModule module : modules) { | ||
| 164 | const auto found = hooks.modules.find(module); | ||
| 165 | if (found == hooks.modules.end()) continue; | ||
| 166 | bytes += found->second.first; | ||
| 167 | if (compute) start.program = found->second.second; | ||
| 168 | } | ||
| 169 | c.large = start.large = bytes > (compute ? 128 * 1024 : leonida::PipelineCompiles::large_spirv_bytes - 1); | ||
| 170 | c.estimate = leonida::large_module_estimate(bytes); | ||
| 171 | ++hooks.in_flight; | ||
| 172 | hooks.started.push_back(start); | ||
| 173 | if (hooks.track && !tracked.on) { | ||
| 174 | tracked.on = true; | ||
| 175 | ++hooks.live; | ||
| 176 | } | ||
| 177 | if (c.large) { | ||
| 178 | ++hooks.large_in; | ||
| 179 | if (compute) ++hooks.large_compute_in; | ||
| 180 | hooks.most_large = std::max(hooks.most_large, hooks.large_in); | ||
| 181 | hooks.most_large_compute = std::max(hooks.most_large_compute, hooks.large_compute_in); | ||
| 182 | hooks.reserved += c.estimate; | ||
| 183 | // One large compute module at a time; with a budget, several only within it. | ||
| 184 | if (serial && hooks.large_compute_in > 1) ++hooks.over; | ||
| 185 | if (budgeted && fixed_budget && hooks.large_in > 1 && hooks.reserved > *fixed_budget) ++hooks.over; | ||
| 186 | if (compute && hooks.large_compute++ == hooks.fail_large) c.refused = true; | ||
| 187 | } | ||
| 188 | hooks.changed.notify_all(); | ||
| 189 | if (c.large) hooks.changed.wait(lock, [] { return !hooks.closed; }); | ||
| 190 | const auto linger = hooks.linger; | ||
| 191 | lock.unlock(); | ||
| 192 | if (c.large && linger > 0ms) std::this_thread::sleep_for(linger); | ||
| 193 | return c; | ||
| 194 | } | ||
| 195 | |||
| 196 | void leave_create(const Creation& c) { | ||
| 197 | std::lock_guard lock(hooks.mutex); | ||
| 198 | --hooks.in_flight; | ||
| 199 | if (c.large) { | ||
| 200 | --hooks.large_in; | ||
| 201 | if (c.compute) --hooks.large_compute_in; | ||
| 202 | hooks.reserved -= c.estimate; | ||
| 203 | } else { | ||
| 204 | ++hooks.small_done; | ||
| 205 | } | ||
| 206 | hooks.changed.notify_all(); | ||
| 207 | } | ||
| 208 | |||
| 209 | VKAPI_ATTR VkResult VKAPI_CALL create_module(VkDevice device, const VkShaderModuleCreateInfo* info, | ||
| 210 | const VkAllocationCallbacks* allocator, VkShaderModule* out) { | ||
| 211 | const VkResult result = real_create_module(device, info, allocator, out); | ||
| 212 | if (result == VK_SUCCESS) { | ||
| 213 | std::lock_guard lock(hooks.mutex); | ||
| 214 | const auto found = hooks.programs.find(hash(info->pCode, info->codeSize / 4)); | ||
| 215 | hooks.modules[*out] = {info->codeSize, found == hooks.programs.end() ? -1 : found->second}; | ||
| 216 | } | ||
| 217 | return result; | ||
| 218 | } | ||
| 219 | |||
| 220 | // Called between the place a compile gives back and the next one its thread asks for: with a linger, time for another | ||
| 221 | // thread to take that place. | ||
| 222 | VKAPI_ATTR void VKAPI_CALL destroy_module(VkDevice device, VkShaderModule module, const VkAllocationCallbacks* allocator) { | ||
| 223 | std::chrono::milliseconds linger; | ||
| 224 | { | ||
| 225 | std::lock_guard lock(hooks.mutex); | ||
| 226 | linger = hooks.linger; | ||
| 227 | } | ||
| 228 | if (linger > 0ms) std::this_thread::sleep_for(linger); | ||
| 229 | real_destroy_module(device, module, allocator); | ||
| 230 | } | ||
| 231 | |||
| 232 | VKAPI_ATTR VkResult VKAPI_CALL create_graphics(VkDevice device, VkPipelineCache cache, std::uint32_t count, | ||
| 233 | const VkGraphicsPipelineCreateInfo* infos, | ||
| 234 | const VkAllocationCallbacks* allocator, VkPipeline* out) { | ||
| 235 | VkShaderModule modules[2]{}; | ||
| 236 | for (std::uint32_t i = 0; i < std::min(infos->stageCount, 2u); ++i) modules[i] = infos->pStages[i].module; | ||
| 237 | const Creation c = enter_create(false, {modules[0], modules[1]}); | ||
| 238 | const VkResult result = real_create_graphics(device, cache, count, infos, allocator, out); | ||
| 239 | leave_create(c); | ||
| 240 | return result; | ||
| 241 | } | ||
| 242 | |||
| 243 | VKAPI_ATTR VkResult VKAPI_CALL create_compute(VkDevice device, VkPipelineCache cache, std::uint32_t count, | ||
| 244 | const VkComputePipelineCreateInfo* infos, | ||
| 245 | const VkAllocationCallbacks* allocator, VkPipeline* out) { | ||
| 246 | const Creation c = enter_create(true, {infos->stage.module}); | ||
| 247 | const VkResult result = c.refused ? VK_ERROR_UNKNOWN | ||
| 248 | : real_create_compute(device, cache, count, infos, allocator, out); | ||
| 249 | leave_create(c); | ||
| 250 | return result; | ||
| 251 | } | ||
| 252 | |||
| 253 | VKAPI_ATTR VkResult VKAPI_CALL cache_data(VkDevice device, VkPipelineCache cache, std::size_t* size, void* data) { | ||
| 254 | { | ||
| 255 | std::lock_guard lock(hooks.mutex); | ||
| 256 | ++hooks.cache_reads; | ||
| 257 | if (hooks.in_flight) hooks.cache_read_during_create = true; | ||
| 258 | if (hooks.live > (tracked.on ? 1 : 0)) hooks.cache_read_with_workers = true; | ||
| 259 | } | ||
| 260 | return real_cache_data(device, cache, size, data); | ||
| 261 | } | ||
| 262 | |||
| 263 | VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL device_proc(VkDevice device, const char* name) { | ||
| 264 | const PFN_vkVoidFunction real = real_device_proc(device, name); | ||
| 265 | if (!real) return nullptr; | ||
| 266 | const auto watch = [&](const char* which, auto& kept, auto* mine) -> PFN_vkVoidFunction { | ||
| 267 | if (std::strcmp(name, which)) return nullptr; | ||
| 268 | kept = reinterpret_cast<std::remove_reference_t<decltype(kept)>>(real); | ||
| 269 | return reinterpret_cast<PFN_vkVoidFunction>(mine); | ||
| 270 | }; | ||
| 271 | if (const auto f = watch("vkCreateShaderModule", real_create_module, create_module)) return f; | ||
| 272 | if (const auto f = watch("vkDestroyShaderModule", real_destroy_module, destroy_module)) return f; | ||
| 273 | if (const auto f = watch("vkCreateGraphicsPipelines", real_create_graphics, create_graphics)) return f; | ||
| 274 | if (const auto f = watch("vkCreateComputePipelines", real_create_compute, create_compute)) return f; | ||
| 275 | if (const auto f = watch("vkGetPipelineCacheData", real_cache_data, cache_data)) return f; | ||
| 276 | return real; | ||
| 277 | } | ||
| 278 | |||
| 279 | VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL instance_proc(VkInstance instance, const char* name) { | ||
| 280 | if (!std::strcmp(name, "vkGetDeviceProcAddr")) { | ||
| 281 | real_device_proc = reinterpret_cast<PFN_vkGetDeviceProcAddr>(real_instance_proc(instance, name)); | ||
| 282 | return reinterpret_cast<PFN_vkVoidFunction>(device_proc); | ||
| 283 | } | ||
| 284 | return real_instance_proc(instance, name); | ||
| 285 | } | ||
| 286 | |||
| 287 | // ---- The shaders. ---- | ||
| 288 | |||
| 289 | std::uint32_t bits(float f) { return std::bit_cast<std::uint32_t>(f); } | ||
| 290 | |||
| 291 | // 1.25 MiB of OpString, which compile to nothing: a graphics pipeline that is large. | ||
| 292 | void pad(Words& w, std::uint32_t first_id) { | ||
| 293 | for (std::uint32_t i = 0; i < 5; ++i) { | ||
| 294 | const std::size_t at = w.size(); | ||
| 295 | w.resize(at + 65535, 0x20202020u); | ||
| 296 | w[at] = 65535u << 16 | 7; | ||
| 297 | w[at + 1] = first_id + i; | ||
| 298 | w.back() = 0; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | // A full-screen triangle from VertexIndex. | ||
| 303 | Words vertex_words() { | ||
| 304 | Words w = {0x07230203u, 0x00010500u, 0u, 42u, 0u}; | ||
| 305 | op(w, 17, {1}); // Capability Shader | ||
| 306 | op(w, 14, {0, 1}); // MemoryModel Logical GLSL450 | ||
| 307 | op(w, 15, {0, 4, 0x6e69616du, 0u, 10, 11}); // EntryPoint Vertex "main" | ||
| 308 | op(w, 71, {10, 11, 0}); // BuiltIn Position | ||
| 309 | op(w, 71, {11, 11, 42}); // BuiltIn VertexIndex | ||
| 310 | op(w, 19, {1}); | ||
| 311 | op(w, 33, {2, 1}); | ||
| 312 | op(w, 22, {3, 32}); | ||
| 313 | op(w, 23, {6, 3, 4}); | ||
| 314 | op(w, 21, {15, 32, 1}); | ||
| 315 | op(w, 32, {7, 3, 6}); | ||
| 316 | op(w, 32, {8, 1, 15}); | ||
| 317 | op(w, 59, {7, 10, 3}); | ||
| 318 | op(w, 59, {8, 11, 1}); | ||
| 319 | op(w, 43, {15, 24, 1}); | ||
| 320 | op(w, 43, {15, 25, 2}); | ||
| 321 | op(w, 43, {3, 26, bits(2.0f)}); | ||
| 322 | op(w, 43, {3, 27, bits(-1.0f)}); | ||
| 323 | op(w, 43, {3, 28, bits(0.0f)}); | ||
| 324 | op(w, 43, {3, 29, bits(1.0f)}); | ||
| 325 | op(w, 54, {1, 4, 0, 2}); | ||
| 326 | op(w, 248, {30}); | ||
| 327 | op(w, 61, {15, 31, 11}); | ||
| 328 | op(w, 196, {15, 32, 31, 24}); // index << 1 | ||
| 329 | op(w, 199, {15, 33, 32, 25}); // & 2 | ||
| 330 | op(w, 199, {15, 34, 31, 25}); // index & 2 | ||
| 331 | op(w, 111, {3, 35, 33}); | ||
| 332 | op(w, 111, {3, 36, 34}); | ||
| 333 | op(w, 133, {3, 37, 35, 26}); | ||
| 334 | op(w, 133, {3, 38, 36, 26}); | ||
| 335 | op(w, 129, {3, 39, 37, 27}); | ||
| 336 | op(w, 129, {3, 40, 38, 27}); | ||
| 337 | op(w, 80, {6, 41, 39, 40, 28, 29}); | ||
| 338 | op(w, 62, {10, 41}); | ||
| 339 | op(w, 253, {}); | ||
| 340 | op(w, 56, {}); | ||
| 341 | return w; | ||
| 342 | } | ||
| 343 | |||
| 344 | // The colour (n / 255, 1 - n / 255, 0, 1). | ||
| 345 | Words fragment_words(unsigned n) { | ||
| 346 | Words w = {0x07230203u, 0x00010500u, 0u, 40u, 0u}; | ||
| 347 | op(w, 17, {1}); | ||
| 348 | op(w, 14, {0, 1}); | ||
| 349 | op(w, 15, {4, 4, 0x6e69616du, 0u, 9}); // EntryPoint Fragment "main" | ||
| 350 | op(w, 16, {4, 7}); // OriginUpperLeft | ||
| 351 | pad(w, 30); | ||
| 352 | op(w, 71, {9, 30, 0}); // Location 0 | ||
| 353 | op(w, 19, {1}); | ||
| 354 | op(w, 33, {2, 1}); | ||
| 355 | op(w, 22, {3, 32}); | ||
| 356 | op(w, 23, {6, 3, 4}); | ||
| 357 | op(w, 32, {10, 3, 6}); | ||
| 358 | op(w, 59, {10, 9, 3}); | ||
| 359 | op(w, 43, {3, 20, bits(float(n) / 255.0f)}); | ||
| 360 | op(w, 43, {3, 21, bits(float(255 - n) / 255.0f)}); | ||
| 361 | op(w, 43, {3, 22, bits(0.0f)}); | ||
| 362 | op(w, 43, {3, 23, bits(1.0f)}); | ||
| 363 | op(w, 44, {6, 24, 20, 21, 22, 23}); | ||
| 364 | op(w, 54, {1, 4, 0, 2}); | ||
| 365 | op(w, 248, {11}); | ||
| 366 | op(w, 62, {9, 24}); | ||
| 367 | op(w, 253, {}); | ||
| 368 | op(w, 56, {}); | ||
| 369 | return w; | ||
| 370 | } | ||
| 371 | |||
| 372 | std::vector<std::uint8_t> program(unsigned stage, unsigned n) { | ||
| 373 | return d3d12test::fssm_program({0x42434f00u + stage, n, 0xbf810000u}); | ||
| 374 | } | ||
| 375 | |||
| 376 | std::vector<bigmodule::Program> programs(kCompute); // what each compute module runs | ||
| 377 | std::vector<std::size_t> module_bytes(kCompute); | ||
| 378 | |||
| 379 | bool write_library(const std::filesystem::path& dir) { | ||
| 380 | const auto write = [&](const std::string& name, const Words& words) { | ||
| 381 | std::ofstream f(dir / (name + ".spv"), std::ios::binary | std::ios::trunc); | ||
| 382 | f.write(reinterpret_cast<const char*>(words.data()), std::streamsize(words.size() * 4)); | ||
| 383 | return bool(f); | ||
| 384 | }; | ||
| 385 | CHECK(write("vs", vertex_words())); | ||
| 386 | std::ofstream index(dir / "index.json", std::ios::binary | std::ios::trunc); | ||
| 387 | index << "{\"vs\": {\"stage\": \"vs\", \"fssm_sha256\": \"" << leonida::sha256_hex(program(0, 0)) | ||
| 388 | << "\", \"bindings\": [], \"push_constant_bytes\": 144, \"input_signature\": []}"; | ||
| 389 | for (unsigned n = 0; n < kGraphics; ++n) { | ||
| 390 | const std::string name = "ps" + std::to_string(n); | ||
| 391 | CHECK(write(name, fragment_words(n))); | ||
| 392 | index << ",\n\"" << name << "\": {\"stage\": \"ps\", \"fssm_sha256\": \"" << leonida::sha256_hex(program(1, n)) | ||
| 393 | << "\", \"bindings\": [], \"push_constant_bytes\": 144, \"inputs\": [], \"outputs\": [0]}"; | ||
| 394 | } | ||
| 395 | for (unsigned n = 0; n < kCompute; ++n) { | ||
| 396 | const std::string name = "cs" + std::to_string(n); | ||
| 397 | // Every large one a little larger than the one before, so that the order by size is known. | ||
| 398 | const Words words = bigmodule::make(n < kBig ? kBigBytes + n * 1024 : kSmallBytes, 20251125 + n, &programs[n]); | ||
| 399 | module_bytes[n] = words.size() * 4; | ||
| 400 | CHECK((module_bytes[n] > 128 * 1024) == (n < kBig)); | ||
| 401 | hooks.programs[hash(words.data(), words.size())] = int(n); | ||
| 402 | CHECK(write(name, words)); | ||
| 403 | index << ",\n\"" << name << "\": {\"stage\": \"cs\", \"fssm_sha256\": \"" << leonida::sha256_hex(program(2, n)) | ||
| 404 | << "\", \"bindings\": [], \"push_constant_bytes\": 144, \"local_size\": [1, 1, 1], \"wave64\": false}"; | ||
| 405 | } | ||
| 406 | index << "}"; | ||
| 407 | return bool(index); | ||
| 408 | } | ||
| 409 | |||
| 410 | // ---- The device and what it runs. ---- | ||
| 411 | |||
| 412 | struct Setup { | ||
| 413 | vktest::Device* vk; | ||
| 414 | std::string library; | ||
| 415 | |||
| 416 | bool device(const char* threads, Ref<ID3D12Device>& out) const { | ||
| 417 | _putenv_s("LEONIDA_PIPELINE_THREADS", threads); | ||
| 418 | rt::d3d12::VulkanDeviceInfo info{}; | ||
| 419 | info.get_instance_proc = instance_proc; | ||
| 420 | info.instance = vk->instance; | ||
| 421 | info.physical = vk->physical; | ||
| 422 | info.device = vk->device; | ||
| 423 | info.queue = vk->queue; | ||
| 424 | info.queue_family = vk->family; | ||
| 425 | info.buffer_device_address = true; | ||
| 426 | info.subgroup = vk->subgroup; | ||
| 427 | info.features = vk->enabled; | ||
| 428 | info.shader_dir = library; | ||
| 429 | CHECK(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&out.p)))); | ||
| 430 | return true; | ||
| 431 | } | ||
| 432 | }; | ||
| 433 | |||
| 434 | struct Pipelines { | ||
| 435 | Ref<ID3D12RootSignature> root; | ||
| 436 | std::vector<ID3D12PipelineState*> compute, graphics; | ||
| 437 | ~Pipelines() { release(); } | ||
| 438 | void release() { | ||
| 439 | for (auto* s : compute) | ||
| 440 | if (s) s->Release(); | ||
| 441 | for (auto* s : graphics) s->Release(); | ||
| 442 | compute.clear(); | ||
| 443 | graphics.clear(); | ||
| 444 | } | ||
| 445 | |||
| 446 | static ID3D12PipelineState* compute_state(ID3D12Device* dev, ID3D12RootSignature* root, unsigned n) { | ||
| 447 | const auto cs = d3d12test::shader_container(program(2, n)); | ||
| 448 | D3D12_COMPUTE_PIPELINE_STATE_DESC pd{}; | ||
| 449 | pd.pRootSignature = root; | ||
| 450 | pd.CS = {cs.data(), cs.size()}; | ||
| 451 | ID3D12PipelineState* state = nullptr; | ||
| 452 | return SUCCEEDED(dev->CreateComputePipelineState(&pd, IID_PPV_ARGS(&state))) ? state : nullptr; | ||
| 453 | } | ||
| 454 | |||
| 455 | // The compute states on `threads` threads. | ||
| 456 | bool create(ID3D12Device* dev, unsigned threads = 1) { | ||
| 457 | D3D12_ROOT_PARAMETER params[2]{}; | ||
| 458 | params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; | ||
| 459 | params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; | ||
| 460 | params[1].Constants.Num32BitValues = 1; | ||
| 461 | D3D12_VERSIONED_ROOT_SIGNATURE_DESC desc{}; | ||
| 462 | desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; | ||
| 463 | desc.Desc_1_0 = {2, params, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; | ||
| 464 | const auto blob = d3d12test::serialize_root_signature(desc); | ||
| 465 | CHECK(SUCCEEDED(dev->CreateRootSignature(0, blob.data(), blob.size(), IID_PPV_ARGS(&root.p)))); | ||
| 466 | compute.assign(kCompute, nullptr); | ||
| 467 | std::vector<std::thread> creating; | ||
| 468 | for (unsigned t = 1; t < threads; ++t) | ||
| 469 | creating.emplace_back([&, t] { | ||
| 470 | for (unsigned n = t; n < kCompute; n += threads) compute[n] = compute_state(dev, root.p, n); | ||
| 471 | }); | ||
| 472 | for (unsigned n = 0; n < kCompute; n += threads) compute[n] = compute_state(dev, root.p, n); | ||
| 473 | for (auto& t : creating) t.join(); | ||
| 474 | CHECK(std::find(compute.begin(), compute.end(), nullptr) == compute.end()); | ||
| 475 | const auto vs = d3d12test::shader_container(program(0, 0)); | ||
| 476 | for (unsigned n = 0; n < kGraphics; ++n) { | ||
| 477 | const auto ps = d3d12test::shader_container(program(1, n)); | ||
| 478 | D3D12_GRAPHICS_PIPELINE_STATE_DESC pd{}; | ||
| 479 | pd.pRootSignature = root.p; | ||
| 480 | pd.VS = {vs.data(), vs.size()}; | ||
| 481 | pd.PS = {ps.data(), ps.size()}; | ||
| 482 | for (auto& rt : pd.BlendState.RenderTarget) { | ||
| 483 | rt.SrcBlend = rt.SrcBlendAlpha = D3D12_BLEND_ONE; | ||
| 484 | rt.DestBlend = rt.DestBlendAlpha = D3D12_BLEND_ZERO; | ||
| 485 | rt.BlendOp = rt.BlendOpAlpha = D3D12_BLEND_OP_ADD; | ||
| 486 | rt.LogicOp = D3D12_LOGIC_OP_NOOP; | ||
| 487 | rt.RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; | ||
| 488 | } | ||
| 489 | pd.SampleMask = UINT_MAX; | ||
| 490 | pd.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; | ||
| 491 | pd.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; | ||
| 492 | pd.RasterizerState.DepthClipEnable = TRUE; | ||
| 493 | pd.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; | ||
| 494 | pd.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; | ||
| 495 | pd.NumRenderTargets = 1; | ||
| 496 | pd.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 497 | pd.SampleDesc = {1, 0}; | ||
| 498 | ID3D12PipelineState* state = nullptr; | ||
| 499 | CHECK(SUCCEEDED(dev->CreateGraphicsPipelineState(&pd, IID_PPV_ARGS(&state)))); | ||
| 500 | graphics.push_back(state); | ||
| 501 | } | ||
| 502 | return true; | ||
| 503 | } | ||
| 504 | }; | ||
| 505 | |||
| 506 | // A queue and a list that dispatches `compute` (and draws the graphics pipelines), then copies what they wrote back. | ||
| 507 | struct Frame { | ||
| 508 | std::vector<unsigned> compute; | ||
| 509 | bool graphics = false; | ||
| 510 | Ref<ID3D12Resource> target, buffer, readback; | ||
| 511 | Ref<ID3D12DescriptorHeap> rtvs; | ||
| 512 | Ref<ID3D12CommandQueue> queue; | ||
| 513 | Ref<ID3D12CommandAllocator> allocator; | ||
| 514 | Ref<ID3D12GraphicsCommandList> list; | ||
| 515 | Ref<ID3D12Fence> fence; | ||
| 516 | UINT64 fence_value = 0; | ||
| 517 | D3D12_PLACED_SUBRESOURCE_FOOTPRINT foot{}; | ||
| 518 | |||
| 519 | bool create(ID3D12Device* dev, const Pipelines& p, std::vector<unsigned> dispatched, bool draws) { | ||
| 520 | compute = std::move(dispatched); | ||
| 521 | graphics = draws; | ||
| 522 | const D3D12_HEAP_PROPERTIES def{D3D12_HEAP_TYPE_DEFAULT}, back{D3D12_HEAP_TYPE_READBACK}; | ||
| 523 | D3D12_RESOURCE_DESC td{}; | ||
| 524 | td.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 525 | td.Width = td.Height = kExtent; | ||
| 526 | td.DepthOrArraySize = td.MipLevels = 1; | ||
| 527 | td.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 528 | td.SampleDesc = {1, 0}; | ||
| 529 | td.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; | ||
| 530 | CHECK(SUCCEEDED(dev->CreateCommittedResource(&def, D3D12_HEAP_FLAG_NONE, &td, D3D12_RESOURCE_STATE_RENDER_TARGET, | ||
| 531 | nullptr, IID_PPV_ARGS(&target.p)))); | ||
| 532 | const auto bd = d3d12test::buffer_desc(4 * kWords, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); | ||
| 533 | CHECK(SUCCEEDED(dev->CreateCommittedResource(&def, D3D12_HEAP_FLAG_NONE, &bd, | ||
| 534 | D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr, | ||
| 535 | IID_PPV_ARGS(&buffer.p)))); | ||
| 536 | UINT64 image_bytes = 0; | ||
| 537 | dev->GetCopyableFootprints(&td, 0, 1, 0, &foot, nullptr, nullptr, &image_bytes); | ||
| 538 | CHECK(image_bytes <= 4096); | ||
| 539 | const auto rd = d3d12test::buffer_desc(4096 + 4 * kWords); | ||
| 540 | CHECK(SUCCEEDED(dev->CreateCommittedResource(&back, D3D12_HEAP_FLAG_NONE, &rd, D3D12_RESOURCE_STATE_COPY_DEST, | ||
| 541 | nullptr, IID_PPV_ARGS(&readback.p)))); | ||
| 542 | const D3D12_DESCRIPTOR_HEAP_DESC hd{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1}; | ||
| 543 | CHECK(SUCCEEDED(dev->CreateDescriptorHeap(&hd, IID_PPV_ARGS(&rtvs.p)))); | ||
| 544 | const auto rtv = rtvs->GetCPUDescriptorHandleForHeapStart(); | ||
| 545 | dev->CreateRenderTargetView(target.p, nullptr, rtv); | ||
| 546 | const D3D12_COMMAND_QUEUE_DESC qd{D3D12_COMMAND_LIST_TYPE_DIRECT}; | ||
| 547 | CHECK(SUCCEEDED(dev->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue.p)))); | ||
| 548 | CHECK(SUCCEEDED(dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); | ||
| 549 | CHECK(SUCCEEDED(dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); | ||
| 550 | CHECK(SUCCEEDED(dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, | ||
| 551 | IID_PPV_ARGS(&list.p)))); | ||
| 552 | const FLOAT clear[4] = {0, 0, 0, 0}; | ||
| 553 | list->ClearRenderTargetView(rtv, clear, 0, nullptr); | ||
| 554 | list->SetComputeRootSignature(p.root.p); | ||
| 555 | list->SetComputeRootUnorderedAccessView(0, buffer->GetGPUVirtualAddress()); | ||
| 556 | for (const unsigned n : compute) { | ||
| 557 | list->SetPipelineState(p.compute[n]); | ||
| 558 | list->SetComputeRoot32BitConstant(1, n, 0); | ||
| 559 | list->Dispatch(1, 1, 1); | ||
| 560 | } | ||
| 561 | if (graphics) { | ||
| 562 | list->SetGraphicsRootSignature(p.root.p); | ||
| 563 | list->OMSetRenderTargets(1, &rtv, FALSE, nullptr); | ||
| 564 | list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); | ||
| 565 | const D3D12_VIEWPORT vp{0, 0, float(kExtent), float(kExtent), 0, 1}; | ||
| 566 | list->RSSetViewports(1, &vp); | ||
| 567 | for (unsigned n = 0; n < kGraphics; ++n) { | ||
| 568 | const D3D12_RECT cell{LONG(2 * n), 0, LONG(2 * n + 2), 2}; | ||
| 569 | list->RSSetScissorRects(1, &cell); | ||
| 570 | list->SetPipelineState(p.graphics[n]); | ||
| 571 | list->DrawInstanced(3, 1, 0, 0); | ||
| 572 | } | ||
| 573 | } | ||
| 574 | const D3D12_RESOURCE_BARRIER to_source[2] = { | ||
| 575 | d3d12test::transition(target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE), | ||
| 576 | d3d12test::transition(buffer.p, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE)}; | ||
| 577 | list->ResourceBarrier(2, to_source); | ||
| 578 | D3D12_TEXTURE_COPY_LOCATION src{target.p, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; | ||
| 579 | src.SubresourceIndex = 0; | ||
| 580 | D3D12_TEXTURE_COPY_LOCATION dst{readback.p, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; | ||
| 581 | dst.PlacedFootprint = foot; | ||
| 582 | list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); | ||
| 583 | list->CopyBufferRegion(readback.p, 4096, buffer.p, 0, 4 * kWords); | ||
| 584 | const D3D12_RESOURCE_BARRIER back_again[2] = { | ||
| 585 | d3d12test::transition(target.p, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET), | ||
| 586 | d3d12test::transition(buffer.p, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)}; | ||
| 587 | list->ResourceBarrier(2, back_again); | ||
| 588 | CHECK(SUCCEEDED(list->Close())); | ||
| 589 | return true; | ||
| 590 | } | ||
| 591 | |||
| 592 | ReplayResult submit() { | ||
| 593 | ID3D12CommandList* lists[] = {list.p}; | ||
| 594 | queue->ExecuteCommandLists(1, lists); | ||
| 595 | const ReplayResult result = rt::d3d12::last_execute_result(queue.p); | ||
| 596 | queue->Signal(fence.p, ++fence_value); | ||
| 597 | fence->SetEventOnCompletion(fence_value, nullptr); | ||
| 598 | return result; | ||
| 599 | } | ||
| 600 | |||
| 601 | // What the shaders stored and drew; `dropped` is the compute program that was not built, whose word stays 0. The | ||
| 602 | // readback buffer is cleared, so stale bytes cannot pass. | ||
| 603 | bool right(int dropped = -1) { | ||
| 604 | void* mapped = nullptr; | ||
| 605 | CHECK(SUCCEEDED(readback->Map(0, nullptr, &mapped)) && mapped); | ||
| 606 | auto* bytes = static_cast<std::uint8_t*>(mapped); | ||
| 607 | bool ok = true; | ||
| 608 | std::uint32_t words[kWords]; | ||
| 609 | std::memcpy(words, bytes + 4096, sizeof words); | ||
| 610 | for (const unsigned n : compute) { | ||
| 611 | const std::uint32_t want = int(n) == dropped ? 0 : programs[n].run(n); | ||
| 612 | if (words[n] != want) { | ||
| 613 | std::fprintf(stderr, "compute program %u stored 0x%08x, not 0x%08x\n", n, words[n], want); | ||
| 614 | ok = false; | ||
| 615 | } | ||
| 616 | } | ||
| 617 | for (unsigned n = 0; graphics && n < kGraphics; ++n) | ||
| 618 | for (unsigned y = 0; y < 2; ++y) | ||
| 619 | for (unsigned x = 2 * n; x < 2 * n + 2; ++x) { | ||
| 620 | const auto* pixel = bytes + foot.Offset + y * foot.Footprint.RowPitch + 4 * x; | ||
| 621 | if (pixel[0] != n || pixel[1] != 255 - n || pixel[2] != 0 || pixel[3] != 255) { | ||
| 622 | std::fprintf(stderr, "graphics pipeline %u drew %u %u %u %u\n", n, pixel[0], pixel[1], pixel[2], | ||
| 623 | pixel[3]); | ||
| 624 | ok = false; | ||
| 625 | } | ||
| 626 | } | ||
| 627 | std::memset(bytes, 0xee, 4096 + 4 * kWords); | ||
| 628 | readback->Unmap(0, nullptr); | ||
| 629 | return ok; | ||
| 630 | } | ||
| 631 | }; | ||
| 632 | |||
| 633 | std::vector<unsigned> all_compute() { | ||
| 634 | std::vector<unsigned> out; | ||
| 635 | for (unsigned n = 0; n < kCompute; ++n) out.push_back(n); | ||
| 636 | return out; | ||
| 637 | } | ||
| 638 | |||
| 639 | std::uint64_t estimate(unsigned n) { return leonida::large_module_estimate(module_bytes[n]); } | ||
| 640 | |||
| 641 | // How many of the large compute modules are in the driver at once, with five threads to compile them; 0: not known (the | ||
| 642 | // budget is read from the machine). | ||
| 643 | int expected_at_once() { | ||
| 644 | if (serial) return 1; | ||
| 645 | if (unlimited) return 5; | ||
| 646 | if (!fixed_budget) return 0; | ||
| 647 | // The first wave is the largest, in order, while they fit. | ||
| 648 | std::uint64_t sum = 0; | ||
| 649 | int count = 0; | ||
| 650 | for (unsigned n = kBig; n-- > 0 && sum + estimate(n) <= *fixed_budget; ++count) sum += estimate(n); | ||
| 651 | return std::clamp(count, 1, 5); | ||
| 652 | } | ||
| 653 | |||
| 654 | // The large compute modules are held back in the driver until as many as may be are in, and no more come. | ||
| 655 | bool side_by_side(const Setup& setup) { | ||
| 656 | Ref<ID3D12Device> dev; | ||
| 657 | CHECK(setup.device("4", dev)); | ||
| 658 | Pipelines pipelines; | ||
| 659 | Frame frame; | ||
| 660 | hooks.reset(); | ||
| 661 | CHECK(pipelines.create(dev.p) && frame.create(dev.p, pipelines, all_compute(), false)); | ||
| 662 | CHECK(hooks.creates() == 0); // nothing is compiled at creation | ||
| 663 | const int at_once = expected_at_once(); | ||
| 664 | hooks.closed = true; | ||
| 665 | ReplayResult result = ReplayResult::unsupported; | ||
| 666 | std::thread submitting([&] { result = frame.submit(); }); | ||
| 667 | const bool filled = hooks.wait_large(std::max(at_once, 1)); | ||
| 668 | // With a thread to spare, the small ones are compiled while the large ones wait (unlimited: every thread has a large one). | ||
| 669 | const bool small_ones = unlimited || !at_once || hooks.wait([] { return hooks.small_done == int(kSmall); }); | ||
| 670 | std::this_thread::sleep_for(300ms); | ||
| 671 | int held = 0; | ||
| 672 | { | ||
| 673 | std::lock_guard lock(hooks.mutex); | ||
| 674 | held = hooks.large_in; | ||
| 675 | } | ||
| 676 | hooks.open(); | ||
| 677 | submitting.join(); | ||
| 678 | CHECK(filled); | ||
| 679 | CHECK(!at_once || held == at_once); | ||
| 680 | CHECK(small_ones); | ||
| 681 | CHECK(result == ReplayResult::success && frame.right()); | ||
| 682 | CHECK(hooks.over == 0); | ||
| 683 | CHECK(hooks.creates() == kCompute); // each once | ||
| 684 | CHECK(!at_once || hooks.most_large <= at_once || unlimited); | ||
| 685 | CHECK(frame.submit() == ReplayResult::success && frame.right() && hooks.creates() == kCompute); | ||
| 686 | return true; | ||
| 687 | } | ||
| 688 | |||
| 689 | // The same values whatever compiles them, without workers (each where prepare() meets it) and with them. | ||
| 690 | bool same_values(const Setup& setup) { | ||
| 691 | for (const char* threads : {"0", "16"}) { | ||
| 692 | Ref<ID3D12Device> dev; | ||
| 693 | CHECK(setup.device(threads, dev)); | ||
| 694 | Pipelines pipelines; | ||
| 695 | Frame frame; | ||
| 696 | hooks.reset(); | ||
| 697 | CHECK(pipelines.create(dev.p) && frame.create(dev.p, pipelines, all_compute(), true)); | ||
| 698 | CHECK(frame.submit() == ReplayResult::success && frame.right()); | ||
| 699 | CHECK(hooks.over == 0); | ||
| 700 | CHECK(hooks.creates() == kCompute + kGraphics); | ||
| 701 | CHECK(*threads != '0' || hooks.most_large == 1); | ||
| 702 | } | ||
| 703 | return true; | ||
| 704 | } | ||
| 705 | |||
| 706 | // A graphics pipeline whose estimate is past the budget (or the one at a time of serial) is in alone among the large ones, | ||
| 707 | // and the small ones are compiled beside it. | ||
| 708 | bool graphics_among_the_large(const Setup& setup) { | ||
| 709 | Ref<ID3D12Device> dev; | ||
| 710 | CHECK(setup.device("4", dev)); | ||
| 711 | Pipelines pipelines; | ||
| 712 | Frame frame; | ||
| 713 | hooks.reset(); | ||
| 714 | CHECK(pipelines.create(dev.p) && frame.create(dev.p, pipelines, all_compute(), true)); | ||
| 715 | hooks.closed = true; | ||
| 716 | ReplayResult result = ReplayResult::unsupported; | ||
| 717 | std::thread submitting([&] { result = frame.submit(); }); | ||
| 718 | const bool one = hooks.wait_large(1); | ||
| 719 | const bool small_ones = unlimited || !expected_at_once() || | ||
| 720 | hooks.wait([] { return hooks.small_done == int(kSmall); }); | ||
| 721 | std::this_thread::sleep_for(300ms); | ||
| 722 | int held = 0; | ||
| 723 | bool first_is_graphics = false; | ||
| 724 | { | ||
| 725 | std::lock_guard lock(hooks.mutex); | ||
| 726 | held = hooks.large_in; | ||
| 727 | for (const Start& s : hooks.started) | ||
| 728 | if (s.large) { | ||
| 729 | first_is_graphics = s.program < 0; | ||
| 730 | break; | ||
| 731 | } | ||
| 732 | } | ||
| 733 | hooks.open(); | ||
| 734 | submitting.join(); | ||
| 735 | CHECK(one && small_ones); | ||
| 736 | if (budgeted && fixed_budget) CHECK(held == 1 && first_is_graphics); // the largest first | ||
| 737 | if (serial) CHECK(held == 1); | ||
| 738 | CHECK(result == ReplayResult::success && frame.right()); | ||
| 739 | CHECK(hooks.over == 0); | ||
| 740 | CHECK(hooks.creates() == kCompute + kGraphics); | ||
| 741 | return true; | ||
| 742 | } | ||
| 743 | |||
| 744 | // Two large graphics pipelines and nothing else: one after the other, or both at once without a limit. | ||
| 745 | bool graphics_only(const Setup& setup) { | ||
| 746 | Ref<ID3D12Device> dev; | ||
| 747 | CHECK(setup.device("4", dev)); | ||
| 748 | Pipelines pipelines; | ||
| 749 | Frame frame; | ||
| 750 | hooks.reset(); | ||
| 751 | CHECK(pipelines.create(dev.p) && frame.create(dev.p, pipelines, {}, true)); | ||
| 752 | hooks.closed = true; | ||
| 753 | ReplayResult result = ReplayResult::unsupported; | ||
| 754 | std::thread submitting([&] { result = frame.submit(); }); | ||
| 755 | const int at_once = unlimited ? 2 : 1; | ||
| 756 | const bool filled = hooks.wait_large(at_once); | ||
| 757 | std::this_thread::sleep_for(300ms); | ||
| 758 | int held = 0; | ||
| 759 | { | ||
| 760 | std::lock_guard lock(hooks.mutex); | ||
| 761 | held = hooks.large_in; | ||
| 762 | } | ||
| 763 | hooks.open(); | ||
| 764 | submitting.join(); | ||
| 765 | CHECK(filled); | ||
| 766 | if (!budgeted || fixed_budget) CHECK(held == at_once); | ||
| 767 | CHECK(result == ReplayResult::success && frame.right()); | ||
| 768 | CHECK(hooks.over == 0 && hooks.creates() == kGraphics); | ||
| 769 | return true; | ||
| 770 | } | ||
| 771 | |||
| 772 | // The driver refuses one large compute module: its dispatch is dropped, the rest runs, and it is not tried again. Its place | ||
| 773 | // goes to the next. | ||
| 774 | bool refused(const Setup& setup) { | ||
| 775 | for (const char* threads : {"0", "4"}) { | ||
| 776 | Ref<ID3D12Device> dev; | ||
| 777 | CHECK(setup.device(threads, dev)); | ||
| 778 | Pipelines pipelines; | ||
| 779 | Frame frame; | ||
| 780 | hooks.reset(); | ||
| 781 | CHECK(pipelines.create(dev.p) && frame.create(dev.p, pipelines, all_compute(), false)); | ||
| 782 | hooks.fail_large = 2; | ||
| 783 | CHECK(frame.submit() == ReplayResult::success); | ||
| 784 | int dropped = -1; | ||
| 785 | { | ||
| 786 | std::lock_guard lock(hooks.mutex); | ||
| 787 | int seen = 0; | ||
| 788 | for (const Start& s : hooks.started) | ||
| 789 | if (s.large && s.program >= 0 && seen++ == 2) dropped = s.program; | ||
| 790 | } | ||
| 791 | CHECK(dropped >= 0); | ||
| 792 | CHECK(frame.right(dropped)); | ||
| 793 | CHECK(hooks.creates() == kCompute); | ||
| 794 | CHECK(frame.submit() == ReplayResult::success && frame.right(dropped)); | ||
| 795 | CHECK(hooks.creates() == kCompute); | ||
| 796 | } | ||
| 797 | return true; | ||
| 798 | } | ||
| 799 | |||
| 800 | // Two queues submit the same pipelines at once: each is compiled once, the budget is kept, and nobody waits for ever. | ||
| 801 | bool two_queues(const Setup& setup) { | ||
| 802 | Ref<ID3D12Device> dev; | ||
| 803 | CHECK(setup.device("4", dev)); | ||
| 804 | Pipelines pipelines; | ||
| 805 | Frame frames[2]; | ||
| 806 | CHECK(pipelines.create(dev.p)); | ||
| 807 | auto backwards = all_compute(); | ||
| 808 | std::reverse(backwards.begin(), backwards.end()); | ||
| 809 | CHECK(frames[0].create(dev.p, pipelines, all_compute(), true)); | ||
| 810 | CHECK(frames[1].create(dev.p, pipelines, backwards, true)); | ||
| 811 | hooks.reset(); | ||
| 812 | ReplayResult results[2]{}; | ||
| 813 | std::thread threads[2]; | ||
| 814 | for (int i = 0; i < 2; ++i) threads[i] = std::thread([&, i] { results[i] = frames[i].submit(); }); | ||
| 815 | for (auto& t : threads) t.join(); | ||
| 816 | for (int i = 0; i < 2; ++i) CHECK(results[i] == ReplayResult::success && frames[i].right()); | ||
| 817 | CHECK(hooks.creates() == kCompute + kGraphics); | ||
| 818 | CHECK(hooks.over == 0); | ||
| 819 | return true; | ||
| 820 | } | ||
| 821 | |||
| 822 | // The device's last references are released by the thread whose submission compiles, with large ones waiting for room. | ||
| 823 | bool device_destroyed(const Setup& setup) { | ||
| 824 | _putenv_s("LEONIDA_PIPELINE_CACHE_BYTES", "1"); // read the cache at teardown, write no file | ||
| 825 | Ref<ID3D12Device> dev; | ||
| 826 | CHECK(setup.device("4", dev)); | ||
| 827 | auto pipelines = std::make_unique<Pipelines>(); | ||
| 828 | auto frame = std::make_unique<Frame>(); | ||
| 829 | hooks.reset(); | ||
| 830 | CHECK(pipelines->create(dev.p) && frame->create(dev.p, *pipelines, all_compute(), true)); | ||
| 831 | hooks.track = true; | ||
| 832 | hooks.closed = true; | ||
| 833 | ReplayResult result = ReplayResult::unsupported; | ||
| 834 | bool ok = false; | ||
| 835 | std::thread submitting([&] { | ||
| 836 | result = frame->submit(); | ||
| 837 | ok = frame->right(); | ||
| 838 | frame.reset(); | ||
| 839 | pipelines.reset(); | ||
| 840 | }); | ||
| 841 | const bool compiling = hooks.wait_large(1); | ||
| 842 | std::this_thread::sleep_for(100ms); | ||
| 843 | dev.reset(); | ||
| 844 | hooks.open(); | ||
| 845 | submitting.join(); | ||
| 846 | _putenv_s("LEONIDA_PIPELINE_CACHE_BYTES", "0"); | ||
| 847 | CHECK(compiling); | ||
| 848 | CHECK(result == ReplayResult::success && ok); | ||
| 849 | CHECK(hooks.cache_reads > 0); | ||
| 850 | CHECK(!hooks.cache_read_during_create); | ||
| 851 | CHECK(!hooks.cache_read_with_workers); | ||
| 852 | CHECK(hooks.live == 0); | ||
| 853 | return true; | ||
| 854 | } | ||
| 855 | |||
| 856 | // One place for large compiles. The warm-up thread has it, and wants the next one after it; a submission that needs four | ||
| 857 | // others, one of which the warm-up thread waits to build, gets them all first, and builds that one itself. | ||
| 858 | bool warmup(const Setup& setup) { | ||
| 859 | Ref<ID3D12Device> dev; | ||
| 860 | CHECK(setup.device("4", dev)); | ||
| 861 | Pipelines pipelines; | ||
| 862 | Frame frame; | ||
| 863 | hooks.reset(); | ||
| 864 | hooks.closed = true; | ||
| 865 | hooks.linger = 100ms; | ||
| 866 | CHECK(pipelines.create(dev.p)); | ||
| 867 | if (!hooks.wait_large(1)) { | ||
| 868 | hooks.open(); | ||
| 869 | CHECK(!"the warm-up thread compiles a large one"); | ||
| 870 | } | ||
| 871 | std::thread::id warm; | ||
| 872 | int first = -1; | ||
| 873 | { | ||
| 874 | std::lock_guard lock(hooks.mutex); | ||
| 875 | for (const Start& s : hooks.started) | ||
| 876 | if (s.large) warm = s.thread, first = s.program; | ||
| 877 | } | ||
| 878 | CHECK(first == 0); // their sizes rise with their numbers | ||
| 879 | const std::vector<unsigned> wanted = {4, 5, 3, 1}; // 1 is what the warm-up thread builds after 0 | ||
| 880 | CHECK(frame.create(dev.p, pipelines, wanted, false)); | ||
| 881 | const std::size_t before = hooks.creates(); | ||
| 882 | ReplayResult result = ReplayResult::unsupported; | ||
| 883 | std::thread submitting([&] { result = frame.submit(); }); | ||
| 884 | std::this_thread::sleep_for(500ms); | ||
| 885 | const bool waits = hooks.creates() == before; // the place is taken | ||
| 886 | hooks.open(); | ||
| 887 | submitting.join(); | ||
| 888 | const bool done = result == ReplayResult::success && frame.right(); | ||
| 889 | rt::d3d12::PipelineCompiler::instance().wait_idle(); // the warm-up thread builds the rest | ||
| 890 | std::vector<Start> started; | ||
| 891 | { | ||
| 892 | std::lock_guard lock(hooks.mutex); | ||
| 893 | started = hooks.started; | ||
| 894 | } | ||
| 895 | CHECK(waits); | ||
| 896 | CHECK(done); | ||
| 897 | CHECK(hooks.over == 0 && hooks.most_large == 1); | ||
| 898 | std::set<int> once; | ||
| 899 | for (const Start& s : started) once.insert(s.program); | ||
| 900 | CHECK(started.size() == kCompute && once.size() == kCompute); // each once | ||
| 901 | std::erase_if(started, [](const Start& s) { return !s.large; }); | ||
| 902 | std::set<int> built; | ||
| 903 | for (std::size_t i = 1; i <= wanted.size(); ++i) { | ||
| 904 | CHECK(started[i].thread != warm); // none of the warm-up's between two of the submission's | ||
| 905 | built.insert(started[i].program); | ||
| 906 | } | ||
| 907 | CHECK(built == std::set<int>(wanted.begin(), wanted.end())); | ||
| 908 | for (std::size_t i = wanted.size() + 1; i < started.size(); ++i) CHECK(started[i].thread == warm); | ||
| 909 | return true; | ||
| 910 | } | ||
| 911 | |||
| 912 | // The large ones that four threads create at once are held back in the driver until as many as may be are in. | ||
| 913 | bool eager(const Setup& setup) { | ||
| 914 | Ref<ID3D12Device> dev; | ||
| 915 | CHECK(setup.device("4", dev)); | ||
| 916 | Pipelines pipelines; | ||
| 917 | Frame frame; | ||
| 918 | hooks.reset(); | ||
| 919 | hooks.closed = true; | ||
| 920 | bool created = false; | ||
| 921 | std::thread creating([&] { created = pipelines.create(dev.p, 4); }); | ||
| 922 | const bool one = hooks.wait_large(1); | ||
| 923 | std::this_thread::sleep_for(300ms); | ||
| 924 | int held = 0; | ||
| 925 | { | ||
| 926 | std::lock_guard lock(hooks.mutex); | ||
| 927 | held = hooks.large_in; | ||
| 928 | } | ||
| 929 | hooks.open(); | ||
| 930 | creating.join(); | ||
| 931 | CHECK(one && created); | ||
| 932 | CHECK(held == expected_at_once()); | ||
| 933 | CHECK(hooks.over == 0 && hooks.creates() == kCompute); | ||
| 934 | CHECK(frame.create(dev.p, pipelines, all_compute(), false)); | ||
| 935 | CHECK(frame.submit() == ReplayResult::success && frame.right()); | ||
| 936 | CHECK(hooks.creates() == kCompute); | ||
| 937 | return true; | ||
| 938 | } | ||
| 939 | |||
| 940 | bool run(const Setup& setup, const std::string& scenario) { | ||
| 941 | if (scenario == "warmup") return warmup(setup); | ||
| 942 | if (scenario == "eager") return eager(setup); | ||
| 943 | return side_by_side(setup) && same_values(setup) && graphics_among_the_large(setup) && graphics_only(setup) && | ||
| 944 | refused(setup) && two_queues(setup) && device_destroyed(setup); | ||
| 945 | } | ||
| 946 | |||
| 947 | } // namespace | ||
| 948 | |||
| 949 | int main(int argc, char** argv) { | ||
| 950 | const std::string scenario = argc > 1 ? argv[1] : ""; | ||
| 951 | vktest::Device vk; | ||
| 952 | vk.request_buffer_device_address = true; | ||
| 953 | const auto status = vk.initialize(); | ||
| 954 | if (status == vktest::DeviceStatus::unavailable) return 77; | ||
| 955 | if (status != vktest::DeviceStatus::ready) return 1; | ||
| 956 | VkPhysicalDeviceProperties properties{}; | ||
| 957 | vk.instance_fn<PFN_vkGetPhysicalDeviceProperties>("vkGetPhysicalDeviceProperties")(vk.physical, &properties); | ||
| 958 | if (!vk.buffer_device_address || !vk.enabled.shaderInt64) { | ||
| 959 | std::printf("SKIP: the GPU lacks bufferDeviceAddress or shaderInt64\n"); | ||
| 960 | return 77; | ||
| 961 | } | ||
| 962 | if (properties.limits.maxPushConstantsSize < 144) { | ||
| 963 | std::printf("SKIP: maxPushConstantsSize %u, below the 144-byte block\n", properties.limits.maxPushConstantsSize); | ||
| 964 | return 77; | ||
| 965 | } | ||
| 966 | real_instance_proc = vk.instance_proc; | ||
| 967 | _putenv_s("LEONIDA_PIPELINE_CACHE_BYTES", "0"); // no session file in or out | ||
| 968 | if (scenario != "warmup") _putenv_s("LEONIDA_COMPUTE_WARMUP", "0"); // every compile is a submission's | ||
| 969 | const testfiles::ScratchDir library("leonida_big_compute_test"); | ||
| 970 | if (!write_library(library.path())) return 1; | ||
| 971 | if (!run({&vk, library.path().string()}, scenario)) return 1; | ||
| 972 | if (vk.validation_errors()) { | ||
| 973 | std::fprintf(stderr, "FAIL: %u validation errors\n", vk.validation_errors()); | ||
| 974 | return 1; | ||
| 975 | } | ||
| 976 | std::printf("d3d12_big_compute_test: passed on %s\n", properties.deviceName); | ||
| 977 | return 0; | ||
| 978 | } | ||
diff --git a/runtime/test/gfx10_descriptors_test.cpp b/runtime/test/gfx10_descriptors_test.cpp index efd0ce4ee..f30d7f026 100644 --- a/runtime/test/gfx10_descriptors_test.cpp +++ b/runtime/test/gfx10_descriptors_test.cpp | |||
| @@ -114,6 +114,25 @@ int main() { | |||
| 114 | CHECK(is_image(words) && decode(words, out) && out == in); | 114 | CHECK(is_image(words) && decode(words, out) && out == in); |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | ImageWords volume_words = encode(Image{.type = ImageType::image_3d, .depth = 16, | ||
| 118 | .first_mip = 1, .mip_count = 2, .resource_mips = 4}); | ||
| 119 | volume_words[4] |= 37u << 16; | ||
| 120 | CHECK(decode(volume_words, image) && image.first_layer == 37 && image.depth == 16); | ||
| 121 | CHECK(!has_hardware_volume_uav_slice_offset(volume_words, image)); | ||
| 122 | const auto volume_range = vulkan_subresource_range(image, VK_IMAGE_ASPECT_COLOR_BIT); | ||
| 123 | CHECK(volume_range.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT); | ||
| 124 | CHECK(volume_range.baseMipLevel == 1 && volume_range.levelCount == 2); | ||
| 125 | CHECK(volume_range.baseArrayLayer == 0 && volume_range.layerCount == 1); | ||
| 126 | volume_words = encode(Image{.type = ImageType::image_3d, .depth = 7, .first_mip = 1, .resource_mips = 4}); | ||
| 127 | volume_words[4] |= 2u << 16; | ||
| 128 | volume_words[5] |= 1u; | ||
| 129 | CHECK(decode(volume_words, image) && has_hardware_volume_uav_slice_offset(volume_words, image)); | ||
| 130 | volume_words[4] &= 0xffffu; | ||
| 131 | CHECK(decode(volume_words, image) && !has_hardware_volume_uav_slice_offset(volume_words, image)); | ||
| 132 | |||
| 133 | const auto array_range = vulkan_subresource_range(images[2], VK_IMAGE_ASPECT_COLOR_BIT); | ||
| 134 | CHECK(array_range.baseArrayLayer == 3 && array_range.layerCount == 7); | ||
| 135 | |||
| 117 | // Bits this header does not encode are ignored: SW_MODE and BC_SWIZZLE (word 3), PITCH_MSB (word 4), PERF_MOD | 136 | // Bits this header does not encode are ignored: SW_MODE and BC_SWIZZLE (word 3), PITCH_MSB (word 4), PERF_MOD |
| 118 | // and the rest of word 5, the compression and metadata fields of words 6 and 7, RESOURCE_LEVEL (word 2 bit 31). | 137 | // and the rest of word 5, the compression and metadata fields of words 6 and 7, RESOURCE_LEVEL (word 2 bit 31). |
| 119 | ImageWords busy = encode(images[3]); | 138 | ImageWords busy = encode(images[3]); |
diff --git a/runtime/test/large_module_gate.h b/runtime/test/large_module_gate.h index ea805c926..fcd29ed0e 100644 --- a/runtime/test/large_module_gate.h +++ b/runtime/test/large_module_gate.h | |||
| @@ -1,181 +1,4 @@ | |||
| 1 | // How many large SPIR-V modules vk_pipeline_check lets into the driver at once, by a memory budget. | 1 | // Moved to runtime/src: the runtime uses it too (large_module_gate.h there). |
| 2 | // | ||
| 3 | // Measured on RADV GFX1201 (Mesa 25.3.6) with the Mesa shader cache off: a 5.7 MB translated ray-tracing compute | ||
| 4 | // module peaks at 1.96 GB of process memory compiling alone (~330 bytes per SPIR-V byte), a 0.36 MB one at 94 MB. | ||
| 5 | // Compiling the large ones strictly one at a time left the other threads idle: 40 modules (27 over 4 MiB) on 6 | ||
| 6 | // threads took 308 s exclusively and 75 s in parallel, 7.0 GB and 8.7 GB peak. kBytesPerSpirvByte keeps a margin | ||
| 7 | // over the measurement. | ||
| 8 | // | ||
| 9 | // The process grew past one module's peak even one at a time because glibc keeps freed memory in per-thread arenas | ||
| 10 | // (6 threads: 6.2 GB; MALLOC_ARENA_MAX=1: 2.1 GB, same modules). release_free_memory() hands it back after each | ||
| 11 | // large module. | ||
| 12 | #pragma once | 2 | #pragma once |
| 13 | 3 | ||
| 14 | #include <algorithm> | 4 | #include "../src/large_module_gate.h" |
| 15 | #include <condition_variable> | ||
| 16 | #include <cstdint> | ||
| 17 | #include <cstdio> | ||
| 18 | #include <mutex> | ||
| 19 | #include <string> | ||
| 20 | |||
| 21 | #ifdef _WIN32 | ||
| 22 | #ifndef NOMINMAX | ||
| 23 | #define NOMINMAX | ||
| 24 | #endif | ||
| 25 | #include <windows.h> | ||
| 26 | #endif | ||
| 27 | #if defined(__GLIBC__) | ||
| 28 | #include <malloc.h> | ||
| 29 | #endif | ||
| 30 | |||
| 31 | namespace leonida { | ||
| 32 | |||
| 33 | inline constexpr uint64_t kBytesPerSpirvByte = 512; | ||
| 34 | |||
| 35 | inline uint64_t large_module_estimate(uint64_t spirv_bytes) { return spirv_bytes * kBytesPerSpirvByte; } | ||
| 36 | |||
| 37 | #if defined(__linux__) | ||
| 38 | // The first unsigned number in a file ("max" and unreadable files give no value). | ||
| 39 | inline bool read_u64_file(const std::string& path, uint64_t& value) { | ||
| 40 | std::FILE* f = std::fopen(path.c_str(), "r"); | ||
| 41 | if (!f) return false; | ||
| 42 | unsigned long long v = 0; | ||
| 43 | const bool ok = std::fscanf(f, "%llu", &v) == 1; | ||
| 44 | std::fclose(f); | ||
| 45 | value = v; | ||
| 46 | return ok; | ||
| 47 | } | ||
| 48 | |||
| 49 | // The room left under the cgroup v2 memory.max of this process and of every cgroup above it (a container or | ||
| 50 | // systemd-run -p MemoryMax=...), or UINT64_MAX when none sets a limit. cgroup_root is a parameter for the tests. | ||
| 51 | inline uint64_t cgroup_memory_room(const std::string& self_cgroup_file = "/proc/self/cgroup", | ||
| 52 | const std::string& cgroup_root = "/sys/fs/cgroup") { | ||
| 53 | std::FILE* f = std::fopen(self_cgroup_file.c_str(), "r"); | ||
| 54 | if (!f) return UINT64_MAX; | ||
| 55 | char line[4096]; | ||
| 56 | std::string rel; | ||
| 57 | while (std::fgets(line, sizeof(line), f)) { | ||
| 58 | std::string l(line); | ||
| 59 | if (l.rfind("0::", 0) == 0) { // the cgroup v2 line | ||
| 60 | rel = l.substr(3); | ||
| 61 | while (!rel.empty() && (rel.back() == '\n' || rel.back() == '\r')) rel.pop_back(); | ||
| 62 | break; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | std::fclose(f); | ||
| 66 | uint64_t room = UINT64_MAX; | ||
| 67 | for (std::string dir = rel; !dir.empty();) { | ||
| 68 | // In a cgroup namespace, "/" can be a constrained cgroup mounted as the visible root. | ||
| 69 | const std::string base = dir == "/" ? cgroup_root : cgroup_root + dir; | ||
| 70 | uint64_t max = 0, current = 0; | ||
| 71 | if (read_u64_file(base + "/memory.max", max) && read_u64_file(base + "/memory.current", current)) | ||
| 72 | room = std::min(room, max > current ? max - current : 0); | ||
| 73 | const auto slash = dir.find_last_of('/'); | ||
| 74 | if (dir == "/" || slash == std::string::npos) break; | ||
| 75 | dir.resize(slash == 0 ? 1 : slash); | ||
| 76 | } | ||
| 77 | return room; | ||
| 78 | } | ||
| 79 | #endif | ||
| 80 | |||
| 81 | // Memory the OS could give this process now, or 0 when it can't tell (then the caller keeps the old exclusive gate). | ||
| 82 | inline uint64_t available_memory_bytes() { | ||
| 83 | #ifdef _WIN32 | ||
| 84 | MEMORYSTATUSEX status{}; | ||
| 85 | status.dwLength = sizeof(status); | ||
| 86 | return GlobalMemoryStatusEx(&status) ? uint64_t(status.ullAvailPhys) : 0; | ||
| 87 | #elif defined(__linux__) | ||
| 88 | std::FILE* f = std::fopen("/proc/meminfo", "r"); | ||
| 89 | if (!f) return 0; | ||
| 90 | char line[256]; | ||
| 91 | unsigned long long kb = 0; | ||
| 92 | bool found = false; | ||
| 93 | while (std::fgets(line, sizeof(line), f)) | ||
| 94 | if (std::sscanf(line, "MemAvailable: %llu kB", &kb) == 1) { found = true; break; } | ||
| 95 | std::fclose(f); | ||
| 96 | if (!found) return 0; | ||
| 97 | const uint64_t room = cgroup_memory_room(); | ||
| 98 | return std::max<uint64_t>(1, std::min(uint64_t(kb) * 1024, room)); // 0 would mean "unknown" | ||
| 99 | #else | ||
| 100 | return 0; | ||
| 101 | #endif | ||
| 102 | } | ||
| 103 | |||
| 104 | // Return memory freed by the driver's compiler to the OS (glibc keeps it in the arena of the thread that freed it). | ||
| 105 | inline void release_free_memory() { | ||
| 106 | #if defined(__GLIBC__) | ||
| 107 | malloc_trim(0); | ||
| 108 | #endif | ||
| 109 | } | ||
| 110 | |||
| 111 | // Lets large modules in while their estimates fit the budget and fewer than max_inflight are in. One always gets in | ||
| 112 | // when none is, so a module bigger than the whole budget still compiles (alone among the large ones). | ||
| 113 | class LargeModuleGate { | ||
| 114 | public: | ||
| 115 | LargeModuleGate(uint64_t budget, unsigned max_inflight) | ||
| 116 | : budget_(budget), max_inflight_(max_inflight ? max_inflight : 1) {} | ||
| 117 | |||
| 118 | bool admits(uint64_t estimate) const { | ||
| 119 | std::lock_guard<std::mutex> g(mu_); | ||
| 120 | return admits_locked(estimate); | ||
| 121 | } | ||
| 122 | void enter(uint64_t estimate) { | ||
| 123 | std::unique_lock<std::mutex> l(mu_); | ||
| 124 | cv_.wait(l, [&] { return admits_locked(estimate); }); | ||
| 125 | used_ += estimate; | ||
| 126 | ++inflight_; | ||
| 127 | } | ||
| 128 | void leave(uint64_t estimate) { | ||
| 129 | { | ||
| 130 | std::lock_guard<std::mutex> g(mu_); | ||
| 131 | used_ -= estimate; | ||
| 132 | --inflight_; | ||
| 133 | } | ||
| 134 | cv_.notify_all(); | ||
| 135 | } | ||
| 136 | unsigned inflight() const { | ||
| 137 | std::lock_guard<std::mutex> g(mu_); | ||
| 138 | return inflight_; | ||
| 139 | } | ||
| 140 | uint64_t budget() const { return budget_; } | ||
| 141 | unsigned max_inflight() const { return max_inflight_; } | ||
| 142 | |||
| 143 | // Holds a place from construction until destruction (or release()), then hands freed memory back. | ||
| 144 | class Ticket { | ||
| 145 | public: | ||
| 146 | Ticket() = default; | ||
| 147 | Ticket(LargeModuleGate& gate, uint64_t estimate) : gate_(&gate), estimate_(estimate) { gate.enter(estimate); } | ||
| 148 | Ticket(Ticket&& o) noexcept : gate_(o.gate_), estimate_(o.estimate_) { o.gate_ = nullptr; } | ||
| 149 | Ticket& operator=(Ticket&& o) noexcept { | ||
| 150 | if (this != &o) { release(); gate_ = o.gate_; estimate_ = o.estimate_; o.gate_ = nullptr; } | ||
| 151 | return *this; | ||
| 152 | } | ||
| 153 | Ticket(const Ticket&) = delete; | ||
| 154 | Ticket& operator=(const Ticket&) = delete; | ||
| 155 | ~Ticket() { release(); } | ||
| 156 | void release() { | ||
| 157 | if (!gate_) return; | ||
| 158 | gate_->leave(estimate_); | ||
| 159 | gate_ = nullptr; | ||
| 160 | release_free_memory(); | ||
| 161 | } | ||
| 162 | private: | ||
| 163 | LargeModuleGate* gate_ = nullptr; | ||
| 164 | uint64_t estimate_ = 0; | ||
| 165 | }; | ||
| 166 | |||
| 167 | private: | ||
| 168 | bool admits_locked(uint64_t estimate) const { | ||
| 169 | if (inflight_ == 0) return true; | ||
| 170 | return inflight_ < max_inflight_ && used_ + estimate <= budget_; | ||
| 171 | } | ||
| 172 | |||
| 173 | const uint64_t budget_; | ||
| 174 | const unsigned max_inflight_; | ||
| 175 | mutable std::mutex mu_; | ||
| 176 | std::condition_variable cv_; | ||
| 177 | uint64_t used_ = 0; | ||
| 178 | unsigned inflight_ = 0; | ||
| 179 | }; | ||
| 180 | |||
| 181 | } // namespace leonida | ||
diff --git a/runtime/test/replay_pipeline_compiles_test.cpp b/runtime/test/replay_pipeline_compiles_test.cpp index 45a28f7c2..a8983f7ce 100644 --- a/runtime/test/replay_pipeline_compiles_test.cpp +++ b/runtime/test/replay_pipeline_compiles_test.cpp | |||
| @@ -2,13 +2,16 @@ | |||
| 2 | // prepared twice, once with every pipeline built where prepare() meets it and once with them left to PipelineCompiles | 2 | // prepared twice, once with every pipeline built where prepare() meets it and once with them left to PipelineCompiles |
| 3 | // and built on PipelineWorkers. Both have to give the same result, the same pipeline to every draw and dispatch, the same | 3 | // and built on PipelineWorkers. Both have to give the same result, the same pipeline to every draw and dispatch, the same |
| 4 | // commands dropped, and the same pipelines built, each once. Fixed cases, then random ones. Run a second time with | 4 | // commands dropped, and the same pipelines built, each once. Fixed cases, then random ones. Run a second time with |
| 5 | // LEONIDA_STRICT_REPLAY=1, where a dispatch without a pipeline fails its list. No GPU: pipelines and resources are fakes. | 5 | // LEONIDA_STRICT_REPLAY=1, where a dispatch without a pipeline fails its list. Also which large pipelines compile at once |
| 6 | // within a budget (PipelineCompiles::build). No GPU: pipelines and resources are fakes. | ||
| 7 | #include "../src/large_module_gate.h" | ||
| 6 | #include "../src/pipeline_workers.h" | 8 | #include "../src/pipeline_workers.h" |
| 7 | #include "../src/vulkan_replay.h" | 9 | #include "../src/vulkan_replay.h" |
| 8 | 10 | ||
| 9 | #include <algorithm> | 11 | #include <algorithm> |
| 10 | #include <atomic> | 12 | #include <atomic> |
| 11 | #include <chrono> | 13 | #include <chrono> |
| 14 | #include <condition_variable> | ||
| 12 | #include <cstdint> | 15 | #include <cstdint> |
| 13 | #include <cstdio> | 16 | #include <cstdio> |
| 14 | #include <functional> | 17 | #include <functional> |
| @@ -39,9 +42,10 @@ T handle(std::uintptr_t n) { | |||
| 39 | constexpr ResourceId color_id = 1, depth_id = 2, arguments_id = 3; | 42 | constexpr ResourceId color_id = 1, depth_id = 2, arguments_id = 3; |
| 40 | constexpr std::uint64_t bad_va = 0xbad0000; | 43 | constexpr std::uint64_t bad_va = 0xbad0000; |
| 41 | 44 | ||
| 42 | // The large pipelines being built now, and how often there were two. | 45 | // The large pipelines being built now, and how often there were two, where they are to compile one after another. |
| 43 | std::atomic<int> large_now = 0; | 46 | std::atomic<int> large_now = 0; |
| 44 | std::atomic<int> large_together = 0; | 47 | std::atomic<int> large_together = 0; |
| 48 | std::atomic<bool> large_one_at_a_time = true; | ||
| 45 | 49 | ||
| 46 | // What a pipeline state owns: its variants, built on demand under its own lock, and what was asked of it. | 50 | // What a pipeline state owns: its variants, built on demand under its own lock, and what was asked of it. |
| 47 | struct Owner { | 51 | struct Owner { |
| @@ -60,7 +64,7 @@ struct Owner { | |||
| 60 | if (failed && compute) return VK_NULL_HANDLE; // a refused compute pipeline is not tried again | 64 | if (failed && compute) return VK_NULL_HANDLE; // a refused compute pipeline is not tried again |
| 61 | attempts.push_back(flags); | 65 | attempts.push_back(flags); |
| 62 | if (large) { | 66 | if (large) { |
| 63 | if (++large_now > 1) ++large_together; | 67 | if (++large_now > 1 && large_one_at_a_time) ++large_together; |
| 64 | const auto end = std::chrono::steady_clock::now() + std::chrono::microseconds(20); | 68 | const auto end = std::chrono::steady_clock::now() + std::chrono::microseconds(20); |
| 65 | while (std::chrono::steady_clock::now() < end) std::this_thread::yield(); | 69 | while (std::chrono::steady_clock::now() < end) std::this_thread::yield(); |
| 66 | --large_now; | 70 | --large_now; |
| @@ -84,6 +88,7 @@ struct PipelineSpec { | |||
| 84 | bool fails = false; | 88 | bool fails = false; |
| 85 | bool with_built = true; | 89 | bool with_built = true; |
| 86 | bool large = false; | 90 | bool large = false; |
| 91 | std::uint64_t estimate = 0; | ||
| 87 | bool skipped = false; // compute: never built, its dispatches are dropped | 92 | bool skipped = false; // compute: never built, its dispatches are dropped |
| 88 | bool bindless = false; // compute: needs a set 0, which these functions cannot bind | 93 | bool bindless = false; // compute: needs a set 0, which these functions cannot bind |
| 89 | bool async = false; // its null handle may be waiting for another worker: never left to PipelineCompiles | 94 | bool async = false; // its null handle may be waiting for another worker: never left to PipelineCompiles |
| @@ -114,6 +119,7 @@ struct Registry { | |||
| 114 | ComputePipeline p{}; | 119 | ComputePipeline p{}; |
| 115 | p.layout = handle<VkPipelineLayout>(7); | 120 | p.layout = handle<VkPipelineLayout>(7); |
| 116 | p.large = spec.large; | 121 | p.large = spec.large; |
| 122 | p.estimate = spec.estimate; | ||
| 117 | p.async_build = spec.async; | 123 | p.async_build = spec.async; |
| 118 | p.skipped = spec.skipped; | 124 | p.skipped = spec.skipped; |
| 119 | if (spec.bindless) p.bindless_set = handle<VkDescriptorSet>(6); | 125 | if (spec.bindless) p.bindless_set = handle<VkDescriptorSet>(6); |
| @@ -138,6 +144,7 @@ struct Registry { | |||
| 138 | } | 144 | } |
| 139 | p.mesh = spec.mesh; | 145 | p.mesh = spec.mesh; |
| 140 | p.large = spec.large; | 146 | p.large = spec.large; |
| 147 | p.estimate = spec.estimate; | ||
| 141 | p.vertex_slots = spec.vertices ? 1 : 0; | 148 | p.vertex_slots = spec.vertices ? 1 : 0; |
| 142 | if (spec.rectangle) p.rectangle_variant_topology = 17; | 149 | if (spec.rectangle) p.rectangle_variant_topology = 17; |
| 143 | p.variant = [&owner](std::uint32_t flags) { return owner.build(flags); }; | 150 | p.variant = [&owner](std::uint32_t flags) { return owner.build(flags); }; |
| @@ -273,12 +280,12 @@ struct Outcome { | |||
| 273 | std::multiset<std::pair<std::uintptr_t, std::uint32_t>> built; | 280 | std::multiset<std::pair<std::uintptr_t, std::uint32_t>> built; |
| 274 | std::size_t jobs = 0; | 281 | std::size_t jobs = 0; |
| 275 | std::size_t runs = 0; | 282 | std::size_t runs = 0; |
| 276 | std::size_t units = 0; // what run() was given: the large pipelines together are one | 283 | std::size_t units = 0; // what run() was given: the large pipelines together are one (no budget) |
| 277 | }; | 284 | }; |
| 278 | 285 | ||
| 279 | // `workers` null: prepare() builds each pipeline itself, as without workers. | 286 | // `workers` null: prepare() builds each pipeline itself, as without workers. |
| 280 | Outcome prepare_all(const std::vector<CommandList>& lists, const std::vector<PipelineSpec>& specs, | 287 | Outcome prepare_all(const std::vector<CommandList>& lists, const std::vector<PipelineSpec>& specs, |
| 281 | PipelineWorkers* workers) { | 288 | PipelineWorkers* workers, const std::function<std::uint64_t()>& budget = {}) { |
| 282 | Registry registry(specs); | 289 | Registry registry(specs); |
| 283 | ReplayFunctions vk = functions(registry); | 290 | ReplayFunctions vk = functions(registry); |
| 284 | PipelineCompiles compiles; | 291 | PipelineCompiles compiles; |
| @@ -294,7 +301,8 @@ Outcome prepare_all(const std::vector<CommandList>& lists, const std::vector<Pip | |||
| 294 | ++out.runs; | 301 | ++out.runs; |
| 295 | out.units = count; | 302 | out.units = count; |
| 296 | workers->run(count, job); | 303 | workers->run(count, job); |
| 297 | }); | 304 | }, |
| 305 | budget); | ||
| 298 | if (out.result == ReplayResult::success) | 306 | if (out.result == ReplayResult::success) |
| 299 | for (std::size_t i = 0; i < lists.size(); ++i) { | 307 | for (std::size_t i = 0; i < lists.size(); ++i) { |
| 300 | for (std::size_t c = 0; c < prepared[i].commands.size(); ++c) { | 308 | for (std::size_t c = 0; c < prepared[i].commands.size(); ++c) { |
| @@ -312,10 +320,13 @@ Outcome prepare_all(const std::vector<CommandList>& lists, const std::vector<Pip | |||
| 312 | } | 320 | } |
| 313 | 321 | ||
| 314 | Outcome compare(const std::string& name, const std::vector<PipelineSpec>& specs, | 322 | Outcome compare(const std::string& name, const std::vector<PipelineSpec>& specs, |
| 315 | const std::vector<std::vector<CommandSpec>>& commands, PipelineWorkers& workers) { | 323 | const std::vector<std::vector<CommandSpec>>& commands, PipelineWorkers& workers, |
| 324 | const std::function<std::uint64_t()>& budget = {}) { | ||
| 316 | const auto lists = record(commands); | 325 | const auto lists = record(commands); |
| 317 | const Outcome serial = prepare_all(lists, specs, nullptr); | 326 | const Outcome serial = prepare_all(lists, specs, nullptr); |
| 318 | const Outcome together = prepare_all(lists, specs, &workers); | 327 | large_one_at_a_time = !budget; |
| 328 | const Outcome together = prepare_all(lists, specs, &workers, budget); | ||
| 329 | large_one_at_a_time = true; | ||
| 319 | check(serial.result == together.result, name + ": another result"); | 330 | check(serial.result == together.result, name + ": another result"); |
| 320 | check(serial.pipelines == together.pipelines, name + ": another pipeline for a draw or dispatch"); | 331 | check(serial.pipelines == together.pipelines, name + ": another pipeline for a draw or dispatch"); |
| 321 | if (serial.result == ReplayResult::success) | 332 | if (serial.result == ReplayResult::success) |
| @@ -516,6 +527,7 @@ void random_submissions(PipelineWorkers& workers) { | |||
| 516 | s.with_built = chance(90); | 527 | s.with_built = chance(90); |
| 517 | s.async = chance(8); | 528 | s.async = chance(8); |
| 518 | s.large = chance(15); | 529 | s.large = chance(15); |
| 530 | s.estimate = rng() % 12; | ||
| 519 | s.bindless = chance(2); | 531 | s.bindless = chance(2); |
| 520 | s.skipped = s.compute && chance(4); | 532 | s.skipped = s.compute && chance(4); |
| 521 | if (s.skipped) s.fails = s.pending = true; | 533 | if (s.skipped) s.fails = s.pending = true; |
| @@ -540,8 +552,12 @@ void random_submissions(PipelineWorkers& workers) { | |||
| 540 | c.mesh = c.pipeline <= specs.size() && specs[c.pipeline - 1].mesh; | 552 | c.mesh = c.pipeline <= specs.size() && specs[c.pipeline - 1].mesh; |
| 541 | list.push_back(c); | 553 | list.push_back(c); |
| 542 | } | 554 | } |
| 543 | succeeded += compare("random submission " + std::to_string(round), specs, commands, workers).result == | 555 | const std::uint64_t memory = rng() % 24; // 0: no budget |
| 544 | ReplayResult::success; | 556 | const auto budget = [memory] { return memory; }; |
| 557 | const bool limited = chance(50); | ||
| 558 | succeeded += compare("random submission " + std::to_string(round), specs, commands, workers, | ||
| 559 | limited ? std::function<std::uint64_t()>(budget) : std::function<std::uint64_t()>{}) | ||
| 560 | .result == ReplayResult::success; | ||
| 545 | } | 561 | } |
| 546 | check(succeeded > 1000 && succeeded < 2900, "the random submissions both succeed and fail: " + | 562 | check(succeeded > 1000 && succeeded < 2900, "the random submissions both succeed and fail: " + |
| 547 | std::to_string(succeeded)); | 563 | std::to_string(succeeded)); |
| @@ -582,7 +598,7 @@ void prior_compiles_reuse(PipelineWorkers& workers) { | |||
| 582 | const ReplayResult b1 = build_pipelines( | 598 | const ReplayResult b1 = build_pipelines( |
| 583 | second, std::span(prepared), r1, | 599 | second, std::span(prepared), r1, |
| 584 | [&](std::size_t count, const std::function<void(std::size_t)>& job) { workers.run(count, job); }, | 600 | [&](std::size_t count, const std::function<void(std::size_t)>& job) { workers.run(count, job); }, |
| 585 | &first); | 601 | {}, &first); |
| 586 | check(b1 == ReplayResult::success, "prior_compiles_reuse: pass 1 succeeded"); | 602 | check(b1 == ReplayResult::success, "prior_compiles_reuse: pass 1 succeeded"); |
| 587 | check(registry.owners[0]->attempts.size() == 1 && registry.owners[1]->attempts.size() == 1, | 603 | check(registry.owners[0]->attempts.size() == 1 && registry.owners[1]->attempts.size() == 1, |
| 588 | "prior_compiles_reuse: pass 1 did not re-invoke build() on prior failure"); | 604 | "prior_compiles_reuse: pass 1 did not re-invoke build() on prior failure"); |
| @@ -621,6 +637,267 @@ void serial_stops_at_refused_draw() { | |||
| 621 | "serial_stops: did not compile draws past the first refused draw"); | 637 | "serial_stops: did not compile draws past the first refused draw"); |
| 622 | } | 638 | } |
| 623 | 639 | ||
| 640 | // ---- Large pipelines within a budget. ---- | ||
| 641 | |||
| 642 | // Pipelines that note the order they start in. One with `hold` does not end before `release` is called. | ||
| 643 | struct Shop { | ||
| 644 | struct Item { | ||
| 645 | Shop* shop = nullptr; | ||
| 646 | ComputePipeline pipeline{}; | ||
| 647 | std::string name; | ||
| 648 | bool hold = false; | ||
| 649 | }; | ||
| 650 | std::mutex mutex; | ||
| 651 | std::condition_variable changed; | ||
| 652 | std::vector<std::string> started; | ||
| 653 | std::vector<std::unique_ptr<Item>> items; | ||
| 654 | bool released = false; | ||
| 655 | int large_in = 0, most_large_in = 0; | ||
| 656 | LargeModuleGate* gate = nullptr; // set: a large one takes its place there, as in the layer | ||
| 657 | |||
| 658 | ComputePipeline& add(const std::string& name, std::uint64_t estimate, bool large, bool hold = false) { | ||
| 659 | auto& item = *items.emplace_back(std::make_unique<Item>()); | ||
| 660 | item.shop = this; | ||
| 661 | item.name = name; | ||
| 662 | item.hold = hold; | ||
| 663 | item.pipeline.large = large; | ||
| 664 | item.pipeline.estimate = estimate; | ||
| 665 | item.pipeline.build_context = &item; | ||
| 666 | item.pipeline.build = [](void* context) { | ||
| 667 | auto& i = *static_cast<Item*>(context); | ||
| 668 | Shop& shop = *i.shop; | ||
| 669 | LargeModuleGate::Ticket place; | ||
| 670 | if (shop.gate && i.pipeline.large) place = LargeModuleGate::Ticket(*shop.gate, i.pipeline.estimate); | ||
| 671 | std::unique_lock lock(shop.mutex); | ||
| 672 | shop.started.push_back(i.name); | ||
| 673 | if (i.pipeline.large) shop.most_large_in = std::max(shop.most_large_in, ++shop.large_in); | ||
| 674 | shop.changed.notify_all(); | ||
| 675 | if (i.hold) shop.changed.wait_for(lock, std::chrono::seconds(10), [&] { return shop.released; }); | ||
| 676 | if (i.pipeline.large) --shop.large_in; | ||
| 677 | return handle<VkPipeline>(std::uintptr_t(&i) | 1); | ||
| 678 | }; | ||
| 679 | return item.pipeline; | ||
| 680 | } | ||
| 681 | void fill(PipelineCompiles& compiles) { | ||
| 682 | for (auto& item : items) compiles.add(item->pipeline, PipelineCompiles::User::dispatch); | ||
| 683 | } | ||
| 684 | // Whether `count` have started, within 10 s. | ||
| 685 | bool wait_started(std::size_t count) { | ||
| 686 | std::unique_lock lock(mutex); | ||
| 687 | return changed.wait_for(lock, std::chrono::seconds(10), [&] { return started.size() >= count; }); | ||
| 688 | } | ||
| 689 | void release() { | ||
| 690 | std::lock_guard lock(mutex); | ||
| 691 | released = true; | ||
| 692 | changed.notify_all(); | ||
| 693 | } | ||
| 694 | }; | ||
| 695 | |||
| 696 | // On `threads` threads, which take the jobs in order. | ||
| 697 | void on_threads(unsigned threads, std::size_t count, const std::function<void(std::size_t)>& job) { | ||
| 698 | std::atomic<std::size_t> next = 0; | ||
| 699 | std::vector<std::thread> workers; | ||
| 700 | for (unsigned t = 0; t < threads; ++t) | ||
| 701 | workers.emplace_back([&] { | ||
| 702 | for (std::size_t i; (i = next++) < count;) job(i); | ||
| 703 | }); | ||
| 704 | for (auto& w : workers) w.join(); | ||
| 705 | } | ||
| 706 | |||
| 707 | using Names = std::vector<std::string>; | ||
| 708 | |||
| 709 | // The order and the jobs of the run, on one thread: the largest first, then the others, and the budget read once. | ||
| 710 | void budget_order() { | ||
| 711 | struct Case { | ||
| 712 | std::vector<std::uint64_t> large; | ||
| 713 | std::uint64_t budget; | ||
| 714 | std::size_t lanes; | ||
| 715 | Names order; // of the large ones | ||
| 716 | }; | ||
| 717 | for (const Case& c : {Case{{5, 2, 3, 2, 1}, 5, 3, {"5", "3", "2", "2", "1"}}, | ||
| 718 | Case{{5, 2, 3, 2, 1}, 4, 2, {"5", "3", "2", "2", "1"}}, | ||
| 719 | Case{{5, 2, 3, 2, 1}, 0, 1, {"5", "2", "3", "2", "1"}}, // no budget: in the order they came | ||
| 720 | Case{{5, 2, 3, 2, 1}, 100, 5, {"5", "3", "2", "2", "1"}}, | ||
| 721 | Case{{9}, 5, 1, {"9"}}, // more than the budget: one, alone | ||
| 722 | Case{{9, 9}, 5, 1, {"9", "9"}}, | ||
| 723 | Case{{9, 1}, 5, 1, {"9", "1"}}, | ||
| 724 | Case{{4, 4}, 8, 2, {"4", "4"}}}) { | ||
| 725 | Shop shop; | ||
| 726 | for (const auto size : c.large) shop.add(std::to_string(size), size, true); | ||
| 727 | for (int i = 0; i < 3; ++i) shop.add("s" + std::to_string(i), 0, false); | ||
| 728 | PipelineCompiles compiles; | ||
| 729 | shop.fill(compiles); | ||
| 730 | std::size_t units = 0; | ||
| 731 | unsigned reads = 0; | ||
| 732 | compiles.build([&](std::size_t count, const std::function<void(std::size_t)>& job) { | ||
| 733 | units = count; | ||
| 734 | for (std::size_t i = 0; i < count; ++i) job(i); | ||
| 735 | }, [&] { ++reads; return c.budget; }); | ||
| 736 | Names want = c.order; | ||
| 737 | for (int i = 0; i < 3; ++i) want.push_back("s" + std::to_string(i)); | ||
| 738 | const std::string name = "budget " + std::to_string(c.budget) + " of " + std::to_string(c.large.size()); | ||
| 739 | check(shop.started == want, name + ": another order"); | ||
| 740 | check(units == c.lanes + 3, name + ": " + std::to_string(units) + " jobs, not " + std::to_string(c.lanes + 3)); | ||
| 741 | check(reads == 1, name + ": the budget read " + std::to_string(reads) + " times"); | ||
| 742 | for (std::size_t i = 0; i < shop.items.size(); ++i) | ||
| 743 | check(compiles.built(PipelineCompiles::Ticket(i + 1)) != VK_NULL_HANDLE, name + ": not built"); | ||
| 744 | } | ||
| 745 | // Without a large pipeline the budget is not read, and the jobs are the pipelines. | ||
| 746 | Shop shop; | ||
| 747 | for (int i = 0; i < 3; ++i) shop.add("s" + std::to_string(i), 0, false); | ||
| 748 | PipelineCompiles compiles; | ||
| 749 | shop.fill(compiles); | ||
| 750 | std::size_t units = 0; | ||
| 751 | unsigned reads = 0; | ||
| 752 | compiles.build([&](std::size_t count, const std::function<void(std::size_t)>& job) { | ||
| 753 | units = count; | ||
| 754 | for (std::size_t i = 0; i < count; ++i) job(i); | ||
| 755 | }, [&] { ++reads; return 100u; }); | ||
| 756 | check(units == 3 && reads == 0, "the budget read for pipelines that are not large"); | ||
| 757 | } | ||
| 758 | |||
| 759 | // A large one that does not fit beside the one in is not waited for while others are left: they come first. | ||
| 760 | void budget_others_first() { | ||
| 761 | Shop shop; | ||
| 762 | shop.add("8", 8, true, true); | ||
| 763 | shop.add("4a", 4, true); | ||
| 764 | shop.add("4b", 4, true); | ||
| 765 | for (int i = 0; i < 3; ++i) shop.add("s" + std::to_string(i), 0, false); | ||
| 766 | PipelineCompiles compiles; | ||
| 767 | shop.fill(compiles); | ||
| 768 | std::thread building([&] { | ||
| 769 | compiles.build([&](std::size_t count, const std::function<void(std::size_t)>& job) { | ||
| 770 | check(count == 2 + 3, "two lanes: the two of 4 fit 8 together"); | ||
| 771 | on_threads(2, count, job); | ||
| 772 | }, [] { return 8; }); | ||
| 773 | }); | ||
| 774 | const bool all = shop.wait_started(6); // "8" is held: the other lane does the rest | ||
| 775 | Names seen; | ||
| 776 | { | ||
| 777 | std::lock_guard lock(shop.mutex); | ||
| 778 | seen = shop.started; | ||
| 779 | } | ||
| 780 | shop.release(); | ||
| 781 | building.join(); | ||
| 782 | check(all && seen.size() == 6 && seen[0] == "8", "the large one first"); | ||
| 783 | check(all && std::set<std::string>(seen.begin() + 1, seen.begin() + 4) == std::set<std::string>{"s0", "s1", "s2"}, | ||
| 784 | "the others before the large ones that do not fit"); | ||
| 785 | check(all && seen[4] == "4a" && seen[5] == "4b", "then the next large ones"); | ||
| 786 | } | ||
| 787 | |||
| 788 | // Those that fit together start together, before the others. | ||
| 789 | void budget_fits_together() { | ||
| 790 | Shop shop; | ||
| 791 | shop.add("3", 3, true, true); | ||
| 792 | shop.add("5", 5, true, true); | ||
| 793 | for (int i = 0; i < 2; ++i) shop.add("s" + std::to_string(i), 0, false); | ||
| 794 | PipelineCompiles compiles; | ||
| 795 | shop.fill(compiles); | ||
| 796 | std::thread building([&] { | ||
| 797 | compiles.build([&](std::size_t count, const std::function<void(std::size_t)>& job) { on_threads(2, count, job); }, | ||
| 798 | [] { return 8; }); | ||
| 799 | }); | ||
| 800 | const bool both = shop.wait_started(2); | ||
| 801 | Names seen; | ||
| 802 | { | ||
| 803 | std::lock_guard lock(shop.mutex); | ||
| 804 | seen = shop.started; | ||
| 805 | } | ||
| 806 | shop.release(); | ||
| 807 | building.join(); | ||
| 808 | check(both && std::set<std::string>(seen.begin(), seen.begin() + 2) == std::set<std::string>{"3", "5"}, | ||
| 809 | "two that fit the budget are in together"); | ||
| 810 | check(shop.most_large_in == 2, "two that fit the budget at once"); | ||
| 811 | } | ||
| 812 | |||
| 813 | // The room that a large one leaves is the next large one's, before the others. | ||
| 814 | void budget_room_is_given_back() { | ||
| 815 | Shop shop; | ||
| 816 | shop.add("5", 5, true, true); | ||
| 817 | for (const char* name : {"3a", "3b", "3c"}) shop.add(name, 3, true); | ||
| 818 | for (int i = 0; i < 3; ++i) shop.add("s" + std::to_string(i), 0, false); | ||
| 819 | PipelineCompiles compiles; | ||
| 820 | shop.fill(compiles); | ||
| 821 | std::thread building([&] { | ||
| 822 | compiles.build([&](std::size_t count, const std::function<void(std::size_t)>& job) { | ||
| 823 | check(count == 2 + 3, "two lanes: two of 3 fit 8, three do not"); | ||
| 824 | on_threads(2, count, job); | ||
| 825 | }, [] { return 8; }); | ||
| 826 | }); | ||
| 827 | const bool all = shop.wait_started(7); // "5" is held: the other lane has room for one of 3 at a time | ||
| 828 | Names seen; | ||
| 829 | { | ||
| 830 | std::lock_guard lock(shop.mutex); | ||
| 831 | seen = shop.started; | ||
| 832 | } | ||
| 833 | shop.release(); | ||
| 834 | building.join(); | ||
| 835 | check(all && Names(seen.begin(), seen.begin() + 4) == Names{"5", "3a", "3b", "3c"}, | ||
| 836 | "a large one does not leave its room"); | ||
| 837 | } | ||
| 838 | |||
| 839 | // Graphics and compute pipelines share the budget: what each is expected to take counts alike. | ||
| 840 | void budget_graphics_and_compute() { | ||
| 841 | Shop shop; | ||
| 842 | shop.add("3", 3, true); | ||
| 843 | shop.add("4", 4, true); | ||
| 844 | GraphicsPipeline graphics{}; | ||
| 845 | graphics.large = true; | ||
| 846 | graphics.estimate = 5; | ||
| 847 | graphics.variant = [&](std::uint32_t) { | ||
| 848 | std::lock_guard lock(shop.mutex); | ||
| 849 | shop.started.push_back("5"); | ||
| 850 | return handle<VkPipeline>(5); | ||
| 851 | }; | ||
| 852 | PipelineCompiles compiles; | ||
| 853 | shop.fill(compiles); | ||
| 854 | compiles.add(graphics, 0); | ||
| 855 | std::size_t units = 0; | ||
| 856 | compiles.build([&](std::size_t count, const std::function<void(std::size_t)>& job) { | ||
| 857 | units = count; | ||
| 858 | for (std::size_t i = 0; i < count; ++i) job(i); | ||
| 859 | }, [] { return 8; }); | ||
| 860 | check(shop.started == Names{"5", "4", "3"}, "the largest first, graphics or compute"); | ||
| 861 | check(units == 2, "3 and 4 fit 8, and 5 does not beside them"); | ||
| 862 | } | ||
| 863 | |||
| 864 | // The lanes are no more than could be in: a thread is not kept waiting for room there is none of. | ||
| 865 | void budget_lanes_are_bounded() { | ||
| 866 | Shop shop; | ||
| 867 | for (int i = 0; i < 3; ++i) shop.add("5" + std::to_string(i), 5, true); | ||
| 868 | for (int i = 0; i < 6; ++i) shop.add("s" + std::to_string(i), 0, false); | ||
| 869 | PipelineCompiles compiles; | ||
| 870 | shop.fill(compiles); | ||
| 871 | compiles.build([&](std::size_t count, const std::function<void(std::size_t)>& job) { | ||
| 872 | check(count == 2 + 6, "10 bytes hold two of 5"); | ||
| 873 | on_threads(4, count, job); | ||
| 874 | }, [] { return 10; }); | ||
| 875 | check(shop.started.size() == 9 && shop.most_large_in <= 2, "no more than two large ones at once"); | ||
| 876 | } | ||
| 877 | |||
| 878 | // Every one of them is built once, with the gate of the runtime holding what does not fit, whatever sizes and budget. | ||
| 879 | void budget_random(PipelineWorkers& workers) { | ||
| 880 | std::mt19937 rng(20251127); | ||
| 881 | for (int round = 0; round < 300; ++round) { | ||
| 882 | Shop shop; | ||
| 883 | const std::uint64_t budget = rng() % 20; | ||
| 884 | LargeModuleGate gate([budget] { return budget; }); | ||
| 885 | shop.gate = &gate; | ||
| 886 | const unsigned large = rng() % 10, others = rng() % 10; | ||
| 887 | for (unsigned i = 0; i < large; ++i) shop.add("l" + std::to_string(i), rng() % 12, true); | ||
| 888 | for (unsigned i = 0; i < others; ++i) shop.add("s" + std::to_string(i), 0, false); | ||
| 889 | PipelineCompiles compiles; | ||
| 890 | shop.fill(compiles); | ||
| 891 | compiles.build([&](std::size_t count, const std::function<void(std::size_t)>& job) { workers.run(count, job); }, | ||
| 892 | [budget] { return budget; }); | ||
| 893 | Names names = shop.started; | ||
| 894 | std::sort(names.begin(), names.end()); | ||
| 895 | check(std::adjacent_find(names.begin(), names.end()) == names.end() && names.size() == large + others, | ||
| 896 | "random budget " + std::to_string(round) + ": a pipeline built twice or not at all"); | ||
| 897 | check(gate.inflight() == 0, "random budget " + std::to_string(round) + ": a place not given back"); | ||
| 898 | } | ||
| 899 | } | ||
| 900 | |||
| 624 | } // namespace | 901 | } // namespace |
| 625 | 902 | ||
| 626 | int main() { | 903 | int main() { |
| @@ -631,8 +908,15 @@ int main() { | |||
| 631 | reused_lists(workers); | 908 | reused_lists(workers); |
| 632 | prior_compiles_reuse(workers); | 909 | prior_compiles_reuse(workers); |
| 633 | random_submissions(workers); | 910 | random_submissions(workers); |
| 911 | budget_random(workers); | ||
| 634 | } | 912 | } |
| 635 | serial_stops_at_refused_draw(); | 913 | serial_stops_at_refused_draw(); |
| 914 | budget_order(); | ||
| 915 | budget_others_first(); | ||
| 916 | budget_fits_together(); | ||
| 917 | budget_room_is_given_back(); | ||
| 918 | budget_graphics_and_compute(); | ||
| 919 | budget_lanes_are_bounded(); | ||
| 636 | check(large_together == 0, "two large pipelines were built at once"); | 920 | check(large_together == 0, "two large pipelines were built at once"); |
| 637 | if (failures) return 1; | 921 | if (failures) return 1; |
| 638 | std::printf("replay_pipeline_compiles_test: passed\n"); | 922 | std::printf("replay_pipeline_compiles_test: passed\n"); |