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_rest.inc

73 lines · 3627 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 * Allocation, addresses and bit transport only. Omega defines the hierarchy. */
3#if defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_REST_DIFF)
4#undef omega_reduce_identity
5#undef omega_reduce_reference
6#undef omega_reduce_cpu
7#undef omega_reduce_rows_reference
8#undef omega_reduce_rows_cpu
9#undef omega_reduce_sequential_sum_not_contract
10#endif
11#if !defined(CUDAPP_MIGRATION_PENDING_C_NUMERIC_REST)
12float omega_reduce_identity(OmegaReduceOp op) {
13 return omega_bits_to_float((uint32_t)omega_numeric_rest(20, (unsigned)op, 0, 0, 0, 0));
14}
15static int numeric_reduce_adapter(OmegaReduceOp op, const float *x, size_t n, float *out, unsigned cpu) {
16 uint64_t flags = (uint64_t)!!x | ((uint64_t)!!out << 3) | ((uint64_t)omega_numeric_fpenv_ok() << 4);
17 int rc = -(int)omega_numeric_rest(22, (unsigned)op, n, flags, 0, 0);
18 if (rc) return rc;
19 size_t len = n, tiles = omega_numeric_rest(23, op, len, 0, 0, 0);
20 const float *cur = x;
21 float *owned = NULL;
22 uint64_t result = omega_numeric_rest(21, op, 0, 0, 0, 0);
23 while (tiles) {
24 float *next = malloc(tiles * sizeof(*next));
25 if (!next) { free(owned); return OMEGA_NUMERIC_ERR_BAD_ARGS; }
26 for (size_t j = 0; j < tiles; ++j) {
27 uint64_t tile[32];
28 size_t valid = omega_numeric_rest(18, 0, len - j*32, 0, 0, 0);
29 for (size_t k = 0; k < valid; ++k) tile[k] = omega_float_to_bits(cur[j*32+k]);
30 if (omega_numeric_reduce(&result, op, valid, tile, 32, 0, 0) != OMEGA_OK) __builtin_trap();
31 next[j] = omega_bits_to_float((uint32_t)result);
32 }
33 free(owned);
34 owned = next; cur = next; len = tiles;
35 tiles = omega_numeric_rest(30, 0, len, 0, 0, 0);
36 }
37 if (n) result = omega_numeric_rest(25, op, n, result, 0, cpu);
38 *out = omega_bits_to_float((uint32_t)result);
39 free(owned);
40 return OMEGA_NUMERIC_OK;
41}
42int omega_reduce_reference(OmegaReduceOp op, const float *x, size_t n, float *out) {
43 return numeric_reduce_adapter(op, x, n, out, 0);
44}
45int omega_reduce_cpu(OmegaReduceOp op, const float *x, size_t n, float *out) {
46 return numeric_reduce_adapter(op, x, n, out, 1);
47}
48static int numeric_reduce_rows_adapter(OmegaReduceOp op, const float *x, size_t rows, size_t n,
49 size_t stride, float *out, unsigned cpu) {
50 uint64_t flags = (uint64_t)!!x | ((uint64_t)!!out << 3);
51 int rc = -(int)omega_numeric_rest(26, (unsigned)op, n, rows, stride, flags);
52 for (size_t r = 0; rc == OMEGA_NUMERIC_OK && r < rows; ++r)
53 rc = numeric_reduce_adapter(op, n ? x + r*stride : NULL, n, &out[r], cpu);
54 return rc;
55}
56int omega_reduce_rows_reference(OmegaReduceOp op, const float *x, size_t rows, size_t n, size_t stride, float *out) {
57 return numeric_reduce_rows_adapter(op, x, rows, n, stride, out, 0);
58}
59int omega_reduce_rows_cpu(OmegaReduceOp op, const float *x, size_t rows, size_t n, size_t stride, float *out) {
60 return numeric_reduce_rows_adapter(op, x, rows, n, stride, out, 1);
61}
62float omega_reduce_sequential_sum_not_contract(const float *x, size_t n) {
63 uint64_t result = omega_numeric_rest(31, 0, n, 0, 0, 0);
64 for (size_t base = 0; base < n;) {
65 uint64_t tile[32];
66 size_t valid = omega_numeric_rest(18, 0, n - base, 0, 0, 0);
67 for (size_t k = 0; k < valid; ++k) tile[k] = omega_float_to_bits(x[base+k]);
68 if (omega_numeric_reduce(&result, 0, valid, tile, 32, 1, result) != OMEGA_OK) __builtin_trap();
69 base += valid;
70 }
71 return omega_bits_to_float((uint32_t)result);
72}
73#endif