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 / gpu_isolation_test.c

372 lines · 19746 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* gpu_isolation_test.c -- row C5 of docs/OMEGA.md: two processes on one GPU, and a never-ending kernel, on the GB10.
3 *
4 * --host-only codegen + encode of the work kernel, its mutant and the never-ending kernel; the simulator runs the work
5 * kernel exactly and catches the mutant; the never-ending kernel never reaches its exit in the simulator
6 * (no flag) the chip, under the GB10 lock, every part in its own child process:
7 * hang a work launch (values checked), then a never-ending kernel (one warp: count, branch back) with a
8 * HANG_MS timeout: the launch fails "timed out" and the process latches; red check: a work launch is
9 * refused "process latched"; omega_gpu_session_recover() closes the device (the RM client close destroys
10 * the channel and the grid on it); the work kernel uploaded again on the new device runs, values right.
11 * two two processes, each its own device, ROUNDS work launches with their own inputs: both all right.
12 * two-hang process 1 runs ROUNDS work launches while process 2 runs the hang part: process 1 all right (unharmed),
13 * process 2 recovers and is right too.
14 * Kernels from Omega's IR (omega_bw_em.h), our own sm_121 encoder; no CUDA. */
15#include "omega_gpu_cmdlist.h"
16#include "omega_gpu_session.h"
17#include "bw_warp_sim.h" /* brings math.h, whose fadd() / ffma() would hide the emitter's */
18#define fadd em_fadd
19#define ffma em_ffma
20#include "omega_bw_em.h"
21
22#include <fcntl.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/select.h>
27#include <sys/wait.h>
28#include <time.h>
29#include <unistd.h>
30
31#define THREADS 128u
32#define CTAS 32u
33#define NEL (THREADS * CTAS)
34#define ROUNDS 300
35#define HANG_MS 2000ull
36#define POISON 0x7fa00badu
37#define ARGS 5u /* in u64, out u64, tag f32 */
38
39static int g_failed, g_checks;
40static void check(int ok, const char *what) {
41 g_checks++;
42 if (!ok) g_failed++;
43 printf("[%d] %s: %s\n", (int)getpid(), ok ? "ok" : "FAIL", what);
44 fflush(stdout);
45}
46static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
47static uint64_t now_ns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; }
48
49/* ------------------------------------------------------------------ the kernels */
50
51typedef struct { int bssy, bra; } Region;
52static Region skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; }
53static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); }
54
55/* work: out[gid] = in[gid] * 2 + tag (mutant 1: * 2 + tag + 1) */
56static int gen_work(Em *e, int mutant) {
57 int udesc = UV64(e), pin = V64(e), pout = V64(e), a64 = V64(e), tag = V(e), n = V(e), tid = V(e), cta = V(e), gid = V(e), two = V(e), v = V(e), one = V(e);
58 if (e->err) return -1;
59 ldcu64(e, udesc, 0x358);
60 ldc64(e, pin, 0x380); ldc64(e, pout, 0x388); ldc32(e, tag, 0x390);
61 s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X);
62 imadi(e, gid, cta, 0, THREADS, tid);
63 movi(e, n, NEL); movi(e, two, 0x40000000u); movi(e, one, 0x3f800000u);
64 isetp_ge_u32(e, gid, n);
65 {
66 Region r = skip_if_p0(e);
67 imadw(e, a64, gid, 4u, pin); ldg32(e, v, a64, udesc, 1);
68 ffma(e, v, v, two, 0, tag);
69 if (mutant == 1) fadd(e, v, v, one);
70 imadw(e, a64, gid, 4u, pout); stg(e, a64, 0, v, udesc);
71 region_end(e, r);
72 }
73 tail(e);
74 return e->err;
75}
76
77/* never-ending: x = x + 1, branch back, for ever; the exit after the loop is never reached */
78static int gen_hang(Em *e) {
79 int x = V(e), one = V(e);
80 if (e->err) return -1;
81 movi(e, x, 0); movi(e, one, 1);
82 const int top = here(e);
83 loop_begin(e);
84 iadd(e, x, x, one);
85 bra(e, top);
86 loop_end(e);
87 tail(e);
88 return e->err;
89}
90
91static int codegen(int hang, int mutant, BlackwellIRProgram *prog) {
92 omega_bw_ir_init(prog);
93 Em em; memset(&em, 0, sizeof em); em.p = prog;
94 if ((hang ? gen_hang(&em) : gen_work(&em, mutant)) != 0 || em.err || em.depth != 0) return -1;
95 return omega_bw_regalloc_solve(prog);
96}
97
98static float input(unsigned i, unsigned seed) { return (float)((i * 2654435761u + seed * 40503u) % 100000u); }
99static float expect(unsigned i, unsigned seed, float tag) { return input(i, seed) * 2.0f + tag; }
100
101/* ------------------------------------------------------------------ host */
102
103static const char *g_dump; /* --dump-hang PATH: code bytes for the disassembler; PATH must not already exist */
104
105static void host_only(void) {
106 char w[200];
107 for (int k = 0; k < 3; k++) {
108 BlackwellIRProgram *p = calloc(1, sizeof *p);
109 size_t need = 0;
110 int ok = p && codegen(k == 2, k == 1, p) == 0 && (need = omega_bw_program_code_bytes(p)) > 0;
111 void *code = ok ? calloc(1, need) : NULL;
112 OmegaBlackwellKernel kk;
113 ok = ok && code && omega_blackwell_kernel_encode_into(p, code, need, &kk) == 0 && kk.code_size == need;
114 if (ok && k == 2 && g_dump) {
115 /* Never truncate an existing file or follow a final-component symlink.
116 * A new dump is private even with a permissive caller umask. */
117 int fd = open(g_dump, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
118 FILE *f = fd >= 0 ? fdopen(fd, "wb") : NULL;
119 ok = f && fwrite(code, 1, need, f) == need;
120 if (f) { if (fclose(f) != 0) ok = 0; }
121 else if (fd >= 0) close(fd);
122 if (!ok) fprintf(stderr, "cannot write private kernel dump: %s (requires a new path)\n", g_dump);
123 }
124 snprintf(w, sizeof w, "codegen + encode: %s (%zu bytes)", k == 0 ? "work kernel" : k == 1 ? "work kernel mutant 1" : "never-ending kernel", need);
125 check(ok, w);
126 free(code); free(p);
127 }
128 static float in[NEL], out[NEL];
129 WsRange rg[2] = { { (uintptr_t)in, (uintptr_t)(in + NEL) }, { (uintptr_t)out, (uintptr_t)(out + NEL) } };
130 for (int mut = 0; mut < 2; mut++) {
131 BlackwellIRProgram *p = calloc(1, sizeof *p);
132 static WsSim s;
133 memset(&s, 0, sizeof s);
134 for (unsigned i = 0; i < NEL; i++) { in[i] = input(i, 7); uint32_t pz = POISON; memcpy(&out[i], &pz, 4); }
135 int rc = !p || codegen(0, mut, p) != 0;
136 if (!rc) {
137 const uint64_t a = (uintptr_t)in, b = (uintptr_t)out;
138 s.p = p; s.threads = THREADS;
139 s.cbank[0x380 / 4] = (uint32_t)a; s.cbank[0x384 / 4] = (uint32_t)(a >> 32);
140 s.cbank[0x388 / 4] = (uint32_t)b; s.cbank[0x38c / 4] = (uint32_t)(b >> 32); s.cbank[0x390 / 4] = f2u(3.0f);
141 memcpy(s.ranges, rg, sizeof rg); s.nranges = 2;
142 rc = ws_run(&s, CTAS, 1);
143 }
144 unsigned bad = 0;
145 for (unsigned i = 0; i < NEL; i++) bad += f2u(out[i]) != f2u(expect(i, 7, 3.0f));
146 snprintf(w, sizeof w, mut ? "simulator red check: work mutant caught (%u wrong)" : "simulator: work kernel exact on all %u elements", mut ? bad : NEL);
147 check(mut ? rc != 0 || bad > 0 : rc == 0 && bad == 0, w);
148 free(p);
149 }
150}
151
152/* ------------------------------------------------------------------ the chip */
153
154typedef struct { BlackwellIRProgram prog; OmegaGpuMem code; uint32_t gpr; } Kern;
155
156/* lock held */
157static int upload(Kern *k, int hang) {
158 memset(&k->code, 0, sizeof k->code);
159 if (codegen(hang, 0, &k->prog) != 0) return -1;
160 size_t need = omega_bw_program_code_bytes(&k->prog);
161 if (need == 0 || omega_gpu_session_alloc_code(need, &k->code) != 0) return -1;
162 OmegaBlackwellKernel kk;
163 if (omega_blackwell_kernel_encode_into(&k->prog, k->code.cpu, need, &kk) != 0 || omega_gpu_session_sync_to_gpu(&k->code) != 0) return -1;
164 k->gpr = omega_bw_gpr_declared(kk.gpr_count);
165 if (k->gpr < 64u) k->gpr = 64u;
166 return 0;
167}
168
169static Kern g_work, g_hang;
170static OmegaGpuMem g_buf; /* [in | out] */
171
172/* lock held, device open: device buffer + work kernel */
173static int setup(void) {
174 if (!omega_gpu_session_open()) return -1;
175 if (omega_gpu_session_alloc(2u * NEL * 4u, &g_buf) != 0) return -1;
176 return upload(&g_work, 0);
177}
178
179/* lock held: one work launch with inputs from seed; returns the count of wrong outputs, or -1 if the launch failed */
180static long work_once(unsigned seed, float tag, uint64_t *ns) {
181 float *in = g_buf.cpu, *out = in + NEL;
182 for (unsigned i = 0; i < NEL; i++) { in[i] = input(i, seed); uint32_t pz = POISON; memcpy(&out[i], &pz, 4); }
183 if (omega_gpu_session_sync_to_gpu(&g_buf) != 0) return -1;
184 uint32_t a[ARGS];
185 const uint64_t vi = g_buf.va, vo = g_buf.va + NEL * 4u;
186 a[0] = (uint32_t)vi; a[1] = (uint32_t)(vi >> 32); a[2] = (uint32_t)vo; a[3] = (uint32_t)(vo >> 32); a[4] = f2u(tag);
187 OmegaGpuLaunch L = { .code_va = g_work.code.va, .gpr_count = g_work.gpr, .threads_x = THREADS, .threads_y = 1, .grid_x = CTAS, .grid_y = 1,
188 .num_elements = NEL, .args = a, .n_args = ARGS, .timeout_ms = 20000 };
189 uint64_t t = 0;
190 if (omega_gpu_session_launch(&L, &t, NULL) != 0) return -1;
191 if (ns) *ns = t;
192 if (omega_gpu_session_sync_to_cpu(&g_buf) != 0) return -1;
193 long bad = 0;
194 for (unsigned i = 0; i < NEL; i++) bad += f2u(out[i]) != f2u(expect(i, seed, tag));
195 return bad;
196}
197
198/* Child processes talk to the parent and to each other over pipes (one byte per event, then raw timestamps). */
199static void say(int fd, char c) { if (fd >= 0 && write(fd, &c, 1) != 1) printf("[%d] pipe write failed\n", (int)getpid()); }
200static int hear(int fd) { char c = 0; return read(fd, &c, 1) == 1 ? c : -1; }
201static int heard_now(int fd) { /* non-blocking: 1 when a byte is waiting */
202 fd_set s; FD_ZERO(&s); FD_SET(fd, &s);
203 struct timeval tv = { 0, 0 };
204 return select(fd + 1, &s, NULL, NULL, &tv) == 1;
205}
206
207/* the hang part; sig_fd (or -1) gets 'H' right before the never-ending launch and 'D' when the part is over */
208static int part_hang(int sig_fd) {
209 char w[300];
210 omega_gpu_session_lock();
211 if (setup() != 0) { check(0, "hang part: device, buffer, work kernel"); printf(" %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); say(sig_fd, 'D'); return 1; }
212 uint64_t ns0 = 0;
213 long bad = work_once(11, 1.0f, &ns0);
214 snprintf(w, sizeof w, "hang part: a work launch before the hang is right (%ld wrong, %.3f ms)", bad, ns0 / 1e6);
215 check(bad == 0, w);
216 check(upload(&g_hang, 1) == 0, "hang part: never-ending kernel uploaded");
217 OmegaGpuCmdList *old_list = NULL; /* made on the device the recovery throws away */
218 omega_gpu_session_unlock();
219 check(omega_gpu_cmdlist_create(2, &old_list) == 0, "hang part: a command list made before the hang");
220 omega_gpu_session_lock();
221 uint32_t dummy = 0; /* the kernel reads no argument; a launch needs at least one word */
222 OmegaGpuLaunch H = { .code_va = g_hang.code.va, .gpr_count = g_hang.gpr, .threads_x = 32, .threads_y = 1, .grid_x = 1, .grid_y = 1,
223 .num_elements = 32, .args = &dummy, .n_args = 1, .timeout_ms = HANG_MS };
224 say(sig_fd, 'H');
225 const uint64_t t0 = now_ns();
226 int rc = omega_gpu_session_launch(&H, NULL, NULL);
227 const double waited = (now_ns() - t0) / 1e6;
228 snprintf(w, sizeof w, "the never-ending kernel times out after %.0f ms (rc %d: %s), the process latches", waited, rc, omega_gpu_session_last_error());
229 check(rc != 0 && strstr(omega_gpu_session_last_error(), "timed out") && omega_gpu_session_is_blocked() && waited >= (double)HANG_MS * 0.9, w);
230 bad = work_once(12, 1.0f, NULL);
231 snprintf(w, sizeof w, "red check: while latched a work launch is refused (%s)", omega_gpu_session_last_error());
232 check(bad == -1 && strstr(omega_gpu_session_last_error(), "process latched") != NULL, w);
233 omega_gpu_session_unlock();
234
235 const uint64_t r0 = now_ns();
236 rc = omega_gpu_session_recover();
237 const double rec_ms = (now_ns() - r0) / 1e6;
238 /* the driver's clean close of the RM client is what says the channel, and the grid on it, are gone: a teardown fault
239 * would keep the session latched (tests/test_omega_gpu_session_platform.c RECOVER_TEARDOWN_FAULT_STAYS_LATCHED) */
240 snprintf(w, sizeof w, "recover: the device closed with no driver teardown fault in %.1f ms (rc %d %s)", rec_ms, rc, rc ? omega_gpu_session_last_error() : "");
241 check(rc == 0 && !omega_gpu_session_is_blocked() && omega_gpu_session_recoveries() == 1, w);
242 memset(&g_buf, 0, sizeof g_buf); /* handles of the old device are gone */
243 omega_gpu_session_lock();
244 if (setup() != 0) { check(0, "after recover: new device, buffer, work kernel"); printf(" %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); say(sig_fd, 'D'); return 1; }
245 uint64_t ns1 = 0, ns2 = 0;
246 bad = work_once(13, 4.0f, &ns1);
247 long bad2 = work_once(14, 5.0f, &ns2);
248 snprintf(w, sizeof w, "after recover, same process: work launches right (%ld, %ld wrong; %.3f, %.3f ms vs %.3f ms before the hang), device opens %u",
249 bad, bad2, ns1 / 1e6, ns2 / 1e6, ns0 / 1e6, omega_gpu_session_open_count());
250 check(bad == 0 && bad2 == 0, w);
251 {
252 uint32_t a1 = 0;
253 OmegaGpuLaunch L = { .code_va = g_work.code.va, .gpr_count = g_work.gpr, .threads_x = 32, .threads_y = 1, .grid_x = 1, .grid_y = 1,
254 .num_elements = 32, .args = &a1, .n_args = 1 };
255 int refused = old_list && omega_gpu_cmdlist_add(old_list, &L) == -1 && strstr(omega_gpu_session_last_error(), "before a session recovery") != NULL;
256 snprintf(w, sizeof w, "a command list from before the recovery is refused (%s)", omega_gpu_session_last_error());
257 check(refused, w);
258 }
259 omega_gpu_session_unlock();
260 omega_gpu_cmdlist_free(old_list);
261 omega_gpu_session_close();
262 say(sig_fd, 'D');
263 return g_failed ? 1 : 0;
264}
265
266typedef struct { uint64_t start, end; uint32_t launches, during; } Span;
267
268/* work launches with own inputs. ready_fd gets 'R' once set up; then it waits for one byte on go_fd. It runs for at
269 * least RUN_MS and ROUNDS launches; with a hang_fd it waits for 'H' first and keeps going until 'D', counting the
270 * launches made in between. Writes its Span to span_fd. */
271#define RUN_MS 3000ull
272static int part_rounds(unsigned seed_base, const char *who, int ready_fd, int go_fd, int hang_fd, int span_fd) {
273 char w[300];
274 omega_gpu_session_lock();
275 if (setup() != 0) { check(0, "rounds: device, buffer, work kernel"); printf(" %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); say(ready_fd, 'X'); return 1; }
276 say(ready_fd, 'R');
277 if (go_fd >= 0) (void)hear(go_fd);
278 if (hang_fd >= 0 && hear(hang_fd) != 'H') { check(0, "rounds: the hang process never started its hang"); omega_gpu_session_unlock(); return 1; }
279 Span sp = { now_ns(), 0, 0, 0 };
280 long wrong_total = 0; int failed = 0, hang_live = hang_fd >= 0;
281 uint64_t ns, worst = 0;
282 for (unsigned r = 0;; r++) {
283 if (hang_live && heard_now(hang_fd)) { (void)hear(hang_fd); hang_live = 0; }
284 if (!hang_live && r >= ROUNDS && now_ns() - sp.start >= RUN_MS * 1000000ull) break;
285 long bad = work_once(seed_base + r, (float)(r % 7), &ns);
286 if (bad < 0) { failed++; printf(" [%d] launch %u: %s\n", (int)getpid(), r, omega_gpu_session_last_error()); break; }
287 wrong_total += bad;
288 sp.launches++;
289 if (hang_live) sp.during++;
290 if (ns > worst) worst = ns;
291 }
292 sp.end = now_ns();
293 snprintf(w, sizeof w, "%s: %u work launches over %.0f ms, own inputs: %ld wrong values, %d failed launches, slowest %.3f ms%s",
294 who, sp.launches, (sp.end - sp.start) / 1e6, wrong_total, failed, worst / 1e6, "");
295 check(wrong_total == 0 && failed == 0, w);
296 if (hang_fd >= 0) {
297 snprintf(w, sizeof w, "%s: %u of its launches ran while the other process's kernel was hung or being torn down", who, sp.during);
298 check(sp.during > 0, w);
299 }
300 if (span_fd >= 0 && write(span_fd, &sp, sizeof sp) != (ssize_t)sizeof sp) printf("[%d] span write failed\n", (int)getpid());
301 omega_gpu_session_unlock();
302 omega_gpu_session_close();
303 return g_failed ? 1 : 0;
304}
305
306static int reap(pid_t p) { int st = 0; if (p < 0 || waitpid(p, &st, 0) != p) return -1; return WIFEXITED(st) ? WEXITSTATUS(st) : 128 + (WIFSIGNALED(st) ? WTERMSIG(st) : 0); }
307static int pipe_or_die(int fd[2]) { if (pipe(fd) != 0) { printf("NOT_RUN: pipe\n"); exit(2); } return 0; }
308
309static void chip_tier(int values_only) {
310 char w[300];
311 fflush(stdout);
312 if (!values_only) {
313 pid_t h = fork();
314 if (h == 0) _exit(part_hang(-1));
315 int rc = reap(h);
316 snprintf(w, sizeof w, "C5 hang: never-ending kernel timed out, recovered, correct launch in the same process (child exit %d)", rc);
317 check(rc == 0, w);
318 }
319
320 /* two processes, started together after both are set up, each running RUN_MS: their spans must overlap */
321 int ready[2], go[2], span[2];
322 pipe_or_die(ready); pipe_or_die(go); pipe_or_die(span);
323 pid_t a = fork();
324 if (a == 0) _exit(part_rounds(1000, "process 1", ready[1], go[0], -1, span[1]));
325 pid_t b = fork();
326 if (b == 0) _exit(part_rounds(5000, "process 2", ready[1], go[0], -1, span[1]));
327 int r1 = hear(ready[0]), r2 = hear(ready[0]);
328 say(go[1], 'G'); say(go[1], 'G');
329 int ra = reap(a), rb = reap(b);
330 Span s1 = {0}, s2 = {0};
331 int got = read(span[0], &s1, sizeof s1) == (ssize_t)sizeof s1 && read(span[0], &s2, sizeof s2) == (ssize_t)sizeof s2;
332 const uint64_t lo = s1.start > s2.start ? s1.start : s2.start, hi = s1.end < s2.end ? s1.end : s2.end;
333 const double overlap_ms = got && hi > lo ? (hi - lo) / 1e6 : 0.0;
334 snprintf(w, sizeof w, "C5 two processes on one GPU: both right (ready %c %c, exits %d, %d), running side by side for %.0f ms",
335 r1 > 0 ? r1 : '?', r2 > 0 ? r2 : '?', ra, rb, overlap_ms);
336 check(ra == 0 && rb == 0 && overlap_ms >= (double)RUN_MS * 0.5, w);
337 close(ready[0]); close(ready[1]); close(go[0]); close(go[1]); close(span[0]); close(span[1]);
338
339 if (values_only) {
340 puts("C5 recovery: NOT_RUN (values-only; hang needs a separate reservation)");
341 return;
342 }
343
344 /* a hang beside a working process: process 1 starts when process 2's hang begins and runs until process 2 is done */
345 int sig[2], rdy[2];
346 pipe_or_die(sig); pipe_or_die(rdy);
347 a = fork();
348 if (a == 0) { close(sig[1]); _exit(part_rounds(9000, "process 1 beside a hang", rdy[1], -1, sig[0], -1)); }
349 b = fork();
350 if (b == 0) { close(sig[0]); _exit(part_hang(sig[1])); }
351 close(sig[0]); close(sig[1]);
352 ra = reap(a); rb = reap(b);
353 snprintf(w, sizeof w, "C5 a hang in process 2 leaves process 1 unharmed, and process 2 recovers (exits %d, %d)", ra, rb);
354 check(ra == 0 && rb == 0, w);
355 close(rdy[0]); close(rdy[1]);
356}
357
358int main(int argc, char **argv) {
359 const char *tier = "chip";
360 for (int i = 1; i < argc; i++) {
361 if (!strcmp(argv[i], "--host-only")) tier = "host";
362 else if (!strcmp(argv[i], "--values-only")) tier = "values";
363 else if (!strcmp(argv[i], "--dump-hang") && i + 1 < argc) { tier = "host"; g_dump = argv[++i]; }
364 else { fprintf(stderr, "usage: %s [--host-only | --values-only] [--dump-hang PATH]\n", argv[0]); return 2; }
365 }
366 setvbuf(stdout, NULL, _IOLBF, 0);
367 if (!strcmp(tier, "host")) host_only();
368 else chip_tier(!strcmp(tier, "values"));
369 if (g_failed) printf("C5 isolation %s: FAIL (%d of %d)\n", tier, g_failed, g_checks);
370 else printf("C5 isolation %s: PASS (%d checks)\n", tier, g_checks);
371 return g_failed ? 1 : 0;
372}