Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/bench/prime_race/gb10_native.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / bench / prime_race / gb10_native.c` 309 lines ยท 15091 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* AIEN Prime Drag Race: native GB10 implementation (no CUDA). 18 * 19 * Kernel: gen_prime_sieve in src/omega_gpu_elementwise_api.c (Omega's own Blackwell 20 * codegen; word-parallel odd-only sieve, one writer per 32-bit word, no atomics). 21 * Launch: the process-wide session (src/omega_gpu_session.h): shader-cache invalidate, 22 * constant bank, QMD, marker 1, L2_FLUSH_DIRTY, marker 2 on uncached memory; every 23 * buffer here is GPU-uncached (session rule), so host writes and host reads need no 24 * extra flush beyond the dsb before the launch. 25 * 26 * One-time setup (untimed): session lock + open, kernel codegen + upload, persistent 27 * output / prime-table buffers sized for the limit. 28 * Pass (timed): reset the output words (poison), host sieve of the odd base primes 29 * <= isqrt(limit) with their division magics written straight into the device-visible 30 * table, one launch (<= OMEGA_GPU_EW_MAX_CTAS CTAs, grid-stride over words), wait for 31 * both markers. Persistent buffers => faithful=no. 32 * 33 * CTA budget (optimization campaign C1, 2026-10-05): PR_GB10_CTA_BUDGET=N (1..4096) in the 34 * environment lifts or lowers the launch cap; unset keeps OMEGA_GPU_EW_MAX_CTAS (the proven 35 * I42 envelope). Any other value fails setup. The budget used is reported in the detail JSON. 36 * 37 * Marker wait (campaign C4): PR_GB10_SPIN_US=N (0..`1000000`) polls the completion marker without 38 * sleeping for up to N us before the session's usual 50 us sleep-poll; unset or 0 keeps the old 39 * wait. Any other value fails setup. Reported in the detail JSON as spin_us. 40 * 41 * Mark loop (omega #308): PR_GB10_MARK=uniform (default, campaign C5), divergent (the original 42 * lane-dependent mark loop inside a BSSY/BSYNC reconvergence region, regression R1) or divergent2 43 * (the prime loop in a region as well, nested). Any other value fails setup. Reported as mark. 44 * 45 * Labels (handoff 2026-10-05 DESIGN): algorithm=other, faithful=no, bits=1, 46 * environment linux-hosted-gb10-native. Build with -DPR_GB10_MUTANT=1 only for the chip 47 * test's negative control (the r == 0 fix is dropped; the oracle must catch it). 48 */ 49 #include "prime_race_impl.h" 50 #include "omega_gpu_elementwise_api.h" 51 #include "omega_gpu_session.h" 52 #include "omega_blackwell_qmd.h" /* OMEGA_BW_CBANK_MATMUL_ARGS_WORDS */ 53 #include 54 #include 55 #include 56 #include 57 58 #ifndef PR_GB10_MUTANT 59 #define PR_GB10_MUTANT 0 60 #endif 61 62 #define GB_THREADS OMEGA_GPU_EW_THREADS 63 #define GB_MAX_CTAS OMEGA_GPU_EW_MAX_CTAS 64 #define GB_GPR 64u /* the QMD register count the elementwise launcher uses */ 65 #define GB_WAIT_MS 600000ull /* same marker wait as the elementwise launcher (omega#198) */ 66 #define GB_POISON 0xffbadbadu 67 #define GB_MAX_LIMIT 0xffffffffull /* kernel range: base, k0 < 2^31 */ 68 #define GB_BUDGET_MAX 4096u 69 #define GB_ARGS 11u /* a, b, c (64-bit each) then P0..P4 */ 70 71 /* PR_GB10_SPIN_US or 0; UINT32_MAX on a malformed or out-of-range value. */ 72 static uint32_t gb_spin_us(void) { 73 const char *v = getenv("PR_GB10_SPIN_US"); 74 if (!v) return 0; 75 char *end = NULL; 76 unsigned long n = strtoul(v, &end, 10); 77 if (end == v || *end != 0 || v[0] == '-' || n > OMEGA_GPU_SESSION_MAX_SPIN_US) return UINT32_MAX; 78 return (uint32_t)n; 79 } 80 81 /* PR_GB10_CTA_BUDGET or the I42 default; 0 on a malformed or out-of-range value. */ 82 static uint32_t gb_cta_budget(void) { 83 const char *v = getenv("PR_GB10_CTA_BUDGET"); 84 if (!v) return GB_MAX_CTAS; 85 char *end = NULL; 86 unsigned long n = strtoul(v, &end, 10); 87 if (end == v || *end != 0 || v[0] == '-' || n == 0 || n > GB_BUDGET_MAX) return 0; 88 return (uint32_t)n; 89 } 90 91 /* PR_GB10_TABLE: "auto" or unset (shared-memory table when it fits), "global" (never staged); 92 * -1 on any other value. The A/B switch for the staged table (campaign C6). */ 93 static int gb_table_global(void) { 94 const char *v = getenv("PR_GB10_TABLE"); 95 if (!v || strcmp(v, "auto") == 0) return 0; 96 if (strcmp(v, "global") == 0) return 1; 97 return -1; 98 } 99 100 /* PR_GB10_MARK (omega #308): "uniform" or unset = the C5 warp-uniform mark loop; "divergent" = 101 * the original lane-dependent mark loop inside a BSSY/BSYNC region (regression R1); "divergent2" = 102 * the prime loop in a region as well (nested). -1 on any other value. */ 103 static int gb_mark_mode(void) { 104 const char *v = getenv("PR_GB10_MARK"); 105 if (!v || strcmp(v, "uniform") == 0) return 0; 106 if (strcmp(v, "divergent") == 0) return 1; 107 if (strcmp(v, "divergent2") == 0) return 2; 108 return -1; 109 } 110 static const char *const GB_MARK_NAMES[3] = { "uniform", "divergent", "divergent2" }; 111 112 typedef struct { 113 uint64_t limit, odd; 114 uint32_t nwords, ctas, budget, stride, lastw, tailinv; 115 uint32_t spin_us; /* marker wait spin window (campaign C4) */ 116 int table_global; /* PR_GB10_TABLE=global: never stage the table (campaign C6) */ 117 int mark; /* PR_GB10_MARK: 0 uniform (C5), 1 regioned mark loop, 2 nested regions (omega #308) */ 118 uint32_t root; /* isqrt(limit) */ 119 uint32_t nprimes; /* odd primes in the table of the last pass (sentinel excluded) */ 120 size_t table_cap; /* entries, sentinel included */ 121 uint32_t entries; /* table entries for this limit, sentinel included (fixed by the limit) */ 122 uint32_t shared_bytes; /* 0: global-table kernel; else the staged kernel's shared memory per CTA */ 123 OmegaBlackwellKernel k; 124 OmegaGpuMem code, out, table; 125 uint64_t diag_ns, launches; 126 int locked, opened; 127 } gb_state; 128 129 /* floor(sqrt(n)). The root of any u64 is below 2^32, so the top trial bit is 2^31: with that start 130 * (r + bit) <= 2^32 - 1 and its square cannot wrap. (A 2^32 start squared to 2^64 = 0 on the first 131 * step and returned 0 for every n, so no base primes were ever sieved; caught on chip at limit 9.) */ 132 static uint32_t isqrt_u64(uint64_t n) { 133 uint64_t r = 0; 134 for (uint64_t bit = 1ull << 31; bit; bit >>= 1) 135 if ((r + bit) * (r + bit) <= n) r += bit; 136 return (uint32_t)r; 137 } 138 139 /* odd primes <= root (the table the pass builds, without the sentinel) */ 140 static uint32_t gb_count_odd_primes(uint32_t root) { 141 uint32_t n = 0, half = root / 2 + 1; 142 uint8_t *flags = calloc(half, 1); 143 if (!flags) return 0xfffffffeu; /* makes the per-pass check fail closed */ 144 for (uint32_t i = 1; i < half; i++) { 145 uint32_t p = 2 * i + 1; 146 if (p > root) break; 147 if (flags[i]) continue; 148 for (uint64_t c = (uint64_t)p * p; c <= root; c += 2ull * p) flags[c / 2] = 1; 149 n++; 150 } 151 free(flags); 152 return n; 153 } 154 155 static void gb_release(gb_state *s) { 156 if (s->locked) { 157 omega_gpu_session_free(&s->table); 158 omega_gpu_session_free(&s->out); 159 omega_gpu_session_free(&s->code); 160 omega_gpu_session_unlock(); 161 s->locked = 0; 162 } 163 if (s->opened) { omega_gpu_session_close(); s->opened = 0; } /* refused (no-op) after a latch */ 164 free(s->k.code); s->k.code = NULL; 165 } 166 167 static int gb_setup(void **state, uint64_t limit) { 168 if (limit > GB_MAX_LIMIT) { fprintf(stderr, "gb10-native: limit %" PRIu64 " above %llu\n", limit, GB_MAX_LIMIT); return -1; } 169 gb_state *s = calloc(1, sizeof *s); 170 if (!s) return -1; 171 *state = s; 172 s->limit = limit; 173 s->odd = pr_odd_count(limit); 174 s->nwords = (uint32_t)((s->odd + 31) / 32); 175 s->budget = gb_cta_budget(); 176 if (s->budget == 0) { fprintf(stderr, "gb10-native: PR_GB10_CTA_BUDGET must be 1..%u\n", GB_BUDGET_MAX); return -1; } 177 s->spin_us = gb_spin_us(); 178 if (s->spin_us == UINT32_MAX) { fprintf(stderr, "gb10-native: PR_GB10_SPIN_US must be 0..%u\n", OMEGA_GPU_SESSION_MAX_SPIN_US); return -1; } 179 s->table_global = gb_table_global(); 180 if (s->table_global < 0) { fprintf(stderr, "gb10-native: PR_GB10_TABLE must be auto or global\n"); return -1; } 181 s->mark = gb_mark_mode(); 182 if (s->mark < 0) { fprintf(stderr, "gb10-native: PR_GB10_MARK must be uniform, divergent or divergent2\n"); return -1; } 183 uint32_t blocks = (s->nwords + GB_THREADS - 1) / GB_THREADS; 184 s->ctas = blocks == 0 ? 1 : (blocks > s->budget ? s->budget : blocks); 185 s->stride = s->ctas * GB_THREADS; 186 s->lastw = s->nwords ? s->nwords - 1 : 0; 187 uint32_t valid = s->nwords ? (uint32_t)(s->odd - 32ull * s->lastw) : 32; /* 1..32 */ 188 uint32_t tailmask = valid >= 32 ? 0xffffffffu : ((1u << valid) - 1u); 189 s->tailinv = ~tailmask; 190 s->root = isqrt_u64(limit); 191 if ((uint64_t)s->root * s->root > limit || ((uint64_t)s->root + 1) * ((uint64_t)s->root + 1) <= limit) { 192 fprintf(stderr, "gb10-native: isqrt(%" PRIu64 ") = %u is wrong\n", limit, s->root); return -1; /* fail closed */ 193 } 194 s->table_cap = (size_t)s->root / 2 + 2; /* every odd number <= root, plus the sentinel */ 195 s->entries = gb_count_odd_primes(s->root) + 1; 196 /* campaign C3: stage the table in shared memory whenever it fits the QMD bound */ 197 if (!s->table_global && (uint64_t)s->entries * 12 <= OMEGA_BW_QMD_MAX_SHARED_BYTES) s->shared_bytes = s->entries * 12; 198 OmegaGpuEwOp op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED : OMEGA_GPU_EW_PRIME_SIEVE; 199 if (s->mark == 1) op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 : OMEGA_GPU_EW_PRIME_SIEVE_DIV1; 200 if (s->mark == 2) op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2 : OMEGA_GPU_EW_PRIME_SIEVE_DIV2; 201 202 if (omega_gpu_elementwise_codegen_ir(op, PR_GB10_MUTANT, NULL, &s->k) != OMEGA_GPU_EW_OK) { 203 fprintf(stderr, "gb10-native: kernel codegen failed\n"); return -1; 204 } 205 omega_gpu_session_lock(); 206 s->locked = 1; 207 if (!omega_gpu_session_open()) { fprintf(stderr, "gb10-native: session open failed: %s\n", omega_gpu_session_last_error()); return -1; } 208 s->opened = 1; 209 size_t out_bytes = (size_t)(s->nwords ? s->nwords : 1) * 4; 210 if (omega_gpu_session_alloc(s->k.code_size, &s->code) != 0 || 211 omega_gpu_session_alloc(out_bytes, &s->out) != 0 || 212 omega_gpu_session_alloc(s->table_cap * 12, &s->table) != 0) { 213 fprintf(stderr, "gb10-native: alloc failed: %s\n", omega_gpu_session_last_error()); return -1; 214 } 215 memcpy(s->code.cpu, s->k.code, s->k.code_size); 216 __asm__ volatile("dsb sy" ::: "memory"); 217 return 0; 218 } 219 220 static int gb_pass(void *state, uint64_t limit) { 221 gb_state *s = state; 222 if (!s || limit != s->limit || omega_gpu_session_is_blocked()) return -1; 223 /* reset the sieve state: every output word poisoned, the kernel must overwrite all */ 224 uint32_t *out = s->out.cpu; 225 for (uint32_t i = 0; i < s->nwords; i++) out[i] = GB_POISON; 226 /* odd base primes <= isqrt(limit): flags[i] marks 2i+1 composite */ 227 uint32_t half = s->root / 2 + 1; 228 uint8_t *flags = calloc(half, 1); 229 if (!flags) return -1; 230 uint32_t *tab = s->table.cpu, n = 0; 231 for (uint32_t i = 1; i < half; i++) { 232 uint32_t p = 2 * i + 1; 233 if (p > s->root) break; 234 if (flags[i]) continue; 235 for (uint64_t c = (uint64_t)p * p; c <= s->root; c += 2ull * p) flags[c / 2] = 1; 236 tab[3 * n + 0] = p; 237 tab[3 * n + 1] = (uint32_t)(((1ull << 32) + p - 1) / p); /* ceil(2^32 / p) */ 238 tab[3 * n + 2] = (uint32_t)(((uint64_t)p * p - 1) / 2); 239 n++; 240 } 241 free(flags); 242 tab[3 * n + 0] = 1; tab[3 * n + 1] = 0; tab[3 * n + 2] = 0xffffffffu; /* sentinel: ends every scan */ 243 s->nprimes = n; 244 if (n + 1 != s->entries) { fprintf(stderr, "gb10-native: %u table entries, setup counted %u\n", n + 1, s->entries); return -1; } 245 __asm__ volatile("dsb sy" ::: "memory"); 246 247 uint32_t args[GB_ARGS] = {0}; 248 args[0] = (uint32_t)s->table.va; args[1] = (uint32_t)(s->table.va >> 32); 249 args[4] = (uint32_t)s->out.va; args[5] = (uint32_t)(s->out.va >> 32); 250 args[6] = s->nwords; args[7] = s->stride; args[8] = s->lastw; args[9] = s->tailinv; 251 args[10] = s->entries * 3; /* P4: table words, read by the staged kernel only */ 252 OmegaGpuLaunch L = { 253 .code_va = s->code.va, .gpr_count = GB_GPR, .threads_x = GB_THREADS, .threads_y = 1, 254 .grid_x = s->ctas, .grid_y = 1, .num_elements = s->nwords ? s->nwords : 1, 255 .args = args, .n_args = GB_ARGS, .timeout_ms = GB_WAIT_MS, .shared_bytes = s->shared_bytes, .spin_us = s->spin_us, 256 }; 257 uint64_t ns = 0; uint32_t marker = 0; 258 if (omega_gpu_session_launch(&L, &ns, &marker) != 0) { 259 fprintf(stderr, "gb10-native: launch failed: %s\n", omega_gpu_session_last_error()); 260 return -1; 261 } 262 s->diag_ns += ns; 263 s->launches++; 264 return 0; 265 } 266 267 static int gb_export(void *state, uint64_t limit, uint64_t *words) { 268 gb_state *s = state; 269 if (!s || limit != s->limit) return -1; 270 size_t bytes64 = pr_words64(limit) * 8, bytes32 = (size_t)s->nwords * 4; 271 if (bytes32 > bytes64) return -1; 272 memcpy(words, s->out.cpu, bytes32); 273 memset((uint8_t *)words + bytes32, 0, bytes64 - bytes32); 274 return 0; 275 } 276 277 static uint64_t gb_threads(void *state) { gb_state *s = state; return s ? s->stride : 0; } 278 279 static int gb_detail(void *state, FILE *f) { 280 gb_state *s = state; 281 if (!s) return -1; 282 fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"table\": \"%s\", \"shared_bytes\": %u, \"spin_us\": %u, \"mark\": \"%s\", \"kernel_code_sha256\": \"", 283 s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes, s->shared_bytes ? "shared" : "global", s->shared_bytes, s->spin_us, GB_MARK_NAMES[s->mark]); 284 for (int i = 0; i < 32; i++) fprintf(f, "%02x", s->k.code_digest[i]); 285 fprintf(f, "\", \"kernel_insns\": %zu, \"kernel_gpr\": %u, \"mutant\": %d, \"launches\": %" PRIu64 286 ", \"diag_kernel_ns_total\": %" PRIu64 287 ", \"diag_note\": \"sum of session elapsed_ns (submit to second marker), diagnostic only\"}", 288 s->k.insn_count, s->k.gpr_count, PR_GB10_MUTANT, s->launches, s->diag_ns); 289 return ferror(f) ? -1 : 0; 290 } 291 292 static void gb_teardown(void *state) { 293 gb_state *s = state; 294 if (!s) return; 295 gb_release(s); 296 free(s); 297 } 298 299 static const pr_impl GB10_NATIVE = { 300 .name = PR_GB10_MUTANT ? "gb10-native-mutant" : "gb10-native", 301 .algorithm = "other", .faithful = "no", .bits = 1, 302 .environment = "linux-hosted-gb10-native", 303 .setup = gb_setup, .pass = gb_pass, .export_bitmap = gb_export, 304 .threads = gb_threads, .detail_json = gb_detail, .teardown = gb_teardown, 305 }; 306 307 #ifndef PR_NO_MAIN 308 int main(int argc, char **argv) { return pr_main(argc, argv, &GB10_NATIVE); } 309 #endif