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
omega-mv / src / omega_gpu_scan_ref.c
| 1 | /* omega_gpu_scan_ref.c -- the host references of omega_gpu_scan.h: the frozen order, in plain C on the CPU. |
| 2 | * Separate from the chip code so a CPU-only program can check them against the E1 reduction reference |
| 3 | * (tests/gpu_scan_ref_check.c). Additions only, so no fused multiply-add can change a bit. */ |
| 4 | #include "omega_gpu_scan.h" |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | #define PAD_SUM 0x80000000u /* -0.0 */ |
| 10 | #define PAD_MINMAX 0x7fc00000u /* quiet NaN */ |
| 11 | |
| 12 | |
| 13 | static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } |
| 14 | |
| 15 | static float combine(OmegaGpuRedOp op, float a, float b) { |
| 16 | if (op == OMEGA_GPU_RED_SUM) return a + b; |
| 17 | return op == OMEGA_GPU_RED_MAX ? __builtin_fmaxf(a, b) : __builtin_fminf(a, b); /* AArch64 FMAXNM / FMINNM: NaN ignored */ |
| 18 | } |
| 19 | |
| 20 | int omega_gpu_reduce_reference(OmegaGpuRedOp op, const float *x, size_t n, float *out) { |
| 21 | if ((unsigned)op > OMEGA_GPU_RED_MIN || !x || !out || n == 0) return -1; |
| 22 | const float pad = u2f(op == OMEGA_GPU_RED_SUM ? PAD_SUM : PAD_MINMAX); |
| 23 | float *cur = NULL; |
| 24 | size_t len = n; |
| 25 | const float *in = x; |
| 26 | do { |
| 27 | size_t nt = (len + 31) / 32; |
| 28 | float *next = malloc(nt * sizeof *next); |
| 29 | if (!next) { free(cur); return -1; } |
| 30 | for (size_t j = 0; j < nt; j++) { |
| 31 | float v[32]; |
| 32 | for (size_t i = 0; i < 32; i++) v[i] = j * 32 + i < len ? in[j * 32 + i] : pad; |
| 33 | for (size_t d = 16; d >= 1; d /= 2) |
| 34 | for (size_t i = 0; i + d < 32; i++) v[i] = combine(op, v[i], v[i + d]); /* ascending: v[i + d] is still old */ |
| 35 | next[j] = v[0]; |
| 36 | } |
| 37 | free(cur); |
| 38 | cur = next; in = next; len = nt; |
| 39 | } while (len > 1); |
| 40 | *out = cur[0]; |
| 41 | free(cur); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /* one scan level of len values: out (may alias in) and the tile totals */ |
| 46 | static void ref_scan_level(const float *in, float *out, float *tot, size_t len) { |
| 47 | for (size_t j = 0; j * 32 < len; j++) { |
| 48 | float v[32]; |
| 49 | for (size_t i = 0; i < 32; i++) v[i] = j * 32 + i < len ? in[j * 32 + i] : u2f(PAD_SUM); |
| 50 | for (size_t d = 1; d <= 16; d *= 2) |
| 51 | for (size_t i = 31; i >= d; i--) v[i] = v[i - d] + v[i]; /* descending: v[i - d] is still old */ |
| 52 | for (size_t i = 0; i < 32 && j * 32 + i < len; i++) out[j * 32 + i] = v[i]; |
| 53 | tot[j] = v[31]; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static int ref_prefix(const float *in, float *out, size_t len) { |
| 58 | size_t nt = (len + 31) / 32; |
| 59 | float *tot = malloc(nt * sizeof *tot); |
| 60 | if (!tot) return -1; |
| 61 | ref_scan_level(in, out, tot, len); |
| 62 | int rc = 0; |
| 63 | if (nt > 1) { |
| 64 | rc = ref_prefix(tot, tot, nt); |
| 65 | for (size_t e = 32; rc == 0 && e < len; e++) out[e] = tot[e / 32 - 1] + out[e]; |
| 66 | } |
| 67 | free(tot); |
| 68 | return rc; |
| 69 | } |
| 70 | |
| 71 | int omega_gpu_prefix_sum_reference(const float *x, size_t n, float *y) { |
| 72 | if (!x || !y || n == 0) return -1; |
| 73 | return ref_prefix(x, y, n); |
| 74 | } |
| 75 |