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/CUDApp/blob/main/src/omega_gpu_scan_ref.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_gpu_scan_ref.c` 99 lines · 3736 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* omega_gpu_scan_ref.c -- the host references of omega_gpu_scan.h: the frozen order, in plain C on the CPU. 3 * Separate from the chip code so a CPU-only program can check them against the E1 reduction reference 4 * (tests/gpu_scan_ref_check.c). Additions only, so no fused multiply-add can change a bit. */ 5 #include "omega_gpu_scan.h" 6 7 #include 8 #include 9 #include "omega_gpu_apiviews.h" 10 11 int omega_gpu_reduce_reference(OmegaGpuRedOp op, const float *x, size_t n, float *out) { 12 if ((unsigned)op > OMEGA_GPU_RED_MIN || !x || !out || n == 0) return -1; 13 float *cur = NULL; 14 size_t len = n; 15 const float *in = x; 16 do { 17 size_t nt = (len + 31) / 32; 18 float *next = malloc(nt * sizeof *next); 19 if (!next) { free(cur); return -1; } 20 for (size_t j = 0; j < nt; j++) { 21 float v[32]; 22 size_t live = len - j * 32 < 32 ? len - j * 32 : 32; 23 if (av_scan_tile(op, live, in + j * 32, v)) { free(next); free(cur); return -1; } 24 next[j] = v[0]; 25 } 26 free(cur); 27 cur = next; in = next; len = nt; 28 } while (len > 1); 29 *out = cur[0]; 30 free(cur); 31 return 0; 32 } 33 34 /* one scan level of len values: out (may alias in) and the tile totals */ 35 static void ref_scan_level(const float *in, float *out, float *tot, size_t len) { 36 for (size_t j = 0; j * 32 < len; j++) { 37 float v[32]; 38 size_t live = len - j * 32 < 32 ? len - j * 32 : 32; 39 if (av_scan_tile(3, live, in + j * 32, v)) return; 40 for (size_t i = 0; i < 32 && j * 32 + i < len; i++) out[j * 32 + i] = v[i]; 41 tot[j] = v[31]; 42 } 43 } 44 45 static int ref_prefix(const float *in, float *out, size_t len) { 46 size_t nt = (len + 31) / 32; 47 float *tot = malloc(nt * sizeof *tot); 48 if (!tot) return -1; 49 ref_scan_level(in, out, tot, len); 50 int rc = 0; 51 if (nt > 1) { 52 rc = ref_prefix(tot, tot, nt); 53 #ifdef CUDAPP_MIGRATION_PENDING_C_SCAN_REFERENCE_REST 54 for (size_t e = 32; rc == 0 && e < len; e++) out[e] = tot[e / 32 - 1] + out[e]; 55 #else 56 if (rc == 0) { 57 uint64_t status = 0; 58 if (omega_ar_carry(&status, tot, nt, out, len) != OMEGA_OK || status != 0) rc = -1; 59 } 60 #endif 61 } 62 free(tot); 63 return rc; 64 } 65 66 int omega_gpu_prefix_sum_reference(const float *x, size_t n, float *y) { 67 if (!x || !y || n == 0) return -1; 68 return ref_prefix(x, y, n); 69 } 70 71 72 /* u32 additions wrap modulo 2^32 and are associative, so the running total equals the tile order bit for bit */ 73 int omega_gpu_prefix_sum_u32_reference(const uint32_t *x, size_t n, uint32_t *y) { 74 if (!x || !y || n == 0) return -1; 75 uint32_t s = 0; 76 for (size_t i = 0; i < n; i++) { s += x[i]; y[i] = s; } 77 return 0; 78 } 79 80 /* least significant digit first, 4-bit digits, a stable counting pass per digit: the one sorted order */ 81 int omega_gpu_radix_sort_reference(uint32_t *keys, uint32_t *vals, size_t n) { 82 if (!keys || n == 0) return -1; 83 uint32_t *tk = malloc(n * sizeof *tk), *tv = vals ? malloc(n * sizeof *tv) : NULL; 84 if (!tk || (vals && !tv)) { free(tk); free(tv); return -1; } 85 for (unsigned sh = 0; sh < 32; sh += 4) { 86 size_t at[16] = { 0 }; 87 for (size_t i = 0; i < n; i++) at[(keys[i] >> sh) & 15u]++; 88 for (size_t d = 0, s = 0; d < 16; d++) { size_t c = at[d]; at[d] = s; s += c; } 89 for (size_t i = 0; i < n; i++) { 90 size_t to = at[(keys[i] >> sh) & 15u]++; 91 tk[to] = keys[i]; 92 if (vals) tv[to] = vals[i]; 93 } 94 memcpy(keys, tk, n * sizeof *tk); 95 if (vals) memcpy(vals, tv, n * sizeof *tv); 96 } 97 free(tk); free(tv); 98 return 0; 99 }