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 / src / omega_blackwell_submit.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_fixture_rest.h" |
| 3 | #include <math.h> |
| 4 | #include "omega_blackwell_submit.h" |
| 5 | #include "omega_blackwell_qmd.h" |
| 6 | #include "omega_blackwell_encoder.h" |
| 7 | #include "omega_blackwell_engine.h" |
| 8 | #include "omega_gpu_engine.h" |
| 9 | #include "m16_native.h" |
| 10 | #include "omega_gpu_wait.h" |
| 11 | #include "cpu_barrier.h" |
| 12 | #include <errno.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <sys/wait.h> |
| 18 | #include <time.h> |
| 19 | #include <unistd.h> |
| 20 | #include "omega_gpu_code_alloc.h" |
| 21 | /* Argument envelope, launch shapes, page rounding, precision and the job |
| 22 | * timeouts come from om/engine/eng.om (src/omega_eng_gates.h); the C oracle is |
| 23 | * kept behind CUDAPP_MIGRATION_PENDING_C_SUBMIT. */ |
| 24 | #if defined(CUDAPP_MIGRATION_PENDING_C_SUBMIT) |
| 25 | #define ENG_C_PART 1 |
| 26 | #endif |
| 27 | #include "omega_eng_gates.h" |
| 28 | _Static_assert(OMEGA_BW_MATMUL_MAX_M == OMEGA_GATE_MM_MAX_M && OMEGA_BW_MATMUL_MAX_K == OMEGA_GATE_MM_MAX_K && |
| 29 | OMEGA_BW_MATMUL_MAX_N == OMEGA_GATE_MM_MAX_N, |
| 30 | "eng.om subm k0 limits must equal the OMEGA_BW_MATMUL_MAX_* envelope"); |
| 31 | |
| 32 | /* Second completion marker payload for the C3 tail (marker page + 0x10), the same value as |
| 33 | * src/omega_blackwell_engine.c BE_MARKER2_PAYLOAD and src/omega_numeric_divsqrt_gb10.c:1152. */ |
| 34 | |
| 35 | static const uint32_t SETUP_WORDS[18] = { |
| 36 | 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, |
| 37 | 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, |
| 38 | 0x20012559, 0x00000000, |
| 39 | }; |
| 40 | |
| 41 | static uint64_t current_time_ns(void) { |
| 42 | struct timespec t; |
| 43 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 44 | return omega_fixture_calc(FR_NS, (uint64_t)t.tv_sec, (uint64_t)t.tv_nsec, 0); |
| 45 | } |
| 46 | |
| 47 | /* Production entry points run on AArch64. The host simulator defines |
| 48 | * OMEGA_BLACKWELL_HOST_SIMULATOR and keeps the argument checks below. */ |
| 49 | static int bw_refuse_non_aarch64(void) |
| 50 | { |
| 51 | #if !defined(__aarch64__) && !defined(OMEGA_BLACKWELL_HOST_SIMULATOR) |
| 52 | errno = ENOTSUP; |
| 53 | return -1; |
| 54 | #else |
| 55 | return 0; |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | int omega_blackwell_execute_vector(const OmegaVectorSpec *spec, |
| 60 | const OmegaBlackwellRealization *real, |
| 61 | const uint32_t *h_a, |
| 62 | const uint32_t *h_b, |
| 63 | uint32_t *h_c_out, |
| 64 | OmegaBlackwellVectorExecution *exec_info) { |
| 65 | if (bw_refuse_non_aarch64() != 0) return -1; |
| 66 | if (!spec || !real || !h_a || !h_b || !h_c_out) return -1; |
| 67 | uint32_t n = spec->element_count; |
| 68 | if (n == 0) return -1; |
| 69 | |
| 70 | if (exec_info) { |
| 71 | memset(exec_info, 0, sizeof(*exec_info)); |
| 72 | strncpy(exec_info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)", sizeof(exec_info->target_chip) - 1); |
| 73 | exec_info->sm_architecture = real->sm_architecture; |
| 74 | exec_info->element_count = n; |
| 75 | exec_info->zero_libcuda_linkage = (omega_blackwell_verify_zero_libcuda_linkage(NULL) == 0); |
| 76 | exec_info->zero_cuda_symbols = (omega_blackwell_verify_zero_cuda_symbols(NULL) == 0); |
| 77 | exec_info->zero_libcuda_runtime = (omega_blackwell_verify_zero_libcuda_runtime() == 0); |
| 78 | } |
| 79 | /* This executor no longer talks to the device itself. It builds the program |
| 80 | * and the poison words on the host, hands the job to omega_gpu_execute with |
| 81 | * the Blackwell backend (src/omega_blackwell_engine.c), and then does the |
| 82 | * arithmetic check here. The engine only checks that no output word is still |
| 83 | * equal to its poison word; it never judges the math. */ |
| 84 | |
| 85 | /* Program bytes. Capacity 0x1000 as before (the code page was 0x1000). */ |
| 86 | uint8_t code[0x1000]; |
| 87 | size_t encoded_len = 0; |
| 88 | if (omega_blackwell_encode_vecadd(code, sizeof(code), &encoded_len) != 0 || encoded_len == 0) return -1; |
| 89 | |
| 90 | /* Poison words. The expected output word is computed here, on the host, |
| 91 | * before launch: expected[i] = (a[i] + b[i]) mod 2^32 (the same sum |
| 92 | * omega_vector_verify_oracle uses). The poison word is the bit complement of |
| 93 | * it. A word and its bit complement always differ, so a poison word can |
| 94 | * never equal the expected output word at the same position. If the device |
| 95 | * leaves a word unwritten the engine sees poison and reports |
| 96 | * OUTPUT_UNCHANGED. (This replaces the old constant OMEGA_VECTOR_POISON_VALUE, |
| 97 | * which could in principle collide with a legitimate sum.) */ |
| 98 | uint32_t *poison = (uint32_t *)malloc((size_t)n * sizeof(uint32_t)); |
| 99 | if (!poison) return -1; |
| 100 | for (size_t i = 0; i < n; i++) { |
| 101 | poison[i] = ~(uint32_t)(h_a[i] + h_b[i]); |
| 102 | } |
| 103 | |
| 104 | OmegaGpuJob job; |
| 105 | OmegaGpuResult res; |
| 106 | memset(&job, 0, sizeof(job)); |
| 107 | job.program = code; |
| 108 | job.program_len = encoded_len; |
| 109 | job.layout = OMEGA_GPU_LAYOUT_VECTOR_1D; |
| 110 | job.element_count = n; |
| 111 | job.input_a = h_a; |
| 112 | job.input_a_len = (size_t)n * sizeof(uint32_t); |
| 113 | job.input_b = h_b; |
| 114 | job.input_b_len = (size_t)n * sizeof(uint32_t); |
| 115 | job.output = h_c_out; |
| 116 | job.output_len = (size_t)n * sizeof(uint32_t); |
| 117 | job.poison = poison; |
| 118 | job.poison_count = n; |
| 119 | /* 5000 ms is the marker wait this executor always used (the old |
| 120 | * m16_native_wait_marker(..., 5000) call). The same limit is used for the |
| 121 | * second marker and the semaphore, which the old code did not wait for, and |
| 122 | * for the barrier, which has no wait. flags 0 means every protection ON: |
| 123 | * the L2 flush and second marker are now part of this launch. */ |
| 124 | { |
| 125 | uint32_t tm[4]; |
| 126 | if (omega_gate_timeouts(tm) != 0) { |
| 127 | free(poison); |
| 128 | return -1; |
| 129 | } |
| 130 | job.timeouts.marker_ms = tm[0]; |
| 131 | job.timeouts.release_semaphore_ms = tm[1]; |
| 132 | job.timeouts.marker2_ms = tm[2]; |
| 133 | job.timeouts.visibility_ms = tm[3]; |
| 134 | } |
| 135 | job.flags = 0; |
| 136 | |
| 137 | omega_gpu_engine_set_backend(omega_blackwell_engine_backend()); |
| 138 | int erc = omega_gpu_execute(&job, &res); |
| 139 | free(poison); |
| 140 | if (erc != OMEGA_GPU_ENGINE_OK) { |
| 141 | fprintf(stderr, "GB10_VECTOR_FAIL failure=%s step=%s state=%s drv_rc=%d errno=%d text=\"%s\"\n", |
| 142 | omega_gpu_engine_failure_name(res.failure), omega_gpu_engine_step_name(res.failed_step), |
| 143 | omega_gpu_engine_state_name(res.last_state), res.drv_rc, res.err_no, res.drv_text); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | /* The engine returns only pass or fail. The marker, the semaphore and the |
| 148 | * timestamps come from what the backend observed in its own wait callbacks |
| 149 | * (the engine result carries them only on failure). Both values were |
| 150 | * already required by the backend; checked again here because the gates |
| 151 | * depend on them: marker payload 0x44444444, semaphore DONE (6). */ |
| 152 | OmegaBlackwellEngineRunInfo info; |
| 153 | omega_blackwell_engine_run_info(&info); |
| 154 | if (info.marker != OMEGA_BW_MARKER_COMPLETION_PAYLOAD) return -1; |
| 155 | if (info.semaphore != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) return -1; |
| 156 | |
| 157 | /* Verify against OMEGA semantic oracle */ |
| 158 | size_t mismatch_idx = 0; |
| 159 | int parity_res = omega_vector_verify_oracle(h_a, h_b, h_c_out, n, &mismatch_idx); |
| 160 | |
| 161 | if (exec_info) { |
| 162 | exec_info->launch_timestamp_ns = info.launch_ns; |
| 163 | exec_info->completion_timestamp_ns = info.marker_done_ns; |
| 164 | exec_info->elapsed_ns = (info.marker_done_ns > info.launch_ns) ? (info.marker_done_ns - info.launch_ns) : 0; |
| 165 | exec_info->completion_marker = info.marker; |
| 166 | exec_info->intermediate_semaphore = info.semaphore; |
| 167 | exec_info->parity_verified = (parity_res == 0); |
| 168 | } |
| 169 | |
| 170 | return parity_res; |
| 171 | } |
| 172 | |
| 173 | /* CHIPWAIT-2: the two direct launchers wait through the shared primitive |
| 174 | * (src/omega_gpu_wait.c): exact-value wait on the marker with marker2 required |
| 175 | * (marker2 lands only after the L2 flush, so the output is in memory), progress |
| 176 | * stall 5000 ms (the old budget), hard total 600000 ms. One tagged line on failure. |
| 177 | * The engine-routed launcher keeps the engine's own timed waits. */ |
| 178 | static int bw_wait_one(const char *site, const char *what, volatile uint32_t *w, uint32_t want, |
| 179 | volatile uint32_t *m2, uint32_t m2want) { |
| 180 | omega_gpu_wait_cfg_t cfg; |
| 181 | memset(&cfg, 0, sizeof(cfg)); |
| 182 | cfg.progress_timeout_ms = 5000; |
| 183 | cfg.total_timeout_ms = 600000; |
| 184 | cfg.marker2 = m2; |
| 185 | cfg.marker2_want = m2want; |
| 186 | omega_gpu_wait_report_t r; |
| 187 | if (omega_gpu_wait_fixed(w, want, &r, &cfg)) return 0; |
| 188 | fprintf(stderr, |
| 189 | "BW_WAIT_FAIL site=%s wait=%s kind=%s result=%s expected=0x%x observed=0x%x " |
| 190 | "marker2_observed=0x%x elapsed_ns=%llu progress_count=%llu polls=%llu\n", |
| 191 | site, what, omega_gpu_wait_kind_name(r.wait_kind), omega_gpu_wait_result_name(r.result), |
| 192 | r.expected, r.last_observed, r.last_marker2, (unsigned long long)r.elapsed_ns, |
| 193 | (unsigned long long)r.progress_count, (unsigned long long)r.polls); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | int omega_blackwell_execute_matmul(const OmegaMatMulSpec *spec, |
| 198 | const OmegaBlackwellKernel *kernel, |
| 199 | const uint32_t *h_a, |
| 200 | const uint32_t *h_b, |
| 201 | uint32_t *h_c_out, |
| 202 | OmegaBlackwellMatMulExecution *exec_info) { |
| 203 | if (bw_refuse_non_aarch64() != 0) return -1; |
| 204 | { |
| 205 | int ptrs = spec && kernel && h_a && h_b && h_c_out; |
| 206 | int code = kernel && kernel->code && kernel->code_size != 0; |
| 207 | /* CudaAuditFable F3: past the envelope (omega_blackwell_matmul_spec_init's own limits) the size_t products |
| 208 | * below wrap (m = k = 2^31: m * k * 4 is 2^64), a 4 KiB buffer is allocated and then written as the whole |
| 209 | * matrix. At the limits the largest product, k * n * 4, is 2^34 bytes. */ |
| 210 | if (!omega_gate_matmul_args(ptrs, code, spec ? spec->m : 0u, spec ? spec->k : 0u, spec ? spec->n : 0u)) |
| 211 | return -1; |
| 212 | } |
| 213 | const size_t code_bytes = omega_gpu_code_alloc_bytes(kernel->code_size); /* code + 2 KB prefetch tail, page rounded */ |
| 214 | if (code_bytes == 0) return -1; /* refused before the device is opened, so nothing is left open */ |
| 215 | |
| 216 | if (exec_info) { |
| 217 | memset(exec_info, 0, sizeof(*exec_info)); |
| 218 | strncpy(exec_info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)", sizeof(exec_info->target_chip) - 1); |
| 219 | exec_info->sm_architecture = 121; |
| 220 | exec_info->m = spec->m; |
| 221 | exec_info->k = spec->k; |
| 222 | exec_info->n = spec->n; |
| 223 | exec_info->zero_libcuda_linkage = (omega_blackwell_verify_zero_libcuda_linkage(NULL) == 0); |
| 224 | exec_info->zero_cuda_symbols = (omega_blackwell_verify_zero_cuda_symbols(NULL) == 0); |
| 225 | exec_info->zero_libcuda_runtime = (omega_blackwell_verify_zero_libcuda_runtime() == 0); |
| 226 | } |
| 227 | |
| 228 | M16NativeContext ctx; |
| 229 | if (m16_native_open(&ctx) != 0) { return -1; } |
| 230 | if (m16_native_create_channel(&ctx) != 0) { m16_native_close(&ctx); return -1; } |
| 231 | |
| 232 | /* GB10: every buffer the CPU and GPU both touch is GPU-uncached. A plain nvrm_alloc forces |
| 233 | * GPU_CACHEABLE_YES, which nvos.h:1115-1118 says is not coherent with CPU mappings on |
| 234 | * system memory (R11 wrong results under load, hive-phases I37). */ |
| 235 | NvrmMem large_pb; |
| 236 | if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x10000, &large_pb) != 0) { m16_native_close(&ctx); return -1; } |
| 237 | ctx.pb_mem = large_pb; |
| 238 | |
| 239 | /* Sizing buffer allocations */ |
| 240 | uint64_t a_bytes, b_bytes, c_bytes; |
| 241 | if (omega_gate_round_pages((uint64_t)spec->m * spec->k * sizeof(uint32_t), &a_bytes) != 0 || |
| 242 | omega_gate_round_pages((uint64_t)spec->k * spec->n * sizeof(uint32_t), &b_bytes) != 0 || |
| 243 | omega_gate_round_pages((uint64_t)spec->m * spec->n * sizeof(uint32_t), &c_bytes) != 0) { |
| 244 | m16_native_close(&ctx); |
| 245 | return -1; |
| 246 | } |
| 247 | /* code_bytes was sized and checked before the device was opened */ |
| 248 | |
| 249 | NvrmMem code_mem, cbank_mem, a_mem, b_mem, c_mem, marker_mem, qmd_mem; |
| 250 | if (nvrm_alloc_gpu_uncached(&ctx.rm, code_bytes, &code_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 251 | if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &cbank_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 252 | if (nvrm_alloc_gpu_uncached(&ctx.rm, a_bytes, &a_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 253 | if (nvrm_alloc_gpu_uncached(&ctx.rm, b_bytes, &b_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 254 | if (nvrm_alloc_gpu_uncached(&ctx.rm, c_bytes, &c_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 255 | if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &marker_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 256 | if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x10000, &qmd_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 257 | |
| 258 | /* Copy dynamic machine code into code_mem */ |
| 259 | memcpy(code_mem.cpu, kernel->code, kernel->code_size); |
| 260 | memset((uint8_t *)code_mem.cpu + kernel->code_size, 0, code_bytes - kernel->code_size); |
| 261 | |
| 262 | /* Populate input buffers A, B and initialize C with poison */ |
| 263 | memcpy(a_mem.cpu, h_a, (size_t)spec->m * spec->k * sizeof(uint32_t)); |
| 264 | memcpy(b_mem.cpu, h_b, (size_t)spec->k * spec->n * sizeof(uint32_t)); |
| 265 | uint32_t *c_dev = (uint32_t *)c_mem.cpu; |
| 266 | for (size_t i = 0; i < (size_t)spec->m * spec->n; i++) { |
| 267 | c_dev[i] = OMEGA_VECTOR_POISON_VALUE; |
| 268 | } |
| 269 | |
| 270 | /* 2D Tile Configuration: 16x16 threads per block */ |
| 271 | uint32_t shape[4]; |
| 272 | if (omega_gate_matmul_shape(0, spec->m, spec->n, shape) != 0) { m16_native_close(&ctx); return -1; } |
| 273 | uint32_t grid_x = shape[0]; |
| 274 | uint32_t grid_y = shape[1]; |
| 275 | uint32_t threads_x = shape[2]; |
| 276 | uint32_t threads_y = shape[3]; |
| 277 | |
| 278 | uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 279 | omega_blackwell_build_cbank_driver_2d(cbank_data, cbank_mem.va, threads_x, threads_y, grid_x, grid_y); |
| 280 | |
| 281 | uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; |
| 282 | omega_blackwell_build_cbank_args_matmul(cbank_args, a_mem.va, b_mem.va, c_mem.va, spec->m, spec->k, spec->n); |
| 283 | |
| 284 | memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data)); |
| 285 | memcpy((uint8_t *)cbank_mem.cpu + 0x380, cbank_args, sizeof(cbank_args)); |
| 286 | |
| 287 | uint64_t qmd0_va = qmd_mem.va; |
| 288 | uint64_t qmd1_va = qmd_mem.va + 0x1000; |
| 289 | uint64_t sem_va = qmd_mem.va + 0x2000; |
| 290 | uint64_t scratch_va = qmd_mem.va + 0x4000; |
| 291 | |
| 292 | OmegaBlackwellQmdConfig qmd_cfg = { |
| 293 | .code_va = code_mem.va, |
| 294 | .cbank_va = cbank_mem.va, |
| 295 | .scratch_va = scratch_va, |
| 296 | .sem_va = sem_va, |
| 297 | .qmd0_va = qmd0_va, |
| 298 | .qmd1_va = qmd1_va, |
| 299 | .num_elements = spec->m * spec->n, |
| 300 | .threads_per_block = 256, |
| 301 | .grid_width = grid_x * grid_y, |
| 302 | .threads_x = threads_x, |
| 303 | .threads_y = threads_y, |
| 304 | .grid_x = grid_x, |
| 305 | .grid_y = grid_y, |
| 306 | .gpr_count = (kernel->gpr_count > 16) ? ((kernel->gpr_count + 15) & ~15U) : 32 |
| 307 | }; |
| 308 | |
| 309 | uint32_t qmd0_words[OMEGA_BW_QMD_WORDS]; |
| 310 | uint32_t qmd1_words[OMEGA_BW_QMD_WORDS]; |
| 311 | omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); |
| 312 | omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg); |
| 313 | |
| 314 | if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { m16_native_close(&ctx); return -1; } |
| 315 | |
| 316 | memcpy(qmd_mem.cpu, qmd0_words, sizeof(qmd0_words)); |
| 317 | memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1_words, sizeof(qmd1_words)); |
| 318 | |
| 319 | volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)qmd_mem.cpu + 0x2000); |
| 320 | volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu; |
| 321 | volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10); |
| 322 | *hsem = 0; |
| 323 | *hmarker = 0; |
| 324 | *hmarker2 = 0; |
| 325 | nvrm_cpu_barrier(); |
| 326 | |
| 327 | /* Assemble unified pushbuffer */ |
| 328 | uint32_t pb[1024]; |
| 329 | size_t pb_len = 0; |
| 330 | |
| 331 | /* 1. Setup words (18 words) */ |
| 332 | memcpy(&pb[pb_len], SETUP_WORDS, sizeof(SETUP_WORDS)); |
| 333 | pb_len += sizeof(SETUP_WORDS) / 4; |
| 334 | |
| 335 | /* 2. Cbank driver data upload (224 words via DMA) */ |
| 336 | pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); |
| 337 | pb[pb_len++] = (uint32_t)(cbank_mem.va >> 32); |
| 338 | pb[pb_len++] = (uint32_t)cbank_mem.va; |
| 339 | pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); |
| 340 | pb[pb_len++] = 0x00000380; |
| 341 | pb[pb_len++] = 0x00000001; |
| 342 | pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); |
| 343 | pb[pb_len++] = 0x00000041; |
| 344 | pb[pb_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 345 | memcpy(&pb[pb_len], cbank_data, 224 * 4); |
| 346 | pb_len += 224; |
| 347 | |
| 348 | /* 3. Kernel args upload (10 words via DMA = 40 = 0x28 bytes) */ |
| 349 | pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); |
| 350 | pb[pb_len++] = (uint32_t)((cbank_mem.va + 0x380) >> 32); |
| 351 | pb[pb_len++] = (uint32_t)(cbank_mem.va + 0x380); |
| 352 | pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); |
| 353 | pb[pb_len++] = 0x00000028; |
| 354 | pb[pb_len++] = 0x00000001; |
| 355 | pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); |
| 356 | pb[pb_len++] = 0x00000041; |
| 357 | pb[pb_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 358 | memcpy(&pb[pb_len], cbank_args, 10 * 4); |
| 359 | pb_len += 10; |
| 360 | |
| 361 | /* 4. Inline QMD 0 (98 words: 2 addr + 96 QMD words) */ |
| 362 | pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 363 | pb[pb_len++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); |
| 364 | pb[pb_len++] = (uint32_t)(qmd0_va >> 8); |
| 365 | memcpy(&pb[pb_len], qmd0_words, 96 * 4); |
| 366 | pb_len += 96; |
| 367 | |
| 368 | /* 5. Intermediate semaphore upload (1 word = 5 via DMA) */ |
| 369 | pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); |
| 370 | pb[pb_len++] = (uint32_t)(sem_va >> 32); |
| 371 | pb[pb_len++] = (uint32_t)sem_va; |
| 372 | pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); |
| 373 | pb[pb_len++] = 0x00000004; |
| 374 | pb[pb_len++] = 0x00000001; |
| 375 | pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); |
| 376 | pb[pb_len++] = 0x00000041; |
| 377 | pb[pb_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 378 | pb[pb_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; |
| 379 | |
| 380 | /* 6. Inline QMD 1 (98 words: 2 addr + 96 QMD words) */ |
| 381 | pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 382 | pb[pb_len++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); |
| 383 | pb[pb_len++] = (uint32_t)(qmd1_va >> 8); |
| 384 | memcpy(&pb[pb_len], qmd1_words, 96 * 4); |
| 385 | pb_len += 96; |
| 386 | |
| 387 | /* 7. Subchannel 0 completion release (WFI) */ |
| 388 | pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); |
| 389 | pb[pb_len++] = (uint32_t)marker_mem.va; |
| 390 | pb[pb_len++] = (uint32_t)(marker_mem.va >> 32); |
| 391 | pb[pb_len++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; |
| 392 | pb[pb_len++] = 0; |
| 393 | pb[pb_len++] = 0x1 | (1u << 20); // RELEASE | WFI |
| 394 | |
| 395 | /* 8. C3 tail, the same words as src/omega_numeric_gb10.c and |
| 396 | * src/omega_blackwell_engine.c: flush GPU L2 dirty lines (NVC96F_MEM_OP_A..D = |
| 397 | * 0x28..0x34, D bits 31:27 OPERATION = L2_FLUSH_DIRTY 0x10), then a second WFI |
| 398 | * release at marker page + 0x10 that the host waits for before the readback. */ |
| 399 | pb[pb_len++] = nvrm_mthd(0, 0x0028, 4); |
| 400 | pb[pb_len++] = 0; |
| 401 | pb[pb_len++] = 0; |
| 402 | pb[pb_len++] = 0; |
| 403 | pb[pb_len++] = (0x10u << 27); |
| 404 | pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); |
| 405 | pb[pb_len++] = (uint32_t)(marker_mem.va + 0x10); |
| 406 | pb[pb_len++] = (uint32_t)((marker_mem.va + 0x10) >> 32); |
| 407 | pb[pb_len++] = OMEGA_BW_MARKER2_PAYLOAD; |
| 408 | pb[pb_len++] = 0; |
| 409 | pb[pb_len++] = 0x1 | (1u << 20); // RELEASE | WFI |
| 410 | |
| 411 | /* Submit methods to hardware GPFIFO ring */ |
| 412 | uint64_t t_start = current_time_ns(); |
| 413 | if (m16_native_submit_methods(&ctx, pb, pb_len) != 0) { m16_native_close(&ctx); return -1; } |
| 414 | |
| 415 | /* Wait for hardware completion marker */ |
| 416 | /* CHIPWAIT-2: marker with marker2 required (C3: the second release lands only after the L2 flush). */ |
| 417 | if (bw_wait_one(__func__, "marker", hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, hmarker2, OMEGA_BW_MARKER2_PAYLOAD) != 0) { m16_native_close(&ctx); return -1; } |
| 418 | nvrm_cpu_barrier(); |
| 419 | uint64_t t_end = current_time_ns(); |
| 420 | |
| 421 | /* Validate intermediate semaphore updated to 6 */ |
| 422 | if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { m16_native_close(&ctx); return -1; } |
| 423 | |
| 424 | /* Copy device output back to host */ |
| 425 | memcpy(h_c_out, c_mem.cpu, (size_t)spec->m * spec->n * sizeof(uint32_t)); |
| 426 | |
| 427 | /* Verify against OMEGA MatMul mathematical reference oracle */ |
| 428 | uint32_t *h_expected = (uint32_t *)malloc((size_t)spec->m * spec->n * sizeof(uint32_t)); |
| 429 | if (!h_expected) { m16_native_close(&ctx); return -1; } |
| 430 | |
| 431 | omega_matmul_cpu_oracle_i32(h_a, h_b, h_expected, spec->m, spec->k, spec->n); |
| 432 | |
| 433 | size_t mismatches = 0; |
| 434 | for (size_t i = 0; i < (size_t)spec->m * spec->n; i++) { |
| 435 | if (h_c_out[i] != h_expected[i]) { |
| 436 | mismatches++; |
| 437 | } |
| 438 | } |
| 439 | free(h_expected); |
| 440 | |
| 441 | if (exec_info) { |
| 442 | exec_info->launch_timestamp_ns = t_start; |
| 443 | exec_info->completion_timestamp_ns = t_end; |
| 444 | exec_info->elapsed_ns = (t_end > t_start) ? (t_end - t_start) : 0; |
| 445 | exec_info->completion_marker = *hmarker; |
| 446 | exec_info->intermediate_semaphore = *hsem; |
| 447 | exec_info->mismatch_count = mismatches; |
| 448 | exec_info->parity_verified = (mismatches == 0); |
| 449 | } |
| 450 | |
| 451 | m16_native_close(&ctx); |
| 452 | return (mismatches == 0) ? 0 : -1; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | int omega_blackwell_execute_matmul_tensor(const OmegaMatMulSpec *spec, |
| 457 | const OmegaBlackwellKernel *kernel, |
| 458 | const void *h_a, |
| 459 | const void *h_b, |
| 460 | float *h_c_out, |
| 461 | OmegaBlackwellMatMulExecution *exec_info, |
| 462 | float *out_max_abs_err, |
| 463 | float *out_max_rel_err) { |
| 464 | if (bw_refuse_non_aarch64() != 0) return -1; |
| 465 | { |
| 466 | int ptrs = spec && kernel && h_a && h_b && h_c_out; |
| 467 | int code = kernel && kernel->code && kernel->code_size != 0; |
| 468 | /* CudaAuditFable F3: dimensions past the envelope and overflowing code sizes are refused before the open, |
| 469 | * as in omega_blackwell_execute_matmul. */ |
| 470 | if (!omega_gate_matmul_args(ptrs, code, spec ? spec->m : 0u, spec ? spec->k : 0u, spec ? spec->n : 0u)) |
| 471 | return -1; |
| 472 | } |
| 473 | const size_t code_bytes = omega_gpu_code_alloc_bytes(kernel->code_size); /* code + 2 KB prefetch tail, page rounded */ |
| 474 | if (code_bytes == 0) return -1; |
| 475 | if (!omega_gate_precision((uint64_t)spec->precision, (uint64_t)OMEGA_MATMUL_PRECISION_FP16, |
| 476 | (uint64_t)OMEGA_MATMUL_PRECISION_BF16)) |
| 477 | return -1; |
| 478 | |
| 479 | if (exec_info) { |
| 480 | memset(exec_info, 0, sizeof(*exec_info)); |
| 481 | strncpy(exec_info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)", sizeof(exec_info->target_chip) - 1); |
| 482 | exec_info->sm_architecture = 121; |
| 483 | exec_info->m = spec->m; |
| 484 | exec_info->k = spec->k; |
| 485 | exec_info->n = spec->n; |
| 486 | exec_info->zero_libcuda_linkage = (omega_blackwell_verify_zero_libcuda_linkage(NULL) == 0); |
| 487 | exec_info->zero_cuda_symbols = (omega_blackwell_verify_zero_cuda_symbols(NULL) == 0); |
| 488 | exec_info->zero_libcuda_runtime = (omega_blackwell_verify_zero_libcuda_runtime() == 0); |
| 489 | } |
| 490 | |
| 491 | M16NativeContext ctx; |
| 492 | if (m16_native_open(&ctx) != 0) { return -1; } |
| 493 | if (m16_native_create_channel(&ctx) != 0) { m16_native_close(&ctx); return -1; } |
| 494 | |
| 495 | /* GB10: all buffers GPU-uncached, see omega_blackwell_execute_matmul. */ |
| 496 | NvrmMem large_pb; |
| 497 | if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x10000, &large_pb) != 0) { m16_native_close(&ctx); return -1; } |
| 498 | ctx.pb_mem = large_pb; |
| 499 | |
| 500 | size_t a_elems = (size_t)spec->m * spec->k; |
| 501 | size_t b_elems = (size_t)spec->k * spec->n; |
| 502 | size_t c_elems = (size_t)spec->m * spec->n; |
| 503 | |
| 504 | uint64_t a_bytes, b_bytes, c_bytes; |
| 505 | if (omega_gate_round_pages(a_elems * 2, &a_bytes) != 0 || omega_gate_round_pages(b_elems * 2, &b_bytes) != 0 || |
| 506 | omega_gate_round_pages(c_elems * 4, &c_bytes) != 0) { |
| 507 | m16_native_close(&ctx); |
| 508 | return -1; |
| 509 | } |
| 510 | /* code_bytes was sized and checked before the device was opened */ |
| 511 | |
| 512 | NvrmMem code_mem, cbank_mem, a_mem, b_mem, c_mem, marker_mem, qmd_mem; |
| 513 | if (nvrm_alloc_gpu_uncached(&ctx.rm, code_bytes, &code_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 514 | if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &cbank_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 515 | if (nvrm_alloc_gpu_uncached(&ctx.rm, a_bytes, &a_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 516 | if (nvrm_alloc_gpu_uncached(&ctx.rm, b_bytes, &b_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 517 | if (nvrm_alloc_gpu_uncached(&ctx.rm, c_bytes, &c_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 518 | if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &marker_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 519 | if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x10000, &qmd_mem) != 0) { m16_native_close(&ctx); return -1; } |
| 520 | |
| 521 | /* Copy dynamic machine code into code_mem */ |
| 522 | memcpy(code_mem.cpu, kernel->code, kernel->code_size); |
| 523 | memset((uint8_t *)code_mem.cpu + kernel->code_size, 0, code_bytes - kernel->code_size); |
| 524 | |
| 525 | /* Populate input buffers A, B and initialize C with poison */ |
| 526 | memcpy(a_mem.cpu, h_a, a_elems * 2); |
| 527 | memcpy(b_mem.cpu, h_b, b_elems * 2); |
| 528 | float *c_dev = (float *)c_mem.cpu; |
| 529 | for (size_t i = 0; i < c_elems; i++) { |
| 530 | c_dev[i] = -999.0f; |
| 531 | } |
| 532 | |
| 533 | uint32_t shape[4]; |
| 534 | if (omega_gate_matmul_shape(1, spec->m, spec->n, shape) != 0) { m16_native_close(&ctx); return -1; } |
| 535 | uint32_t grid_x = shape[0]; |
| 536 | uint32_t grid_y = shape[1]; |
| 537 | uint32_t threads_x = shape[2]; |
| 538 | uint32_t threads_y = shape[3]; |
| 539 | |
| 540 | uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 541 | omega_blackwell_build_cbank_driver_2d(cbank_data, cbank_mem.va, threads_x, threads_y, grid_x, grid_y); |
| 542 | |
| 543 | uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; |
| 544 | omega_blackwell_build_cbank_args_matmul(cbank_args, a_mem.va, b_mem.va, c_mem.va, spec->m, spec->k, spec->n); |
| 545 | |
| 546 | memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data)); |
| 547 | memcpy((uint8_t *)cbank_mem.cpu + 0x380, cbank_args, sizeof(cbank_args)); |
| 548 | |
| 549 | uint64_t qmd0_va = qmd_mem.va; |
| 550 | uint64_t qmd1_va = qmd_mem.va + 0x1000; |
| 551 | uint64_t sem_va = qmd_mem.va + 0x2000; |
| 552 | uint64_t scratch_va = qmd_mem.va + 0x4000; |
| 553 | |
| 554 | OmegaBlackwellQmdConfig qmd_cfg = { |
| 555 | .code_va = code_mem.va, |
| 556 | .cbank_va = cbank_mem.va, |
| 557 | .scratch_va = scratch_va, |
| 558 | .sem_va = sem_va, |
| 559 | .qmd0_va = qmd0_va, |
| 560 | .qmd1_va = qmd1_va, |
| 561 | .num_elements = c_elems, |
| 562 | .threads_per_block = 32, |
| 563 | .grid_width = grid_x * grid_y, |
| 564 | .threads_x = threads_x, |
| 565 | .threads_y = threads_y, |
| 566 | .grid_x = grid_x, |
| 567 | .grid_y = grid_y, |
| 568 | .gpr_count = 64 |
| 569 | }; |
| 570 | |
| 571 | uint32_t qmd0_words[OMEGA_BW_QMD_WORDS]; |
| 572 | uint32_t qmd1_words[OMEGA_BW_QMD_WORDS]; |
| 573 | omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); |
| 574 | omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg); |
| 575 | |
| 576 | if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { m16_native_close(&ctx); return -1; } |
| 577 | |
| 578 | memcpy(qmd_mem.cpu, qmd0_words, sizeof(qmd0_words)); |
| 579 | memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1_words, sizeof(qmd1_words)); |
| 580 | |
| 581 | volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)qmd_mem.cpu + 0x2000); |
| 582 | volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu; |
| 583 | volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10); |
| 584 | *hsem = 0; |
| 585 | *hmarker = 0; |
| 586 | *hmarker2 = 0; |
| 587 | nvrm_cpu_barrier(); |
| 588 | |
| 589 | uint32_t pb[1024]; |
| 590 | size_t pb_len = 0; |
| 591 | |
| 592 | memcpy(&pb[pb_len], SETUP_WORDS, sizeof(SETUP_WORDS)); |
| 593 | pb_len += sizeof(SETUP_WORDS) / 4; |
| 594 | |
| 595 | pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); |
| 596 | pb[pb_len++] = (uint32_t)(cbank_mem.va >> 32); |
| 597 | pb[pb_len++] = (uint32_t)cbank_mem.va; |
| 598 | pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); |
| 599 | pb[pb_len++] = 0x00000380; |
| 600 | pb[pb_len++] = 0x00000001; |
| 601 | pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); |
| 602 | pb[pb_len++] = 0x00000041; |
| 603 | pb[pb_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 604 | memcpy(&pb[pb_len], cbank_data, 224 * 4); |
| 605 | pb_len += 224; |
| 606 | |
| 607 | pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); |
| 608 | pb[pb_len++] = (uint32_t)((cbank_mem.va + 0x380) >> 32); |
| 609 | pb[pb_len++] = (uint32_t)(cbank_mem.va + 0x380); |
| 610 | pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); |
| 611 | pb[pb_len++] = 0x00000028; |
| 612 | pb[pb_len++] = 0x00000001; |
| 613 | pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); |
| 614 | pb[pb_len++] = 0x00000041; |
| 615 | pb[pb_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 616 | memcpy(&pb[pb_len], cbank_args, 10 * 4); |
| 617 | pb_len += 10; |
| 618 | |
| 619 | pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 620 | pb[pb_len++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); |
| 621 | pb[pb_len++] = (uint32_t)(qmd0_va >> 8); |
| 622 | memcpy(&pb[pb_len], qmd0_words, 96 * 4); |
| 623 | pb_len += 96; |
| 624 | |
| 625 | pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); |
| 626 | pb[pb_len++] = (uint32_t)(sem_va >> 32); |
| 627 | pb[pb_len++] = (uint32_t)sem_va; |
| 628 | pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); |
| 629 | pb[pb_len++] = 0x00000004; |
| 630 | pb[pb_len++] = 0x00000001; |
| 631 | pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); |
| 632 | pb[pb_len++] = 0x00000041; |
| 633 | pb[pb_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 634 | pb[pb_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; |
| 635 | |
| 636 | pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 637 | pb[pb_len++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); |
| 638 | pb[pb_len++] = (uint32_t)(qmd1_va >> 8); |
| 639 | memcpy(&pb[pb_len], qmd1_words, 96 * 4); |
| 640 | pb_len += 96; |
| 641 | |
| 642 | pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); |
| 643 | pb[pb_len++] = (uint32_t)marker_mem.va; |
| 644 | pb[pb_len++] = (uint32_t)(marker_mem.va >> 32); |
| 645 | pb[pb_len++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; |
| 646 | pb[pb_len++] = 0; |
| 647 | pb[pb_len++] = 0x1 | (1u << 20); |
| 648 | |
| 649 | /* C3 tail, same words as omega_blackwell_execute_matmul above: L2_FLUSH_DIRTY, then a |
| 650 | * second WFI release at marker page + 0x10 that the host waits for before the readback. */ |
| 651 | pb[pb_len++] = nvrm_mthd(0, 0x0028, 4); |
| 652 | pb[pb_len++] = 0; |
| 653 | pb[pb_len++] = 0; |
| 654 | pb[pb_len++] = 0; |
| 655 | pb[pb_len++] = (0x10u << 27); |
| 656 | pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); |
| 657 | pb[pb_len++] = (uint32_t)(marker_mem.va + 0x10); |
| 658 | pb[pb_len++] = (uint32_t)((marker_mem.va + 0x10) >> 32); |
| 659 | pb[pb_len++] = OMEGA_BW_MARKER2_PAYLOAD; |
| 660 | pb[pb_len++] = 0; |
| 661 | pb[pb_len++] = 0x1 | (1u << 20); |
| 662 | |
| 663 | uint64_t t_start = current_time_ns(); |
| 664 | if (m16_native_submit_methods(&ctx, pb, pb_len) != 0) { m16_native_close(&ctx); return -1; } |
| 665 | |
| 666 | /* CHIPWAIT-2: marker with marker2 required (C3: the second release lands only after the L2 flush). */ |
| 667 | if (bw_wait_one(__func__, "marker", hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, hmarker2, OMEGA_BW_MARKER2_PAYLOAD) != 0) { m16_native_close(&ctx); return -1; } |
| 668 | nvrm_cpu_barrier(); |
| 669 | uint64_t t_end = current_time_ns(); |
| 670 | |
| 671 | if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { m16_native_close(&ctx); return -1; } |
| 672 | |
| 673 | memcpy(h_c_out, c_mem.cpu, c_elems * sizeof(float)); |
| 674 | |
| 675 | float *h_expected = (float *)malloc(c_elems * sizeof(float)); |
| 676 | if (!h_expected) { m16_native_close(&ctx); return -1; } |
| 677 | |
| 678 | if (spec->precision == OMEGA_MATMUL_PRECISION_FP16) { |
| 679 | omega_matmul_cpu_oracle_f16((const uint16_t *)h_a, (const uint16_t *)h_b, h_expected, spec->m, spec->k, spec->n); |
| 680 | } else { |
| 681 | omega_matmul_cpu_oracle_bf16((const uint16_t *)h_a, (const uint16_t *)h_b, h_expected, spec->m, spec->k, spec->n); |
| 682 | } |
| 683 | |
| 684 | float max_abs = 0.0f; |
| 685 | float max_rel = 0.0f; |
| 686 | size_t mismatches = 0; |
| 687 | for (size_t i = 0; i < c_elems; i++) { |
| 688 | float exp = h_expected[i]; |
| 689 | float act = h_c_out[i]; |
| 690 | float diff = fabsf(exp - act); |
| 691 | float rel = (fabsf(exp) > 1e-6f) ? (diff / fabsf(exp)) : diff; |
| 692 | if (diff > max_abs) max_abs = diff; |
| 693 | if (rel > max_rel) max_rel = rel; |
| 694 | if (diff > 1e-4f) { |
| 695 | mismatches++; |
| 696 | } |
| 697 | } |
| 698 | free(h_expected); |
| 699 | |
| 700 | if (out_max_abs_err) *out_max_abs_err = max_abs; |
| 701 | if (out_max_rel_err) *out_max_rel_err = max_rel; |
| 702 | |
| 703 | if (exec_info) { |
| 704 | exec_info->launch_timestamp_ns = t_start; |
| 705 | exec_info->completion_timestamp_ns = t_end; |
| 706 | exec_info->elapsed_ns = (t_end > t_start) ? (t_end - t_start) : 0; |
| 707 | exec_info->completion_marker = *hmarker; |
| 708 | exec_info->intermediate_semaphore = *hsem; |
| 709 | exec_info->mismatch_count = mismatches; |
| 710 | exec_info->parity_verified = (mismatches == 0); |
| 711 | } |
| 712 | |
| 713 | m16_native_close(&ctx); |
| 714 | return (mismatches == 0) ? 0 : -1; |
| 715 | } |
| 716 | |
| 717 | /* stdout of argv, stderr discarded. -1 is a pipe or fork failure, the same |
| 718 | * class as a failed popen. The child exits 127 when exec fails, and the |
| 719 | * caller then sees an empty stream, as it did when the shell could not run |
| 720 | * the tool. binary_path is one argv element: it is not a shell command. */ |
| 721 | struct tool_stdout { |
| 722 | FILE *out; |
| 723 | pid_t pid; |
| 724 | }; |
| 725 | |
| 726 | static int spawn_tool_stdout(char *const argv[], struct tool_stdout *sp) { |
| 727 | int fds[2]; |
| 728 | pid_t pid; |
| 729 | |
| 730 | /* O_CLOEXEC matches glibc popen: a fork in another thread must not inherit the pipe. */ |
| 731 | if (pipe2(fds, O_CLOEXEC) != 0) return -1; |
| 732 | pid = fork(); |
| 733 | if (pid < 0) { |
| 734 | close(fds[0]); |
| 735 | close(fds[1]); |
| 736 | return -1; |
| 737 | } |
| 738 | if (pid == 0) { |
| 739 | int devnull = open("/dev/null", O_WRONLY); |
| 740 | if (devnull >= 0) { |
| 741 | dup2(devnull, STDERR_FILENO); |
| 742 | if (devnull != STDERR_FILENO) close(devnull); |
| 743 | } |
| 744 | if (dup2(fds[1], STDOUT_FILENO) < 0) _exit(127); |
| 745 | close(fds[0]); |
| 746 | if (fds[1] != STDOUT_FILENO) close(fds[1]); |
| 747 | execvp(argv[0], argv); |
| 748 | _exit(127); |
| 749 | } |
| 750 | close(fds[1]); |
| 751 | sp->out = fdopen(fds[0], "r"); |
| 752 | if (!sp->out) { |
| 753 | close(fds[0]); |
| 754 | while (waitpid(pid, NULL, 0) < 0 && errno == EINTR) {} |
| 755 | return -1; |
| 756 | } |
| 757 | sp->pid = pid; |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | static void finish_tool_stdout(struct tool_stdout *sp) { |
| 762 | fclose(sp->out); |
| 763 | while (waitpid(sp->pid, NULL, 0) < 0 && errno == EINTR) {} |
| 764 | } |
| 765 | |
| 766 | int omega_blackwell_verify_zero_libcuda_linkage(const char *binary_path) { |
| 767 | const char *path = binary_path ? binary_path : "/proc/self/exe"; |
| 768 | char *path_arg = strdup(path); |
| 769 | char *argv[3]; |
| 770 | struct tool_stdout sp; |
| 771 | char line[512]; |
| 772 | bool found_cuda = false; |
| 773 | |
| 774 | if (!path_arg) return -1; |
| 775 | argv[0] = "ldd"; |
| 776 | argv[1] = path_arg; |
| 777 | argv[2] = NULL; |
| 778 | if (spawn_tool_stdout(argv, &sp) != 0) { |
| 779 | free(path_arg); |
| 780 | return -1; |
| 781 | } |
| 782 | free(path_arg); |
| 783 | while (fgets(line, sizeof(line), sp.out)) { |
| 784 | if (strstr(line, "libcuda.so") || strstr(line, "libcudart.so")) { |
| 785 | found_cuda = true; |
| 786 | break; |
| 787 | } |
| 788 | } |
| 789 | finish_tool_stdout(&sp); |
| 790 | return found_cuda ? -1 : 0; |
| 791 | } |
| 792 | |
| 793 | int omega_blackwell_verify_zero_cuda_symbols(const char *binary_path) { |
| 794 | const char *path = binary_path ? binary_path : "/proc/self/exe"; |
| 795 | char *path_arg = strdup(path); |
| 796 | char *argv[4]; |
| 797 | struct tool_stdout sp; |
| 798 | char line[512]; |
| 799 | bool found_cuda_sym = false; |
| 800 | |
| 801 | if (!path_arg) return -1; |
| 802 | argv[0] = "nm"; |
| 803 | argv[1] = "-u"; |
| 804 | argv[2] = path_arg; |
| 805 | argv[3] = NULL; |
| 806 | if (spawn_tool_stdout(argv, &sp) != 0) { |
| 807 | free(path_arg); |
| 808 | return -1; |
| 809 | } |
| 810 | free(path_arg); |
| 811 | while (fgets(line, sizeof(line), sp.out)) { |
| 812 | char sym[256]; |
| 813 | if (sscanf(line, " %*s %255s", sym) == 1 || sscanf(line, "%255s", sym) == 1) { |
| 814 | if (strncmp(sym, "cu", 2) == 0 || strncmp(sym, "cuda", 4) == 0) { |
| 815 | found_cuda_sym = true; |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | finish_tool_stdout(&sp); |
| 821 | return found_cuda_sym ? -1 : 0; |
| 822 | } |
| 823 | |
| 824 | int omega_blackwell_verify_zero_libcuda_runtime(void) { |
| 825 | FILE *f = fopen("/proc/self/maps", "r"); |
| 826 | if (!f) return -1; |
| 827 | |
| 828 | char line[512]; |
| 829 | bool found_cuda = false; |
| 830 | while (fgets(line, sizeof(line), f)) { |
| 831 | if (strstr(line, "libcuda") || strstr(line, "libcudart")) { |
| 832 | found_cuda = true; |
| 833 | break; |
| 834 | } |
| 835 | } |
| 836 | fclose(f); |
| 837 | return found_cuda ? -1 : 0; |
| 838 | } |