upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/large_module_gate_test.cpp
diff options
context:
space:
mode:
authorc0pperlane <84517100+c0pperlane@users.noreply.github.com>2026-11-25 20:34:18 +0100
committerc0pperlane <84517100+c0pperlane@users.noreply.github.com>2026-11-25 20:53:43 +0100
commit6c6a8cd47d74e5dc94751f4efdaa603e893b20f9 (patch)
treea36cab7d5c5c026e913eb20b8ef3ef65ae15f3df /runtime/test/large_module_gate_test.cpp
parentdb1a22d290f5e86fff9282fce3d4e02e788b05b4 (diff)
vk_pipeline_check: a large module that must wait frees its worker (VK-PIPELINE-CHECK-LARGE-ADMISSION)
A worker took the next index and only then waited in the large-module gate, so with the memory budget full the modules behind a large one sat untaken while spare workers blocked, and the pool ran below --jobs. LargeModuleQueue (large_module_gate.h) now hands out the indices. A large module the gate can't admit yet is put back and the worker takes the next one. The oldest module put back goes first as soon as the gate admits it and no later large module passes it; once every index is out, a worker waits for it. Nothing is put back while the gate admits each large module as it comes, so with one worker or room under the budget the order is unchanged, and --large-parallel 1 and --large-module-bytes 0 never ask the queue to admit. large_module_gate_test: the waiting module frees its worker (fails with the old claim-then-admit order), oldest-first and no double entry, the unchanged order, and every module once under 8 threads. AI-assisted (Claude Code).
Diffstat (limited to 'runtime/test/large_module_gate_test.cpp')
-rw-r--r--runtime/test/large_module_gate_test.cpp150
1 files changed, 150 insertions, 0 deletions
diff --git a/runtime/test/large_module_gate_test.cpp b/runtime/test/large_module_gate_test.cpp
index d114095fd..ce39ec88f 100644
--- a/runtime/test/large_module_gate_test.cpp
+++ b/runtime/test/large_module_gate_test.cpp
@@ -7,7 +7,9 @@
7#include <unistd.h> 7#include <unistd.h>
8#endif 8#endif
9#include <chrono> 9#include <chrono>
10#include <condition_variable>
10#include <cstdio> 11#include <cstdio>
12#include <mutex>
11#include <thread> 13#include <thread>
12#include <vector> 14#include <vector>
13 15
@@ -19,6 +21,7 @@
19} while (0) 21} while (0)
20 22
21using leonida::LargeModuleGate; 23using leonida::LargeModuleGate;
24using leonida::LargeModuleQueue;
22 25
23static int budget_and_count() { 26static int budget_and_count() {
24 LargeModuleGate gate(10, 3); 27 LargeModuleGate gate(10, 3);
@@ -102,6 +105,149 @@ static int threads_never_exceed_the_budget() {
102 return 0; 105 return 0;
103} 106}
104 107
108// A worker of vk_pipeline_check over modules given by their estimate (0: a small one): enter(k) is module k going
109// into the driver, while its claim (and ticket) is held.
110template <class Enter>
111static void run_worker(LargeModuleQueue& queue, const std::vector<uint64_t>& estimates, Enter enter) {
112 while (auto claim = queue.take()) {
113 const uint64_t estimate = estimates[claim->index];
114 if (estimate && !queue.admit(*claim, estimate)) continue;
115 enter(claim->index);
116 }
117}
118
119static int a_waiting_large_module_frees_its_worker() {
120 // Before: the worker took module 0 and waited in the gate behind another worker's large module, and the small
121 // modules 1 and 2 waited untaken until that one was done.
122 LargeModuleGate gate(10, 4);
123 LargeModuleGate::Ticket other(gate, 8); // another worker's large module, in the driver
124 const std::vector<uint64_t> estimates{6, 0, 0}; // 8 + 6 > 10
125 LargeModuleQueue queue(gate, estimates.size());
126 std::mutex mu;
127 std::condition_variable cv;
128 std::vector<size_t> entered, while_other_in;
129 std::thread worker([&] {
130 run_worker(queue, estimates, [&](size_t k) {
131 {
132 std::lock_guard<std::mutex> g(mu);
133 entered.push_back(k);
134 }
135 cv.notify_all();
136 });
137 });
138 {
139 std::unique_lock<std::mutex> l(mu);
140 cv.wait_for(l, std::chrono::seconds(10), [&] { return entered.size() == 2; });
141 while_other_in = entered;
142 }
143 other.release();
144 worker.join();
145 CHECK((while_other_in == std::vector<size_t>{1, 2}));
146 CHECK((entered == std::vector<size_t>{1, 2, 0})); // and module 0 once the other one was done
147 CHECK(gate.inflight() == 0);
148 return 0;
149}
150
151static int the_oldest_module_put_back_goes_first() {
152 LargeModuleGate gate(10, 4);
153 LargeModuleQueue queue(gate, 4);
154 LargeModuleGate::Ticket other(gate, 8);
155 auto a = queue.take();
156 CHECK(a && a->index == 0 && !queue.admit(*a, 6)); // 8 + 6 > 10: put back
157 a.reset();
158 auto b = queue.take();
159 CHECK(b && b->index == 1 && !queue.admit(*b, 2)); // 8 + 2 fits, but module 0 waits ahead of it
160 b.reset();
161 auto c = queue.take();
162 CHECK(c && c->index == 2 && !c->ticket); // a small module meanwhile
163 c.reset();
164 other.release();
165 a = queue.take(); // before the new module 3
166 CHECK(a && a->index == 0 && a->ticket && gate.inflight() == 1);
167 CHECK(queue.admit(*a, 6) && gate.inflight() == 1); // it came back with its place: not counted twice
168 b = queue.take();
169 CHECK(b && b->index == 1 && b->ticket && gate.inflight() == 2); // 6 + 2 fits
170 c = queue.take();
171 CHECK(c && c->index == 3 && !c->ticket);
172 CHECK(!queue.admit(*c, 4) && gate.inflight() == 2); // 6 + 2 + 4 > 10
173 c.reset();
174 a.reset();
175 c = queue.take();
176 CHECK(c && c->index == 3 && c->ticket && gate.inflight() == 2); // 2 + 4 fits
177 b.reset();
178 c.reset();
179 CHECK(!queue.take());
180 CHECK(gate.inflight() == 0);
181 return 0;
182}
183
184static int nothing_is_put_back_while_the_gate_admits() {
185 // One worker: no other large module is in when one comes, so even one over the budget gets in and the order is
186 // the old one.
187 LargeModuleGate gate(10, 2);
188 const std::vector<uint64_t> estimates{6, 0, 100, 6, 0, 9};
189 LargeModuleQueue queue(gate, estimates.size());
190 std::vector<size_t> entered;
191 run_worker(queue, estimates, [&](size_t k) { entered.push_back(k); });
192 CHECK((entered == std::vector<size_t>{0, 1, 2, 3, 4, 5}));
193 // --large-parallel 2 (a count, no memory budget) with a place left beside another worker's large module.
194 LargeModuleGate counted(UINT64_MAX, 2);
195 LargeModuleGate::Ticket other(counted, 8);
196 LargeModuleQueue roomy(counted, 3);
197 entered.clear();
198 run_worker(roomy, {6, 0, 6}, [&](size_t k) { entered.push_back(k); });
199 CHECK((entered == std::vector<size_t>{0, 1, 2}));
200 other.release();
201 // The gate disabled (--large-module-bytes 0) or --large-parallel 1 (its exclusive lock): admit() is never asked,
202 // and the queue hands out 0, 1, 2 as the old counter did.
203 LargeModuleQueue counter(gate, 3);
204 for (size_t k = 0; k < 3; ++k) {
205 auto claim = counter.take();
206 CHECK(claim && claim->index == k && !claim->ticket);
207 }
208 CHECK(!counter.take());
209 CHECK(gate.inflight() == 0 && counted.inflight() == 0);
210 return 0;
211}
212
213static int threads_check_every_module_once() {
214 constexpr uint64_t kBudget = 10;
215 LargeModuleGate gate(kBudget, 3);
216 std::vector<uint64_t> estimates(300);
217 for (size_t k = 0; k < estimates.size(); ++k)
218 estimates[k] = k % 3 ? 0 : 1 + (k * 7) % 12; // a third large, 1..12: some over the budget alone
219 LargeModuleQueue queue(gate, estimates.size());
220 std::mutex mu;
221 std::vector<int> entered(estimates.size());
222 std::atomic<uint64_t> used{0};
223 std::atomic<unsigned> in{0};
224 std::atomic<bool> over{false};
225 std::vector<std::thread> threads;
226 for (int t = 0; t < 8; ++t) {
227 threads.emplace_back([&] {
228 run_worker(queue, estimates, [&](size_t k) {
229 {
230 std::lock_guard<std::mutex> g(mu);
231 ++entered[k];
232 }
233 const uint64_t est = estimates[k];
234 if (!est) return;
235 const uint64_t u = used += est;
236 const unsigned n = ++in;
237 if (n > 3 || (n > 1 && u > kBudget)) over = true;
238 std::this_thread::sleep_for(std::chrono::microseconds(50));
239 --in;
240 used -= est;
241 });
242 });
243 }
244 for (auto& th : threads) th.join();
245 for (const int n : entered) CHECK(n == 1); // every module once, none lost while put back
246 CHECK(!over);
247 CHECK(gate.inflight() == 0);
248 return 0;
249}
250
105static int estimate_and_memory_query() { 251static int estimate_and_memory_query() {
106 CHECK(leonida::large_module_estimate(4u << 20) == (uint64_t(4) << 20) * leonida::kBytesPerSpirvByte); 252 CHECK(leonida::large_module_estimate(4u << 20) == (uint64_t(4) << 20) * leonida::kBytesPerSpirvByte);
107#if defined(__linux__) || defined(_WIN32) 253#if defined(__linux__) || defined(_WIN32)
@@ -162,6 +308,10 @@ int main() {
162 if (int r = max_inflight_zero_means_one()) return r; 308 if (int r = max_inflight_zero_means_one()) return r;
163 if (int r = ticket_releases()) return r; 309 if (int r = ticket_releases()) return r;
164 if (int r = threads_never_exceed_the_budget()) return r; 310 if (int r = threads_never_exceed_the_budget()) return r;
311 if (int r = a_waiting_large_module_frees_its_worker()) return r;
312 if (int r = the_oldest_module_put_back_goes_first()) return r;
313 if (int r = nothing_is_put_back_while_the_gate_admits()) return r;
314 if (int r = threads_check_every_module_once()) return r;
165 if (int r = estimate_and_memory_query()) return r; 315 if (int r = estimate_and_memory_query()) return r;
166 std::puts("large_module_gate: ok"); 316 std::puts("large_module_gate: ok");
167 return 0; 317 return 0;