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