interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tests / test_omega_gpu_wait.c

304 lines · 14278 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Host test for src/omega_gpu_wait.c: fake memory, fake clock, no GPU.
3 * Every scenario runs against the real primitive (must behave) and against
4 * mutants (a naive wall-clock loop, a fixed wait that accepts overshoot, a
5 * sequence wait with an unsigned compare). Each mutant must be killed by the
6 * scenarios named for it, otherwise this test fails. */
7#include "omega_gpu_wait.h"
8
9#include <stdio.h>
10#include <string.h>
11
12typedef struct {
13 uint64_t t_ns;
14 uint64_t step_ns; /* clock advance per relax */
15 uint64_t relaxes;
16 uint64_t barriers;
17 uint64_t at[32];
18 volatile uint32_t *target[32];
19 uint32_t value[32];
20 int nsched;
21 uint32_t invalid_value; /* valid() rejects this value when nonzero */
22 int abort_at; /* abort() true once relaxes >= abort_at when nonzero */
23 uint64_t snaps; /* polls seen by the snapshot hook (it runs right after the reads) */
24 uint64_t last_snap_barriers;
25 uint64_t order_violations; /* polls whose reads were not preceded by a fresh barrier */
26} Fake;
27
28static uint64_t fk_now(void *c) { return ((Fake *)c)->t_ns; }
29static void fk_barrier(void *c) { ((Fake *)c)->barriers++; }
30static void fk_relax(void *c) {
31 Fake *f = c;
32 f->relaxes++;
33 f->t_ns += f->step_ns;
34 for (int i = 0; i < f->nsched; i++)
35 if (f->at[i] == f->relaxes) *f->target[i] = f->value[i];
36}
37static void fk_snapshot(void *c, uint32_t out[2]) {
38 Fake *f = c;
39 out[0] = out[1] = 0;
40 f->snaps++;
41 if (f->barriers <= f->last_snap_barriers) f->order_violations++;
42 f->last_snap_barriers = f->barriers;
43}
44static bool fk_valid(void *c, uint32_t v) { return !((Fake *)c)->invalid_value || v != ((Fake *)c)->invalid_value; }
45static bool fk_abort(void *c) { Fake *f = c; return f->abort_at && f->relaxes >= (uint64_t)f->abort_at; }
46
47typedef bool (*WaitFn)(volatile uint32_t *, uint32_t, omega_gpu_wait_report_t *, const omega_gpu_wait_cfg_t *);
48
49/* Mutants. mode 0: naive wall clock (single deadline, unsigned <, no progress
50 * tracking, no marker2, no barrier, no report). mode 1: exact for sequence too
51 * is not needed; mode "fixed_ge": signed >= for a fixed wait. mode "seq_unsigned":
52 * unsigned >= for a sequence wait. The last two keep everything else correct. */
53static bool naive_wait(volatile uint32_t *word, uint32_t want, omega_gpu_wait_report_t *rpt,
54 const omega_gpu_wait_cfg_t *cfg) {
55 uint64_t start = cfg->now_ns(cfg->hook_ctx);
56 uint64_t lim = 1000000ull * cfg->progress_timeout_ms;
57 if (rpt) memset(rpt, 0, sizeof(*rpt));
58 while (*word < want) {
59 if (cfg->now_ns(cfg->hook_ctx) - start >= lim) return false;
60 cfg->relax(cfg->hook_ctx);
61 }
62 return true;
63}
64static bool mut_loop(int cmp, volatile uint32_t *w, uint32_t want, omega_gpu_wait_report_t *rpt,
65 const omega_gpu_wait_cfg_t *cfg, omega_gpu_wait_kind_t kind) {
66 /* Correct kind-aware wait with one comparison replaced; delegate the rest
67 * to the real primitive by wrapping the watched value would be circular, so
68 * implement the minimal real loop here. */
69 uint64_t start = cfg->now_ns(cfg->hook_ctx), last = start;
70 uint64_t stall = 1000000ull * cfg->progress_timeout_ms;
71 uint32_t prev = *w;
72 if (rpt) { memset(rpt, 0, sizeof(*rpt)); rpt->wait_kind = kind; rpt->expected = want; }
73 for (;;) {
74 cfg->barrier(cfg->hook_ctx);
75 uint32_t m = *w, m2 = cfg->marker2 ? *cfg->marker2 : 0;
76 bool ok = cmp == 0 ? m == want : cmp == 1 ? (int32_t)(m - want) >= 0 : m >= want;
77 if (rpt) { rpt->last_observed = m; rpt->last_marker2 = m2; }
78 if (ok && (!cfg->marker2 || m2 == cfg->marker2_want)) {
79 cfg->barrier(cfg->hook_ctx);
80 if (rpt) rpt->result = OMEGA_GPU_WAIT_PASS;
81 return true;
82 }
83 uint64_t now = cfg->now_ns(cfg->hook_ctx);
84 if (m != prev) { prev = m; last = now; if (rpt) rpt->progress_count++; }
85 if (now - last >= stall) { if (rpt) rpt->result = OMEGA_GPU_WAIT_STALLED; return false; }
86 cfg->relax(cfg->hook_ctx);
87 }
88}
89static bool mut_fixed_ge(volatile uint32_t *w, uint32_t v, omega_gpu_wait_report_t *r, const omega_gpu_wait_cfg_t *c) {
90 return mut_loop(1, w, v, r, c, OMEGA_GPU_WAIT_FIXED);
91}
92static bool mut_seq_unsigned(volatile uint32_t *w, uint32_t v, omega_gpu_wait_report_t *r, const omega_gpu_wait_cfg_t *c) {
93 return mut_loop(2, w, v, r, c, OMEGA_GPU_WAIT_SEQUENCE);
94}
95
96static int g_fail, g_checks, g_quiet;
97#define CHECK(c, ...) do { g_checks++; if (!(c)) { g_fail++; if (!g_quiet) { printf(" FAIL: " __VA_ARGS__); printf("\n"); } } } while (0)
98
99static void setup(Fake *f, omega_gpu_wait_cfg_t *cfg) {
100 memset(f, 0, sizeof(*f));
101 f->step_ns = 1000000ull; /* 1 ms per poll */
102 memset(cfg, 0, sizeof(*cfg));
103 cfg->progress_timeout_ms = 100;
104 cfg->total_timeout_ms = 100000;
105 cfg->now_ns = fk_now;
106 cfg->barrier = fk_barrier;
107 cfg->relax = fk_relax;
108 cfg->valid = fk_valid;
109 cfg->abort = fk_abort;
110 cfg->hook_ctx = f;
111}
112static void sched(Fake *f, uint64_t at, volatile uint32_t *t, uint32_t v) {
113 f->at[f->nsched] = at; f->target[f->nsched] = t; f->value[f->nsched] = v; f->nsched++;
114}
115
116enum { S_NEVER, S_LATE, S_MARKER2, S_SEQ_WRAP, S_PROGRESS_RESETS, S_BARRIER, S_HARD_DEADLINE,
117 S_FIXED_OVERSHOOT, S_INVALID, S_ABORT, S_COUNT };
118static const char *names[S_COUNT] = {"never_written", "written_late", "marker2_gate", "seq_wrap32",
119 "progress_resets_idle", "barrier_each_read", "hard_deadline_with_progress",
120 "fixed_overshoot", "invalid_state", "abort"};
121
122/* Returns failed checks. fx and sq are the fixed and sequence implementations. */
123static int scenario(const char *name, WaitFn fx, WaitFn sq, int which) {
124 volatile uint32_t w = 0, w2 = 0;
125 Fake f;
126 omega_gpu_wait_cfg_t cfg;
127 omega_gpu_wait_report_t r;
128 int before = g_fail;
129 bool ok;
130 setup(&f, &cfg);
131
132 switch (which) {
133 case S_NEVER:
134 ok = sq(&w, 5, &r, &cfg);
135 CHECK(!ok, "%s: never-written marker must not pass", name);
136 CHECK(r.result == OMEGA_GPU_WAIT_STALLED, "%s: result STALLED (got %d)", name, r.result);
137 CHECK(r.last_observed == 0 && r.expected == 5 && r.wait_kind == OMEGA_GPU_WAIT_SEQUENCE,
138 "%s: report carries kind/expected/last-seen 0", name);
139 CHECK(r.elapsed_ns >= 100000000ull && r.progress_count == 0, "%s: elapsed %llu progress %llu", name,
140 (unsigned long long)r.elapsed_ns, (unsigned long long)r.progress_count);
141 break;
142 case S_LATE:
143 sched(&f, 60, &w, 7);
144 ok = fx(&w, 7, &r, &cfg);
145 CHECK(ok && r.result == OMEGA_GPU_WAIT_PASS && r.last_observed == 7, "%s: late fixed marker seen", name);
146 CHECK(r.progress_count == 1 && r.last_progress_ns > r.start_ns, "%s: progress recorded", name);
147 break;
148 case S_MARKER2:
149 cfg.marker2 = &w2;
150 cfg.marker2_want = 0x46464646u;
151 sched(&f, 10, &w, 3);
152 ok = fx(&w, 3, &r, &cfg);
153 CHECK(!ok, "%s: marker without marker2 must not pass", name);
154 CHECK(r.last_observed == 3 && r.last_marker2 == 0, "%s: report shows marker 3, marker2 0", name);
155 w = 0; w2 = 0; setup(&f, &cfg);
156 cfg.marker2 = &w2;
157 cfg.marker2_want = 0x46464646u;
158 sched(&f, 10, &w, 3);
159 sched(&f, 20, &w2, 0x46464646u);
160 ok = fx(&w, 3, &r, &cfg);
161 CHECK(ok && r.last_marker2 == 0x46464646u, "%s: both markers pass", name);
162 break;
163 case S_SEQ_WRAP: {
164 static const uint32_t tg[4] = {0xfffffffeu, 0xffffffffu, 0u, 1u};
165 for (int i = 0; i < 4; i++) {
166 w = tg[i] - 1u; /* one step behind, wrapping through 0 */
167 setup(&f, &cfg);
168 sched(&f, 10, &w, tg[i]);
169 ok = sq(&w, tg[i], &r, &cfg);
170 CHECK(ok, "%s: reaches target %08x from %08x", name, tg[i], tg[i] - 1u);
171 }
172 w = 3; setup(&f, &cfg);
173 ok = sq(&w, 0xffffffffu, &r, &cfg);
174 CHECK(ok, "%s: 3 is past 0xffffffff", name);
175 w = 0; setup(&f, &cfg);
176 ok = sq(&w, 0xfffffffeu, &r, &cfg);
177 CHECK(ok, "%s: 0 is past 0xfffffffe", name);
178 w = 0xfffffff0u; setup(&f, &cfg);
179 ok = sq(&w, 0xffffffffu, &r, &cfg);
180 CHECK(!ok, "%s: 0xfffffff0 is before 0xffffffff", name);
181 w = 0xfffffffeu; setup(&f, &cfg);
182 ok = sq(&w, 1, &r, &cfg);
183 CHECK(!ok, "%s: 0xfffffffe is before 1", name);
184 break;
185 }
186 case S_PROGRESS_RESETS:
187 for (int i = 0; i < 4; i++) sched(&f, 60u * (i + 1), &w, (uint32_t)(i + 1));
188 ok = sq(&w, 4, &r, &cfg); /* 240 ms total, 60 ms gaps, 100 ms stall bound */
189 CHECK(ok, "%s: steady progress must not stall", name);
190 CHECK(r.progress_count == 4, "%s: progress_count %llu", name, (unsigned long long)r.progress_count);
191 break;
192 case S_BARRIER:
193 sched(&f, 5, &w, 1);
194 cfg.snapshot = fk_snapshot; /* runs after the reads of each poll: marks where the read happened */
195 cfg.snapshot_ctx = &f;
196 ok = sq(&w, 1, &r, &cfg);
197 CHECK(ok && r.polls > 0 && f.barriers >= r.polls + 1, "%s: barriers %llu polls %llu", name,
198 (unsigned long long)f.barriers, (unsigned long long)r.polls);
199 CHECK(f.snaps == r.polls && f.order_violations == 0, "%s: a barrier must precede every read (%llu of %llu polls unordered)", name,
200 (unsigned long long)f.order_violations, (unsigned long long)f.snaps);
201 CHECK(f.barriers > f.last_snap_barriers, "%s: barrier after the PASS observation", name);
202 break;
203 case S_HARD_DEADLINE:
204 cfg.total_timeout_ms = 200;
205 for (int i = 0; i < 30; i++) sched(&f, (uint64_t)(20 * (i + 1)), &w, (uint32_t)(i + 1));
206 ok = sq(&w, 99, &r, &cfg); /* moves every 20 ms, never arrives */
207 CHECK(!ok && r.result == OMEGA_GPU_WAIT_TIMEOUT, "%s: hard deadline TIMEOUT (got %d)", name, r.result);
208 CHECK(r.progress_count > 3, "%s: progress was occurring (%llu)", name, (unsigned long long)r.progress_count);
209 break;
210 case S_FIXED_OVERSHOOT:
211 sched(&f, 10, &w, 5); /* jumps past the fixed value 3 */
212 ok = fx(&w, 3, &r, &cfg);
213 CHECK(!ok, "%s: fixed wait must not accept overshoot 5 for 3", name);
214 CHECK(r.result == OMEGA_GPU_WAIT_STALLED && r.last_observed == 5, "%s: stalled, saw 5", name);
215 w = 0; setup(&f, &cfg);
216 sched(&f, 10, &w, 5);
217 ok = sq(&w, 3, &r, &cfg);
218 CHECK(ok, "%s: sequence wait accepts 5 for target 3", name);
219 break;
220 case S_INVALID:
221 f.invalid_value = 9;
222 sched(&f, 10, &w, 9);
223 ok = sq(&w, 20, &r, &cfg);
224 CHECK(!ok && r.result == OMEGA_GPU_WAIT_INVALID_STATE && r.last_observed == 9,
225 "%s: invalid value reported (got %d)", name, r.result);
226 break;
227 case S_ABORT:
228 f.abort_at = 7;
229 ok = sq(&w, 20, &r, &cfg);
230 CHECK(!ok && r.result == OMEGA_GPU_WAIT_ABORTED, "%s: abort reported (got %d)", name, r.result);
231 break;
232 }
233 return g_fail - before;
234}
235
236/* CHIPWAIT-1c: QUEUE_WRAP marker2 pattern (src/omega_world_gates.c). Every entry of a
237 * batch but the last writes its dispatch_id to marker2; the last writes the sentinel
238 * 0x46464646. A sequence wait for the sentinel must not accept dispatch ids (even a
239 * high one), and must pass when the sentinel lands. */
240static int sentinel_batch(void) {
241 volatile uint32_t w2 = 0;
242 Fake f;
243 omega_gpu_wait_cfg_t cfg;
244 omega_gpu_wait_report_t r;
245 int before = g_fail;
246 setup(&f, &cfg);
247 for (uint32_t id = 1; id <= 15; id++) sched(&f, id, &w2, 3000u + id);
248 bool ok = omega_gpu_wait_sequence(&w2, 0x46464646u, &r, &cfg);
249 CHECK(!ok && r.result == OMEGA_GPU_WAIT_STALLED && r.last_observed == 3015u,
250 "sentinel: dispatch ids alone must not satisfy the sentinel wait (got %d, observed %u)", r.result, r.last_observed);
251 w2 = 0;
252 setup(&f, &cfg);
253 for (uint32_t id = 1; id <= 15; id++) sched(&f, id, &w2, 3000u + id);
254 sched(&f, 16, &w2, 0x46464646u);
255 ok = omega_gpu_wait_sequence(&w2, 0x46464646u, &r, &cfg);
256 CHECK(ok && r.result == OMEGA_GPU_WAIT_PASS && r.last_observed == 0x46464646u,
257 "sentinel: last entry's sentinel passes (got %d, observed %u)", r.result, r.last_observed);
258 return g_fail - before;
259}
260
261typedef struct {
262 const char *label;
263 WaitFn fx, sq;
264 unsigned must_kill; /* bitmask of scenarios that must kill this mutant */
265} Mutant;
266
267int main(void) {
268 for (int s = 0; s < S_COUNT; s++) {
269 int bad = scenario(names[s], omega_gpu_wait_fixed, omega_gpu_wait_sequence, s);
270 printf("real %-28s %s\n", names[s], bad ? "FAIL" : "ok");
271 }
272 { int bad = sentinel_batch(); printf("real %-28s %s\n", "queue_wrap_sentinel", bad ? "FAIL" : "ok"); }
273 int real_fail = g_fail;
274
275 const Mutant muts[] = {
276 {"naive_wall_clock", naive_wait, naive_wait, (1u << S_COUNT) - 1u},
277 {"fixed_accepts_overshoot", mut_fixed_ge, omega_gpu_wait_sequence, 1u << S_FIXED_OVERSHOOT},
278 {"seq_unsigned_compare", omega_gpu_wait_fixed, mut_seq_unsigned, 1u << S_SEQ_WRAP},
279 };
280 int saved = g_fail, kill_total = 0, kill_need = 0;
281 g_quiet = 1;
282 for (unsigned m = 0; m < sizeof(muts) / sizeof(muts[0]); m++) {
283 unsigned killed = 0;
284 for (int s = 0; s < S_COUNT; s++) {
285 if (scenario(names[s], muts[m].fx, muts[m].sq, s) > 0) killed |= 1u << s;
286 }
287 g_fail = saved;
288 g_quiet = 0;
289 for (int s = 0; s < S_COUNT; s++) {
290 if (!(muts[m].must_kill & (1u << s))) continue;
291 kill_need++;
292 g_checks++;
293 if (killed & (1u << s)) kill_total++;
294 else { g_fail++; printf(" FAIL: mutant %s survived scenario %s\n", muts[m].label, names[s]); }
295 }
296 printf("mutant %-26s killed mask %03x (required %03x)\n", muts[m].label, killed, muts[m].must_kill);
297 saved = g_fail;
298 g_quiet = 1;
299 }
300 g_quiet = 0;
301 printf("gpu_wait: checks=%d real_failures=%d required_kills=%d/%d\n", g_checks, real_fail, kill_total, kill_need);
302 printf("%s\n", g_fail ? "OMEGA_GPU_WAIT_HOST FAIL" : "OMEGA_GPU_WAIT_HOST PASS");
303 return g_fail ? 1 : 0;
304}