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 / m16 / m16_concurrent.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_fixture_rest.h" |
| 3 | /* |
| 4 | * m16_concurrent.c -- M16 concurrent multi-channel qualification test. |
| 5 | * |
| 6 | * Sequence: |
| 7 | * 1. Initialize two independent M16NativeContext instances (ctxA, ctxB). |
| 8 | * 2. Allocate separate channels, GPFIFO rings, USERD, and tokens. |
| 9 | * 3. Allocate independent GPU-uncached marker pages (nvrm_alloc_gpu_uncached, |
| 10 | * nvrm/nvrm.c:473): markerA, markerB. Each page also holds marker2 at +0x10. |
| 11 | * 4. Submit independent work: |
| 12 | * Channel A -> 0x16000001, L2 flush, then 0x46464646 at +0x10 |
| 13 | * Channel B -> 0x16000002, L2 flush, then 0x46464646 at +0x10 |
| 14 | * 5. Verify independent causality: |
| 15 | * Both withheld -> markerA=0, markerB=0 |
| 16 | * Ring A -> markerA=0x16000001, markerB=0 |
| 17 | * Ring B -> markerB=0x16000002 |
| 18 | * 6. Prove non-collision and dynamic resource independence. |
| 19 | * |
| 20 | * Waits use the canonical primitive m16/m16_gpu_wait.c: monotonic clock, hard |
| 21 | * 2000 ms deadline that is never extended, 2000 ms stall deadline (equal, so the |
| 22 | * old 2 s pass window is unchanged) that restarts only on progress, barrier before every read, PASS needs marker AND marker2. |
| 23 | * Every failure prints one line tagged |
| 24 | * class=<submission_fail|no_progress|late_progress|wrong_marker|output_parity> |
| 25 | * followed by the full wait report. |
| 26 | * |
| 27 | * Exit codes (unchanged unless marked NEW): |
| 28 | * 1 init failure or enqueue failure (class submission_fail) |
| 29 | * 2 premature execution on withheld doorbells (wrong_marker) |
| 30 | * 3 markerA wait failed (no_progress / late_progress / wrong_marker) |
| 31 | * 4 cross-talk: B executed on A's doorbell (wrong_marker) |
| 32 | * 5 markerB wait failed (no_progress / late_progress / wrong_marker) |
| 33 | * 6 root handle collision |
| 34 | * 7 NEW: output_parity. Either a marker re-read right after its PASS disagrees |
| 35 | * with the value just accepted (nearly a tautology, it only catches a torn |
| 36 | * or unstable word), or, new tightening, the final check that both pages still |
| 37 | * hold their payload and 0x46464646 after B completed fails. |
| 38 | * This test has no output buffer; the markers are the output. |
| 39 | * |
| 40 | * The doorbell is a plain store (nvrm_ring, nvrm/nvrm.c:742, returns void), so a |
| 41 | * doorbell write that fails cannot be detected; one that never reaches the chip |
| 42 | * shows up as no_progress. |
| 43 | */ |
| 44 | #include "m16_native.h" |
| 45 | #include "cpu_barrier.h" |
| 46 | #include "m16_gpu_wait.h" |
| 47 | /* The withheld, parity, cross-talk, final-page and root-collision decisions and |
| 48 | * their exit codes come from om/engine/eng.om (op CONC, src/omega_eng_gates.h); |
| 49 | * the C oracle is behind CUDAPP_MIGRATION_PENDING_C_CONCURRENT. */ |
| 50 | #if defined(CUDAPP_MIGRATION_PENDING_C_CONCURRENT) |
| 51 | #define ENG_C_PART 1 |
| 52 | #endif |
| 53 | #include "omega_eng_gates.h" |
| 54 | #include <stdio.h> |
| 55 | #include <stdlib.h> |
| 56 | #include <string.h> |
| 57 | #include <unistd.h> |
| 58 | |
| 59 | #define PAYLOAD_A 0x16000001u |
| 60 | #define PAYLOAD_B 0x16000002u |
| 61 | #define MARKER2_PAYLOAD 0x46464646u |
| 62 | #define MARKER2_OFF 0x10u |
| 63 | #define WAIT_TOTAL_MS 2000u /* hard deadline: same 2 s window as the old wait */ |
| 64 | #define WAIT_STALL_MS 2000u /* >= hard bound so the pass window is not narrower than the old wait */ |
| 65 | |
| 66 | static void fence(void) { nvrm_cpu_barrier(); } |
| 67 | |
| 68 | /* Marker stream: bind class, WFI release of payload at marker_va, L2_FLUSH_DIRTY |
| 69 | * mem-op (NVC96F_MEM_OP_A..D = 0x28..0x34, D bits 31:27 = 0x10), then a second WFI |
| 70 | * release of 0x46464646 at marker_va + 0x10. Same block as omega's |
| 71 | * src/omega_numeric_gb10.c (the C3 hardening block, "L2_FLUSH_DIRTY" comment |
| 72 | * above the second 0x005c packet); nvrm_mthd is nvrm/nvrm.h:112. */ |
| 73 | static int build_release_flushed(uint32_t *w, uint64_t marker_va, uint32_t payload, uint32_t cls) { |
| 74 | int n = m16_native_build_release(w, marker_va, payload, cls); |
| 75 | if (n < 0) return -1; |
| 76 | /* The loop only transports words computed by the seeded unit. */ |
| 77 | for (uint64_t i = 0; i < 11; ++i) |
| 78 | w[n++] = omega_fixture_flush(i, marker_va); |
| 79 | return n; |
| 80 | } |
| 81 | |
| 82 | static void print_report(const char *cls, const char *stage, int code, const m16_gpu_wait_report_t *r) { |
| 83 | fprintf(stderr, |
| 84 | "FAIL class=%s stage=%s exit=%d kind=%s result=%s expected=0x%08x last_marker=0x%08x " |
| 85 | "last_marker2=0x%08x elapsed_ms=%llu last_progress_ms=%llu " |
| 86 | "progress_count=%llu polls=%llu\n", |
| 87 | cls, stage, code, m16_gpu_wait_kind_name(r->wait_kind), m16_gpu_wait_result_name(r->result), |
| 88 | r->expected, r->last_observed, r->last_marker2, |
| 89 | (unsigned long long)omega_fixture_calc(FR_NS_MS, r->elapsed_ns, 0, 0), |
| 90 | (unsigned long long)omega_fixture_calc(FR_NS_MS, |
| 91 | omega_fixture_calc(FR_SUB, r->last_progress_ns, r->start_ns, 0), 0, 0), |
| 92 | (unsigned long long)r->progress_count, (unsigned long long)r->polls); |
| 93 | } |
| 94 | |
| 95 | static void print_simple(const char *cls, const char *stage, int code, const char *what) { |
| 96 | fprintf(stderr, "FAIL class=%s stage=%s exit=%d %s\n", cls, stage, code, what); |
| 97 | } |
| 98 | |
| 99 | /* Wait marker==want and marker2==MARKER2_PAYLOAD; on failure print the tagged |
| 100 | * line and return the class's exit code, on PASS re-read both (parity). */ |
| 101 | static int wait_marker(volatile uint32_t *marker, uint32_t want, const char *stage, int fail_exit) { |
| 102 | volatile uint32_t *marker2 = (volatile uint32_t *)((volatile uint8_t *)marker + MARKER2_OFF); |
| 103 | m16_gpu_wait_cfg_t cfg; |
| 104 | m16_gpu_wait_report_t rpt; |
| 105 | memset(&cfg, 0, sizeof cfg); |
| 106 | cfg.total_timeout_ms = WAIT_TOTAL_MS; |
| 107 | cfg.progress_timeout_ms = WAIT_STALL_MS; |
| 108 | cfg.marker2 = marker2; |
| 109 | cfg.marker2_want = MARKER2_PAYLOAD; |
| 110 | if (!m16_gpu_wait_fixed(marker, want, &rpt, &cfg)) { |
| 111 | print_report(m16_gpu_wait_failure_class(&rpt, MARKER2_PAYLOAD), stage, fail_exit, &rpt); |
| 112 | return fail_exit; |
| 113 | } |
| 114 | fence(); |
| 115 | uint32_t m = *marker, m2 = *marker2; |
| 116 | int ex = 7; |
| 117 | if (!omega_gate_conc(OMEGA_CONC_PARITY, m, want, m2, 0u, &ex)) { |
| 118 | char what[160]; |
| 119 | snprintf(what, sizeof what, "reread_marker=0x%08x want=0x%08x reread_marker2=0x%08x want2=0x%08x", |
| 120 | m, want, m2, MARKER2_PAYLOAD); |
| 121 | print_simple("output_parity", stage, ex, what); |
| 122 | return ex; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int main(void) { |
| 128 | printf("=== M16 CONCURRENT MULTI-CHANNEL QUALIFICATION ===\n"); |
| 129 | |
| 130 | M16NativeContext ctxA, ctxB; |
| 131 | |
| 132 | printf("\n--- INITIALIZING CONTEXT A ---\n"); |
| 133 | if (m16_native_open(&ctxA) != 0 || m16_native_create_channel(&ctxA) != 0) { |
| 134 | fprintf(stderr, "FATAL: Failed to initialize Context A: %s\n", ctxA.rm.err); |
| 135 | return 1; |
| 136 | } |
| 137 | printf("[OBSERVED] Context A: token=0x%08x gpfifo=0x%08x fifo_va=0x%lx\n", |
| 138 | ctxA.rm.token, ctxA.rm.gpfifo, (unsigned long)ctxA.rm.fifo.va); |
| 139 | |
| 140 | printf("\n--- INITIALIZING CONTEXT B ---\n"); |
| 141 | if (m16_native_open(&ctxB) != 0 || m16_native_create_channel(&ctxB) != 0) { |
| 142 | fprintf(stderr, "FATAL: Failed to initialize Context B: %s\n", ctxB.rm.err); |
| 143 | m16_native_close(&ctxA); |
| 144 | return 1; |
| 145 | } |
| 146 | printf("[OBSERVED] Context B: token=0x%08x gpfifo=0x%08x fifo_va=0x%lx\n", |
| 147 | ctxB.rm.token, ctxB.rm.gpfifo, (unsigned long)ctxB.rm.fifo.va); |
| 148 | |
| 149 | /* Allocate markers: GPU-uncached, so a GPU store reaches memory instead of |
| 150 | * sitting in GPU L2 (was nvrm_alloc via m16_native_alloc_memory: cacheable). */ |
| 151 | NvrmMem memA, memB; |
| 152 | if (nvrm_alloc_gpu_uncached(&ctxA.rm, 0x1000, &memA) != 0 || |
| 153 | nvrm_alloc_gpu_uncached(&ctxB.rm, 0x1000, &memB) != 0) { |
| 154 | fprintf(stderr, "FATAL: Failed to allocate marker memories\n"); |
| 155 | m16_native_close(&ctxA); |
| 156 | m16_native_close(&ctxB); |
| 157 | return 1; |
| 158 | } |
| 159 | uint64_t vaA = memA.va, vaB = memB.va; |
| 160 | volatile uint32_t *markerA = (volatile uint32_t *)memA.cpu; |
| 161 | volatile uint32_t *markerB = (volatile uint32_t *)memB.cpu; |
| 162 | volatile uint32_t *marker2A = (volatile uint32_t *)((volatile uint8_t *)memA.cpu + MARKER2_OFF); |
| 163 | volatile uint32_t *marker2B = (volatile uint32_t *)((volatile uint8_t *)memB.cpu + MARKER2_OFF); |
| 164 | *markerA = 0; |
| 165 | *markerB = 0; |
| 166 | *marker2A = 0; |
| 167 | *marker2B = 0; |
| 168 | fence(); |
| 169 | |
| 170 | printf("[OBSERVED] Marker A: VA=0x%lx CPU=%p (initial=0, GPU-uncached)\n", (unsigned long)vaA, memA.cpu); |
| 171 | printf("[OBSERVED] Marker B: VA=0x%lx CPU=%p (initial=0, GPU-uncached)\n", (unsigned long)vaB, memB.cpu); |
| 172 | |
| 173 | /* Build methods */ |
| 174 | uint32_t mthdA[32], mthdB[32]; |
| 175 | int nA = build_release_flushed(mthdA, vaA, PAYLOAD_A, ctxA.rm.compute_class); |
| 176 | int nB = build_release_flushed(mthdB, vaB, PAYLOAD_B, ctxB.rm.compute_class); |
| 177 | |
| 178 | /* Enqueue to both without ringing */ |
| 179 | printf("\n--- ENQUEUING WORK WITHOUT DOORBELLS ---\n"); |
| 180 | if (nA < 0 || nB < 0 || |
| 181 | m16_native_enqueue_methods(&ctxA, mthdA, (size_t)nA) != 0 || |
| 182 | m16_native_enqueue_methods(&ctxB, mthdB, (size_t)nB) != 0) { |
| 183 | print_simple("submission_fail", "enqueue", 1, "enqueue of method stream failed"); |
| 184 | m16_native_close(&ctxA); |
| 185 | m16_native_close(&ctxB); |
| 186 | return 1; |
| 187 | } |
| 188 | usleep(50000); |
| 189 | fence(); |
| 190 | printf("[OBSERVED] Withheld doorbells: markerA=0x%08x markerB=0x%08x\n", *markerA, *markerB); |
| 191 | int ex = 2; |
| 192 | if (!omega_gate_conc(OMEGA_CONC_WITHHELD, *markerA, *markerB, *marker2A, *marker2B, &ex)) { |
| 193 | char what[160]; |
| 194 | snprintf(what, sizeof what, "premature markerA=0x%08x markerB=0x%08x marker2A=0x%08x marker2B=0x%08x", |
| 195 | *markerA, *markerB, *marker2A, *marker2B); |
| 196 | print_simple("wrong_marker", "withheld", ex, what); |
| 197 | m16_native_close(&ctxA); |
| 198 | m16_native_close(&ctxB); |
| 199 | return ex; |
| 200 | } |
| 201 | |
| 202 | /* Ring Doorbell A only */ |
| 203 | printf("\n--- RINGING DOORBELL A ONLY ---\n"); |
| 204 | m16_native_ring_doorbell(&ctxA); |
| 205 | int rc = wait_marker(markerA, PAYLOAD_A, "A", 3); |
| 206 | if (rc != 0) { |
| 207 | m16_native_close(&ctxA); |
| 208 | m16_native_close(&ctxB); |
| 209 | return rc; |
| 210 | } |
| 211 | printf("[OBSERVED] After Doorbell A: markerA=0x%08x markerB=0x%08x (expected A=0x16000001, B=0x00000000)\n", |
| 212 | *markerA, *markerB); |
| 213 | fence(); |
| 214 | if (!omega_gate_conc(OMEGA_CONC_CROSSTALK, *markerB, *marker2B, 0u, 0u, &ex)) { |
| 215 | char what[120]; |
| 216 | snprintf(what, sizeof what, "cross-talk markerB=0x%08x marker2B=0x%08x with only doorbell A rung", |
| 217 | *markerB, *marker2B); |
| 218 | print_simple("wrong_marker", "crosstalk", ex, what); |
| 219 | m16_native_close(&ctxA); |
| 220 | m16_native_close(&ctxB); |
| 221 | return ex; |
| 222 | } |
| 223 | |
| 224 | /* Ring Doorbell B */ |
| 225 | printf("\n--- RINGING DOORBELL B ---\n"); |
| 226 | m16_native_ring_doorbell(&ctxB); |
| 227 | rc = wait_marker(markerB, PAYLOAD_B, "B", 5); |
| 228 | if (rc != 0) { |
| 229 | m16_native_close(&ctxA); |
| 230 | m16_native_close(&ctxB); |
| 231 | return rc; |
| 232 | } |
| 233 | printf("[OBSERVED] After Doorbell B: markerA=0x%08x markerB=0x%08x (expected A=0x16000001, B=0x16000002)\n", |
| 234 | *markerA, *markerB); |
| 235 | /* output_parity, final: B's work must not have disturbed A's page. The old log |
| 236 | * printed this as "expected" but never enforced it (NEW tightening). */ |
| 237 | fence(); |
| 238 | if (!omega_gate_conc(OMEGA_CONC_FINAL, *markerA, *marker2A, *markerB, *marker2B, &ex)) { |
| 239 | char what[200]; |
| 240 | snprintf(what, sizeof what, "final markerA=0x%08x marker2A=0x%08x markerB=0x%08x marker2B=0x%08x", |
| 241 | *markerA, *marker2A, *markerB, *marker2B); |
| 242 | print_simple("output_parity", "final", ex, what); |
| 243 | m16_native_close(&ctxA); |
| 244 | m16_native_close(&ctxB); |
| 245 | return ex; |
| 246 | } |
| 247 | |
| 248 | /* Verify distinct resource handles */ |
| 249 | printf("\n--- VERIFYING RESOURCE INDEPENDENCE ---\n"); |
| 250 | printf("[OBSERVED] Handle check: Root A=0x%x B=0x%x, Channel A=0x%x B=0x%x, Token A=0x%x B=0x%x\n", |
| 251 | ctxA.rm.root, ctxB.rm.root, ctxA.rm.gpfifo, ctxB.rm.gpfifo, ctxA.rm.token, ctxB.rm.token); |
| 252 | |
| 253 | if (!omega_gate_conc(OMEGA_CONC_ROOTS, ctxA.rm.root, ctxB.rm.root, 0u, 0u, &ex)) { |
| 254 | fprintf(stderr, "FATAL: Root handles collided between sessions!\n"); |
| 255 | m16_native_close(&ctxA); |
| 256 | m16_native_close(&ctxB); |
| 257 | return ex; |
| 258 | } |
| 259 | |
| 260 | printf("CONCURRENT_CHANNEL_PASS: 2 independent channels executed without collision.\n"); |
| 261 | |
| 262 | nvrm_retire(&ctxA.rm, ctxA.rm.put); |
| 263 | nvrm_retire(&ctxB.rm, ctxB.rm.put); |
| 264 | m16_native_close(&ctxA); |
| 265 | m16_native_close(&ctxB); |
| 266 | return 0; |
| 267 | } |