interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / bench / prime_race / tests / gb10_launch_cost.c

138 lines · 7666 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/* DIAGNOSTIC, not a benchmark result: where does a GB10 native launch spend its time?
18 *
19 * gb10_launch_cost empty N GRID BLOCK EXIT-only kernel, N launches
20 * gb10_launch_cost sieve N GRID BLOCK LIMIT prime sieve kernel at LIMIT, N launches
21 *
22 * Uses the session API exactly as bench/prime_race/gb10_native.c does (lock, open, uncached
23 * allocations, omega_gpu_session_launch with gpr 64 and a 600 s marker wait). Per launch it
24 * records the session's submit->marker2 time and the host wall time of the launch call, then
25 * prints median/min/max of each. The sieve kernel family assumes 128-thread CTAs (OMEGA_GPU_EW_THREADS), so
26 * BLOCK must be 128 for sieve; only the grid varies. The sieve run is checked: the bitmap of the last launch must
27 * hold pi(LIMIT) - 1 set bits (odd primes; bit i is 2i+1). Needs GB10_CHIP_RUN=1 and the GPU
28 * lock (/tmp/aien-gb10.lock) held by the caller. No CUDA. */
29#include "omega_gpu_elementwise_api.h"
30#include "omega_gpu_session.h"
31#include "omega_blackwell_codegen.h"
32#include "omega_blackwell_qmd.h" /* OMEGA_BW_CBANK_MATMUL_ARGS_WORDS */
33#include <inttypes.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <time.h>
38
39#define MAXN 100000
40
41static uint64_t now_ns(void) {
42 struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t);
43 return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec;
44}
45static int cmp_u64(const void *a, const void *b) {
46 uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y;
47}
48static void stats(const char *what, uint64_t *v, int n) {
49 qsort(v, (size_t)n, sizeof *v, cmp_u64);
50 uint64_t med = n % 2 ? v[n / 2] : (v[n / 2 - 1] + v[n / 2]) / 2;
51 printf(" %-22s median=%8.1f us min=%8.1f us max=%8.1f us\n", what, med / 1e3, v[0] / 1e3, v[n - 1] / 1e3);
52}
53
54/* EXIT plus the self-branch every kernel here ends with; nothing else. */
55static int build_empty(OmegaBlackwellKernel *k) {
56 static BlackwellIRProgram prog;
57 omega_bw_ir_init(&prog);
58 BlackwellIRInsn x; memset(&x, 0, sizeof x);
59 x.dst_vreg = x.src1_vreg = x.src2_vreg = x.src3_vreg = x.ureg = -1;
60 x.op = BW_IR_EXIT;
61 if (omega_bw_ir_append(&prog, &x) < 0) return -1; /* returns the index */
62 x.op = BW_IR_BRA; x.imm = 0;
63 if (omega_bw_ir_append(&prog, &x) < 0) return -1; /* returns the index */
64 if (omega_bw_regalloc_solve(&prog) != 0) return -1;
65 enum { CAP = 32 * 16 }; /* the encoder pads every program to at least 32 instructions */
66 k->code = malloc(CAP);
67 size_t len = 0;
68 if (!k->code || omega_bw_encode_program(&prog, k->code, CAP, &len) != 0) return -1;
69 k->code_size = len; k->insn_count = prog.count; k->gpr_count = 1;
70 return 0;
71}
72
73int main(int argc, char **argv) {
74 if (argc < 5 || (strcmp(argv[1], "empty") && strcmp(argv[1], "sieve")) || (!strcmp(argv[1], "sieve") && argc < 6)) {
75 fprintf(stderr, "usage: %s empty N GRID BLOCK | sieve N GRID BLOCK LIMIT\n", argv[0]); return 64;
76 }
77 const int sieve = !strcmp(argv[1], "sieve");
78 int n = atoi(argv[2]); uint32_t grid = (uint32_t)atoi(argv[3]), block = (uint32_t)atoi(argv[4]);
79 uint64_t limit = sieve ? strtoull(argv[5], NULL, 10) : 0;
80 if (n < 1 || n > MAXN || grid < 1 || grid > OMEGA_GPU_EW_MAX_CTAS || block < 32 || block > 1024 || block % 32 ||
81 limit > 0xffffffffull || (sieve && block != OMEGA_GPU_EW_THREADS)) { fprintf(stderr, "argument out of range\n"); return 64; }
82
83 OmegaBlackwellKernel k; memset(&k, 0, sizeof k);
84 if (sieve ? omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE, 0, NULL, &k) != OMEGA_GPU_EW_OK
85 : build_empty(&k) != 0) { fprintf(stderr, "codegen failed\n"); return 2; }
86
87 if (!getenv("GB10_CHIP_RUN")) { fprintf(stderr, "codegen ok (%zu insns, %zu bytes); refused: set GB10_CHIP_RUN=1 (chip launch)\n", k.insn_count, k.code_size); return 2; }
88
89 /* sieve geometry as gb10_native computes it, with the grid and block taken from the caller */
90 uint64_t odd = (limit + 1) / 2;
91 uint32_t nwords = (uint32_t)((odd + 31) / 32), lastw = nwords ? nwords - 1 : 0;
92 uint32_t valid = nwords ? (uint32_t)(odd - 32ull * lastw) : 32;
93 uint32_t tailinv = ~(valid >= 32 ? 0xffffffffu : ((1u << valid) - 1u));
94 uint32_t root = 0; while ((uint64_t)(root + 1) * (root + 1) <= limit) root++;
95
96 omega_gpu_session_lock();
97 if (!omega_gpu_session_open()) { fprintf(stderr, "open failed: %s\n", omega_gpu_session_last_error()); return 2; }
98 OmegaGpuMem code, out, tab;
99 if (omega_gpu_session_alloc(k.code_size, &code) || omega_gpu_session_alloc((size_t)(nwords ? nwords : 1) * 4, &out) ||
100 omega_gpu_session_alloc(((size_t)root / 2 + 2) * 12, &tab)) { fprintf(stderr, "alloc failed\n"); return 2; }
101 memcpy(code.cpu, k.code, k.code_size);
102 uint32_t *t = tab.cpu, np = 0;
103 for (uint32_t p = 3; p <= root; p += 2) {
104 int pr = 1; for (uint32_t d = 3; d * d <= p; d += 2) if (p % d == 0) { pr = 0; break; }
105 if (!pr) continue;
106 t[3 * np] = p; t[3 * np + 1] = (uint32_t)(((1ull << 32) + p - 1) / p); t[3 * np + 2] = (uint32_t)(((uint64_t)p * p - 1) / 2); np++;
107 }
108 t[3 * np] = 1; t[3 * np + 1] = 0; t[3 * np + 2] = 0xffffffffu;
109 __asm__ volatile("dsb sy" ::: "memory");
110
111 uint32_t args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS] = {0};
112 args[0] = (uint32_t)tab.va; args[1] = (uint32_t)(tab.va >> 32);
113 args[4] = (uint32_t)out.va; args[5] = (uint32_t)(out.va >> 32);
114 args[6] = nwords; args[7] = grid * block; args[8] = lastw; args[9] = tailinv;
115 OmegaGpuLaunch L = {
116 .code_va = code.va, .gpr_count = 64, .threads_x = block, .threads_y = 1, .grid_x = grid, .grid_y = 1,
117 .num_elements = nwords ? nwords : 1, .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = 600000,
118 };
119 /* PR_GB10_SPIN_US: marker wait spin window, as in gb10_native.c (campaign C4) */
120 if (getenv("PR_GB10_SPIN_US")) L.spin_us = (uint32_t)strtoul(getenv("PR_GB10_SPIN_US"), NULL, 10);
121 static uint64_t dev[MAXN], wall[MAXN];
122 for (int i = 0; i < n; i++) {
123 uint64_t ns = 0; uint32_t marker = 0; /* elapsed_ns is added to, so reset per launch */
124 uint64_t t0 = now_ns();
125 if (omega_gpu_session_launch(&L, &ns, &marker) != 0) { fprintf(stderr, "launch %d failed: %s\n", i, omega_gpu_session_last_error()); return 2; }
126 wall[i] = now_ns() - t0; dev[i] = ns;
127 }
128 long bits = 0;
129 if (sieve) { uint32_t *o = out.cpu; for (uint32_t i = 0; i < nwords; i++) bits += __builtin_popcount(o[i]); }
130 printf("DIAGNOSTIC %s launches=%d grid=%u block=%u insns=%zu limit=%" PRIu64 " words=%u base_primes=%u odd_primes_found=%ld spin_us=%u\n",
131 argv[1], n, grid, block, k.insn_count, limit, nwords, np, bits, L.spin_us);
132 stats("submit->marker2", dev, n);
133 stats("host wall per launch", wall, n);
134 omega_gpu_session_free(&tab); omega_gpu_session_free(&out); omega_gpu_session_free(&code);
135 omega_gpu_session_unlock(); omega_gpu_session_close();
136 free(k.code);
137 return 0;
138}