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/aien-yardsticks/patches/240361090da31fe4 # aien-yardsticks aien-dev/aien-yardsticks Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/aien-yardsticks#5 (code only) closed derived `240361090da31fe4` #5 Imported from GitHub aien-dev/aien-yardsticks#5 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `def06e5ee9ccaef1c472af482bff3430cc34272f` · Head `a8b24a10f963302d5ed608303605a718a6d0a8b0` ### Apply it ``` git checkout -b patch-240361090da31fe4 def06e5ee9ccaef1c472af482bff3430cc34272f curl -fsSL https://interplane.io/drakestapleton/aien-yardsticks/patches/240361090da31fe4.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aien-yardsticks 5 a8b24a10f963302d5ed608303605a718a6d0a8b0 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aien-yardsticks","number":5,"url":"https://github.com/aien-dev/aien-yardsticks/pull/5","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:39:54Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/README.md b/README.md index 5bc8553..1b404a0 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,6 @@ Outside yardsticks AIEN is measured against (MAX, llama.cpp, vLLM). Never part o |---|---| | Modular MAX, Nemotron-H KV-head expansion | `yardsticks/modular-nemotron-h-kvexp/` | | TinyLlama reference oracle (HF Transformers, CPU FP32) | `yardsticks/tinyllama-oracle/` | +| CUDA mirror of the AIEN native prime sieve kernel (Prime Drag Race) | `yardsticks/prime-drag-race-cuda/` | Python is tolerated here only until it is replaced by Rust. diff --git a/yardsticks/prime-drag-race-cuda/.crumb b/yardsticks/prime-drag-race-cuda/.crumb new file mode 100644 index 0000000..c2429a4 --- /dev/null +++ b/yardsticks/prime-drag-race-cuda/.crumb @@ -0,0 +1,20 @@ +{ + "schema_version": "1.0", + "dir_path": "./yardsticks/prime-drag-race-cuda", + "purpose": { + "summary": "Outside CUDA yardstick for the AIEN Prime Drag Race: mirrors the AIEN native GPU sieve kernel algorithm; outside AIEN, never linked into AIEN binaries", + "created_by": "AIEN", + "session_id": "prime-drag-race", + "created_at": "2026-10-05T19:00:00Z" + }, + "parent": { + "canonical": "./yardsticks", + "name": "yardsticks" + }, + "invariants": [ + "outside AIEN; never linked into AIEN binaries", + "builds against omega bench/prime_race protocol via OMEGA_DIR, never copies it", + "must not be executed outside a quiet-flag window (GPU)" + ], + "whispers": [] +} diff --git a/yardsticks/prime-drag-race-cuda/.gitignore b/yardsticks/prime-drag-race-cuda/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/yardsticks/prime-drag-race-cuda/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/yardsticks/prime-drag-race-cuda/Makefile b/yardsticks/prime-drag-race-cuda/Makefile new file mode 100644 index 0000000..a0af9a6 --- /dev/null +++ b/yardsticks/prime-drag-race-cuda/Makefile @@ -0,0 +1,45 @@ +# Build the CUDA mirror yardstick (host compile only needs nvcc; running needs a quiet GPU). +# Usage: make OMEGA_DIR=/path/to/omega-checkout (needs bench/prime_race/prime_race_impl.{h,c}) +NVCC ?= /usr/local/cuda/bin/nvcc +ARCH ?= sm_121 +CC ?= cc +ifeq ($(OMEGA_DIR),) +$(error OMEGA_DIR is required, e.g. make OMEGA_DIR=$$HOME/workspace/omega) +endif +PRDIR := $(OMEGA_DIR)/bench/prime_race +OMEGA_COMMIT := $(shell git -C $(OMEGA_DIR) rev-parse HEAD) +OMEGA_DIRTY := $(shell git -C $(OMEGA_DIR) status --porcelain -- bench/prime_race | head -c1) +YARD_COMMIT := $(shell git -C $(CURDIR) rev-parse HEAD) +NVCC_VERSION := $(shell $(NVCC) --version | tail -n2 | tr '\n' ' ' | sed 's/ *$$//') +NVCC_FLAGS := -O3 -arch=$(ARCH) -Xcompiler -O3,-mcpu=native +CFLAGS_HOST := -O3 -mcpu=native -Wall -Wextra + +BUILD := build +all: $(BUILD)/prime-race-cuda-mirror + +$(BUILD)/build_info.h: FORCE | $(BUILD) + @printf '#define YS_NVCC_VERSION "%s"\n#define YS_NVCC_FLAGS "%s"\n#define YS_OMEGA_COMMIT "%s%s"\n#define YS_YARD_COMMIT "%s"\n' \ + "$(NVCC_VERSION)" "$(NVCC_FLAGS)" "$(OMEGA_COMMIT)" "$(if $(OMEGA_DIRTY),+dirty)" "$(YARD_COMMIT)" > $@.tmp + @cmp -s $@.tmp $@ && rm $@.tmp || mv $@.tmp $@ + +# build_info.h changes whenever a recorded commit changes, so the baked-in +# PR_SOURCE_COMMIT label can never go stale. +$(BUILD)/prime_race_impl.o: $(PRDIR)/prime_race_impl.c $(PRDIR)/prime_race_impl.h $(BUILD)/build_info.h | $(BUILD) + nice -n 19 $(CC) $(CFLAGS_HOST) -I$(PRDIR) \ + -DPR_SOURCE_COMMIT='"omega=$(OMEGA_COMMIT)$(if $(OMEGA_DIRTY),+dirty);yardsticks=$(YARD_COMMIT)"' \ + -DPR_CC='"$(CC) + nvcc"' -DPR_CFLAGS='"host: $(CFLAGS_HOST); nvcc: $(NVCC_FLAGS)"' -c $< -o $@ + +$(BUILD)/sieve_cuda.o: sieve_cuda.cu $(BUILD)/build_info.h | $(BUILD) + nice -n 19 $(NVCC) $(NVCC_FLAGS) -Xcompiler -Wall,-Wextra -I$(PRDIR) -I$(BUILD) -c $< -o $@ + +$(BUILD)/prime-race-cuda-mirror: $(BUILD)/sieve_cuda.o $(BUILD)/prime_race_impl.o + nice -n 19 $(NVCC) -arch=$(ARCH) $^ -o $@ + +$(BUILD): + mkdir -p $@ + +clean: + rm -rf $(BUILD) + +FORCE: +.PHONY: all clean FORCE diff --git a/yardsticks/prime-drag-race-cuda/README.md b/yardsticks/prime-drag-race-cuda/README.md new file mode 100644 index 0000000..4b02efd --- /dev/null +++ b/yardsticks/prime-drag-race-cuda/README.md @@ -0,0 +1,30 @@ +# prime-drag-race-cuda + +An outside CUDA yardstick for the AIEN Prime Drag Race (a prime sieve speed contest). It runs the same +algorithm as the AIEN native GPU kernel, written in CUDA, so the two can be compared fairly on the same +GB10 chip. It is a yardstick only: it lives outside AIEN and nothing here ships in AIEN. + +## What it does +Each thread owns one 32-bit word of the answer and loops over the odd base primes up to sqrt(limit). No +atomics. The host builds the base primes and their division constants, copies them over, launches, then +copies the whole result back to host memory (the copy is counted in every pass, because the result must be +host-readable). Labels match the native kernel: algorithm=other, faithful=no, bits=1, environment +linux-hosted-gb10-cuda. Kernel-only time (cudaEvents) is a diagnostic in the report, not the score. + +The command line, timing loop, report and bitmap export come from the shared protocol in omega +(`bench/prime_race/prime_race_impl.{h,c}`). This repo does not copy it. The build records the omega commit, +this repo's commit, the nvcc version and the flags inside the binary. + +## Upstream relation +Upstream PrimeCUDA/solution_2 (PlummersSoftwareLLC/Primes, pinned a2899c96752b65ac1b08a0e1418ab7e708040ed0) +splits the work differently. It is reference reading only and was not copied. This program is NONCONFORMING +for upstream submission (different algorithm split, shared protocol harness). + +## Build (compile only, safe anywhere with nvcc) + make OMEGA_DIR=/path/to/omega-checkout +Needs nvcc at /usr/local/cuda/bin (13.0 tested) and `-arch=sm_121` (GB10). Override with `ARCH=...` or `NVCC=...`. + +## Run (uses the GPU: only inside a quiet-flag window) + build/prime-race-cuda-mirror --limit 1000000 --min-seconds 5 --report-out r.json --bitmap-out b.bin + build/prime-race-cuda-mirror --limit 1000000 --audit-passes 5 --bitmap-out audit.bin +Limit 1,000,000 must give 78,498 primes. Limits above 2^33-2 are refused (32-bit indices). diff --git a/yardsticks/prime-drag-race-cuda/sieve_cuda.cu b/yardsticks/prime-drag-race-cuda/sieve_cuda.cu new file mode 100644 index 0000000..7000913 --- /dev/null +++ b/yardsticks/prime-drag-race-cuda/sieve_cuda.cu @@ -0,0 +1,173 @@ +// aien-cuda-mirror: outside CUDA yardstick that mirrors the AIEN native GPU kernel algorithm exactly. +// One thread per 32-bit output word, loop over odd base primes, no atomics. See README.md. +#include + +#include +#include +#include +#include + +extern "C" { +#include "prime_race_impl.h" +} + +#include "build_info.h" + +#define BLOCK 256 + +struct PrimeEnt { uint32_t p, magic, k0; }; + +static __global__ void sieve_words(uint32_t *out, uint32_t nwords, uint32_t odd_count, + const PrimeEnt *primes, uint32_t nprimes) { + uint32_t w = blockIdx.x * blockDim.x + threadIdx.x; + if (w >= nwords) return; + uint32_t base = 32u * w; + uint32_t acc = 0; + for (uint32_t i = 0; i < nprimes; i++) { + PrimeEnt e = primes[i]; + if (e.k0 >= base + 32u) break; + uint32_t j; + if (e.k0 >= base) { + j = e.k0 - base; + } else { + uint32_t d = base - e.k0; + uint32_t q = __umulhi(d, e.magic); + uint32_t r = d - q * e.p; + if ((int32_t)r < 0) r += e.p; // magic = ceil(2^32/p) can overshoot q by one + if (r >= e.p) r -= e.p; + j = (r == 0) ? 0u : e.p - r; + } + while (j < 32u) { acc |= 1u << j; j += e.p; } + } + if (w == 0) acc |= 1u; // number 1 is not prime + uint32_t tail = 0xffffffffu; + if (w == nwords - 1 && (odd_count & 31u)) tail = (1u << (odd_count & 31u)) - 1u; + out[w] = ~acc & tail; +} + +struct State { + uint64_t limit, odd_count; + uint32_t nwords32, nprimes, grid; + uint32_t *d_out; + PrimeEnt *d_primes; + uint32_t *h_out; + PrimeEnt *h_primes; + uint8_t *small; // host small sieve scratch + uint32_t cap_primes; + uint64_t sqrt_limit; + cudaEvent_t ev0, ev1; + double diag_kernel_ns; +}; + +static int cu(cudaError_t e, const char *what) { + if (e != cudaSuccess) { fprintf(stderr, "cuda error in %s: %s\n", what, cudaGetErrorString(e)); return 1; } + return 0; +} + +static uint64_t isqrt64(uint64_t n) { + uint64_t lo = 0, hi = 1ull << 21, r = 0; + while (lo <= hi) { uint64_t m = lo + (hi - lo) / 2; if (m * m <= n) { r = m; lo = m + 1; } else hi = m - 1; } + return r; +} + +static void cuda_teardown(void *vs) { + State *s = (State *)vs; + if (!s) return; + if (s->d_out) cudaFree(s->d_out); + if (s->d_primes) cudaFree(s->d_primes); + if (s->ev0) cudaEventDestroy(s->ev0); + if (s->ev1) cudaEventDestroy(s->ev1); + free(s->h_out); free(s->h_primes); free(s->small); + free(s); +} + +static int cuda_setup(void **state, uint64_t limit) { + if (limit > (1ull << 33) - 2) { fprintf(stderr, "limit too large for 32-bit indices\n"); return 1; } + State *s = (State *)calloc(1, sizeof *s); + if (!s) return 1; + s->limit = limit; + s->odd_count = pr_odd_count(limit); + s->nwords32 = (uint32_t)((s->odd_count + 31) / 32); + s->grid = (s->nwords32 + BLOCK - 1) / BLOCK; + s->sqrt_limit = isqrt64(limit); + s->cap_primes = (uint32_t)(s->sqrt_limit / 2 + 2); + size_t nw = s->nwords32 ? s->nwords32 : 1; + s->h_out = (uint32_t *)calloc(nw, 4); + s->h_primes = (PrimeEnt *)calloc(s->cap_primes, sizeof(PrimeEnt)); + s->small = (uint8_t *)malloc(s->sqrt_limit + 2); + *state = s; + if (!s->h_out || !s->h_primes || !s->small) return 1; + if (cu(cudaSetDevice(0), "cudaSetDevice")) return 1; + if (cu(cudaMalloc(&s->d_out, nw * 4), "cudaMalloc out")) return 1; + if (cu(cudaMalloc(&s->d_primes, s->cap_primes * sizeof(PrimeEnt)), "cudaMalloc primes")) return 1; + if (cu(cudaEventCreate(&s->ev0), "event0") || cu(cudaEventCreate(&s->ev1), "event1")) return 1; + return 0; +} + +static int cuda_pass(void *vs, uint64_t limit) { + State *s = (State *)vs; + (void)limit; + // timed work: host odd primes <= isqrt(limit) plus division magics, then H2D, launch, D2H + uint64_t sq = s->sqrt_limit; + memset(s->small, 1, sq + 1); + uint32_t np = 0; + for (uint64_t p = 3; p <= sq; p += 2) { + if (!s->small[p]) continue; + for (uint64_t m = p * p; m <= sq; m += 2 * p) s->small[m] = 0; + s->h_primes[np].p = (uint32_t)p; + s->h_primes[np].magic = (uint32_t)(((1ull << 32) + p - 1) / p); + s->h_primes[np].k0 = (uint32_t)((p * p - 1) / 2); + np++; + } + s->nprimes = np; + if (s->nwords32 == 0) return 0; + if (np && cu(cudaMemcpy(s->d_primes, s->h_primes, np * sizeof(PrimeEnt), cudaMemcpyHostToDevice), "H2D primes")) return 1; + if (cu(cudaEventRecord(s->ev0), "ev0")) return 1; + sieve_words<<grid, BLOCK>>>(s->d_out, s->nwords32, (uint32_t)s->odd_count, s->d_primes, np); + if (cu(cudaGetLastError(), "kernel launch")) return 1; + if (cu(cudaEventRecord(s->ev1), "ev1")) return 1; + // result must be host-resident: the D2H copy is counted in the pass + if (cu(cudaMemcpy(s->h_out, s->d_out, (size_t)s->nwords32 * 4, cudaMemcpyDeviceToHost), "D2H out")) return 1; + if (cu(cudaDeviceSynchronize(), "sync")) return 1; + if (cu(cudaGetLastError(), "post-run")) return 1; + float ms = 0; + if (cu(cudaEventElapsedTime(&ms, s->ev0, s->ev1), "elapsed")) return 1; + s->diag_kernel_ns += (double)ms * 1e6; + return 0; +} + +static int cuda_export(void *vs, uint64_t limit, uint64_t *out) { + State *s = (State *)vs; + size_t n64 = pr_words64(limit); + for (size_t i = 0; i < n64; i++) { + uint64_t lo = s->h_out[2 * i]; + uint64_t hi = (2 * i + 1 < s->nwords32) ? s->h_out[2 * i + 1] : 0; + out[i] = lo | (hi << 32); + } + return 0; +} + +static uint64_t cuda_threads(void *vs) { State *s = (State *)vs; return (uint64_t)s->grid * BLOCK; } + +static int cuda_detail(void *vs, FILE *f) { + State *s = (State *)vs; + int rt = 0, drv = 0; + cudaRuntimeGetVersion(&rt); + cudaDriverGetVersion(&drv); + fprintf(f, "{\"grid\":%u,\"block\":%d,\"words32\":%u,\"nprimes\":%u," + "\"cuda_runtime\":%d,\"cuda_driver\":%d," + "\"diag_kernel_ns_total\":%.0f,\"diag_kernel_ns_note\":\"sum of cudaEvent kernel-only times, diagnostic, not the score\"," + "\"result_copy\":\"D2H of full output counted in every pass\"," + "\"nvcc_version\":\"%s\",\"nvcc_flags\":\"%s\",\"omega_commit\":\"%s\",\"yardsticks_commit\":\"%s\"}", + s->grid, BLOCK, s->nwords32, s->nprimes, rt, drv, s->diag_kernel_ns, + YS_NVCC_VERSION, YS_NVCC_FLAGS, YS_OMEGA_COMMIT, YS_YARD_COMMIT); + return 0; +} + +int main(int argc, char **argv) { + static const pr_impl impl = { + "cuda-mirror", "other", "no", 1, "linux-hosted-gb10-cuda", + cuda_setup, cuda_pass, cuda_export, cuda_threads, cuda_detail, cuda_teardown, + }; + return pr_main(argc, argv, &impl); +} ```