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 / evidence / graph-residue-20261106 / runtime-hello-sim-oracle.gcov

737 lines · 41191 bytes Raw Blame History
1 -: 0:Source:examples/hello_gpu/hello_gpu.c
2 -: 0:Graph:build/hello_gpu-oracle-hello_gpu.gcno
3 -: 0:Data:build/hello_gpu-oracle-hello_gpu.gcda
4 -: 0:Runs:3
5 -: 1:/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
6 -: 2:/* hello_gpu: the smallest complete Omega program. It adds two arrays of one million floats on the GPU
7 -: 3: * (c[i] = a[i] + b[i]) and checks every answer on the CPU.
8 -: 4: *
9 -: 5: * What makes it "Omega": the GPU program (the kernel) is written below as a short list of machine-level
10 -: 6: * steps, handed to Omega's own code generator, and run through Omega's own launcher. Nothing here uses
11 -: 7: * CUDA, nvcc, PTX or any NVIDIA user-space library.
12 -: 8: *
13 -: 9: * The five steps, in the order they appear in main():
14 -: 10: * 1. Build the kernel build_vecadd() a list of IR instructions, then register allocation
15 -: 11: * 2. Open the GPU omega_gpu_session_open()
16 -: 12: * 3. Upload kernel + data omega_gpu_session_alloc_code(), omega_gpu_session_alloc()
17 -: 13: * 4. Launch omega_gpu_session_launch() (eight launches of 131072 elements each)
18 -: 14: * 5. Check every one of the 1048576 outputs against the CPU's own a + b
19 -: 15: *
20 -: 16: * Prints PASS or FAIL. Exit code: 0 PASS, 1 FAIL (wrong or missing results), 2 the GPU could not be used.
21 -: 17: *
22 -: 18: * `hello_gpu --sim` runs the same kernel program on the host warp simulator instead (tests/bw_warp_sim.h):
23 -: 19: * no GPU is opened. A bad memory access there stops at the launch that made it. The simulator names the
24 -: 20: * pc. This program names the block, thread, address and buffer from the launch it issued (developer bar
25 -: 21: * D3 and D4, docs/PAIN_POINTS.md): the mirrored simulator does not store those fields. `--sim --bug=past-end`
26 -: 22: * shows such a report: the last launch is given one element too many, a classic off-by-one.
27 -: 23: *
28 -: 24: * `--bug=short` is the negative control for the check itself, on the GPU or with --sim: the last launch is
29 -: 25: * given one element too FEW, so c[1048575] is never written. That writes nothing outside a buffer, so it is
30 -: 26: * safe on the chip; the check must report "1 never written", print FAIL and exit 1. `make hello-gpu-control`
31 -: 27: * builds a separate binary, build/hello_gpu_control, with it and passes only when it goes red that way.
32 -: 28: * MIGRATION-PENDING C (GpuStack PLAN, Language rule): it exists only when CUDAPP_MIGRATION_PENDING_HELLO_CONTROL
33 -: 29: * is defined to 1, which is OFF by default (build/hello_gpu never has it). Its .om successor is the native .om
34 -: 30: * hello of aien-dev/CUDApp#79 (OM-GPU-RETIRE), which replaces this whole C program.
35 -: 31: *
36 -: 32: * Both paths print the work the job takes (bytes moved, floating-point operations) and the least time
37 -: 33: * those bytes need at this GB10's measured read ceiling (D12). The mirrored simulator no longer counts
38 -: 34: * those bytes, so --sim accounts the vecadd this program issued: two f32 loads, one f32 store and one
39 -: 35: * FADD per element. Both paths also print the time the kernel took to build, the
40 -: 36: * hash of its machine code (D10, no compiling at run time), and the hash of the output (D7: the same
41 -: 37: * inputs give the same bits on every run).
42 -: 38: *
43 -: 39: * Hardware: NVIDIA GB10 (DGX Spark, sm_121), Linux, the open NVIDIA kernel driver loaded.
44 -: 40: * Built by `make hello-gpu` (see README, "Run your first kernel").
45 -: 41: */
46 -: 42:#ifndef CUDAPP_MIGRATION_PENDING_HELLO_CONTROL
47 -: 43:#define CUDAPP_MIGRATION_PENDING_HELLO_CONTROL 0 /* migration-pending switch, OFF by default (see above) */
48 -: 44:#endif
49 -: 45:#include "omega_blackwell_codegen.h"
50 -: 46:#include "omega_ampere_codegen.h"
51 -: 47:#include "omega_blackwell_qmd.h"
52 -: 48:#include "omega_gpu_session.h"
53 -: 49:#include "sha256.h"
54 -: 50:#include "cudapp_native.h"
55 -: 51:#ifndef CUDAPP_MIGRATION_PENDING_HELLO_HOST
56 -: 52:#define CUDAPP_MIGRATION_PENDING_HELLO_HOST 0
57 -: 53:#endif
58 -: 54:#include "../../tests/bw_warp_sim.h" /* the host warp simulator the host tests use; only --sim calls it */
59 -: 55:#include <stdio.h>
60 -: 56:#include <stdlib.h>
61 -: 57:#include <string.h>
62 -: 58:#include <time.h>
63 -: 59:
64 -: 60:#define N_TOTAL (1u << 20) /* 1048576 floats per array */
65 -: 61:#define THREADS 128u /* threads per block (CTA) */
66 -: 62:#define MAX_CTAS 1024u /* blocks per launch the session is proven for (omega_gpu_elementwise_api.h) */
67 -: 63:#define CHUNK (THREADS * MAX_CTAS) /* elements per launch: 131072, so N_TOTAL is 8 launches */
68 -: 64:#define POISON 0xFFC0FFEEu /* a NaN pattern c starts out as: an element the GPU never wrote stays this */
69 -: 65:#define READ_CEILING_GBPS 195.1 /* GB10 global reads, measured: evidence/OWN30B-T3-TC-PEAK-BW-5faa0f70-20261013T2236Z/ */
70 -: 66:static uint32_t target_sm = 121; /* explicit --target=sm_86; never infer from the build host */
71 -: 67:
72 -: 68:/* ---- Step 1: the kernel, as IR instructions -------------------------------------------------------------
73 -: 69: * Each instruction carries a "control word" that tells the chip how long to wait before the next one.
74 -: 70: * Two rules are enough for this kernel (chip-learned, see the header of src/omega_gpu_elementwise_api.c):
75 -: 71: * - a load or special-register read (LDC, LDCU, S2R, LDG) finishes later, on a scoreboard slot;
76 -: 72: * the next instruction waits for that slot;
77 -: 73: * - plain arithmetic (IMAD, FADD) needs about 6 cycles before its result is read.
78 -: 74: * To stay simple, every instruction here waits for everything still pending. */
79 -: 75:#define NOREG (-1)
80 -: 76:#define CTRL(stall, wbar, rbar, wait) \
81 -: 77: (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20))
82 -: 78:#define YIELD 0x2000u
83 -: 79:enum { SB_LOAD = 0, SB_STORE = 1, SB_UNIFORM = 2, SB_NONE = 7 }; /* scoreboard slots */
84 -: 80:enum { K_ALU, K_PRED, K_LOAD, K_ULOAD, K_STORE, K_EXIT, K_EXIT_IF_P0 }; /* what kind of instruction */
85 -: 81:
86 -: 82:typedef struct { BlackwellIRProgram *p; int desc; uint32_t pending; int err; } Kb; /* desc: the memory descriptor register */
87 -: 83:
88function emit called 57 returned 100% blocks executed 94%
89 57: 84:static void emit(Kb *k, BlackwellIROpcode op, int dst, int a, int b, int c, uint32_t imm, int kind) {
90 57: 85: BlackwellIRInsn in; memset(&in, 0, sizeof in);
91 57: 86: in.op = op; in.dst_vreg = dst; in.src1_vreg = a; in.src2_vreg = b; in.src3_vreg = c; in.ureg = (op == BW_IR_LDG_E || op == BW_IR_STG_E) ? k->desc : NOREG; in.imm = imm;
92branch 0 taken 51 (fallthrough)
93branch 1 taken 6
94branch 2 taken 3 (fallthrough)
95branch 3 taken 48
96 57: 87: uint32_t wait = k->pending;
97 -: 88:#if CUDAPP_MIGRATION_PENDING_HELLO_HOST
98 57: 89: switch (kind) {
99branch 0 taken 18
100branch 1 taken 3
101branch 2 taken 24
102branch 3 taken 3
103branch 4 taken 3
104branch 5 taken 3
105branch 6 taken 3
106branch 7 taken 0
107 18: 90: case K_ALU: in.control = CTRL(6, SB_NONE, SB_NONE, wait); k->pending = 0; break;
108 3: 91: case K_PRED: in.control = CTRL(14, SB_NONE, SB_NONE, wait); k->pending = 0; break;
109 24: 92: case K_LOAD: in.control = CTRL(4, SB_LOAD, SB_NONE, wait) | YIELD; k->pending = 1u << SB_LOAD; break;
110 3: 93: case K_ULOAD: in.control = CTRL(4, SB_UNIFORM, SB_NONE, wait) | YIELD; k->pending = 1u << SB_UNIFORM; in.is_uniform = true; break;
111 3: 94: case K_STORE: in.control = CTRL(4, SB_NONE, SB_STORE, wait) | YIELD; k->pending = 1u << SB_STORE; break;
112 3: 95: case K_EXIT: in.control = CTRL(5, SB_NONE, SB_NONE, wait); k->pending = 0; break;
113 3: 96: case K_EXIT_IF_P0: in.control = CTRL(5, SB_NONE, SB_NONE, wait); in.predicate_p0 = true; break; /* may fall through: keep waiting */
114 -: 97: }
115 -: 98:#else
116 -: 99: const uint64_t control_cfg[16] = {(uint64_t)kind, wait}; uint64_t d[16], result = 0;
117 -: 100: if (omega_hplan(&result, 1, control_cfg, 16, d, 16) != OMEGA_OK || result != 1) abort();
118 -: 101: in.control = (uint32_t)d[0]; k->pending = (uint32_t)d[1];
119 -: 102: in.is_uniform = kind == K_ULOAD; in.predicate_p0 = kind == K_EXIT_IF_P0;
120 -: 103:#endif
121 57*: 104: if (omega_bw_ir_append(k->p, &in) < 0) k->err = -1;
122call 0 returned 57
123branch 1 taken 0 (fallthrough)
124branch 2 taken 57
125 57: 105:}
126 -: 106:
127 -: 107:/* The kernel the chip runs once per thread:
128 -: 108: * i = block_index * 128 + thread_index
129 -: 109: * if (i >= n) exit
130 -: 110: * c[i] = a[i] + b[i]
131 -: 111: * a, b, c and n arrive in the launch's argument block (constant bank 0): a at 0x380, b at 0x388, c at 0x390
132 -: 112: * (64-bit addresses), n at 0x398. 0x358 holds the memory descriptor every load and store names. */
133function build_vecadd called 3 returned 100% blocks executed 97%
134 3: 113:static int build_vecadd(BlackwellIRProgram *prog) {
135 3: 114: omega_bw_ir_init(prog);
136call 0 returned 3
137 3: 115: Kb k = { .p = prog, .desc = omega_bw_ir_alloc_uvreg64(prog) };
138call 0 returned 3
139 3: 116: int pa = omega_bw_ir_alloc_vreg64(prog), pb = omega_bw_ir_alloc_vreg64(prog), pc = omega_bw_ir_alloc_vreg64(prog);
140call 0 returned 3
141call 1 returned 3
142call 2 returned 3
143 3: 117: int tid = omega_bw_ir_alloc_vreg(prog), cta = omega_bw_ir_alloc_vreg(prog), n = omega_bw_ir_alloc_vreg(prog);
144call 0 returned 3
145call 1 returned 3
146call 2 returned 3
147 3: 118: int i = omega_bw_ir_alloc_vreg(prog), at = omega_bw_ir_alloc_vreg64(prog);
148call 0 returned 3
149call 1 returned 3
150 3: 119: int x = omega_bw_ir_alloc_vreg(prog), y = omega_bw_ir_alloc_vreg(prog), z = omega_bw_ir_alloc_vreg(prog);
151call 0 returned 3
152call 1 returned 3
153call 2 returned 3
154 -: 120:
155 3: 121: emit(&k, BW_IR_LDCU64, k.desc, NOREG, NOREG, NOREG, 0x358, K_ULOAD);
156call 0 returned 3
157 3: 122: emit(&k, BW_IR_LDC64, pa, NOREG, NOREG, NOREG, 0x380, K_LOAD);
158call 0 returned 3
159 3: 123: emit(&k, BW_IR_LDC64, pb, NOREG, NOREG, NOREG, 0x388, K_LOAD);
160call 0 returned 3
161 3: 124: emit(&k, BW_IR_LDC64, pc, NOREG, NOREG, NOREG, 0x390, K_LOAD);
162call 0 returned 3
163 3: 125: emit(&k, BW_IR_LDC, n, NOREG, NOREG, NOREG, 0x398, K_LOAD);
164call 0 returned 3
165 3: 126: emit(&k, BW_IR_S2R, tid, NOREG, NOREG, NOREG, BW_SR_TID_X, K_LOAD);
166call 0 returned 3
167 3: 127: emit(&k, BW_IR_S2R, cta, NOREG, NOREG, NOREG, BW_SR_CTAID_X, K_LOAD);
168call 0 returned 3
169 3: 128: emit(&k, BW_IR_IMAD, i, cta, NOREG, tid, THREADS, K_ALU); /* i = cta * 128 + tid */
170call 0 returned 3
171 3: 129: emit(&k, BW_IR_ISETP_GE_U32, NOREG, i, n, NOREG, 0, K_PRED); /* P0 = (i >= n) */
172call 0 returned 3
173 3: 130: emit(&k, BW_IR_EXIT, NOREG, NOREG, NOREG, NOREG, 0, K_EXIT_IF_P0); /* @P0 EXIT */
174call 0 returned 3
175 -: 131:
176 -: 132: /* IMAD.WIDE builds a 64-bit byte address: at = i * 4 + base pointer */
177 3: 133: emit(&k, BW_IR_IMAD_WIDE, at, i, NOREG, pa, 4, K_ALU);
178call 0 returned 3
179 3: 134: emit(&k, BW_IR_LDG_E, x, at, NOREG, NOREG, 0, K_LOAD); /* x = a[i] */
180call 0 returned 3
181 3: 135: emit(&k, BW_IR_IMAD_WIDE, at, i, NOREG, pb, 4, K_ALU);
182call 0 returned 3
183 3: 136: emit(&k, BW_IR_LDG_E, y, at, NOREG, NOREG, 0, K_LOAD); /* y = b[i] */
184call 0 returned 3
185 3: 137: emit(&k, BW_IR_FADD, z, x, y, NOREG, 0, K_ALU); /* z = x + y */
186call 0 returned 3
187 3: 138: emit(&k, BW_IR_IMAD_WIDE, at, i, NOREG, pc, 4, K_ALU);
188call 0 returned 3
189 3: 139: emit(&k, BW_IR_STG_E, NOREG, at, z, NOREG, 0, K_STORE); /* c[i] = z */
190call 0 returned 3
191 -: 140:
192 -: 141: /* every kernel ends with EXIT, then a branch-to-self the hardware never reaches */
193 3: 142: emit(&k, BW_IR_EXIT, NOREG, NOREG, NOREG, NOREG, 0, K_EXIT);
194call 0 returned 3
195 3: 143: emit(&k, BW_IR_BRA, NOREG, NOREG, NOREG, NOREG, 0, K_ALU);
196call 0 returned 3
197 3: 144: prog->insns[prog->count - 1].control = 0x000fc000u;
198 -: 145:
199 3*: 146: if (k.err) return -1;
200branch 0 taken 0 (fallthrough)
201branch 1 taken 3
202 3: 147: return omega_bw_regalloc_solve(prog); /* picks the physical registers */
203call 0 returned 3
204 -: 148:}
205 -: 149:
206 -: 150:/* ---- helpers shared by both paths ---------------------------------------------------------------------- */
207function now_us called 6 returned 100% blocks executed 100%
208 6: 151:static double now_us(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (double)t.tv_sec * 1e6 + (double)t.tv_nsec / 1e3; }
209call 0 returned 6
210function print_sha called 5 returned 100% blocks executed 100%
211 5: 152:static void print_sha(const char *what, const void *data, size_t len) {
212 -: 153: uint8_t d[SHA256_DIGEST_SIZE];
213 5: 154: sha256_hash((const uint8_t *)data, len, d);
214call 0 returned 5
215 5: 155: printf("%s sha256 ", what);
216call 0 returned 5
217 165: 156: for (int i = 0; i < SHA256_DIGEST_SIZE; i++) printf("%02x", d[i]);
218call 0 returned 160
219branch 1 taken 160
220branch 2 taken 5 (fallthrough)
221 5: 157: printf("\n");
222call 0 returned 5
223 5: 158:}
224function print_work called 2 returned 100% blocks executed 67%
225 2: 159:static void print_work(uint64_t read, uint64_t written, uint64_t fp_ops, double measured_us) {
226 -: 160:#if CUDAPP_MIGRATION_PENDING_HELLO_HOST
227 2: 161: uint64_t moved = read + written;
228 2*: 162: double floor_us = target_sm == 121 ? (double)moved / (READ_CEILING_GBPS * 1e3) : 0;
229branch 0 taken 2 (fallthrough)
230branch 1 taken 0
231 2*: 163: double percent = measured_us > 0 ? 100.0 * floor_us / measured_us : 0;
232branch 0 taken 0 (fallthrough)
233branch 1 taken 2
234 -: 164:#else
235 -: 165: uint64_t bits, result = 0, d[16]; memcpy(&bits, &measured_us, sizeof bits);
236 -: 166: const uint64_t c[16] = {target_sm, read, written, bits};
237 -: 167: if (omega_hplan(&result, 2, c, 16, d, 16) != OMEGA_OK || result != 1) abort();
238 -: 168: uint64_t moved = d[0]; double floor_us, percent;
239 -: 169: memcpy(&floor_us, d + 1, sizeof floor_us); memcpy(&percent, d + 2, sizeof percent);
240 -: 170:#endif
241 2: 171: printf("work: %llu bytes moved (%llu read, %llu written), %llu floating-point operations\n",
242call 0 returned 2
243 -: 172: (unsigned long long)moved, (unsigned long long)read, (unsigned long long)written, (unsigned long long)fp_ops);
244 2: 173: if (target_sm == 121) {
245branch 0 taken 2 (fallthrough)
246branch 1 taken 0
247 2: 174: printf(" at the GB10's measured %.1f GB/s read ceiling this needs at least %.1f us", READ_CEILING_GBPS, floor_us);
248call 0 returned 2
249 2*: 175: if (measured_us > 0) printf("; the GPU took %.1f us (%.0f%% of the ceiling)", measured_us, percent);
250branch 0 taken 0 (fallthrough)
251branch 1 taken 2
252call 2 never executed
253 2: 176: printf("\n");
254call 0 returned 2
255 #####: 177: } else if (measured_us > 0) printf(" sm_86 device time %.1f us (no GB10 bandwidth claim)\n", measured_us);
256branch 0 never executed (fallthrough)
257branch 1 never executed
258call 2 never executed
259 2: 178:}
260function inputs called 3 returned 100% blocks executed 100%
261 3: 179:static void inputs(float *fa, float *fb, uint32_t *wc) {
262 -: 180:#if CUDAPP_MIGRATION_PENDING_HELLO_HOST
263 3145731: 181: for (uint32_t i = 0; i < N_TOTAL; i++) { fa[i] = (float)i * 0.25f; fb[i] = 1.0f / (float)(1 + i % 7); wc[i] = POISON; }
264branch 0 taken 3145728
265branch 1 taken 3 (fallthrough)
266 -: 182:#else
267 -: 183: uint64_t result = 0, stats[4];
268 -: 184: if (omega_hello(&result, 0, fa, N_TOTAL * 4u, fb, N_TOTAL * 4u,
269 -: 185: wc, N_TOTAL * 4u, stats, 4) != OMEGA_OK || result != 1) abort();
270 -: 186:#endif
271 3: 187:}
272 -: 188:/* Step 5, both paths: same IEEE single-precision add on both sides, so the bits must match. */
273function check called 2 returned 100% blocks executed 100%
274 2: 189:static int check(const float *fa, const float *fb, const uint32_t *wc) {
275 2: 190: uint32_t wrong = 0, unwritten = 0, first_bad = 0;
276 -: 191:#if CUDAPP_MIGRATION_PENDING_HELLO_HOST
277 2097154: 192: for (uint32_t i = 0; i < N_TOTAL; i++) {
278branch 0 taken 2097152
279branch 1 taken 2 (fallthrough)
280 2097152: 193: float want = fa[i] + fb[i];
281 2097152: 194: if (wc[i] == POISON) unwritten++;
282branch 0 taken 1 (fallthrough)
283branch 1 taken 2097151
284 2097152: 195: if (memcmp(&want, &wc[i], sizeof want) != 0) { if (!wrong++) first_bad = i; }
285branch 0 taken 1 (fallthrough)
286branch 1 taken 2097151
287branch 2 taken 1 (fallthrough)
288branch 3 taken 0
289 -: 196: }
290 -: 197:#else
291 -: 198: uint64_t result = 0, stats[4];
292 -: 199: if (omega_hello(&result, 1, (void *)fa, N_TOTAL * 4u, (void *)fb, N_TOTAL * 4u,
293 -: 200: (void *)wc, N_TOTAL * 4u, stats, 4) != OMEGA_OK || result != 1) return 1;
294 -: 201: wrong = (uint32_t)stats[0]; unwritten = (uint32_t)stats[1]; first_bad = (uint32_t)stats[2];
295 -: 202:#endif
296 2: 203: printf("checked %u of %u elements: %u wrong, %u never written\n", N_TOTAL, N_TOTAL, wrong, unwritten);
297call 0 returned 2
298 2: 204: if (wrong) { float want, got;
299branch 0 taken 1 (fallthrough)
300branch 1 taken 1
301 -: 205:#if CUDAPP_MIGRATION_PENDING_HELLO_HOST
302 1: 206: want = fa[first_bad] + fb[first_bad];
303 -: 207:#else
304 -: 208: const uint32_t bits = (uint32_t)stats[3]; memcpy(&want, &bits, sizeof want);
305 -: 209:#endif
306 1: 210: memcpy(&got, &wc[first_bad], sizeof got);
307 1: 211: printf("first mismatch: c[%u] = %g, expected %g\n", first_bad, got, want); }
308call 0 returned 1
309 2: 212: return wrong ? 1 : 0;
310 -: 213:}
311function op_text called 1 returned 100% blocks executed 23%
312 1: 214:static const char *op_text(BlackwellIROpcode op) {
313 1: 215: switch (op) {
314branch 0 taken 1
315branch 1 taken 0
316branch 2 taken 0
317branch 3 taken 0
318branch 4 taken 0
319branch 5 taken 0
320branch 6 taken 0
321branch 7 taken 0
322branch 8 taken 0
323branch 9 taken 0
324branch 10 taken 0
325 1: 216: case BW_IR_LDG_E: return "LDG.E (4-byte load)";
326 #####: 217: case BW_IR_STG_E: return "STG.E (4-byte store)";
327 #####: 218: case BW_IR_IMAD_WIDE: return "IMAD.WIDE (64-bit address)";
328 #####: 219: case BW_IR_IMAD: return "IMAD";
329 #####: 220: case BW_IR_FADD: return "FADD";
330 #####: 221: case BW_IR_S2R: return "S2R (read thread or block index)";
331 #####: 222: case BW_IR_LDC: case BW_IR_LDC64: return "LDC (read a launch argument)";
332 #####: 223: case BW_IR_ISETP_GE_U32: return "ISETP.GE.U32";
333 #####: 224: case BW_IR_EXIT: return "EXIT";
334 #####: 225: case BW_IR_BRA: return "BRA";
335 #####: 226: default: return "instruction";
336 -: 227: }
337 -: 228:}
338function chunk_plan called 27 returned 100% blocks executed 100%
339 27: 229:static void chunk_plan(uint32_t base, int bug, uint64_t d[16]) {
340 -: 230:#if CUDAPP_MIGRATION_PENDING_HELLO_HOST
341 27: 231: uint32_t n = N_TOTAL - base < CHUNK ? N_TOTAL - base : CHUNK;
342 27: 232: if (base + CHUNK >= N_TOTAL) { if (bug == 1) ++n; if (bug == 2) --n; }
343branch 0 taken 3 (fallthrough)
344branch 1 taken 24
345branch 2 taken 1 (fallthrough)
346branch 3 taken 2
347branch 4 taken 1 (fallthrough)
348branch 5 taken 2
349 27: 233: d[0] = (uint64_t)base * 4u; d[1] = n;
350 27: 234: d[2] = (n + THREADS - 1) / THREADS; d[3] = (N_TOTAL + CHUNK - 1) / CHUNK;
351 27: 235: d[4] = (uint64_t)n * 8u; d[5] = (uint64_t)n * 4u;
352 -: 236:#else
353 -: 237: const uint64_t c[16] = {base, N_TOTAL, CHUNK, THREADS, (uint64_t)bug}; uint64_t result = 0;
354 -: 238: if (omega_hplan(&result, 0, c, 16, d, 16) != OMEGA_OK || result != 1) abort();
355 -: 239:#endif
356 27: 240:}
357 -: 241:
358 -: 242:/* ---- --sim: the same program on the host warp simulator --------------------------------------------------- */
359function run_sim called 3 returned 100% blocks executed 82%
360 3: 243:static int run_sim(const BlackwellIRProgram *prog, int bug) {
361 3: 244: const size_t bytes = (size_t)N_TOTAL * sizeof(float);
362 3: 245: float *fa = aligned_alloc(64, bytes), *fb = aligned_alloc(64, bytes);
363 3: 246: uint32_t *wc = aligned_alloc(64, bytes);
364 3*: 247: if (!fa || !fb || !wc) { printf("FAIL: host memory\n"); return 1; }
365branch 0 taken 3 (fallthrough)
366branch 1 taken 0
367branch 2 taken 3 (fallthrough)
368branch 3 taken 0
369branch 4 taken 0 (fallthrough)
370branch 5 taken 3
371call 6 never executed
372 3: 248: inputs(fa, fb, wc);
373call 0 returned 3
374 -: 249: static WsSim s;
375 3: 250: memset(&s, 0, sizeof s);
376 3: 251: s.p = prog; s.threads = THREADS;
377 3: 252: const char *names[3] = { "a", "b", "c" };
378 3: 253: const uintptr_t base_of[3] = { (uintptr_t)fa, (uintptr_t)fb, (uintptr_t)wc };
379 12: 254: for (int k = 0; k < 3; k++) { s.ranges[k].lo = base_of[k]; s.ranges[k].hi = base_of[k] + bytes; }
380branch 0 taken 9
381branch 1 taken 3 (fallthrough)
382 3: 255: s.nranges = 3; /* every global access must land inside a, b or c */
383 3: 256: uint64_t plan[16]; chunk_plan(0, 0, plan);
384call 0 returned 3
385 3: 257: const uint32_t launches = (uint32_t)plan[3];
386 3: 258: uint32_t li = 0;
387 3: 259: uint64_t read_b = 0, wrote_b = 0, flops = 0;
388 26: 260: for (uint32_t base = 0; base < N_TOTAL; base += CHUNK) {
389branch 0 taken 24
390branch 1 taken 2 (fallthrough)
391 24: 261: li++;
392 24: 262: chunk_plan(base, bug, plan);
393call 0 returned 24
394 24: 263: uint64_t off = plan[0];
395 24: 264: uint32_t n = (uint32_t)plan[1];
396 24: 265: uint64_t pa = base_of[0] + off, pb = base_of[1] + off, pc = base_of[2] + off;
397 24: 266: memset(s.cbank, 0, sizeof s.cbank);
398 24: 267: s.cbank[0x380 / 4] = (uint32_t)pa; s.cbank[0x384 / 4] = (uint32_t)(pa >> 32);
399 24: 268: s.cbank[0x388 / 4] = (uint32_t)pb; s.cbank[0x38c / 4] = (uint32_t)(pb >> 32);
400 24: 269: s.cbank[0x390 / 4] = (uint32_t)pc; s.cbank[0x394 / 4] = (uint32_t)(pc >> 32);
401 24: 270: s.cbank[0x398 / 4] = n;
402 24: 271: uint32_t grid = (uint32_t)plan[2];
403 24: 272: if (ws_run(&s, grid, 1) != 0) {
404call 0 returned 24
405branch 1 taken 1 (fallthrough)
406branch 2 taken 23
407 1*: 273: const BlackwellIRInsn *in = (s.err_pc >= 0 && (size_t)s.err_pc < prog->count) ? &prog->insns[s.err_pc] : NULL;
408branch 0 taken 1 (fallthrough)
409branch 1 taken 0
410branch 2 taken 1 (fallthrough)
411branch 3 taken 0
412 1*: 274: int store = in && in->op == BW_IR_STG_E;
413branch 0 taken 1 (fallthrough)
414branch 1 taken 0
415branch 2 taken 0 (fallthrough)
416branch 3 taken 1
417 -: 275: /* --bug=past-end: the first out-of-range access is a[N_TOTAL], a 4-byte LDG.E.
418 -: 276: The mirrored simulator records the pc, not the lane or the address. */
419 -: 277: uint64_t fault_plan[16];
420 -: 278:#if CUDAPP_MIGRATION_PENDING_HELLO_HOST
421 1: 279: fault_plan[0] = base + n - 1;
422 1: 280: fault_plan[1] = (fault_plan[0] - base) / THREADS;
423 1: 281: fault_plan[2] = (fault_plan[0] - base) % THREADS;
424 1: 282: fault_plan[3] = (uint64_t)(uintptr_t)fa + fault_plan[0] * sizeof(float);
425 -: 283:#else
426 -: 284: const uint64_t fc[16] = {base, n, THREADS, (uint64_t)(uintptr_t)fa}; uint64_t fr = 0;
427 -: 285: if (omega_hplan(&fr, 3, fc, 16, fault_plan, 16) != OMEGA_OK || fr != 1) abort();
428 -: 286:#endif
429 1: 287: uint32_t block = (uint32_t)fault_plan[1], thread = (uint32_t)fault_plan[2];
430 1: 288: uint64_t fault_addr = fault_plan[3];
431 1: 289: unsigned fault_bytes = 4;
432 1*: 290: if (s.err == WS_E_MEM) printf("FAULT at launch %u of %u of kernel vecadd (simulator): global %s outside every buffer\n", li, launches, store ? "store" : "load");
433branch 0 taken 1 (fallthrough)
434branch 1 taken 0
435branch 2 taken 0 (fallthrough)
436branch 3 taken 1
437call 4 returned 1
438 #####: 291: else printf("FAULT at launch %u of %u of kernel vecadd (simulator): %s\n", li, launches, ws_err_name(s.err));
439call 0 never executed
440call 1 never executed
441 1: 292: printf(" launch: grid %u x 1 blocks of %u threads, n = %u, first element %u\n", grid, THREADS, n, base);
442call 0 returned 1
443 1: 293: if (in) printf(" instruction: %d of %zu, %s\n", s.err_pc, prog->count, op_text(in->op));
444branch 0 taken 1 (fallthrough)
445branch 1 taken 0
446call 2 returned 1
447call 3 returned 1
448 1: 294: printf(" thread: block %u, thread %u (element %llu of the whole job)\n", block, thread,
449 1: 295: (unsigned long long)fault_plan[0]);
450call 0 returned 1
451 1: 296: int near = 0; uint64_t dist = UINT64_MAX; int past = 1;
452 1: 297: uint64_t remaining = 0;
453 1: 298: if (s.err == WS_E_MEM) {
454branch 0 taken 1 (fallthrough)
455branch 1 taken 0
456 -: 299:#if CUDAPP_MIGRATION_PENDING_HELLO_HOST
457 4: 300: for (int k = 0; k < 3; k++) {
458branch 0 taken 3
459branch 1 taken 1 (fallthrough)
460 3: 301: uint64_t lo = s.ranges[k].lo, hi = s.ranges[k].hi, d; int pst;
461 3: 302: if (fault_addr >= hi) { d = fault_addr - hi; pst = 1; }
462branch 0 taken 3 (fallthrough)
463branch 1 taken 0
464 #####: 303: else if (fault_addr + fault_bytes <= lo) { d = lo - (fault_addr + fault_bytes); pst = 0; }
465branch 0 never executed (fallthrough)
466branch 1 never executed
467 #####: 304: else { d = 0; pst = 1; } /* starts inside, runs past the end */
468 3: 305: if (d < dist) { dist = d; near = k; past = pst; }
469branch 0 taken 1 (fallthrough)
470branch 1 taken 2
471 -: 306: }
472 1: 307: remaining = (s.ranges[near].hi - (s.ranges[near].lo + off)) / sizeof(float);
473 -: 308:#else
474 -: 309: uint64_t ranges[16] = {0}, nearest[16], nr = 0;
475 -: 310: for (int k = 0; k < 3; k++) { ranges[k * 2] = s.ranges[k].lo; ranges[k * 2 + 1] = s.ranges[k].hi; }
476 -: 311: ranges[6] = fault_addr; ranges[7] = fault_bytes; ranges[8] = off;
477 -: 312: if (omega_hplan(&nr, 4, ranges, 16, nearest, 16) != OMEGA_OK || nr != 1) abort();
478 -: 313: near = (int)nearest[0]; dist = nearest[1]; past = (int)nearest[2];
479 -: 314: remaining = nearest[3];
480 -: 315:#endif
481 1*: 316: printf(" address: 0x%llx (%u bytes), %llu bytes %s buffer %s = [0x%llx, 0x%llx) (%zu bytes)\n",
482branch 0 taken 1 (fallthrough)
483branch 1 taken 0
484 -: 317: (unsigned long long)fault_addr, fault_bytes, (unsigned long long)dist, past ? "past the end of" : "before the start of",
485 1: 318: names[near], (unsigned long long)s.ranges[near].lo, (unsigned long long)s.ranges[near].hi, bytes);
486call 0 returned 1
487 -: 319: }
488 1: 320: printf(" simulator: %s\n", s.msg);
489call 0 returned 1
490 1: 321: if (s.err == WS_E_MEM)
491branch 0 taken 1 (fallthrough)
492branch 1 taken 0
493 1: 322: printf(" next: launch %u was given n = %u, and buffer %s holds %llu elements from this launch's first element.\n"
494call 0 returned 1
495 -: 323: " Fix the launch or the buffer, run hello_gpu --sim until it passes, then run hello_gpu on the GPU.\n",
496 -: 324: li, n, names[near], (unsigned long long)remaining);
497 -: 325: else
498 #####: 326: printf(" next: the instruction above is where the program stopped; fix it and run hello_gpu --sim again.\n");
499call 0 never executed
500 1: 327: free(fa); free(fb); free(wc);
501 1: 328: return 1;
502 -: 329: }
503 23: 330: read_b += plan[4];
504 23: 331: wrote_b += plan[5];
505 23: 332: flops += n;
506 -: 333: }
507 2: 334: int rc = check(fa, fb, wc);
508call 0 returned 2
509 2: 335: print_work(read_b, wrote_b, flops, 0);
510call 0 returned 2
511 2: 336: print_sha("output", wc, bytes);
512call 0 returned 2
513 2: 337: printf("%s\n", rc ? "FAIL" : "PASS");
514branch 0 taken 1 (fallthrough)
515branch 1 taken 1
516call 2 returned 2
517 2: 338: free(fa); free(fb); free(wc);
518 2: 339: return rc;
519 -: 340:}
520 -: 341:
521function main called 3 returned 100% blocks executed 27%
522 3: 342:int main(int argc, char **argv) {
523 3: 343: int sim = 0, bug = 0;
524 8: 344: for (int i = 1; i < argc; i++) {
525branch 0 taken 5
526branch 1 taken 3 (fallthrough)
527 5: 345: if (!strcmp(argv[i], "--sim")) sim = 1;
528branch 0 taken 3 (fallthrough)
529branch 1 taken 2
530 2*: 346: else if (!strcmp(argv[i], "--target=sm_86")) target_sm = 86;
531branch 0 taken 0 (fallthrough)
532branch 1 taken 2
533 2*: 347: else if (!strcmp(argv[i], "--target=sm_121")) target_sm = 121;
534branch 0 taken 0 (fallthrough)
535branch 1 taken 2
536 2: 348: else if (!strcmp(argv[i], "--bug=past-end")) bug = 1;
537branch 0 taken 1 (fallthrough)
538branch 1 taken 1
539 -: 349:#if CUDAPP_MIGRATION_PENDING_HELLO_CONTROL
540 1: 350: else if (!strcmp(argv[i], "--bug=short")) bug = 2;
541branch 0 taken 1 (fallthrough)
542branch 1 taken 0
543 #####: 351: else { printf("usage: hello_gpu [--sim] [--bug=short] | hello_gpu --sim --bug=past-end\n"); return 2; }
544call 0 never executed
545 -: 352:#else
546 -: 353: else { printf("usage: hello_gpu [--target=sm_86|--target=sm_121] [--sim [--bug=past-end]]\n"); return 2; }
547 -: 354:#endif
548 -: 355: }
549 3*: 356: if (bug == 1 && !sim) { printf("--bug=past-end is only for --sim: on the GPU it would write past a buffer\n"); return 2; }
550branch 0 taken 1 (fallthrough)
551branch 1 taken 2
552branch 2 taken 0 (fallthrough)
553branch 3 taken 1
554call 4 never executed
555 3: 357: int rc = 1;
556 3: 358: OmegaGpuMem code = { 0 }, a = { 0 }, b = { 0 }, c = { 0 };
557 -: 359: static BlackwellIRProgram prog;
558 -: 360:
559 3*: 361: printf("hello_gpu: c = a + b over %u floats %s, built with Omega's own kernel builder\n", N_TOTAL,
560branch 0 taken 3 (fallthrough)
561branch 1 taken 0
562call 2 returned 3
563 -: 362: sim ? "on the host simulator (no GPU)" : "on the GPU");
564 3: 363: double t0 = now_us();
565call 0 returned 3
566 3*: 364: if (build_vecadd(&prog) != 0) { printf("FAIL: the kernel did not build\n"); return 1; }
567call 0 returned 3
568branch 1 taken 0 (fallthrough)
569branch 2 taken 3
570call 3 never executed
571 -: 365: { /* the machine code, encoded once here so its hash and build time can be printed (D10) */
572 3: 366: size_t need = omega_bw_program_code_bytes(&prog);
573call 0 returned 3
574 3: 367: void *words = calloc(1, need);
575 3: 368: size_t written = 0;
576 3*: 369: if (!words || omega_gpu_encode_program_target(&prog, target_sm, words, need, &written) != 0) { printf("FAIL: the kernel did not encode\n"); free(words); return 1; }
577branch 0 taken 3 (fallthrough)
578branch 1 taken 0
579call 2 returned 3
580branch 3 taken 0 (fallthrough)
581branch 4 taken 3
582call 5 never executed
583 3: 370: double t1 = now_us();
584call 0 returned 3
585 3: 371: printf("kernel built in %.0f us: %zu instructions, %u registers, sm_%u, no compiler run, no cache\n", t1 - t0, written / 16u, prog.regalloc.peak_gpr_usage, target_sm);
586call 0 returned 3
587 3: 372: print_sha("machine code", words, written);
588call 0 returned 3
589 3: 373: free(words);
590 -: 374: }
591 3: 375: if (sim) return run_sim(&prog, bug);
592branch 0 taken 3
593branch 1 taken 0
594call 2 returned 3
595 -: 376:
596 -: 377: /* ---- Step 2: open the GPU. One process owns one device channel; the lock serialises threads. */
597 #####: 378: omega_gpu_session_lock();
598call 0 never executed
599 #####: 379: if (!omega_gpu_session_open()) {
600call 0 never executed
601branch 1 never executed (fallthrough)
602branch 2 never executed
603 #####: 380: printf("GPU not available (stage: %s)\nNOT_RUN\n", omega_gpu_session_last_error());
604call 0 never executed
605call 1 never executed
606 #####: 381: omega_gpu_session_unlock();
607call 0 never executed
608 #####: 382: return 2;
609 -: 383: }
610 #####: 384: if (omega_gpu_session_target_sm() != target_sm) {
611call 0 never executed
612branch 1 never executed (fallthrough)
613branch 2 never executed
614 #####: 385: printf("NOT_RUN: requested sm_%u does not match opened device sm_%u\n", target_sm, omega_gpu_session_target_sm());
615call 0 never executed
616call 1 never executed
617 #####: 386: rc = 2; goto done;
618 -: 387: }
619 -: 388:
620 -: 389: /* ---- Step 3: upload. The code buffer is written by the CPU and executed by the GPU. */
621 #####: 390: size_t bytes = (size_t)N_TOTAL * sizeof(float), need = omega_bw_program_code_bytes(&prog);
622call 0 never executed
623 #####: 391: size_t written = 0;
624 #####: 392: if (omega_gpu_session_alloc_code(need, &code) != 0 || omega_gpu_encode_program_target(&prog, target_sm, code.cpu, need, &written) != 0 ||
625call 0 never executed
626branch 1 never executed (fallthrough)
627branch 2 never executed
628call 3 never executed
629branch 4 never executed (fallthrough)
630branch 5 never executed
631branch 6 never executed (fallthrough)
632branch 7 never executed
633 #####: 393: omega_gpu_session_alloc(bytes, &a) != 0 || omega_gpu_session_alloc(bytes, &b) != 0 || omega_gpu_session_alloc(bytes, &c) != 0) {
634call 0 never executed
635call 1 never executed
636branch 2 never executed (fallthrough)
637branch 3 never executed
638call 4 never executed
639branch 5 never executed (fallthrough)
640branch 6 never executed
641 #####: 394: printf("FAIL: could not set up buffers (stage: %s)\n", omega_gpu_session_last_error());
642call 0 never executed
643call 1 never executed
644 #####: 395: goto done;
645 -: 396: }
646 #####: 397: printf("kernel: %zu instructions, %u registers\n", written / 16u, prog.regalloc.peak_gpr_usage);
647call 0 never executed
648 #####: 398: float *fa = a.cpu, *fb = b.cpu;
649 #####: 399: uint32_t *wc = c.cpu;
650 #####: 400: inputs(fa, fb, wc);
651call 0 never executed
652 #####: 401: if (omega_gpu_session_sync_to_gpu(&code) || omega_gpu_session_sync_to_gpu(&a) || omega_gpu_session_sync_to_gpu(&b) ||
653call 0 never executed
654branch 1 never executed (fallthrough)
655branch 2 never executed
656call 3 never executed
657branch 4 never executed (fallthrough)
658branch 5 never executed
659call 6 never executed
660branch 7 never executed (fallthrough)
661branch 8 never executed
662branch 9 never executed (fallthrough)
663branch 10 never executed
664 #####: 402: omega_gpu_session_sync_to_gpu(&c)) { printf("FAIL: sync to GPU (stage: %s)\n", omega_gpu_session_last_error()); goto done; }
665call 0 never executed
666call 1 never executed
667call 2 never executed
668 -: 403:
669 -: 404: /* ---- Step 4: launch. The argument words land in the kernel's constant bank: three addresses (low word,
670 -: 405: * high word) and n. Each launch is pointed at its own slice of the arrays. */
671 #####: 406: uint32_t declared = omega_bw_gpr_declared(prog.regalloc.peak_gpr_usage);
672call 0 never executed
673 #####: 407: uint64_t gpu_ns = 0;
674 #####: 408: for (uint32_t base = 0; base < N_TOTAL; base += CHUNK) {
675branch 0 never executed
676branch 1 never executed (fallthrough)
677 #####: 409: uint64_t plan[16]; chunk_plan(base, bug, plan);
678call 0 never executed
679 #####: 410: uint64_t off = plan[0];
680 #####: 411: uint32_t n = (uint32_t)plan[1];
681 #####: 412: uint32_t args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS] = { (uint32_t)(a.va + off), (uint32_t)((a.va + off) >> 32),
682 #####: 413: (uint32_t)(b.va + off), (uint32_t)((b.va + off) >> 32), (uint32_t)(c.va + off), (uint32_t)((c.va + off) >> 32), n };
683 #####: 414: OmegaGpuLaunch L = { .code_va = code.va, .gpr_count = declared < 64 ? 64 : declared, .threads_x = THREADS, .threads_y = 1,
684 #####: 415: .grid_x = (uint32_t)plan[2], .grid_y = 1, .num_elements = n,
685 -: 416: .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = 10000, .target_sm = target_sm };
686 #####: 417: uint64_t el = 0;
687 #####: 418: if (omega_gpu_session_launch(&L, &el, NULL) != 0) { printf("FAIL: launch at element %u (stage: %s)\n", base, omega_gpu_session_last_error()); goto done; }
688call 0 never executed
689branch 1 never executed (fallthrough)
690branch 2 never executed
691call 3 never executed
692call 4 never executed
693 #####: 419: gpu_ns += el;
694 -: 420: }
695 #####: 421: if (omega_gpu_session_sync_to_cpu(&c) != 0) { printf("FAIL: sync to CPU (stage: %s)\n", omega_gpu_session_last_error()); goto done; }
696call 0 never executed
697branch 1 never executed (fallthrough)
698branch 2 never executed
699call 3 never executed
700call 4 never executed
701 -: 422:
702 -: 423: /* ---- Step 5: check every element. Same IEEE single-precision add on both sides, so the bits must match. */
703 #####: 424: rc = check(fa, fb, (const uint32_t *)wc);
704call 0 never executed
705 -: 425: /* vecadd reads a[i] and b[i] and writes c[i]: 8 bytes in, 4 out and one add per element */
706 #####: 426: print_work(8ull * N_TOTAL, 4ull * N_TOTAL, N_TOTAL, (double)gpu_ns / 1e3);
707call 0 never executed
708 #####: 427: print_sha("output", wc, bytes);
709call 0 never executed
710 #####: 428: printf("%s\n", rc ? "FAIL" : "PASS");
711branch 0 never executed (fallthrough)
712branch 1 never executed
713call 2 never executed
714 -: 429:
715 #####: 430:done:
716 #####: 431: if (code.cpu) omega_gpu_session_free(&code);
717branch 0 never executed (fallthrough)
718branch 1 never executed
719call 2 never executed
720 #####: 432: if (a.cpu) omega_gpu_session_free(&a);
721branch 0 never executed (fallthrough)
722branch 1 never executed
723call 2 never executed
724 #####: 433: if (b.cpu) omega_gpu_session_free(&b);
725branch 0 never executed (fallthrough)
726branch 1 never executed
727call 2 never executed
728 #####: 434: if (c.cpu) omega_gpu_session_free(&c);
729branch 0 never executed (fallthrough)
730branch 1 never executed
731call 2 never executed
732 #####: 435: omega_gpu_session_unlock();
733call 0 never executed
734 #####: 436: omega_gpu_session_close();
735call 0 never executed
736 #####: 437: return rc;
737 -: 438:}