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_atomf32_probe_test.c

156 lines · 10452 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* gpu_atomf32_probe_test.c (training R5 part 2, 2026-10-15): do the f32 global atomic adds add right on GB10?
3 * One kernel shape: every thread atomically adds the f32 argument v to one word (start value s) and stores what it got
4 * back (or, in the no-destination form, its own thread index). Because every thread adds the same v, the sequence of
5 * values the word takes is fixed whatever order the threads land in: o0 = s, o(k+1) = ftz(ftz(ok) + ftz(v)) rounded to
6 * nearest even. Each case checks the final word against o(n) bit for bit and the returned old values against
7 * { o0 .. o(n-1) } as a multiset (no update lost or doubled). Cases:
8 * 0 .STRONG.SYS 131072 x add 1.0 from 0 exact sums
9 * 1 .STRONG.SYS 131072 x add -0.25 from 0 exact sums, negative
10 * 2 .STRONG.SYS 32 x add 2^-125 to the subnormal 2^-140 FTZ: 0x03800000; no FTZ: 0x03800008; a no-op: 0x00000200
11 * 3 .STRONG.SYS 32 x add 1.0 to 2^24 + 2 nearest-even: 2^24 + 4; toward zero: 2^24 + 2 (= no-op)
12 * 4 .STRONG.GPU 131072 x add 1.0 from 0
13 * 5 .STRONG.GPU no destination (RZ): 131072 x add 1.0; each thread then stores its index, which must survive
14 * 6 CONTROL: the integer ATOMG.ADD on the 1.0f bits must FAIL the same check (proves the check can say no)
15 * --host-only every case in the warp simulator (small grids)
16 * (default) the chip, one short window; --cases DIGITS picks cases and their order
17 * Throughput (printed for the large cases only; 32 adds only measure the launch) is every thread on ONE word in
18 * GPU-uncached host-visible memory: a contention measurement, not a training figure and not the hardware ceiling. Verdict line last: "GPU_ATOMF32_PROBE: PASS|FAIL". */
19/* omega_bw_em.h names two helpers fadd and ffma, which collide with C23 math.h (pulled in by the simulator) */
20#define fadd omega_em_fadd
21#define ffma omega_em_ffma
22#include "omega_bw_em.h"
23#undef fadd
24#undef ffma
25#include "omega_gpu_session.h"
26#include "bw_warp_sim_tc.h"
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
31typedef struct { BlackwellIROpcode op; int has_dst; uint32_t threads, grid, start, v; const char *name; int control; } Case;
32static const Case cs[] = {
33 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 1, 256, 512, 0x00000000u, 0x3f800000u, "SYS add 1.0 from 0", 0 },
34 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 1, 256, 512, 0x00000000u, 0xbe800000u, "SYS add -0.25 from 0", 0 },
35 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 1, 32, 1, 0x00000200u, 0x01000000u, "SYS add 2^-125 to subnormal 2^-140 (FTZ)", 0 },
36 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 1, 32, 1, 0x4b800001u, 0x3f800000u, "SYS add 1.0 to 2^24+2 (round to nearest even)", 0 },
37 { BW_IR_ATOMG_ADD_F32_STRONG_GPU, 1, 256, 512, 0x00000000u, 0x3f800000u, "GPU add 1.0 from 0", 0 },
38 { BW_IR_ATOMG_ADD_F32_STRONG_GPU, 0, 256, 512, 0x00000000u, 0x3f800000u, "GPU add 1.0 from 0, no destination (RZ)", 0 },
39 { BW_IR_ATOMG_ADD_STRONG_SYS, 1, 256, 512, 0x00000000u, 0x3f800000u, "CONTROL integer ATOMG.ADD of 1.0f bits", 1 },
40};
41#define NCASES (sizeof cs / sizeof cs[0])
42
43/* args: word (u64 at 0x380), output array (u64 at 0x388), v bits (u32 at 0x390) */
44static int atomf32_prog(const Case *c, uint32_t threads, BlackwellIRProgram *prog) {
45 omega_bw_ir_init(prog);
46 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
47 int udesc = UV64(e), pw = V64(e), po = V64(e), val = V(e), tid = V(e), cta = V(e), t = V(e), old = c->has_dst ? V(e) : NOREG;
48 if (e->err) return -1;
49 ldcu64(e, udesc, 0x358); ldc64(e, pw, 0x380); ldc64(e, po, 0x388); ldc32(e, val, 0x390);
50 s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X);
51 imadi(e, t, cta, 0, threads, tid); imadw(e, po, t, 4u, po);
52 BlackwellIRInsn a = I0(c->op); a.dst_vreg = old; a.src1_vreg = pw; a.src2_vreg = val; a.ureg = udesc;
53 emit(e, a, K_VAR);
54 stg(e, po, 0, c->has_dst ? old : t, udesc); /* no destination: t was live across the atomic and must be intact */
55 tail(e);
56 if (e->err) return -1;
57 return omega_bw_regalloc_solve(prog);
58}
59
60static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
61static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
62static uint32_t ftz(uint32_t u) { return (u & 0x7f800000u) == 0u ? (u & 0x80000000u) : u; }
63static int cmp_u32(const void *a, const void *b) { uint32_t x = *(const uint32_t *)a, y = *(const uint32_t *)b; return x < y ? -1 : x > y; }
64
65/* the reference: want[0 .. n-1] = old values (sorted), returns o(n). with_ftz = 0 gives the no-FTZ alternative. */
66static uint32_t reference(uint32_t s, uint32_t v, size_t n, uint32_t *want, int with_ftz) {
67 uint32_t o = s;
68 for (size_t k = 0; k < n; k++) {
69 if (want) want[k] = o;
70 o = with_ftz ? ftz(f2u(u2f(ftz(o)) + u2f(ftz(v)))) : f2u(u2f(o) + u2f(v));
71 }
72 if (want) qsort(want, n, 4, cmp_u32);
73 return o;
74}
75
76/* faults: final word, then every output slot (old values as a sorted multiset, or the thread index if no destination) */
77static size_t check(const Case *c, uint32_t word, uint32_t *out, size_t n, uint32_t *want, uint32_t *wfinal) {
78 *wfinal = reference(c->start, c->v, n, want, 1);
79 size_t bad = word != *wfinal;
80 if (c->has_dst) { qsort(out, n, 4, cmp_u32); for (size_t i = 0; i < n; i++) bad += out[i] != want[i]; }
81 else for (size_t i = 0; i < n; i++) bad += out[i] != (uint32_t)i;
82 return bad;
83}
84
85static void report(const char *where, const Case *c, size_t n, uint32_t word, uint32_t wfinal, size_t bad, uint64_t ns) {
86 uint32_t noftz = reference(c->start, c->v, n, NULL, 0);
87 printf("%s %s x %zu: word 0x%08x = %.9g (want 0x%08x", where, c->name, n, word, (double)u2f(word), wfinal);
88 if (noftz != wfinal) printf(", no-FTZ would be 0x%08x", noftz);
89 if (c->start != wfinal) printf(", a no-op would leave 0x%08x", c->start);
90 printf("), %zu faults", bad);
91 if (ns && n >= 4096) printf(", %.1f us = %.2f M adds/s, all on one word in uncached host-visible memory (contention, not a ceiling)",
92 ns / 1e3, (double)n * 1e3 / (double)ns);
93 printf("\n");
94 if (c->control) printf(" control %s: the check %s the wrong arithmetic\n", bad ? "ok" : "BROKEN", bad ? "caught" : "missed");
95}
96
97static int verdict(const Case *c, size_t bad) { return c->control ? bad == 0 : bad != 0; }
98
99static int host_only(void) {
100 static BlackwellIRProgram p; static uint32_t word, out[2 * 64], want[2 * 64]; int fail = 0;
101 for (size_t i = 0; i < NCASES; i++) {
102 const Case *c = &cs[i]; const uint32_t T = c->threads < 64 ? c->threads : 64, G = c->grid < 2 ? c->grid : 2; const size_t n = (size_t)T * G;
103 if (atomf32_prog(c, T, &p) != 0) { printf("sim %s: codegen refused\n", c->name); fail++; continue; }
104 static WsSim s; memset(&s, 0, sizeof s); s.p = &p; s.threads = T;
105 word = c->start; memset(out, 0xee, sizeof out);
106 uint64_t wa = (uintptr_t)&word, oa = (uintptr_t)out;
107 s.cbank[0x380 / 4] = (uint32_t)wa; s.cbank[0x384 / 4] = (uint32_t)(wa >> 32);
108 s.cbank[0x388 / 4] = (uint32_t)oa; s.cbank[0x38c / 4] = (uint32_t)(oa >> 32); s.cbank[0x390 / 4] = c->v;
109 s.ranges[0].lo = wa; s.ranges[0].hi = wa + 4; s.ranges[1].lo = oa; s.ranges[1].hi = oa + 4u * n; s.nranges = 2;
110 int err = ws_run(&s, G, 1); uint32_t wf = 0;
111 size_t bad = err ? 1 : check(c, word, out, n, want, &wf);
112 if (err) printf("sim %s: %s\n", c->name, s.msg); else report("sim", c, n, word, wf, bad, 0);
113 if (err || verdict(c, bad)) fail++;
114 }
115 return fail;
116}
117
118int main(int argc, char **argv) {
119 int fail = host_only();
120 if (argc > 1 && !strcmp(argv[1], "--host-only")) { printf("GPU_ATOMF32_PROBE: %s\n", fail ? "FAIL" : "PASS"); return fail ? 1 : 0; }
121 if (fail) { printf("GPU_ATOMF32_PROBE: FAIL\n"); return 1; }
122 const char *order = argc > 2 && !strcmp(argv[1], "--cases") ? argv[2] : "0123456";
123 const size_t nmax = 256u * 512u;
124 uint32_t *want = malloc(nmax * 4u), *got = malloc(nmax * 4u);
125 omega_gpu_session_lock();
126 if (!want || !got || !omega_gpu_session_open()) { printf("open failed: %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); free(want); free(got); return 1; }
127 static BlackwellIRProgram p;
128 for (const char *q = order; *q && !fail; q++) {
129 const size_t i = (size_t)(*q - '0');
130 if (i >= NCASES) { printf("bad case %c\n", *q); fail++; break; }
131 const Case *c = &cs[i]; const size_t n = (size_t)c->threads * c->grid;
132 OmegaGpuMem code = { 0 }, wm = { 0 }, om = { 0 }; OmegaBlackwellKernel k;
133 if (atomf32_prog(c, c->threads, &p) != 0) { printf("codegen refused\n"); fail++; break; }
134 size_t need = omega_bw_program_code_bytes(&p);
135 if (omega_gpu_session_alloc_code(need, &code) != 0 || omega_blackwell_kernel_encode_into(&p, code.cpu, need, &k) != 0 || omega_gpu_session_sync_to_gpu(&code) != 0 ||
136 omega_gpu_session_alloc(4096, &wm) != 0 || omega_gpu_session_alloc(n * 4u, &om) != 0) { printf("upload failed: %s\n", omega_gpu_session_last_error()); fail++; break; }
137 uint32_t *w = wm.cpu, *o = om.cpu; uint64_t ns = 0;
138 w[0] = c->start; memset(o, 0xee, n * 4u);
139 uint32_t args[5] = { (uint32_t)wm.va, (uint32_t)(wm.va >> 32), (uint32_t)om.va, (uint32_t)(om.va >> 32), c->v };
140 OmegaGpuLaunch L = { .code_va = code.va, .gpr_count = omega_bw_gpr_declared(p.regalloc.peak_gpr_usage), .threads_x = c->threads, .threads_y = 1,
141 .grid_x = c->grid, .grid_y = 1, .num_elements = n, .args = args, .n_args = 5, .timeout_ms = 20000 };
142 if (omega_gpu_session_sync_to_gpu(&wm) != 0 || omega_gpu_session_sync_to_gpu(&om) != 0 || omega_gpu_session_launch(&L, &ns, NULL) != 0 ||
143 omega_gpu_session_sync_to_cpu(&wm) != 0 || omega_gpu_session_sync_to_cpu(&om) != 0) { printf("launch failed: %s\n", omega_gpu_session_last_error()); fail++; }
144 else {
145 memcpy(got, o, n * 4u); uint32_t wf = 0;
146 size_t bad = check(c, w[0], got, n, want, &wf);
147 report("chip", c, n, w[0], wf, bad, ns);
148 if (verdict(c, bad)) fail++;
149 }
150 omega_gpu_session_free(&code); omega_gpu_session_free(&wm); omega_gpu_session_free(&om);
151 }
152 omega_gpu_session_unlock();
153 free(want); free(got);
154 printf("GPU_ATOMF32_PROBE: %s\n", fail ? "FAIL" : "PASS");
155 return fail ? 1 : 0;
156}