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
CUDApp / tests / gpu_streams_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* gpu_streams_test.c -- row C4 of docs/OMEGA.md: two independent queues and an event between them, on the GB10. |
| 3 | * |
| 4 | * --host-only codegen + encode of the two kernels (and their mutants), refusals of omega_gpu_cmdlist_wait_for |
| 5 | * --sim the kernels through the warp simulator (tests/bw_warp_sim.h): exact values, mutants caught |
| 6 | * (no flag) the chip, under the GB10 lock, AIEN_OMEGA_GPU_CHANNELS = 2 (L136 extra queue): |
| 7 | * overlap list A and list B each hold the same half-chip spin kernel (24 CTAs x 128 threads); solo A, solo B |
| 8 | * (session queue, one submit each) and the pair (A on queue 0, B on queue 1, omega_gpu_cmdlist_submit_pair), |
| 9 | * medians of REPS: PASS when pair < 0.75 (solo A + solo B), for the default barrier or first_barrier_none |
| 10 | * (both are printed). Red check: both spins in ONE list on one queue (WAIT_FOR_IDLE between them) must |
| 11 | * NOT come in under 0.75, or the measurement cannot tell overlap from order. |
| 12 | * event A: a long spin writes X. B: omega_gpu_cmdlist_wait_for(A, 0), then copy Y = X + 1. X and Y start as a |
| 13 | * signalling-NaN poison; every Y must be the spin's value + 1. Negative control: the same pair without the |
| 14 | * wait must leave at least one Y wrong (B ran while A spun), else the event is "not demonstrated" (FAIL). |
| 15 | * Every kernel comes from Omega's IR (omega_bw_em.h), encoded by our own sm_121 encoder; no CUDA. */ |
| 16 | #include "omega_gpu_cmdlist.h" |
| 17 | #include "omega_gpu_session.h" |
| 18 | #include "bw_warp_sim.h" /* brings math.h, whose fadd() would hide the emitter's */ |
| 19 | #define fadd em_fadd |
| 20 | #define ffma em_ffma |
| 21 | #include "omega_bw_em.h" |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #define THREADS 128u |
| 28 | #define CTAS 24u /* half of the GB10's 48 SMs */ |
| 29 | #define NEL (THREADS * CTAS) |
| 30 | #define UNROLL 4u /* FFMAs per loop turn: x = x * 1 + 1 */ |
| 31 | #define MAX_ITERS ((1u << 22) - 1024u) /* UNROLL * iters + tag stays below 2^24: every value exact in f32 */ |
| 32 | #define POISON 0x7fa00badu /* a signalling NaN no FFMA / FADD writes */ |
| 33 | #define REPS 5 |
| 34 | |
| 35 | /* argument words (constant bank 0x380) */ |
| 36 | #define A_OUT 0x380 /* spin: out u64 | copy: in u64 */ |
| 37 | #define A_ITERS 0x388 /* spin: loop turns | copy: out u64 (0x388..0x38f) */ |
| 38 | #define A_TAG 0x38c /* spin: f32 added at the end */ |
| 39 | #define A_N 0x390 /* elements */ |
| 40 | #define ARGS 5u |
| 41 | |
| 42 | static int g_failed, g_checks; |
| 43 | static void check(int ok, const char *what) { |
| 44 | g_checks++; |
| 45 | if (!ok) g_failed++; |
| 46 | printf("%s: %s\n", ok ? "ok" : "FAIL", what); |
| 47 | } |
| 48 | static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } |
| 49 | |
| 50 | /* ------------------------------------------------------------------ the kernels */ |
| 51 | |
| 52 | typedef struct { int bssy, bra; } Region; |
| 53 | static Region skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } |
| 54 | static 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); } |
| 55 | |
| 56 | /* spin: out[gid] = UNROLL * iters + tag (mutant 1: one FFMA short per turn) */ |
| 57 | static int gen_spin(Em *e, int mutant) { |
| 58 | int udesc = UV64(e), pout = V64(e), a64 = V64(e), iters = V(e), tag = V(e), n = V(e), tid = V(e), cta = V(e), gid = V(e); |
| 59 | int i = V(e), one = V(e), fone = V(e), x = V(e); |
| 60 | if (e->err) return -1; |
| 61 | ldcu64(e, udesc, 0x358); |
| 62 | ldc64(e, pout, A_OUT); ldc32(e, iters, A_ITERS); ldc32(e, tag, A_TAG); ldc32(e, n, A_N); |
| 63 | s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); |
| 64 | imadi(e, gid, cta, 0, THREADS, tid); |
| 65 | movi(e, one, 1); movi(e, fone, 0x3f800000u); movi(e, x, 0); movi(e, i, 0); |
| 66 | const int top = here(e); |
| 67 | loop_begin(e); |
| 68 | isetp_ge_u32(e, i, iters); |
| 69 | const int done = bra_if_p0_fwd(e); |
| 70 | for (unsigned u = 0; u < UNROLL - (mutant == 1 ? 1u : 0u); u++) ffma(e, x, x, fone, 0, fone); |
| 71 | iadd(e, i, i, one); |
| 72 | bra(e, top); |
| 73 | loop_end(e); |
| 74 | patch_bra(e, done, here(e)); |
| 75 | fadd(e, x, x, tag); |
| 76 | isetp_ge_u32(e, gid, n); |
| 77 | { Region r = skip_if_p0(e); imadw(e, a64, gid, 4u, pout); stg(e, a64, 0, x, udesc); region_end(e, r); } |
| 78 | tail(e); |
| 79 | return e->err; |
| 80 | } |
| 81 | |
| 82 | /* copy: out[gid] = in[gid] + 1 (mutant 1: + 2) */ |
| 83 | static int gen_copy(Em *e, int mutant) { |
| 84 | int udesc = UV64(e), pin = V64(e), pout = V64(e), a64 = V64(e), n = V(e), tid = V(e), cta = V(e), gid = V(e), add = V(e), v = V(e); |
| 85 | if (e->err) return -1; |
| 86 | ldcu64(e, udesc, 0x358); |
| 87 | ldc64(e, pin, A_OUT); ldc64(e, pout, A_ITERS); ldc32(e, n, A_N); |
| 88 | s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); |
| 89 | imadi(e, gid, cta, 0, THREADS, tid); |
| 90 | movi(e, add, mutant == 1 ? 0x40000000u : 0x3f800000u); |
| 91 | isetp_ge_u32(e, gid, n); |
| 92 | { |
| 93 | Region r = skip_if_p0(e); |
| 94 | imadw(e, a64, gid, 4u, pin); ldg32(e, v, a64, udesc, 1); |
| 95 | fadd(e, v, v, add); |
| 96 | imadw(e, a64, gid, 4u, pout); stg(e, a64, 0, v, udesc); |
| 97 | region_end(e, r); |
| 98 | } |
| 99 | tail(e); |
| 100 | return e->err; |
| 101 | } |
| 102 | |
| 103 | static int codegen(int copy, int mutant, BlackwellIRProgram *prog) { |
| 104 | omega_bw_ir_init(prog); |
| 105 | Em em; memset(&em, 0, sizeof em); em.p = prog; |
| 106 | if ((copy ? gen_copy(&em, mutant) : gen_spin(&em, mutant)) != 0 || em.err || em.depth != 0) return -1; |
| 107 | return omega_bw_regalloc_solve(prog); |
| 108 | } |
| 109 | |
| 110 | static void spin_args(uint32_t a[ARGS], uint64_t out, uint32_t iters, float tag) { |
| 111 | memset(a, 0, ARGS * 4u); |
| 112 | a[0] = (uint32_t)out; a[1] = (uint32_t)(out >> 32); a[2] = iters; a[3] = f2u(tag); a[4] = NEL; |
| 113 | } |
| 114 | static void copy_args(uint32_t a[ARGS], uint64_t in, uint64_t out) { |
| 115 | memset(a, 0, ARGS * 4u); |
| 116 | a[0] = (uint32_t)in; a[1] = (uint32_t)(in >> 32); a[2] = (uint32_t)out; a[3] = (uint32_t)(out >> 32); a[4] = NEL; |
| 117 | } |
| 118 | static float spin_value(uint32_t iters, float tag) { return (float)(UNROLL * iters) + tag; } |
| 119 | |
| 120 | /* ------------------------------------------------------------------ host + simulator */ |
| 121 | |
| 122 | static void host_only(void) { |
| 123 | char w[200]; |
| 124 | for (int copy = 0; copy < 2; copy++) |
| 125 | for (int mut = 0; mut < 2; mut++) { |
| 126 | BlackwellIRProgram *p = calloc(1, sizeof *p); |
| 127 | size_t need = 0; |
| 128 | int ok = p && codegen(copy, mut, p) == 0 && (need = omega_bw_program_code_bytes(p)) > 0; |
| 129 | void *code = ok ? calloc(1, need) : NULL; |
| 130 | OmegaBlackwellKernel kk; |
| 131 | ok = ok && code && omega_blackwell_kernel_encode_into(p, code, need, &kk) == 0 && kk.code_size == need; |
| 132 | snprintf(w, sizeof w, "codegen + encode: %s kernel%s (%zu bytes, %u registers)", copy ? "copy" : "spin", mut ? " mutant 1" : "", |
| 133 | need, ok ? kk.gpr_count : 0u); |
| 134 | check(ok, w); |
| 135 | free(code); free(p); |
| 136 | } |
| 137 | check(omega_gpu_cmdlist_wait_for(NULL, NULL, 0) == -1, "wait_for refuses missing lists"); |
| 138 | } |
| 139 | |
| 140 | static int sim_run(BlackwellIRProgram *p, const uint32_t a[ARGS], WsRange *rg, int nrg, int order) { |
| 141 | static WsSim s; |
| 142 | memset(&s, 0, sizeof s); |
| 143 | s.p = p; s.threads = THREADS; s.order = order; |
| 144 | for (unsigned i = 0; i < ARGS; i++) s.cbank[0x380 / 4 + i] = a[i]; |
| 145 | memcpy(s.ranges, rg, (size_t)nrg * sizeof *rg); s.nranges = nrg; |
| 146 | if (ws_run(&s, CTAS, 1) != 0) { printf(" simulator: %s at pc %d\n", s.msg, s.err_pc); return -1; } |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static void sim_tier(void) { |
| 151 | char w[200]; |
| 152 | static float X[NEL], Y[NEL]; |
| 153 | WsRange rg[2] = { { (uintptr_t)X, (uintptr_t)(X + NEL) }, { (uintptr_t)Y, (uintptr_t)(Y + NEL) } }; |
| 154 | for (int mut = 0; mut < 2; mut++) { |
| 155 | BlackwellIRProgram *ps = calloc(1, sizeof *ps), *pc = calloc(1, sizeof *pc); |
| 156 | if (!ps || !pc || codegen(0, mut, ps) || codegen(1, mut, pc)) { check(0, "simulator: kernels built"); free(ps); free(pc); return; } |
| 157 | for (int order = 0; order < 2; order++) { |
| 158 | const uint32_t iters = 37u + (uint32_t)order; |
| 159 | const float tag = 5.0f + (float)order; |
| 160 | uint32_t a[ARGS]; |
| 161 | for (unsigned i = 0; i < NEL; i++) { uint32_t pz = POISON; memcpy(&X[i], &pz, 4); memcpy(&Y[i], &pz, 4); } |
| 162 | spin_args(a, (uintptr_t)X, iters, tag); |
| 163 | int rc = sim_run(ps, a, rg, 2, order); |
| 164 | copy_args(a, (uintptr_t)X, (uintptr_t)Y); |
| 165 | rc = rc || sim_run(pc, a, rg, 2, order); |
| 166 | unsigned wx = 0, wy = 0; |
| 167 | for (unsigned i = 0; i < NEL; i++) { |
| 168 | wx += f2u(X[i]) != f2u(spin_value(iters, tag)); |
| 169 | wy += f2u(Y[i]) != f2u(spin_value(iters, tag) + 1.0f); |
| 170 | } |
| 171 | if (mut == 0) snprintf(w, sizeof w, "simulator order %d: spin + copy exact on all %u elements (%u, %u wrong)", order, NEL, wx, wy); |
| 172 | else snprintf(w, sizeof w, "simulator red check order %d: mutant 1 caught (%u, %u wrong)", order, wx, wy); |
| 173 | check(mut == 0 ? rc == 0 && wx == 0 && wy == 0 : rc != 0 || (wx > 0 && wy > 0), w); |
| 174 | } |
| 175 | free(ps); free(pc); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* ------------------------------------------------------------------ the chip */ |
| 180 | |
| 181 | typedef struct { BlackwellIRProgram prog; OmegaGpuMem code; uint32_t gpr; } Kern; |
| 182 | static Kern K_spin, K_copy; |
| 183 | |
| 184 | static int kern_upload(Kern *k, int copy) { |
| 185 | if (codegen(copy, 0, &k->prog) != 0) return -1; |
| 186 | size_t need = omega_bw_program_code_bytes(&k->prog); |
| 187 | if (need == 0 || omega_gpu_session_alloc_code(need, &k->code) != 0) return -1; |
| 188 | OmegaBlackwellKernel kk; |
| 189 | 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; |
| 190 | k->gpr = omega_bw_gpr_declared(kk.gpr_count); |
| 191 | if (k->gpr < 64u) k->gpr = 64u; |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static OmegaGpuMem g_buf; /* [OA | OB | X | Y], NEL floats each */ |
| 196 | static float *buf_at(int i) { return (float *)g_buf.cpu + (size_t)i * NEL; } |
| 197 | static uint64_t va_at(int i) { return g_buf.va + (uint64_t)i * NEL * 4u; } |
| 198 | enum { B_OA = 0, B_OB = 1, B_X = 2, B_Y = 3 }; |
| 199 | |
| 200 | static void poison_all(void) { |
| 201 | uint32_t *u = g_buf.cpu; |
| 202 | for (size_t i = 0; i < 4u * NEL; i++) u[i] = POISON; |
| 203 | (void)omega_gpu_session_sync_to_gpu(&g_buf); |
| 204 | } |
| 205 | static unsigned wrong(int b, float want) { |
| 206 | (void)omega_gpu_session_sync_to_cpu(&g_buf); |
| 207 | const float *p = buf_at(b); |
| 208 | unsigned n = 0; |
| 209 | for (unsigned i = 0; i < NEL; i++) n += f2u(p[i]) != f2u(want); |
| 210 | return n; |
| 211 | } |
| 212 | |
| 213 | static uint32_t g_args[8][ARGS]; /* the launches' argument words stay alive until submit */ |
| 214 | static int add_spin(OmegaGpuCmdList *cl, int slot, int out_buf, uint32_t iters, float tag) { |
| 215 | spin_args(g_args[slot], va_at(out_buf), iters, tag); |
| 216 | OmegaGpuLaunch L = { .code_va = K_spin.code.va, .gpr_count = K_spin.gpr, .threads_x = THREADS, .threads_y = 1, .grid_x = CTAS, .grid_y = 1, |
| 217 | .num_elements = NEL, .args = g_args[slot], .n_args = ARGS }; |
| 218 | return omega_gpu_cmdlist_add(cl, &L) < 0 ? -1 : 0; |
| 219 | } |
| 220 | static int add_copy(OmegaGpuCmdList *cl, int slot) { |
| 221 | copy_args(g_args[slot], va_at(B_X), va_at(B_Y)); |
| 222 | OmegaGpuLaunch L = { .code_va = K_copy.code.va, .gpr_count = K_copy.gpr, .threads_x = THREADS, .threads_y = 1, .grid_x = CTAS, .grid_y = 1, |
| 223 | .num_elements = NEL, .args = g_args[slot], .n_args = ARGS }; |
| 224 | return omega_gpu_cmdlist_add(cl, &L) < 0 ? -1 : 0; |
| 225 | } |
| 226 | |
| 227 | static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; } |
| 228 | static uint64_t median(uint64_t *v, int n) { qsort(v, (size_t)n, sizeof *v, cmp_u64); return v[n / 2]; } |
| 229 | |
| 230 | static OmegaGpuCmdList *g_a, *g_b, *g_ab; |
| 231 | |
| 232 | /* one solo submit of a one-spin list; values checked; elapsed ns or 0 on failure */ |
| 233 | static uint64_t solo(OmegaGpuCmdList *cl, int out_buf, uint32_t iters, float tag, const OmegaGpuCmdListOpts *o) { |
| 234 | OmegaGpuCmdListStats st; |
| 235 | poison_all(); |
| 236 | if (omega_gpu_cmdlist_submit(cl, o, &st) != 0) { printf(" solo submit: %s\n", omega_gpu_session_last_error()); return 0; } |
| 237 | unsigned bad = wrong(out_buf, spin_value(iters, tag)); |
| 238 | if (bad) { printf(" solo: %u wrong values\n", bad); return 0; } |
| 239 | return st.elapsed_ns; |
| 240 | } |
| 241 | |
| 242 | static int overlap(uint32_t iters, int fbn, double *ratio_out) { |
| 243 | char w[300]; |
| 244 | OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); |
| 245 | o.first_barrier_none = fbn; |
| 246 | uint64_t sa[REPS], sb[REPS], sp[REPS]; |
| 247 | for (int r = 0; r < REPS; r++) { |
| 248 | sa[r] = solo(g_a, B_OA, iters, 1.0f, &o); |
| 249 | sb[r] = solo(g_b, B_OB, iters, 2.0f, &o); |
| 250 | OmegaGpuCmdListStats s1, s2; |
| 251 | poison_all(); |
| 252 | int rc = omega_gpu_cmdlist_submit_pair(g_a, g_b, &o, &s1, &s2); |
| 253 | unsigned bad = rc ? NEL : wrong(B_OA, spin_value(iters, 1.0f)) + wrong(B_OB, spin_value(iters, 2.0f)); |
| 254 | if (rc || bad) { printf(" pair: %s, %u wrong values\n", rc ? omega_gpu_session_last_error() : "ok", bad); sp[r] = 0; } |
| 255 | else sp[r] = s1.elapsed_ns; |
| 256 | if (!sa[r] || !sb[r] || !sp[r]) { check(0, "overlap: every submit ran and every value is right"); return -1; } |
| 257 | } |
| 258 | const uint64_t ma = median(sa, REPS), mb = median(sb, REPS), mp = median(sp, REPS); |
| 259 | const double ratio = (double)mp / (double)(ma + mb); |
| 260 | *ratio_out = ratio; |
| 261 | snprintf(w, sizeof w, "overlap (%s): solo A %.3f ms, solo B %.3f ms, pair %.3f ms (medians of %d, values right): pair / (A + B) = %.3f", |
| 262 | fbn ? "first_barrier_none" : "default barrier", ma / 1e6, mb / 1e6, mp / 1e6, REPS, ratio); |
| 263 | printf("%s\n", w); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int serial_control(uint32_t iters, double *ratio_out) { |
| 268 | OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); |
| 269 | uint64_t sa[REPS], sab[REPS]; |
| 270 | for (int r = 0; r < REPS; r++) { |
| 271 | sa[r] = solo(g_a, B_OA, iters, 1.0f, &o); |
| 272 | OmegaGpuCmdListStats st; |
| 273 | poison_all(); |
| 274 | int rc = omega_gpu_cmdlist_submit(g_ab, &o, &st); |
| 275 | unsigned bad = rc ? NEL : wrong(B_OA, spin_value(iters, 1.0f)) + wrong(B_OB, spin_value(iters, 2.0f)); |
| 276 | if (!sa[r] || rc || bad) { check(0, "serial control: every submit ran and every value is right"); return -1; } |
| 277 | sab[r] = st.elapsed_ns; |
| 278 | } |
| 279 | const uint64_t ma = median(sa, REPS), mab = median(sab, REPS); |
| 280 | *ratio_out = (double)mab / (double)(2 * ma); |
| 281 | printf("serial control: solo %.3f ms, both in one list on one queue %.3f ms: ratio %.3f\n", ma / 1e6, mab / 1e6, *ratio_out); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* list A: spin into X; list B: [wait for A launch 0] copy X -> Y. Returns the count of wrong Y, or -1 on a submit failure. */ |
| 286 | static long event_run(uint32_t iters, int with_wait, int fbn) { |
| 287 | OmegaGpuCmdList *a = NULL, *b = NULL; |
| 288 | long res = -1; |
| 289 | if (omega_gpu_cmdlist_create(1, &a) || omega_gpu_cmdlist_create(1, &b)) { printf(" event: create: %s\n", omega_gpu_session_last_error()); goto out; } |
| 290 | omega_gpu_session_lock(); |
| 291 | if (add_spin(a, 4, B_X, iters, 3.0f) || (with_wait && omega_gpu_cmdlist_wait_for(b, a, 0)) || add_copy(b, 5)) { |
| 292 | omega_gpu_session_unlock(); printf(" event: add: %s\n", omega_gpu_session_last_error()); goto out; |
| 293 | } |
| 294 | OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); |
| 295 | o.first_barrier_none = fbn; |
| 296 | OmegaGpuCmdListStats s1, s2; |
| 297 | if (with_wait) { /* the refusals: a list with a wait never runs alone, nor waits on a second list */ |
| 298 | OmegaGpuCmdList *c = NULL; |
| 299 | int alone = omega_gpu_cmdlist_submit(b, &o, &s2) == -1 && strstr(omega_gpu_session_last_error(), "only in a pair") != NULL; |
| 300 | omega_gpu_session_unlock(); |
| 301 | int third = omega_gpu_cmdlist_create(1, &c) == 0; |
| 302 | omega_gpu_session_lock(); |
| 303 | third = third && add_spin(c, 6, B_OA, 1, 0.0f) == 0 && omega_gpu_cmdlist_wait_for(b, c, 0) == -1; |
| 304 | omega_gpu_session_unlock(); |
| 305 | omega_gpu_cmdlist_free(c); |
| 306 | omega_gpu_session_lock(); |
| 307 | check(alone && third, "event refusals: a list with a wait is refused alone, and cannot wait on a second list"); |
| 308 | } |
| 309 | poison_all(); |
| 310 | int rc = omega_gpu_cmdlist_submit_pair(a, b, &o, &s1, &s2); |
| 311 | if (rc) printf(" event: pair: %s\n", omega_gpu_session_last_error()); |
| 312 | else { |
| 313 | unsigned bx = wrong(B_X, spin_value(iters, 3.0f)), by = wrong(B_Y, spin_value(iters, 3.0f) + 1.0f); |
| 314 | printf(" event (%s, %s): X %u wrong, Y %u wrong of %u, pair %.3f ms\n", with_wait ? "wait" : "NO wait", fbn ? "first_barrier_none" : "default barrier", |
| 315 | bx, by, NEL, s1.elapsed_ns / 1e6); |
| 316 | res = bx ? -1 : (long)by; |
| 317 | } |
| 318 | omega_gpu_session_unlock(); |
| 319 | out: |
| 320 | omega_gpu_cmdlist_free(a); omega_gpu_cmdlist_free(b); |
| 321 | return res; |
| 322 | } |
| 323 | |
| 324 | static void chip_tier(int values_only) { |
| 325 | char w[300]; |
| 326 | setenv("AIEN_OMEGA_GPU_CHANNELS", "2", 1); |
| 327 | if (omega_gpu_cmdlist_create(1, &g_a) || omega_gpu_cmdlist_create(1, &g_b) || omega_gpu_cmdlist_create(2, &g_ab)) { |
| 328 | printf("NOT_RUN: command lists: %s\n", omega_gpu_session_last_error()); exit(2); |
| 329 | } |
| 330 | omega_gpu_session_lock(); |
| 331 | if (!omega_gpu_session_open() || omega_gpu_session_alloc(4u * NEL * 4u, &g_buf) || kern_upload(&K_spin, 0) || kern_upload(&K_copy, 1)) { |
| 332 | printf("NOT_RUN: device, buffer or kernels: %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); exit(2); |
| 333 | } |
| 334 | OmegaGpuQueueHandle q1; |
| 335 | check(omega_gpu_session_extra_queue(1, &q1) == 0, "the second queue opens (AIEN_OMEGA_GPU_CHANNELS = 2)"); |
| 336 | |
| 337 | /* calibrate: one spin of about 60 ms */ |
| 338 | uint32_t iters = 1u << 16; |
| 339 | OmegaGpuCmdListOpts o0; memset(&o0, 0, sizeof o0); |
| 340 | omega_gpu_cmdlist_reset(g_a); |
| 341 | if (add_spin(g_a, 0, B_OA, iters, 1.0f)) { check(0, "calibration list"); omega_gpu_session_unlock(); return; } |
| 342 | uint64_t t = solo(g_a, B_OA, iters, 1.0f, &o0); |
| 343 | if (!t) { check(0, "calibration spin ran with right values"); omega_gpu_session_unlock(); return; } |
| 344 | double scaled = (double)iters * 60e6 / (double)t; |
| 345 | iters = scaled > MAX_ITERS ? MAX_ITERS : (uint32_t)scaled; |
| 346 | printf("calibration: 2^16 turns took %.3f ms; using %u turns\n", t / 1e6, iters); |
| 347 | |
| 348 | omega_gpu_cmdlist_reset(g_a); omega_gpu_cmdlist_reset(g_b); omega_gpu_cmdlist_reset(g_ab); |
| 349 | if (add_spin(g_a, 0, B_OA, iters, 1.0f) || add_spin(g_b, 1, B_OB, iters, 2.0f) || |
| 350 | add_spin(g_ab, 2, B_OA, iters, 1.0f) || add_spin(g_ab, 3, B_OB, iters, 2.0f)) { check(0, "timing lists"); omega_gpu_session_unlock(); return; } |
| 351 | |
| 352 | if (!values_only) { |
| 353 | double r_def = 9, r_fbn = 9, r_ser = 0; |
| 354 | int ok_def = overlap(iters, 0, &r_def) == 0, ok_fbn = overlap(iters, 1, &r_fbn) == 0, ok_ser = serial_control(iters, &r_ser) == 0; |
| 355 | snprintf(w, sizeof w, "C4 overlap: two queues of the same work finish in under 0.75 of the solo sum (default %.3f, first_barrier_none %.3f)", r_def, r_fbn); |
| 356 | check((ok_def && r_def < 0.75) || (ok_fbn && r_fbn < 0.75), w); |
| 357 | snprintf(w, sizeof w, "red check: the same two spins in order on one queue do NOT pass the 0.75 bar (ratio %.3f)", r_ser); |
| 358 | check(ok_ser && r_ser >= 0.75, w); |
| 359 | } else puts("C4 overlap: NOT_RUN (values-only; timing needs an exclusive window)"); |
| 360 | omega_gpu_session_unlock(); |
| 361 | |
| 362 | /* the event: positive, then the control without the wait (both barrier modes) */ |
| 363 | long pos = event_run(iters, 1, 0); |
| 364 | snprintf(w, sizeof w, "C4 event: B's copy waited for A's spin on the other queue; every Y right (%ld wrong)", pos); |
| 365 | check(pos == 0, w); |
| 366 | long neg0 = event_run(iters, 0, 0), neg1 = neg0 > 0 ? neg0 : event_run(iters, 0, 1); |
| 367 | snprintf(w, sizeof w, "negative control: without the wait B ran early and Y is wrong (default barrier %ld wrong, first_barrier_none %ld wrong)", neg0, neg1); |
| 368 | check(neg0 > 0 || neg1 > 0, w); |
| 369 | long again = event_run(iters, 1, 1); |
| 370 | snprintf(w, sizeof w, "C4 event with first_barrier_none: every Y right (%ld wrong)", again); |
| 371 | check(again == 0, w); |
| 372 | |
| 373 | omega_gpu_session_lock(); |
| 374 | omega_gpu_session_free(&g_buf); omega_gpu_session_free(&K_spin.code); omega_gpu_session_free(&K_copy.code); |
| 375 | omega_gpu_session_unlock(); |
| 376 | omega_gpu_cmdlist_free(g_a); omega_gpu_cmdlist_free(g_b); omega_gpu_cmdlist_free(g_ab); |
| 377 | } |
| 378 | |
| 379 | int main(int argc, char **argv) { |
| 380 | const char *tier = "chip"; |
| 381 | for (int i = 1; i < argc; i++) { |
| 382 | if (!strcmp(argv[i], "--host-only")) tier = "host"; |
| 383 | else if (!strcmp(argv[i], "--values-only")) tier = "values"; |
| 384 | else if (!strcmp(argv[i], "--sim")) tier = "sim"; |
| 385 | else { fprintf(stderr, "usage: %s [--host-only | --sim | --values-only]\n", argv[0]); return 2; } |
| 386 | } |
| 387 | if (!strcmp(tier, "host")) host_only(); |
| 388 | else if (!strcmp(tier, "sim")) { host_only(); sim_tier(); } |
| 389 | else chip_tier(!strcmp(tier, "values")); |
| 390 | if (g_failed) printf("C4 streams %s: FAIL (%d of %d)\n", tier, g_failed, g_checks); |
| 391 | else printf("C4 streams %s: PASS (%d checks)\n", tier, g_checks); |
| 392 | return g_failed ? 1 : 0; |
| 393 | } |