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 / src / omega_gpu_scan.c

522 lines · 24961 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* omega_gpu_scan.c -- see omega_gpu_scan.h. The kernels and launch plans come from seeded Omega (om/scan/scan_ir.om,
3 * om/scan/scan_plan.om through their generated units); this file replays them, runs a plan through a launch callback,
4 * and stages, launches and checks the calls on the chip.
5 *
6 * MIGRATION-PENDING C: the earlier C generator (gen) and level drivers of kinds 0..4 and ops 0..3 stay below as the
7 * differential oracle, compiled only with -DCUDAPP_MIGRATION_PENDING_C_SCAN (C primary for those kinds and ops) or
8 * -DCUDAPP_MIGRATION_PENDING_SCAN_DIFF (both run, any difference aborts; -DCUDAPP_SCAN_DIFF_SELFTEST flips one Omega
9 * word first and must abort). They are deleted in a later PR. */
10#include "omega_gpu_scan.h"
11#include "omega_gpu_om_ir.h"
12#include "omega_gpu_session.h"
13#include "cudapp_native.h"
14#include "omega_attn_host_policy.h"
15
16#include <stdlib.h>
17#include <string.h>
18
19#if defined(CUDAPP_MIGRATION_PENDING_C_SCAN) && defined(CUDAPP_MIGRATION_PENDING_SCAN_DIFF)
20#error "CUDAPP_MIGRATION_PENDING_C_SCAN and CUDAPP_MIGRATION_PENDING_SCAN_DIFF are exclusive"
21#endif
22#if defined(CUDAPP_SCAN_DIFF_SELFTEST) && !defined(CUDAPP_MIGRATION_PENDING_SCAN_DIFF)
23#error "CUDAPP_SCAN_DIFF_SELFTEST needs CUDAPP_MIGRATION_PENDING_SCAN_DIFF"
24#endif
25#if defined(CUDAPP_MIGRATION_PENDING_C_SCAN) || defined(CUDAPP_MIGRATION_PENDING_SCAN_DIFF)
26#define SCAN_C_ORACLE 1
27#include <stdio.h>
28#include "omega_bw_em.h"
29#endif
30
31#define POISON 0x7fa00badu /* a signalling NaN: no FADD or FMNMX writes it, so a word still holding it was never written */
32#define TIMEOUT_MS 20000ull
33
34/* ------------------------------------------------------------------ Omega: kernels and plans */
35
36static int om_codegen(const OmegaScanSpec *sp, BlackwellIRProgram *prog) {
37 if (sp->mutant < 0) return -1;
38 uint64_t err = 0;
39 return omega_gpu_om_ir_replay(omega_scan_ir, (uint64_t)sp->kind, (uint64_t)sp->mutant, 0, 0, prog, &err) == OMEGA_OM_IR_OK ? 0 : -1;
40}
41
42/* 0, or the unit's refusal mapped to BAD_ARGS (1) / TOO_LARGE (2) */
43static int om_plan(OmegaScanOp op, size_t n, uint64_t in, uint64_t out, uint64_t scr, uint64_t *plan) {
44 uint64_t r = 0;
45 if (omega_scan_plan(&r, (uint64_t)op, (uint64_t)n, in, out, scr, plan, OMEGA_GPU_SCAN_PLAN_WORDS) != OMEGA_OK)
46 return OMEGA_GPU_SCAN_BAD_ARGS;
47 if (r == 0) return OMEGA_GPU_SCAN_OK;
48 return r == 2 ? OMEGA_GPU_SCAN_TOO_LARGE : OMEGA_GPU_SCAN_BAD_ARGS;
49}
50
51#ifdef SCAN_C_ORACLE
52/* ------------------------------------------------------------------ MIGRATION-PENDING C oracle: kinds 0..4 */
53
54#define PAD_SUM 0x80000000u /* -0.0: x + -0.0 == x for every x */
55#define PAD_MINMAX 0x7fc00000u /* quiet NaN: max(NaN, x) == min(NaN, x) == x */
56
57static void fmnmx(Em *e, BlackwellIROpcode op, int d, int a, int b) {
58 BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED);
59}
60typedef struct { int bssy, bra; } Region;
61/* the lanes with P0 set skip to the region's end; the warp joins again there */
62static Region skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; }
63static 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); }
64
65static int gen(Em *e, const OmegaScanSpec *sp) {
66 const OmegaScanKind k = sp->kind;
67 const int red = k <= OMEGA_SCAN_K_RED_MIN;
68 int udesc = UV64(e), pin = V64(e), pout = V64(e), ptot = V64(e), len = V(e), tiles = V(e), warps = V(e);
69 int tid = V(e), cta = V(e), lane = V(e), gw = V(e), one = V(e), tile = V(e), el = V(e), v = V(e), t = V(e);
70 int a64 = V64(e), b64 = V64(e), rl = V(e), nz = V(e), c31 = V(e), m1 = V(e), off = V(e), tm1 = V(e);
71 int mask[5];
72 for (unsigned s = 0; s < 5; s++) mask[s] = k == OMEGA_SCAN_K_TILE ? V(e) : NOREG;
73 if (e->err) return -1;
74 ldcu64(e, udesc, 0x358);
75 ldc64(e, pin, OMEGA_GPU_SCAN_A_IN); ldc64(e, pout, OMEGA_GPU_SCAN_A_OUT);
76 if (k == OMEGA_SCAN_K_TILE) ldc64(e, ptot, OMEGA_GPU_SCAN_A_TOT);
77 ldc32(e, len, OMEGA_GPU_SCAN_A_LEN); ldc32(e, tiles, OMEGA_GPU_SCAN_A_TILES); ldc32(e, warps, OMEGA_GPU_SCAN_A_WARPS);
78 s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X);
79 movi(e, one, 1); movi(e, m1, 0xffffffffu); movi(e, nz, PAD_SUM);
80 andi(e, lane, tid, 0, 31u);
81 shr(e, t, tid, 0, 5);
82 imadi(e, gw, cta, 0, OMEGA_GPU_SCAN_THREADS / 32u, t); /* global warp = cta * 8 + warp */
83 if (k == OMEGA_SCAN_K_ADD) imadi(e, tile, gw, 0, 1u, one); /* the carry starts at tile 1 */
84 else imadi(e, tile, gw, 0, 1u, NOREG);
85 if (k == OMEGA_SCAN_K_TILE) {
86 /* reversed lanes: lane L holds tile position 31 - L, so SHFL.DOWN by d reads position - d */
87 movi(e, c31, 31u);
88 lop3(e, rl, lane, c31, NOREG, 0x3cu); /* 31 - L == L ^ 31 for L < 32 */
89 /* mask[s] = all ones when position - d exists (L + d <= 31), else 0: ((L + d) >> 5) - 1 */
90 for (unsigned s = 0; s < 5; s++) {
91 movi(e, t, 1u << s);
92 iadd(e, mask[s], lane, t);
93 shr(e, mask[s], mask[s], 0, 5);
94 imadi(e, mask[s], mask[s], 0, 1u, m1);
95 }
96 }
97
98 const int top = here(e);
99 loop_begin(e);
100 isetp_ge_u32(e, tile, tiles);
101 const int done = bra_if_p0_fwd(e);
102 if (red) {
103 const BlackwellIROpcode op = k == OMEGA_SCAN_K_RED_MAX ? BW_IR_FMNMX_MAX : BW_IR_FMNMX_MIN;
104 imadi(e, el, tile, 0, 32u, lane);
105 movi(e, v, k == OMEGA_SCAN_K_RED_SUM ? PAD_SUM : PAD_MINMAX);
106 isetp_ge_u32(e, el, len);
107 { Region r = skip_if_p0(e); imadw(e, a64, el, 4u, pin); ldg32(e, v, a64, udesc, 1); region_end(e, r); }
108 for (uint32_t d = 16; d >= 1; d /= 2) {
109 if (sp->mutant == 1 && d == 1) break;
110 shfl_down(e, t, v, d);
111 if (k == OMEGA_SCAN_K_RED_SUM) fadd(e, v, v, t); else fmnmx(e, op, v, v, t);
112 }
113 isetp_ge_u32(e, lane, one);
114 { Region r = skip_if_p0(e); imadw(e, a64, tile, 4u, pout); stg(e, a64, 0, v, udesc); region_end(e, r); }
115 } else if (k == OMEGA_SCAN_K_TILE) {
116 imadi(e, el, tile, 0, 32u, rl);
117 movi(e, v, PAD_SUM);
118 isetp_ge_u32(e, el, len);
119 { Region r = skip_if_p0(e); imadw(e, a64, el, 4u, pin); ldg32(e, v, a64, udesc, 1); region_end(e, r); }
120 for (unsigned s = 0; s < 5; s++) {
121 if (sp->mutant == 2 && s == 4) break;
122 shfl_down(e, t, v, 1u << s);
123 lop3(e, t, t, mask[s], nz, 0xe2u); /* (t & mask) | (-0.0 & ~mask) */
124 fadd(e, v, t, v); /* lower position on the left */
125 }
126 isetp_ge_u32(e, el, len);
127 { Region r = skip_if_p0(e); imadw(e, a64, el, 4u, pout); stg(e, a64, 0, v, udesc); region_end(e, r); }
128 isetp_ge_u32(e, lane, one); /* lane 0 holds position 31: the tile total */
129 { Region r = skip_if_p0(e); imadw(e, a64, tile, 4u, ptot); stg(e, a64, 0, v, udesc); region_end(e, r); }
130 } else {
131 imadi(e, el, tile, 0, 32u, lane);
132 isetp_ge_u32(e, el, len);
133 {
134 Region r = skip_if_p0(e);
135 if (sp->mutant == 3) imadi(e, tm1, tile, 0, 1u, NOREG); else imadi(e, tm1, tile, 0, 1u, m1);
136 imadw(e, b64, tm1, 4u, pout); imadw(e, a64, el, 4u, pin);
137 ldg32(e, off, b64, udesc, 1); ldg32(e, v, a64, udesc, 0);
138 fadd(e, v, off, v);
139 stg(e, a64, 0, v, udesc);
140 region_end(e, r);
141 }
142 }
143 iadd(e, tile, tile, warps);
144 bra(e, top);
145 loop_end(e);
146 patch_bra(e, done, here(e));
147 tail(e);
148 return e->err;
149}
150
151static int c_codegen(const OmegaScanSpec *spec, BlackwellIRProgram *prog) {
152 omega_bw_ir_init(prog);
153 Em em; memset(&em, 0, sizeof em); em.p = prog;
154 if (gen(&em, spec) != 0 || em.err || em.depth != 0) return -1;
155 return omega_bw_regalloc_solve(prog);
156}
157
158/* ------------------------------------------------------------------ MIGRATION-PENDING C oracle: ops 0..3 */
159
160static uint32_t c_grid(size_t len) {
161 size_t tiles = (len + 31) / 32, ctas = (tiles + OMEGA_GPU_SCAN_THREADS / 32u - 1) / (OMEGA_GPU_SCAN_THREADS / 32u);
162 return ctas > OMEGA_GPU_SCAN_MAX_CTAS ? OMEGA_GPU_SCAN_MAX_CTAS : (uint32_t)(ctas ? ctas : 1);
163}
164
165/* one launch record at plan[16 + 12 i] (the shift word stays 0 for these ops) */
166static void c_launch(uint64_t *plan, OmegaScanKind k, uint64_t in, uint64_t out, uint64_t tot, size_t len) {
167 uint64_t *a = plan + OMEGA_GPU_SCAN_PLAN_LAUNCH(plan[0]);
168 a[0] = (uint64_t)k; a[1] = c_grid(len);
169 a[2] = (uint32_t)in; a[3] = (uint32_t)(in >> 32); a[4] = (uint32_t)out; a[5] = (uint32_t)(out >> 32);
170 a[6] = (uint32_t)tot; a[7] = (uint32_t)(tot >> 32);
171 a[8] = (uint32_t)len; a[9] = (uint32_t)((len + 31) / 32); a[10] = c_grid(len) * (OMEGA_GPU_SCAN_THREADS / 32u); a[11] = 0;
172 plan[0]++;
173}
174
175static void c_plan_reduce(OmegaGpuRedOp op, size_t n, uint64_t in_va, uint64_t scratch_va, uint64_t *plan) {
176 const OmegaScanKind k = (OmegaScanKind)(OMEGA_SCAN_K_RED_SUM + (int)op);
177 uint64_t in = in_va, out = scratch_va;
178 size_t len = n, w = 0;
179 do {
180 c_launch(plan, k, in, out, 0, len);
181 len = (len + 31) / 32; w += len;
182 in = out; out += len * 4u;
183 } while (len > 1);
184 plan[1] = w; plan[2] = in;
185}
186
187static int c_plan_prefix(size_t n, uint64_t in_va, uint64_t out_va, uint64_t scratch_va, uint64_t *plan) {
188 enum { MAXL = 8 }; /* 32^6 >= 2^28 */
189 uint64_t buf[MAXL], tot[MAXL];
190 size_t len[MAXL];
191 unsigned L = 0;
192 uint64_t in = in_va, sp = scratch_va;
193 buf[0] = out_va; len[0] = n;
194 for (;;) { /* forward: scan every level, its totals become the next level (scanned in place) */
195 size_t nt = (len[L] + 31) / 32;
196 tot[L] = sp; sp += nt * 4u;
197 c_launch(plan, OMEGA_SCAN_K_TILE, in, buf[L], tot[L], len[L]);
198 if (nt == 1) break;
199 if (L + 1 >= MAXL) return OMEGA_GPU_SCAN_TOO_LARGE;
200 buf[L + 1] = tot[L]; len[L + 1] = nt; in = tot[L];
201 L++;
202 }
203 while (L-- > 0) /* back down: carry the scanned totals of level L + 1 into level L */
204 c_launch(plan, OMEGA_SCAN_K_ADD, buf[L], buf[L + 1], 0, len[L]);
205 plan[1] = (sp - scratch_va) / 4u;
206 return OMEGA_GPU_SCAN_OK;
207}
208
209static int c_plan(OmegaScanOp op, size_t n, uint64_t in, uint64_t out, uint64_t scr, uint64_t *plan) {
210 memset(plan, 0, OMEGA_GPU_SCAN_PLAN_WORDS * sizeof *plan);
211 if (op <= OMEGA_SCAN_OP_RED_MIN) { c_plan_reduce((OmegaGpuRedOp)op, n, in, scr, plan); return OMEGA_GPU_SCAN_OK; }
212 return c_plan_prefix(n, in, out, scr, plan);
213}
214#endif /* SCAN_C_ORACLE */
215
216#ifdef CUDAPP_MIGRATION_PENDING_SCAN_DIFF
217/* ------------------------------------------------------------------ the C-vs-Omega differential */
218
219static void diff_same(const char *what, unsigned a, size_t b) {
220 const char *path = getenv("CUDAPP_SCAN_DIFF_LOG");
221 if (!path || !*path) return;
222 FILE *f = fopen(path, "a");
223 if (!f) return;
224 fprintf(f, "%s %u %zu SAME\n", what, a, b);
225 fclose(f);
226}
227
228static void diff_abort(const char *what, unsigned a, size_t b) {
229 fprintf(stderr, "omega_gpu_scan differential: %s %u %zu differs between C and Omega\n", what, a, b);
230 abort();
231}
232
233#ifdef CUDAPP_SCAN_DIFF_SELFTEST
234/* the self-test build corrupts one Omega result before the comparison: CUDAPP_SCAN_DIFF_TAMPER=codegen or plan */
235static int selftest_is(const char *what) {
236 const char *t = getenv("CUDAPP_SCAN_DIFF_TAMPER");
237 return t && strcmp(t, what) == 0;
238}
239#endif
240
241static void diff_prog(const OmegaScanSpec *sp, BlackwellIRProgram *om) {
242 BlackwellIRProgram *c = calloc(1, sizeof *c);
243 if (!c) diff_abort("codegen (out of memory) kind", (unsigned)sp->kind, (size_t)sp->mutant);
244#ifdef CUDAPP_SCAN_DIFF_SELFTEST
245 if (selftest_is("codegen")) om->insns[0].imm ^= 1u;
246#endif
247 int same = c_codegen(sp, c) == 0 && c->count == om->count &&
248 memcmp(c->insns, om->insns, om->count * sizeof om->insns[0]) == 0 &&
249 memcmp(&c->regalloc, &om->regalloc, sizeof om->regalloc) == 0;
250 free(c);
251 if (!same) diff_abort("codegen kind", (unsigned)sp->kind, (size_t)sp->mutant);
252 diff_same("codegen kind", (unsigned)sp->kind, (size_t)sp->mutant);
253}
254
255static void diff_plan(OmegaScanOp op, size_t n, uint64_t in, uint64_t out, uint64_t scr, uint64_t *om) {
256 uint64_t *c = malloc(OMEGA_GPU_SCAN_PLAN_WORDS * sizeof *c);
257 if (!c) diff_abort("plan (out of memory) op", (unsigned)op, n);
258#ifdef CUDAPP_SCAN_DIFF_SELFTEST
259 if (selftest_is("plan")) om[1] ^= 1u;
260#endif
261 int same = c_plan(op, n, in, out, scr, c) == OMEGA_GPU_SCAN_OK &&
262 memcmp(c, om, OMEGA_GPU_SCAN_PLAN_WORDS * sizeof *c) == 0;
263 free(c);
264 if (!same) diff_abort("plan op", (unsigned)op, n);
265 diff_same("plan op", (unsigned)op, n);
266}
267#endif /* CUDAPP_MIGRATION_PENDING_SCAN_DIFF */
268
269/* ------------------------------------------------------------------ codegen, plan, driver */
270
271int omega_gpu_scan_codegen(const OmegaScanSpec *spec, BlackwellIRProgram *prog) {
272 if (!spec || !prog || (unsigned)spec->kind >= OMEGA_SCAN_K_COUNT) return -1;
273#ifdef CUDAPP_MIGRATION_PENDING_C_SCAN
274 if (spec->kind <= OMEGA_SCAN_K_ADD && spec->mutant >= 0 && spec->mutant <= 3) return c_codegen(spec, prog);
275#endif
276 if (om_codegen(spec, prog) != 0) return -1;
277#ifdef CUDAPP_MIGRATION_PENDING_SCAN_DIFF
278 if (spec->kind <= OMEGA_SCAN_K_ADD && spec->mutant <= 3) diff_prog(spec, prog);
279#endif
280 return 0;
281}
282
283int omega_gpu_scan_plan(OmegaScanOp op, size_t n, uint64_t in_va, uint64_t out_va, uint64_t scratch_va,
284 uint64_t plan[OMEGA_GPU_SCAN_PLAN_WORDS]) {
285 if (!plan || (unsigned)op >= OMEGA_SCAN_OP_COUNT || n == 0) return OMEGA_GPU_SCAN_BAD_ARGS;
286 if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE;
287#ifdef CUDAPP_MIGRATION_PENDING_C_SCAN
288 if (op <= OMEGA_SCAN_OP_PREFIX_F32) return c_plan(op, n, in_va, out_va, scratch_va, plan);
289#endif
290 int rc = om_plan(op, n, in_va, out_va, scratch_va, plan);
291#ifdef CUDAPP_MIGRATION_PENDING_SCAN_DIFF
292 if (rc == OMEGA_GPU_SCAN_OK && op <= OMEGA_SCAN_OP_PREFIX_F32) diff_plan(op, n, in_va, out_va, scratch_va, plan);
293#endif
294 return rc;
295}
296
297size_t omega_gpu_scan_scratch_words(OmegaScanOp op, size_t n) {
298 uint64_t *plan = malloc(OMEGA_GPU_SCAN_PLAN_WORDS * sizeof *plan);
299 size_t w = plan && omega_gpu_scan_plan(op, n, 0, 0, 0, plan) == OMEGA_GPU_SCAN_OK ? (size_t)plan[1] : 0;
300 free(plan);
301 return w;
302}
303
304int omega_gpu_scan_drive(OmegaScanOp op, size_t n, uint64_t in_va, uint64_t out_va, uint64_t scratch_va,
305 uint64_t *result_va, OmegaScanLaunchFn launch, void *ctx, uint32_t *launches) {
306 if (!launch) return OMEGA_GPU_SCAN_BAD_ARGS;
307 uint64_t *plan = malloc(OMEGA_GPU_SCAN_PLAN_WORDS * sizeof *plan);
308 if (!plan) return OMEGA_GPU_SCAN_BAD_ARGS;
309 int rc = omega_gpu_scan_plan(op, n, in_va, out_va, scratch_va, plan);
310 for (uint64_t i = 0; rc == OMEGA_GPU_SCAN_OK && i < plan[0]; i++) {
311 const uint64_t *L = plan + OMEGA_GPU_SCAN_PLAN_LAUNCH(i);
312 uint32_t a[OMEGA_GPU_SCAN_ARGS_WORDS];
313 for (unsigned j = 0; j < OMEGA_GPU_SCAN_ARGS_WORDS; j++) a[j] = (uint32_t)L[2 + j];
314 if (L[0] >= OMEGA_SCAN_K_COUNT) rc = OMEGA_GPU_SCAN_CODEGEN_FAIL;
315 else if (launch(ctx, (OmegaScanKind)L[0], a, (uint32_t)L[1]) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL;
316 else if (launches) (*launches)++;
317 }
318 if (rc == OMEGA_GPU_SCAN_OK && result_va) *result_va = plan[2];
319 free(plan);
320 return rc;
321}
322
323/* ------------------------------------------------------------------ the chip */
324
325static struct {
326 int mutant;
327 OmegaGpuMem code[OMEGA_SCAN_K_COUNT];
328 uint32_t gpr[OMEGA_SCAN_K_COUNT];
329 int hooked;
330} g;
331
332/* lock held; the session calls it right before it closes the device (also in omega_gpu_session_recover): the kernels go
333 * with the device, the next call builds them again on the new one */
334static void on_session_close(void) {
335 for (unsigned k = 0; k < OMEGA_SCAN_K_COUNT; k++) if (g.code[k].cpu) omega_gpu_session_free(&g.code[k]);
336 memset(g.code, 0, sizeof g.code);
337}
338
339void omega_gpu_scan_test_set_mutant(int mutant) {
340 omega_gpu_session_lock();
341 for (unsigned k = 0; k < OMEGA_SCAN_K_COUNT; k++) if (g.code[k].cpu) omega_gpu_session_free(&g.code[k]);
342 memset(g.code, 0, sizeof g.code);
343 g.mutant = mutant;
344 omega_gpu_session_unlock();
345}
346
347const char *omega_gpu_scan_rc_name(int rc) {
348 switch (rc) {
349 case OMEGA_GPU_SCAN_OK: return "OK";
350 case OMEGA_GPU_SCAN_BAD_ARGS: return "BAD_ARGS";
351 case OMEGA_GPU_SCAN_TOO_LARGE: return "TOO_LARGE";
352 case OMEGA_GPU_SCAN_CODEGEN_FAIL: return "CODEGEN_FAIL";
353 case OMEGA_GPU_SCAN_CHIP_FAIL: return "CHIP_FAIL";
354 default: return "?";
355 }
356}
357
358/* lock held */
359static int kernel_locked(OmegaScanKind k) {
360 if (g.code[k].cpu) return OMEGA_GPU_SCAN_OK;
361 if (!g.hooked) { /* without the hook a kernel would outlive its device */
362 if (omega_gpu_session_on_close(on_session_close) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL;
363 g.hooked = 1;
364 }
365 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
366 if (!prog) return OMEGA_GPU_SCAN_CODEGEN_FAIL;
367 OmegaScanSpec s = { k, g.mutant };
368 int rc = OMEGA_GPU_SCAN_OK;
369 size_t need = 0;
370 if (omega_gpu_scan_codegen(&s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_SCAN_CODEGEN_FAIL;
371 else if (omega_gpu_session_alloc_code(need, &g.code[k]) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL;
372 else {
373 OmegaBlackwellKernel kk;
374 if (omega_blackwell_kernel_encode_into(prog, g.code[k].cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_SCAN_CODEGEN_FAIL;
375 else if (omega_gpu_session_sync_to_gpu(&g.code[k]) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL;
376 else { g.gpr[k] = omega_bw_gpr_declared(kk.gpr_count); if (g.gpr[k] < 64u) g.gpr[k] = 64u; }
377 if (rc != OMEGA_GPU_SCAN_OK) { omega_gpu_session_free(&g.code[k]); memset(&g.code[k], 0, sizeof g.code[k]); }
378 }
379 omega_gpu_host_free(prog);
380 return rc;
381}
382
383typedef struct { uint64_t elapsed_ns; } ChipCtx;
384
385/* lock held */
386static int chip_launch(void *ctx, OmegaScanKind k, const uint32_t args[OMEGA_GPU_SCAN_ARGS_WORDS], uint32_t grid_x) {
387 if (kernel_locked(k) != OMEGA_GPU_SCAN_OK) return -1;
388 OmegaGpuLaunch L = {
389 .code_va = g.code[k].va, .gpr_count = g.gpr[k], .threads_x = OMEGA_GPU_SCAN_THREADS, .threads_y = 1,
390 .grid_x = grid_x, .grid_y = 1, .num_elements = (size_t)grid_x * OMEGA_GPU_SCAN_THREADS,
391 .args = args, .n_args = OMEGA_GPU_SCAN_ARGS_WORDS, .timeout_ms = TIMEOUT_MS,
392 };
393 uint64_t ns = 0;
394 if (omega_gpu_session_launch(&L, &ns, NULL) != 0) return -1;
395 ((ChipCtx *)ctx)->elapsed_ns += ns;
396 return 0;
397}
398
399/* lock held: one data buffer [x xw | x2 x2w | out | scratch], the inputs copied in, the rest poisoned */
400static int stage_locked(const void *x, size_t xw, const void *x2, size_t x2w, size_t out_words, size_t scratch_words,
401 OmegaGpuMem *m) {
402 if (!omega_gpu_session_open()) return OMEGA_GPU_SCAN_CHIP_FAIL;
403 const size_t total = xw + x2w + out_words + scratch_words;
404 if (omega_gpu_session_alloc(total * 4u, m) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL;
405 uint32_t *w = m->cpu;
406 memcpy(w, x, xw * 4u);
407 if (x2w) memcpy(w + xw, x2, x2w * 4u);
408 for (size_t i = xw + x2w; i < total; i++) w[i] = POISON;
409 if (omega_gpu_session_sync_to_gpu(m) != 0) { omega_gpu_session_free(m); return OMEGA_GPU_SCAN_CHIP_FAIL; }
410 return OMEGA_GPU_SCAN_OK;
411}
412
413/* the whole call on the chip: stage, plan and launch, read back; *m stays allocated on success for the caller's
414 * readback and is freed by it (lock held throughout) */
415typedef struct {
416 OmegaScanOp op; size_t n;
417 const void *x; size_t xw; const void *x2; size_t x2w; size_t out_words;
418} ChipCall;
419
420static int chip_run_locked(const ChipCall *c, OmegaGpuMem *m, uint64_t *result_va, OmegaGpuScanInfo *info) {
421 const size_t sw = omega_gpu_scan_scratch_words(c->op, c->n);
422 if (sw == 0) return OMEGA_GPU_SCAN_CODEGEN_FAIL;
423 int rc = stage_locked(c->x, c->xw, c->x2, c->x2w, c->out_words, sw, m);
424 if (rc != OMEGA_GPU_SCAN_OK) return rc;
425 ChipCtx cc = { 0 }; uint32_t nl = 0;
426 const uint64_t in = m->va, out = m->va + (c->xw + c->x2w) * 4u, scr = out + c->out_words * 4u;
427 rc = omega_gpu_scan_drive(c->op, c->n, in, c->out_words ? out : 0, scr, result_va, chip_launch, &cc, &nl);
428 if (rc == OMEGA_GPU_SCAN_OK && omega_gpu_session_sync_to_cpu(m) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL;
429 if (info) { info->launches = nl; info->elapsed_ns = cc.elapsed_ns; }
430 if (rc != OMEGA_GPU_SCAN_OK) omega_gpu_session_free(m);
431 return rc;
432}
433
434int omega_gpu_reduce(OmegaGpuRedOp op, const float *x, size_t n, float *out, OmegaGpuScanInfo *info) {
435 uint64_t t0 = omega_gpu_session_now_ns();
436 if (info) memset(info, 0, sizeof *info);
437 if ((unsigned)op > OMEGA_GPU_RED_MIN || !x || !out || n == 0) return OMEGA_GPU_SCAN_BAD_ARGS;
438 if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE;
439 omega_gpu_session_lock();
440 OmegaGpuMem m; uint64_t res = 0;
441 const ChipCall c = { (OmegaScanOp)op, n, x, n, NULL, 0, 0 };
442 int rc = chip_run_locked(&c, &m, &res, info);
443 if (rc == OMEGA_GPU_SCAN_OK) {
444 uint32_t r; memcpy(&r, (const uint8_t *)m.cpu + (res - m.va), 4);
445 if (r == POISON) { omega_gpu_session_set_error("reduce: the result word was never written"); rc = OMEGA_GPU_SCAN_CHIP_FAIL; }
446 else memcpy(out, &r, 4);
447 omega_gpu_session_free(&m);
448 }
449 omega_gpu_session_unlock();
450 if (info) info->call_ns = omega_gpu_session_now_ns() - t0;
451 return rc;
452}
453
454int omega_gpu_prefix_sum(const float *x, size_t n, float *y, OmegaGpuScanInfo *info) {
455 uint64_t t0 = omega_gpu_session_now_ns();
456 if (info) memset(info, 0, sizeof *info);
457 if (!x || !y || n == 0) return OMEGA_GPU_SCAN_BAD_ARGS;
458 if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE;
459 omega_gpu_session_lock();
460 OmegaGpuMem m;
461 const ChipCall c = { OMEGA_SCAN_OP_PREFIX_F32, n, x, n, NULL, 0, n };
462 int rc = chip_run_locked(&c, &m, NULL, info);
463 if (rc == OMEGA_GPU_SCAN_OK) {
464 const uint32_t *w = (const uint32_t *)m.cpu + n;
465 size_t unwritten = ah_check(0,w,w,n);
466 if (unwritten) { omega_gpu_session_set_error("prefix sum: output words never written"); rc = OMEGA_GPU_SCAN_CHIP_FAIL; }
467 else memcpy(y, w, n * 4u);
468 omega_gpu_session_free(&m);
469 }
470 omega_gpu_session_unlock();
471 if (info) info->call_ns = omega_gpu_session_now_ns() - t0;
472 return rc;
473}
474
475int omega_gpu_prefix_sum_u32(const uint32_t *x, size_t n, uint32_t *y, OmegaGpuScanInfo *info) {
476 uint64_t t0 = omega_gpu_session_now_ns();
477 if (info) memset(info, 0, sizeof *info);
478 if (!x || !y || n == 0) return OMEGA_GPU_SCAN_BAD_ARGS;
479 if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE;
480 omega_gpu_session_lock();
481 OmegaGpuMem m;
482 const ChipCall c = { OMEGA_SCAN_OP_PREFIX_U32, n, x, n, NULL, 0, n };
483 int rc = chip_run_locked(&c, &m, NULL, info);
484 if (rc == OMEGA_GPU_SCAN_OK) {
485 const uint32_t *w = (const uint32_t *)m.cpu + n;
486 size_t wrong = ah_check(1,w,x,n);
487 if (wrong) { omega_gpu_session_set_error("u32 prefix sum: output words do not step by the input"); rc = OMEGA_GPU_SCAN_CHIP_FAIL; }
488 else memcpy(y, w, n * 4u);
489 omega_gpu_session_free(&m);
490 }
491 omega_gpu_session_unlock();
492 if (info) info->call_ns = omega_gpu_session_now_ns() - t0;
493 return rc;
494}
495
496static int sort_chip(uint32_t *keys, uint32_t *vals, size_t n, OmegaGpuScanInfo *info) {
497 uint64_t t0 = omega_gpu_session_now_ns();
498 if (info) memset(info, 0, sizeof *info);
499 if (!keys || n == 0) return OMEGA_GPU_SCAN_BAD_ARGS;
500 if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE;
501 omega_gpu_session_lock();
502 OmegaGpuMem m;
503 const ChipCall c = { vals ? OMEGA_SCAN_OP_SORT_PAIRS : OMEGA_SCAN_OP_SORT_KEYS, n, keys, n, vals, vals ? n : 0, 0 };
504 int rc = chip_run_locked(&c, &m, NULL, info);
505 if (rc == OMEGA_GPU_SCAN_OK) {
506 const uint32_t *w = m.cpu;
507 size_t down = ah_check(2,w,w,n);
508 if (down) { omega_gpu_session_set_error("radix sort: output keys out of order"); rc = OMEGA_GPU_SCAN_CHIP_FAIL; }
509 else { memcpy(keys, w, n * 4u); if (vals) memcpy(vals, w + n, n * 4u); }
510 omega_gpu_session_free(&m);
511 }
512 omega_gpu_session_unlock();
513 if (info) info->call_ns = omega_gpu_session_now_ns() - t0;
514 return rc;
515}
516
517int omega_gpu_radix_sort_u32(uint32_t *keys, size_t n, OmegaGpuScanInfo *info) { return sort_chip(keys, NULL, n, info); }
518
519int omega_gpu_radix_sort_pairs_u32(uint32_t *keys, uint32_t *vals, size_t n, OmegaGpuScanInfo *info) {
520 if (!vals) return OMEGA_GPU_SCAN_BAD_ARGS;
521 return sort_chip(keys, vals, n, info);
522}