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/src/omega_gpu_scan.h # 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 / src / omega_gpu_scan.h` 110 lines ยท 6626 bytes Raw Blame History 1 /* omega_gpu_scan.h -- reductions and prefix sums of any length on the GB10 (row C3 of docs/OMEGA.md). 2 * 3 * The sum, the maximum and the minimum of n float values, and the inclusive prefix sum (the running total 4 * y[i] = x[0] + ... + x[i]), for every n from 1 to 2^28. The order of the additions is frozen: it depends only on n 5 * and the element index, never on the grid, the scheduling or the chip, so the chip result equals the host 6 * reference below bit for bit. 7 * 8 * Reductions (the order of docs/numeric/E1_REDUCTION_CONTRACT.md, omega_numeric_reduce.h): one level turns len values 9 * into ceil(len / 32) values; value j is the 32-lane tree over values [32j, 32j + 32) with lane i combining lane i + d 10 * for d = 16, 8, 4, 2, 1 (lower lane on the left), positions past len filled with the identity (-0.0 for the sum, a 11 * quiet NaN for the maximum and minimum, which those operations ignore). Levels repeat until one value is left; at 12 * least one level runs. 13 * 14 * Prefix sum, same tile structure: 15 * level scan inside each tile of 32 (positions past len filled with -0.0), for d = 1, 2, 4, 8, 16 in that order, 16 * every position i >= d takes v[i - d] + v[i] (both from before the step); the tile total T[j] is the 17 * value at position 31. 18 * carry when there is more than one tile, the totals T are prefix-summed by the same definition (recursively) 19 * into S, and every element of tile j >= 1 becomes S[j - 1] + (its tile value). 20 * Inputs are any float values. Results with no NaN are bit-exact; a NaN result is a NaN (the chip writes one canonical 21 * NaN, the host may keep another payload). 22 * 23 * On the chip each level is one launch of a kernel from Omega's own codegen (no CUDA): the reduction tree and the 24 * tile scan are warp shuffles (SHFL.DOWN; the scan holds each tile in reversed lane order so a shuffle down reads the 25 * lower element), the carry is a separate pass. omega_gpu_scan_drive_* run the levels through a launch callback, so 26 * the same driver runs on the chip (omega_gpu_reduce, omega_gpu_prefix_sum) and in the warp simulator 27 * (tests/gpu_scan_test.c). */ 28 #ifndef OMEGA_GPU_SCAN_H 29 #define OMEGA_GPU_SCAN_H 30 31 #include 32 #include 33 #include "omega_blackwell_codegen.h" 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 #define OMEGA_GPU_SCAN_MAX_N ((size_t)1 << 28) 40 #define OMEGA_GPU_SCAN_THREADS 256u /* 8 warps per CTA; each warp walks tiles gw, gw + warps, ... */ 41 #define OMEGA_GPU_SCAN_MAX_CTAS 1024u 42 43 enum { 44 OMEGA_GPU_SCAN_OK = 0, 45 OMEGA_GPU_SCAN_BAD_ARGS = -1, /* NULL pointer, n == 0, unknown op */ 46 OMEGA_GPU_SCAN_TOO_LARGE = -2, /* n > OMEGA_GPU_SCAN_MAX_N */ 47 OMEGA_GPU_SCAN_CODEGEN_FAIL = -3, /* our codegen refused a kernel */ 48 OMEGA_GPU_SCAN_CHIP_FAIL = -4 /* device, allocation, launch, or an output word the chip never wrote */ 49 }; 50 51 typedef enum { OMEGA_GPU_RED_SUM = 0, OMEGA_GPU_RED_MAX = 1, OMEGA_GPU_RED_MIN = 2 } OmegaGpuRedOp; 52 53 /* The kernels. Argument words at constant bank 0x380 (OMEGA_GPU_SCAN_A_*). */ 54 typedef enum { 55 OMEGA_SCAN_K_RED_SUM = 0, OMEGA_SCAN_K_RED_MAX, OMEGA_SCAN_K_RED_MIN, /* one reduction level: in -> out[tile] */ 56 OMEGA_SCAN_K_TILE, /* one scan level: in -> out (may alias), tot[tile] */ 57 OMEGA_SCAN_K_ADD, /* carry: out[e] = S[tile - 1] + out[e], tiles >= 1 */ 58 OMEGA_SCAN_K_COUNT 59 } OmegaScanKind; 60 #define OMEGA_GPU_SCAN_A_IN 0x380u /* u64: level input (K_ADD: the level being carried into) */ 61 #define OMEGA_GPU_SCAN_A_OUT 0x388u /* u64: level output (K_ADD: S, the scanned totals) */ 62 #define OMEGA_GPU_SCAN_A_TOT 0x390u /* u64: tile totals (K_TILE only) */ 63 #define OMEGA_GPU_SCAN_A_LEN 0x398u /* u32: values in this level */ 64 #define OMEGA_GPU_SCAN_A_TILES 0x39cu /* u32: ceil(len / 32) */ 65 #define OMEGA_GPU_SCAN_A_WARPS 0x3a0u /* u32: warps in the grid (the tile stride) */ 66 #define OMEGA_GPU_SCAN_ARGS_WORDS 9u 67 68 /* Mutants for the red checks (each must change some result): 1 the reduction tree skips d = 1, 2 the tile scan skips 69 * d = 16, 3 the carry adds S[tile] instead of S[tile - 1]. 0 = the real kernels. */ 70 typedef struct { OmegaScanKind kind; int mutant; } OmegaScanSpec; 71 72 /* Builds the IR program of one kernel and allocates its registers; 0 or -1. */ 73 int omega_gpu_scan_codegen(const OmegaScanSpec *spec, BlackwellIRProgram *prog); 74 75 /* Host references, the frozen order above. 0, or -1 for bad arguments or out of memory. */ 76 int omega_gpu_reduce_reference(OmegaGpuRedOp op, const float *x, size_t n, float *out); 77 int omega_gpu_prefix_sum_reference(const float *x, size_t n, float *y); 78 79 /* The level driver. Device addresses are plain numbers (host pointers in the simulator). launch runs one kernel over 80 * grid_x CTAs of OMEGA_GPU_SCAN_THREADS threads with the given argument words and returns 0 when it completed. */ 81 typedef int (*OmegaScanLaunchFn)(void *ctx, OmegaScanKind kind, const uint32_t args[OMEGA_GPU_SCAN_ARGS_WORDS], uint32_t grid_x); 82 /* Scratch words a call needs beyond its input and output. */ 83 size_t omega_gpu_scan_reduce_scratch_words(size_t n); 84 size_t omega_gpu_scan_prefix_scratch_words(size_t n); 85 /* Reduction: the result lands in the scratch word the function returns through *out_va. */ 86 int omega_gpu_scan_drive_reduce(OmegaGpuRedOp op, size_t n, uint64_t in_va, uint64_t scratch_va, uint64_t *out_va, 87 OmegaScanLaunchFn launch, void *ctx, uint32_t *launches); 88 /* Prefix sum of in into out (out may equal in). */ 89 int omega_gpu_scan_drive_prefix(size_t n, uint64_t in_va, uint64_t out_va, uint64_t scratch_va, 90 OmegaScanLaunchFn launch, void *ctx, uint32_t *launches); 91 /* CTAs for a level of len values. */ 92 uint32_t omega_gpu_scan_grid(size_t len); 93 94 typedef struct { 95 uint32_t launches; /* kernels run */ 96 uint64_t elapsed_ns; /* sum of launch-to-completion over the launches */ 97 uint64_t call_ns; /* the whole call, uploads and readback included */ 98 } OmegaGpuScanInfo; 99 100 /* On the chip. x is read, out / y written; nothing else is kept between calls except the kernels. */ 101 int omega_gpu_reduce(OmegaGpuRedOp op, const float *x, size_t n, float *out, OmegaGpuScanInfo *info); 102 int omega_gpu_prefix_sum(const float *x, size_t n, float *y, OmegaGpuScanInfo *info); 103 /* Red checks: every later call builds its kernels with this mutant (clears the kernel cache). */ 104 void omega_gpu_scan_test_set_mutant(int mutant); 105 const char *omega_gpu_scan_rc_name(int rc); 106 107 #ifdef __cplusplus 108 } 109 #endif 110 #endif /* OMEGA_GPU_SCAN_H */