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_numeric_reduce.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_numeric_reduce.c` 282 lines · 11562 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 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 /* 18 * E1 WP-D: general reductions with a frozen order. Reference tier and CPU 19 * realization tier. Host only; opens no device. See omega_numeric_reduce.h 20 * and docs/numeric/E1_REDUCTION_CONTRACT.md. 21 */ 22 #include "omega_numeric_reduce.h" 23 #include "omega_numeric.h" 24 #include "omega_numeric_om.h" 25 26 #include 27 #include 28 #include 29 #include "cudapp_native.h" 30 31 #if defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_REST_DIFF) 32 #define omega_reduce_identity numeric_rest_c_reduce_identity 33 #define omega_reduce_reference numeric_rest_c_reduce_reference 34 #define omega_reduce_cpu numeric_rest_c_reduce_cpu 35 #define omega_reduce_rows_reference numeric_rest_c_reduce_rows_reference 36 #define omega_reduce_rows_cpu numeric_rest_c_reduce_rows_cpu 37 #define omega_reduce_sequential_sum_not_contract numeric_rest_c_reduce_sequential_sum_not_contract 38 #endif 39 40 #if defined(__x86_64__) 41 #include 42 #elif !defined(__aarch64__) 43 #error "E1 reduction CPU tier supports AArch64 and x86-64" 44 #endif 45 46 static const char *const RED_NAMES[OMEGA_RED_COUNT] = { "SUM", "MAX", "MIN", "MEAN" }; 47 48 const char *omega_reduce_op_name(OmegaReduceOp op) { 49 return ((unsigned)op < OMEGA_RED_COUNT) ? RED_NAMES[op] : NULL; 50 } 51 52 #if defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_REST) || defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_REST_DIFF) 53 float omega_reduce_identity(OmegaReduceOp op) { 54 if (op == OMEGA_RED_MAX || op == OMEGA_RED_MIN) return omega_bits_to_float(OMEGA_REDUCE_MINMAX_PAD_BITS); 55 return omega_bits_to_float(OMEGA_REDUCE_SUM_PAD_BITS); 56 } 57 #endif 58 59 unsigned omega_reduce_levels(size_t n) { 60 #if defined(OMEGA_NUMERIC_C_BODIES) 61 unsigned l = 0; 62 if (n != 0) { 63 l = 1; 64 size_t cap = 32; 65 while (cap < n) { cap = (cap > ((size_t)-1) / 32) ? (size_t)-1 : cap * 32; l++; } 66 } 67 #endif 68 #if defined(OMEGA_NUMERIC_OM_PATH) 69 unsigned om = (unsigned)omega_numeric_om(OMEGA_NUMERIC_OM_REDUCE_LEVELS, n, 0, 0); 70 #if defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_DIFF) 71 omega_numeric_om_diff("reduce_levels", n, 0, 0, l, om); 72 #endif 73 return om; 74 #else 75 return l; 76 #endif 77 } 78 79 #if defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_REST) || defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_REST_DIFF) 80 static float empty_result(OmegaReduceOp op) { 81 return omega_bits_to_float(op == OMEGA_RED_SUM ? OMEGA_REDUCE_EMPTY_SUM_BITS : OMEGA_REDUCE_EMPTY_OTHER_BITS); 82 } 83 84 static int common_checks(OmegaReduceOp op, const float *x, size_t n, const float *out) { 85 if ((unsigned)op >= OMEGA_RED_COUNT || !out || (n > 0 && !x)) return OMEGA_NUMERIC_ERR_BAD_ARGS; 86 if (op == OMEGA_RED_MEAN && n > OMEGA_REDUCE_MEAN_MAX_N) return OMEGA_NUMERIC_ERR_OPERANDS; 87 if (n > ((size_t)-1) / sizeof(float) - 64) return OMEGA_NUMERIC_ERR_OPERANDS; /* padding must not wrap */ 88 if (!omega_numeric_fpenv_ok()) return OMEGA_NUMERIC_ERR_FPENV; 89 return OMEGA_NUMERIC_OK; 90 } 91 92 /* ---- Reference tier ------------------------------------------------------ */ 93 94 /* The lane tree of omega_warp_reduce_sum with the combine of op. SUM calls 95 * omega_warp_reduce_sum itself, so the tile order is the declared warp order. */ 96 static float ref_tile(OmegaReduceOp op, const float tile[32]) { 97 if (op == OMEGA_RED_SUM || op == OMEGA_RED_MEAN) return omega_warp_reduce_sum(tile); 98 #if defined(OMEGA_NUMERIC_OM_PATH) 99 uint32_t bits[32]; 100 memcpy(bits, tile, sizeof(bits)); 101 uint32_t om = omega_numeric_om_tile((unsigned)op, bits); 102 #if !defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_DIFF) 103 return omega_bits_to_float(om); 104 #endif 105 #endif 106 #if defined(OMEGA_NUMERIC_C_BODIES) 107 float val[32]; 108 memcpy(val, tile, sizeof(val)); 109 static const int deltas[5] = { 16, 8, 4, 2, 1 }; 110 for (int s = 0; s < 5; s++) { 111 int d = deltas[s]; 112 for (int i = 0; i + d < 32; i++) 113 val[i] = (op == OMEGA_RED_MAX) ? omega_ref_fmax(val[i], val[i + d]) : omega_ref_fmin(val[i], val[i + d]); 114 } 115 #if defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_DIFF) 116 omega_numeric_om_diff("reduce_tile", op, bits[0], bits[31], omega_float_to_bits(val[0]), om); 117 #endif 118 return val[0]; 119 #endif 120 } 121 122 static int ref_sum_like(OmegaReduceOp op, const float *x, size_t n, float *out) { 123 if (op == OMEGA_RED_MEAN) op = OMEGA_RED_SUM; /* MEAN reduces like SUM; the division is the caller's */ 124 if (n == 0) { *out = empty_result(op); return OMEGA_NUMERIC_OK; } 125 const float pad = omega_reduce_identity(op); 126 size_t len = n; 127 const float *cur = x; 128 float *owned = NULL; 129 do { 130 size_t next_len = (len + 31) / 32; 131 float *next = malloc(next_len * sizeof(float)); 132 if (!next) { free(owned); return OMEGA_NUMERIC_ERR_BAD_ARGS; } 133 for (size_t j = 0; j < next_len; j++) { 134 float tile[32]; 135 for (size_t k = 0; k < 32; k++) { 136 size_t idx = j * 32 + k; 137 tile[k] = (idx < len) ? cur[idx] : pad; 138 } 139 next[j] = ref_tile(op, tile); 140 } 141 free(owned); 142 owned = next; 143 cur = next; 144 len = next_len; 145 } while (len > 1); 146 *out = cur[0]; 147 free(owned); 148 return OMEGA_NUMERIC_OK; 149 } 150 151 int omega_reduce_reference(OmegaReduceOp op, const float *x, size_t n, float *out) { 152 int rc = common_checks(op, x, n, out); 153 if (rc != OMEGA_NUMERIC_OK) return rc; 154 if (op != OMEGA_RED_MEAN) return ref_sum_like(op, x, n, out); 155 if (n == 0) { *out = empty_result(op); return OMEGA_NUMERIC_OK; } 156 float s; 157 rc = ref_sum_like(OMEGA_RED_SUM, x, n, &s); 158 if (rc != OMEGA_NUMERIC_OK) return rc; 159 *out = omega_math_div(s, omega_ref_u2f((uint32_t)n)); 160 return OMEGA_NUMERIC_OK; 161 } 162 163 /* ---- CPU realization tier ------------------------------------------------- */ 164 165 static inline float cpu_combine(OmegaReduceOp op, float a, float b) { 166 #if defined(__aarch64__) 167 float r; 168 if (op == OMEGA_RED_SUM) { 169 __asm__ volatile("fadd %s0, %s1, %s2" : "=w"(r) : "w"(a), "w"(b)); 170 return r; 171 } 172 /* FMAXNM/FMINNM return NaN for a signaling NaN; Omega treats every NaN as 173 * missing data, so quiet signaling NaNs first (as the FMNMX CPU tier does). */ 174 float qa = omega_isnan(a) ? omega_bits_to_float(omega_float_to_bits(a) | 0x00400000U) : a; 175 float qb = omega_isnan(b) ? omega_bits_to_float(omega_float_to_bits(b) | 0x00400000U) : b; 176 if (op == OMEGA_RED_MAX) __asm__ volatile("fmaxnm %s0, %s1, %s2" : "=w"(r) : "w"(qa), "w"(qb)); 177 else __asm__ volatile("fminnm %s0, %s1, %s2" : "=w"(r) : "w"(qa), "w"(qb)); 178 return r; 179 #else 180 if (op == OMEGA_RED_SUM) 181 return _mm_cvtss_f32(_mm_add_ss(_mm_set_ss(a), _mm_set_ss(b))); 182 /* SSE MINSS/MAXSS do not implement minimumNumber for NaN and signed 183 * zero. Spell that contract explicitly rather than using those opcodes. */ 184 if (omega_isnan(a)) return b; 185 if (omega_isnan(b)) return a; 186 if (a == 0.0f && b == 0.0f) { 187 uint32_t ua = omega_float_to_bits(a), ub = omega_float_to_bits(b); 188 uint32_t sign = op == OMEGA_RED_MAX ? (ua & ub) : (ua | ub); 189 return omega_bits_to_float(sign & 0x80000000u); 190 } 191 return op == OMEGA_RED_MAX ? (a > b ? a : b) : (a < b ? a : b); 192 #endif 193 } 194 195 /* One buffer, compacted in place: tile j's result goes to buf[j] (j <= 32j). */ 196 static int cpu_sum_like(OmegaReduceOp op, const float *x, size_t n, float *out) { 197 if (op == OMEGA_RED_MEAN) op = OMEGA_RED_SUM; /* MEAN reduces like SUM; the division is the caller's */ 198 if (n == 0) { *out = empty_result(op); return OMEGA_NUMERIC_OK; } 199 size_t cap = (n + 31) & ~(size_t)31; 200 float *buf = malloc(cap * sizeof(float)); 201 if (!buf) return OMEGA_NUMERIC_ERR_BAD_ARGS; 202 memcpy(buf, x, n * sizeof(float)); 203 const uint32_t pad_bits = (op == OMEGA_RED_SUM) ? OMEGA_REDUCE_SUM_PAD_BITS : OMEGA_REDUCE_MINMAX_PAD_BITS; 204 size_t len = n; 205 for (;;) { 206 size_t padded = (len + 31) & ~(size_t)31; 207 for (size_t i = len; i < padded; i++) buf[i] = omega_bits_to_float(pad_bits); 208 size_t tiles = padded / 32; 209 for (size_t j = 0; j < tiles; j++) { 210 float *t = buf + j * 32; 211 for (unsigned d = 16; d >= 1; d >>= 1) 212 for (unsigned i = 0; i < d; i++) t[i] = cpu_combine(op, t[i], t[i + d]); 213 buf[j] = t[0]; 214 } 215 len = tiles; 216 if (len == 1) break; 217 } 218 *out = buf[0]; 219 free(buf); 220 return OMEGA_NUMERIC_OK; 221 } 222 223 int omega_reduce_cpu(OmegaReduceOp op, const float *x, size_t n, float *out) { 224 int rc = common_checks(op, x, n, out); 225 if (rc != OMEGA_NUMERIC_OK) return rc; 226 if (op != OMEGA_RED_MEAN) return cpu_sum_like(op, x, n, out); 227 if (n == 0) { *out = empty_result(op); return OMEGA_NUMERIC_OK; } 228 float s, fn, r; 229 rc = cpu_sum_like(OMEGA_RED_SUM, x, n, &s); 230 if (rc != OMEGA_NUMERIC_OK) return rc; 231 uint32_t n32 = (uint32_t)n; 232 #if defined(__aarch64__) 233 __asm__ volatile("ucvtf %s0, %w1" : "=w"(fn) : "r"(n32)); 234 __asm__ volatile("fdiv %s0, %s1, %s2" : "=w"(r) : "w"(s), "w"(fn)); 235 #else 236 fn = (float)n32; 237 r = _mm_cvtss_f32(_mm_div_ss(_mm_set_ss(s), _mm_set_ss(fn))); 238 #endif 239 *out = r; 240 return OMEGA_NUMERIC_OK; 241 } 242 243 /* ---- Batched (last axis) -------------------------------------------------- */ 244 245 static int rows_common(OmegaReduceOp op, const float *x, size_t rows, size_t n, size_t row_stride, 246 const float *out) { 247 if ((unsigned)op >= OMEGA_RED_COUNT || (rows > 0 && !out) || (rows > 0 && n > 0 && !x)) 248 return OMEGA_NUMERIC_ERR_BAD_ARGS; 249 if (rows > 1 && row_stride < n) return OMEGA_NUMERIC_ERR_OPERANDS; 250 if (rows > 0 && n > 0) { 251 /* last element index (rows-1)*row_stride + n-1 and its byte offset must not wrap */ 252 size_t lim = SIZE_MAX / sizeof(float); 253 if (rows > 1 && row_stride > (lim - n) / (rows - 1)) return OMEGA_NUMERIC_ERR_OPERANDS; 254 if (n > lim) return OMEGA_NUMERIC_ERR_OPERANDS; 255 } 256 return OMEGA_NUMERIC_OK; 257 } 258 259 int omega_reduce_rows_reference(OmegaReduceOp op, const float *x, size_t rows, size_t n, 260 size_t row_stride, float *out) { 261 int rc = rows_common(op, x, rows, n, row_stride, out); 262 for (size_t r = 0; rc == OMEGA_NUMERIC_OK && r < rows; r++) 263 rc = omega_reduce_reference(op, n ? x + r * row_stride : NULL, n, &out[r]); 264 return rc; 265 } 266 267 int omega_reduce_rows_cpu(OmegaReduceOp op, const float *x, size_t rows, size_t n, 268 size_t row_stride, float *out) { 269 int rc = rows_common(op, x, rows, n, row_stride, out); 270 for (size_t r = 0; rc == OMEGA_NUMERIC_OK && r < rows; r++) 271 rc = omega_reduce_cpu(op, n ? x + r * row_stride : NULL, n, &out[r]); 272 return rc; 273 } 274 275 float omega_reduce_sequential_sum_not_contract(const float *x, size_t n) { 276 float s = omega_bits_to_float(OMEGA_REDUCE_SUM_PAD_BITS); 277 for (size_t i = 0; i < n; i++) s = s + x[i]; 278 return n ? s : 0.0f; 279 } 280 #endif 281 282 #include "omega_numeric_reduce_rest.inc"