interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_numeric_reduce.h

133 lines · 6624 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#ifndef OMEGA_NUMERIC_REDUCE_H
18#define OMEGA_NUMERIC_REDUCE_H
19
20/*
21 * E1 WP-D: general FP32 reductions with a frozen summation order
22 * (docs/numeric/E1_REDUCTION_CONTRACT.md).
23 *
24 * The order depends only on n and the element index, never on scheduling,
25 * thread count or CTA count:
26 *
27 * level 0 values are x[0..n).
28 * One level turns len values into ceil(len/32) values: value j of the next
29 * level is the 32-lane warp tree (OMEGA_WARP_REDUCTION_DECLARED_ORDER,
30 * lane i combines with lane i+d for d = 16, 8, 4, 2, 1, lower lane on the
31 * left) over values [32j, 32j+32) of this level, positions >= len filled
32 * with the identity of the op.
33 * At least one level is applied; levels repeat until one value is left.
34 * The number of levels is the smallest L >= 1 with 32^L >= n.
35 *
36 * Every level is a pure tile-of-32 tree, so the order is exactly what the
37 * chip-proven REDUCE_SUM warp kernel computes per tile, and what a future
38 * 1024-thread CTA (32 warps: warp tree, then warp 0 tree over the 32 warp
39 * results via shared memory) would compute for two levels.
40 *
41 * Build with -ffp-contract=off.
42 */
43
44#include <stdbool.h>
45#include <stddef.h>
46#include <stdint.h>
47
48#include "omega_numeric.h"
49
50#define OMEGA_REDUCE_DECLARED_ORDER \
51 "RECURSIVE_TILE32_PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1_PAD_IDENTITY_MIN_ONE_LEVEL"
52
53typedef enum {
54 OMEGA_RED_SUM = 0, /* IEEE fadd, RNE, subnormals kept; identity -0.0 */
55 OMEGA_RED_MAX, /* FMNMX_MAX rule: NaN is missing data, -0 < +0;
56 identity: quiet NaN */
57 OMEGA_RED_MIN, /* FMNMX_MIN rule, identity: quiet NaN */
58 OMEGA_RED_MEAN, /* omega_math_div(SUM, (float)n), 1 <= n <= 2^24 */
59 OMEGA_RED_COUNT
60} OmegaReduceOp;
61
62/* Empty input (n = 0): SUM -> +0.0, MAX/MIN/MEAN -> quiet NaN 0x7fc00000. */
63#define OMEGA_REDUCE_EMPTY_SUM_BITS 0x00000000U
64#define OMEGA_REDUCE_EMPTY_OTHER_BITS 0x7fc00000U
65#define OMEGA_REDUCE_SUM_PAD_BITS 0x80000000U /* -0.0: x + -0 == x for all x */
66#define OMEGA_REDUCE_MINMAX_PAD_BITS 0x7fc00000U /* NaN: max(NaN, x) == x */
67#define OMEGA_REDUCE_MEAN_MAX_N (1u << 24) /* n exact in FP32 */
68
69const char *omega_reduce_op_name(OmegaReduceOp op);
70/* Padding value of one level (MEAN pads like SUM). */
71float omega_reduce_identity(OmegaReduceOp op);
72/* Levels applied for n elements: smallest L >= 1 with 32^L >= n (n = 0 -> 0). */
73unsigned omega_reduce_levels(size_t n);
74
75/*
76 * Return codes are the OMEGA_NUMERIC_* codes of omega_numeric.h:
77 * BAD_ARGS (unknown op, NULL buffer with n > 0), OPERANDS (MEAN with
78 * n > 2^24, row_stride < n with rows > 1), FPENV (host FPCR not RNE/no FTZ).
79 */
80
81/* Semantic reference tier: written straight from the definition above (copy
82 * each tile into 32 slots, pad, run the lane tree). */
83int omega_reduce_reference(OmegaReduceOp op, const float *x, size_t n, float *out);
84
85/* CPU realization tier: independent bottom-up code. AArch64 uses explicit
86 * FADD/FMAXNM/FMINNM/FDIV/UCVTF; x86 uses scalar SSE with explicit NaN and
87 * signed-zero handling for minimum/maximum. */
88int omega_reduce_cpu(OmegaReduceOp op, const float *x, size_t n, float *out);
89
90/* Batched: out[r] = reduce(x[r*row_stride .. r*row_stride + n)) for r < rows,
91 * each row with the same frozen order (reduce along the last axis). */
92int omega_reduce_rows_reference(OmegaReduceOp op, const float *x, size_t rows, size_t n,
93 size_t row_stride, float *out);
94int omega_reduce_rows_cpu(OmegaReduceOp op, const float *x, size_t rows, size_t n,
95 size_t row_stride, float *out);
96
97/* NOT the contract. Left-to-right sum, used only by the negative test to
98 * show that a different order gives different bits and is caught. */
99float omega_reduce_sequential_sum_not_contract(const float *x, size_t n);
100
101/*
102 * GB10 realization (src/omega_numeric_reduce_gb10.c), all four ops. Each
103 * level is padded with the op identity to a multiple of 32 on the host and run
104 * through a warp kernel in chunks of at most OMEGA_NUMERIC_MAX_COUNT values;
105 * the host only gathers lane 0 of each warp between launches (data movement,
106 * no arithmetic).
107 * SUM the chip-proven REDUCE_SUM patch words, own launch with a semaphore wait.
108 * MAX, MIN this file's warp patch: five SHFL.DOWN (16..1) + FMNMX pairs.
109 * MEAN SUM levels on the chip, then omega_math_div(SUM, u2f(n)) as the GB10
110 * DIV kernel (E1 row 7), one element. No host step.
111 *
112 * omega_reduce_gb10_check runs every pre-submission check (CHECK: markers)
113 * without opening a device.
114 */
115#define OMEGA_REDUCE_GB10_MAX_N ((size_t)1 << 26)
116int omega_reduce_gb10_check(OmegaReduceOp op, const float *x, size_t n, const float *out,
117 char *err, size_t err_len);
118int omega_reduce_gb10(OmegaReduceOp op, const float *x, size_t n, float *out);
119/* Launches the GB10 path made in the last omega_reduce_gb10 call. */
120unsigned omega_reduce_gb10_last_launches(void);
121
122/* MAX/MIN warp patch (12 instructions written at OMEGA_NUMERIC_PATCH_OFFSET
123 * of the vecadd kernel). Returns the count or OMEGA_NUMERIC_ERR_NOT_ENCODED. */
124int omega_reduce_gb10_minmax_patch(OmegaReduceOp op, OmegaNumericPatchInsn out[OMEGA_NUMERIC_PATCH_MAX]);
125/* Structural check of a MAX/MIN patch against the QMD it runs in: the numeric
126 * executor's generic rules plus deltas 16,8,4,2,1 in order, FMNMX form and
127 * min/max predicate, operand order, waits, stall, store, exit. */
128int omega_reduce_gb10_check_minmax_patch(OmegaReduceOp op, const OmegaNumericPatchInsn *p, int n,
129 const uint32_t *qmd1, char *err, size_t err_len);
130/* vecadd kernel with the MAX/MIN patch applied (what the executor submits). */
131int omega_reduce_gb10_build_minmax_kernel(OmegaReduceOp op, uint8_t *code, size_t code_len, size_t *out_len);
132
133#endif /* OMEGA_NUMERIC_REDUCE_H */