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_gpu_mathlibs_api.c

1204 lines · 55341 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Device execution and byte-moving effects for the seeded Omega mathlibs units.
3 * Host planning/admission is in om/mathlibs/host.om. The default-OFF C branch
4 * is retained solely as the migration differential oracle. */
5#include "omega_gpu_mathlibs_api.h"
6
7#include <stdbool.h>
8#include <stdlib.h>
9#include <string.h>
10#include <time.h>
11
12#include "omega_blackwell_codegen.h"
13#include "omega_gpu_om_ir.h"
14#include "omega_gpu_session.h"
15#include "omega_gpu_mathlibs_host.h"
16
17#ifndef CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
18#define CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST 0
19#endif
20
21#define ML_POISON32 0xffbadbadu
22#define ML_POISON32_ALT 0x00badbadu
23#define ML_POISON64 0xffbadbadffbadbadull
24#define ML_WAIT_MS 600000ull
25#define ML_GOLDEN 11400714819323198485ull /* SplitMix64 increment, 0x9E3779B97F4A7C15 */
26
27#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
28#define ml_sub(a, b) ((a) - (b))
29#define ml_mul(a, b) ((a) * (b))
30#define ml_overlap(a, na, b, nb) ((b) < (a) + (na) && (a) < (b) + (nb))
31#define ml_scan(n, v) ((n) + ((v) == ML_POISON32))
32#else
33#define ml_sub omega_mh_sub
34#define ml_mul omega_mh_mul
35#define ml_overlap(a, na, b, nb) omega_mh_overlap((uintptr_t)(a), (na), (uintptr_t)(b), (nb))
36#define ml_scan(n, v) omega_mh_add((n), omega_mh_poison((v), 0, 0, 0))
37#endif
38
39/* One kernel per (kind, a, b, mutant), device copy uploaded once and resident for the process. */
40typedef struct {
41 int used;
42 uint64_t kind, a, b, mutant;
43 uint32_t threads, declared;
44 uint8_t *buf;
45 OmegaBlackwellKernel kernel;
46 OmegaGpuMem code;
47} Slot;
48static Slot g_cache[64];
49static int g_mutant_which, g_mutant_level;
50static OmegaGpuScratch g_in, g_out, g_tw, g_scr, g_s1, g_w, g_w2, g_b; /* g_s1..g_b: omega_gpu_fft_many */
51static bool g_hooked;
52
53static uint64_t now_ns(void) {
54 struct timespec t;
55 clock_gettime(CLOCK_MONOTONIC, &t);
56#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
57 return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec;
58#else
59 return omega_mh_time((uint64_t)t.tv_sec, (uint64_t)t.tv_nsec);
60#endif
61}
62
63static void slot_clear(Slot *s) {
64 free(s->buf);
65 omega_gpu_session_free(&s->code);
66 memset(s, 0, sizeof *s);
67}
68
69static void cache_clear_locked(void) {
70 for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++)
71 if (g_cache[i].used) slot_clear(&g_cache[i]);
72}
73
74void omega_gpu_mathlibs_cache_clear(void) {
75 omega_gpu_session_lock();
76 cache_clear_locked();
77 omega_gpu_session_unlock();
78}
79
80static void on_session_close(void) {
81 cache_clear_locked();
82 omega_gpu_session_scratch_free(&g_in);
83 omega_gpu_session_scratch_free(&g_out);
84 omega_gpu_session_scratch_free(&g_tw);
85 omega_gpu_session_scratch_free(&g_scr);
86 omega_gpu_session_scratch_free(&g_s1);
87 omega_gpu_session_scratch_free(&g_w);
88 omega_gpu_session_scratch_free(&g_w2);
89 omega_gpu_session_scratch_free(&g_b);
90}
91
92void omega_gpu_mathlibs_test_set_mutant(int which, int level) {
93 omega_gpu_session_lock();
94 g_mutant_which = which;
95 g_mutant_level = level < 0 ? 0 : level > 6 ? 6 : level;
96 cache_clear_locked();
97 omega_gpu_session_unlock();
98}
99
100const char *omega_gpu_mathlibs_last_error(void) { return omega_gpu_session_last_error(); }
101
102/* Caller holds the lock. Builds the kernel on a miss; uploads it when upload is set. */
103/* Mutant targets: which 1 RNG (kind 0), 2 FFT (kind 1), 3 Philox (kind 3), 4 real split / merge (kind 4),
104 * 5 transpose (kind 5), 7 Stockham pass (kind 7), 8 Bluestein step (kind 8), 9 strided copy (kind 9). */
105static int kernel_for(uint64_t kind, uint64_t a, uint64_t b, uint32_t threads, bool upload, Slot **out, bool *hit) {
106 bool target = (g_mutant_which == 1 && kind == 0) || (g_mutant_which == 2 && kind == 1) ||
107 (g_mutant_which >= 3 && (uint64_t)g_mutant_which == kind);
108 uint64_t mutant = target ? (uint64_t)g_mutant_level : 0;
109 Slot *s = NULL;
110 *hit = false;
111 for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++)
112 if (g_cache[i].used && g_cache[i].kind == kind && g_cache[i].a == a && g_cache[i].b == b &&
113 g_cache[i].mutant == mutant) {
114 s = &g_cache[i];
115 *hit = true;
116 break;
117 }
118 if (!s) {
119 BlackwellIRProgram *prog = (BlackwellIRProgram *)malloc(sizeof *prog);
120 uint8_t *buf = (uint8_t *)malloc(OMEGA_BW_CODE_CAP_MAX);
121 OmegaBlackwellKernel k;
122 uint32_t declared = 0;
123 int rc = (prog && buf) ? omega_gpu_om_ir_build(kind, a, b, mutant, threads, prog, buf, OMEGA_BW_CODE_CAP_MAX,
124 &k, &declared, NULL)
125 : OMEGA_OM_IR_REPLAY;
126 free(prog);
127 if (rc != OMEGA_OM_IR_OK) {
128 free(buf);
129 omega_gpu_session_set_error(omega_gpu_om_ir_rc_name(rc));
130 return OMEGA_GPU_ML_CODEGEN_FAIL;
131 }
132 size_t slot = 0;
133 for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++)
134 if (!g_cache[i].used) { slot = i; break; }
135 s = &g_cache[slot];
136 if (s->used) slot_clear(s); /* full: evict slot 0 */
137 s->used = 1;
138 s->kind = kind; s->a = a; s->b = b; s->mutant = mutant;
139 s->threads = threads; s->declared = declared;
140 s->buf = buf;
141 s->kernel = k;
142 }
143 if (upload && !s->code.cpu) {
144 if (omega_gpu_session_alloc_code(s->kernel.code_size, &s->code) != 0) return OMEGA_GPU_ML_CHIP_FAIL;
145 memcpy(s->code.cpu, s->kernel.code, s->kernel.code_size);
146 if (omega_gpu_session_sync_to_gpu(&s->code) != 0) { omega_gpu_session_free(&s->code); return OMEGA_GPU_ML_CHIP_FAIL; }
147 }
148 *out = s;
149 return OMEGA_GPU_ML_OK;
150}
151
152static void info_kernel(OmegaGpuMathInfo *info, const Slot *s, bool hit) {
153 if (!info) return;
154 info->kernel_cache_hit = hit;
155 info->insn_count = (uint32_t)s->kernel.insn_count;
156 info->gpr_peak = s->kernel.gpr_count;
157 info->gpr_declared = s->declared;
158 memcpy(info->code_digest, s->kernel.code_digest, 32);
159 info->threads_per_cta = s->threads;
160}
161
162static void info_begin(OmegaGpuMathInfo *info, const Slot *s, bool hit) {
163 if (!info) return;
164 memset(info, 0, sizeof *info);
165 info_kernel(info, s, hit);
166}
167
168static int codegen_only(uint64_t kind, uint64_t a, uint64_t b, uint32_t threads, OmegaGpuMathInfo *info) {
169 omega_gpu_session_lock();
170 Slot *s;
171 bool hit;
172 int rc = kernel_for(kind, a, b, threads, false, &s, &hit);
173 if (rc == OMEGA_GPU_ML_OK) info_begin(info, s, hit);
174 omega_gpu_session_unlock();
175 return rc;
176}
177
178/* Caller holds the lock: open the shared device, build and upload the kernel. */
179static int begin(uint64_t kind, uint64_t a, uint64_t b, uint32_t threads, Slot **s, OmegaGpuMathInfo *info) {
180 if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; }
181 if (!omega_gpu_session_open()) return OMEGA_GPU_ML_CHIP_FAIL;
182 bool hit;
183 int rc = kernel_for(kind, a, b, threads, true, s, &hit);
184 if (rc == OMEGA_GPU_ML_OK) info_begin(info, *s, hit);
185 return rc;
186}
187
188static int launch(const Slot *s, const uint32_t *args, uint32_t n_args, uint32_t grid, size_t elements,
189 uint32_t shared_bytes, OmegaGpuMathInfo *info) {
190 OmegaGpuLaunch L = {
191 .code_va = s->code.va, .gpr_count = s->declared, .threads_x = s->threads, .threads_y = 1,
192 .grid_x = grid, .grid_y = 1, .num_elements = elements, .args = args, .n_args = n_args,
193 .timeout_ms = ML_WAIT_MS, .shared_bytes = shared_bytes,
194 };
195 uint64_t elapsed = 0;
196 uint32_t marker = 0;
197 if (omega_gpu_session_launch(&L, &elapsed, &marker) != 0) return OMEGA_GPU_ML_CHIP_FAIL;
198 if (info) {
199 info->chip_calls++;
200 info->elapsed_ns += elapsed;
201 info->completion_marker = marker;
202 info->ctas_last_launch = grid;
203 }
204 return OMEGA_GPU_ML_OK;
205}
206
207static void put64(uint32_t *w, uint64_t v) { w[0] = (uint32_t)v; w[1] = (uint32_t)(v >> 32); }
208
209/* ------------------------------------------------------------------ RNG */
210
211#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
212static int rng_args_ok(OmegaGpuRngKind kind, uint32_t nstreams, uint32_t per) {
213 if ((unsigned)kind > OMEGA_GPU_RNG_NORMAL || nstreams == 0 || per == 0) return OMEGA_GPU_ML_BAD_ARGS;
214 if (nstreams > OMEGA_GPU_RNG_MAX_STREAMS || (uint64_t)nstreams * per > OMEGA_GPU_RNG_MAX_OUTPUTS)
215 return OMEGA_GPU_ML_TOO_LARGE;
216 return OMEGA_GPU_ML_OK;
217}
218
219int omega_gpu_rng_codegen(OmegaGpuRngKind kind, OmegaGpuMathInfo *info) {
220 if ((unsigned)kind > OMEGA_GPU_RNG_NORMAL) return OMEGA_GPU_ML_BAD_ARGS;
221 return codegen_only(0, (uint64_t)kind, 0, OMEGA_GPU_RNG_THREADS, info);
222}
223
224int omega_gpu_rng_generate(OmegaGpuRngKind kind, uint64_t seed, uint64_t stream0, uint32_t nstreams,
225 uint32_t offset, uint32_t per_stream, void *out, OmegaGpuMathInfo *info) {
226 if (!out) return OMEGA_GPU_ML_BAD_ARGS;
227 int rc = rng_args_ok(kind, nstreams, per_stream);
228 if (rc != OMEGA_GPU_ML_OK) return rc;
229 uint64_t t0 = now_ns();
230 size_t count = (size_t)nstreams * per_stream;
231 size_t esize = kind == OMEGA_GPU_RNG_U64 ? 8u : 4u;
232 size_t bytes = count * esize;
233 omega_gpu_session_lock();
234 Slot *s;
235 rc = begin(0, (uint64_t)kind, 0, OMEGA_GPU_RNG_THREADS, &s, info);
236 if (rc == OMEGA_GPU_ML_OK && omega_gpu_session_scratch(&g_out, bytes) != 0) rc = OMEGA_GPU_ML_CHIP_FAIL;
237 if (rc == OMEGA_GPU_ML_OK) {
238 uint32_t *dev = (uint32_t *)g_out.mem.cpu;
239 for (size_t i = 0; i < bytes / 4; i++) dev[i] = ML_POISON32;
240 if (omega_gpu_session_sync_to_gpu(&g_out.mem) != 0) rc = OMEGA_GPU_ML_CHIP_FAIL;
241 /* 0x380 out, 0x388 + 8 i base_i, 0x3a8 skip, 0x3ac per-stream count, 0x3b0 streams */
242 uint32_t args[13];
243 put64(&args[0], g_out.mem.va);
244 uint64_t x = seed + stream0;
245 for (int i = 0; i < 4; i++) { x += ML_GOLDEN; put64(&args[2 + 2 * i], x); }
246 args[10] = offset;
247 args[11] = per_stream;
248 args[12] = nstreams;
249 uint32_t grid = (nstreams + OMEGA_GPU_RNG_THREADS - 1) / OMEGA_GPU_RNG_THREADS;
250 if (rc == OMEGA_GPU_ML_OK) rc = launch(s, args, 13, grid, count, 0, info);
251 if (rc == OMEGA_GPU_ML_OK && omega_gpu_session_sync_to_cpu(&g_out.mem) != 0) rc = OMEGA_GPU_ML_CHIP_FAIL;
252 if (rc == OMEGA_GPU_ML_OK) {
253 memcpy(out, g_out.mem.cpu, bytes);
254 uint64_t unwritten = 0;
255 if (esize == 8) {
256 for (size_t i = 0; i < count; i++) unwritten += ((const uint64_t *)out)[i] == ML_POISON64;
257 } else {
258 for (size_t i = 0; i < count; i++) unwritten += ((const uint32_t *)out)[i] == ML_POISON32;
259 }
260 if (info) info->unwritten_words = unwritten;
261 if (unwritten) rc = OMEGA_GPU_ML_UNWRITTEN;
262 }
263 }
264 omega_gpu_session_unlock();
265 if (info) info->call_ns = now_ns() - t0;
266 return rc;
267}
268#endif
269
270/* ------------------------------------------------------------------ FFT */
271
272static int lg_in(uint32_t n, uint32_t lo, uint32_t hi) {
273#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
274 if (n < lo || n > hi || (n & (n - 1)) != 0) return -1;
275 int k = 0;
276 while ((1u << k) < n) k++;
277 return k;
278#else
279 uint64_t k = omega_mh_lg(n, lo, hi);
280 return k == UINT32_MAX ? -1 : (int)k;
281#endif
282}
283
284static int fft_log2(uint32_t n) { return lg_in(n, OMEGA_GPU_FFT_MIN_N, OMEGA_GPU_FFT_MAX_N); }
285
286static float g_twf[2u * OMEGA_GPU_FFT_REAL_MAX_N];
287static uint64_t g_words[OMEGA_OM_IR_WORDS];
288
289/* Caller holds the lock: g_twf = the unit's f32 table of 2^lg points (kind 2). */
290static int twiddles_locked(uint64_t lg, uint64_t inverse) {
291 if (omega_gpu_om_ir_data(2, lg, inverse, 0, g_words) != OMEGA_OM_IR_OK) return OMEGA_GPU_ML_CODEGEN_FAIL;
292 for (uint32_t m = 0; m < (1u << lg); m++) {
293 uint32_t re = (uint32_t)g_words[m], im = (uint32_t)(g_words[m] >> 32);
294 memcpy(&g_twf[2 * m], &re, 4);
295 memcpy(&g_twf[2 * m + 1], &im, 4);
296 }
297 return OMEGA_GPU_ML_OK;
298}
299
300int omega_gpu_fft_codegen(uint32_t n, int inverse, OmegaGpuMathInfo *info) {
301 int lg = fft_log2(n);
302 if (lg < 0 || (inverse != 0 && inverse != 1)) return OMEGA_GPU_ML_BAD_ARGS;
303 return codegen_only(1, (uint64_t)lg, (uint64_t)inverse, n / 4, info);
304}
305
306int omega_gpu_fft_twiddles(uint32_t n, int inverse, float *tw) {
307 int lg = fft_log2(n);
308 if (lg < 0 || !tw || (inverse != 0 && inverse != 1)) return OMEGA_GPU_ML_BAD_ARGS;
309 omega_gpu_session_lock();
310 int rc = twiddles_locked((uint64_t)lg, (uint64_t)inverse);
311 if (rc == OMEGA_GPU_ML_OK) memcpy(tw, g_twf, (size_t)8 * n);
312 omega_gpu_session_unlock();
313 return rc;
314}
315
316static int many_basic(OmegaGpuFftType type, uint32_t rank, const uint32_t *n, uint32_t batch, int inverse,
317 const float *in, float *out, OmegaGpuMathInfo *info);
318
319#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
320int omega_gpu_fft_c2c(const float *in, float *out, uint32_t n, uint32_t batch, int inverse, OmegaGpuMathInfo *info) {
321 int lg = fft_log2(n);
322 if (lg < 0 && n >= 2 && n <= OMEGA_GPU_FFT_MANY_MAX_N)
323 return many_basic(OMEGA_GPU_FFT_TYPE_C2C, 1, &n, batch, inverse, in, out, info);
324 if (lg < 0 || !in || !out || batch == 0 || (inverse != 0 && inverse != 1)) return OMEGA_GPU_ML_BAD_ARGS;
325 size_t floats = (size_t)2 * n * batch;
326 if (floats > ((size_t)1 << 28)) return OMEGA_GPU_ML_TOO_LARGE;
327 if ((out < in + floats) && (in < out + floats)) return OMEGA_GPU_ML_BAD_ARGS;
328 float *tw = (float *)malloc((size_t)2 * n * sizeof *tw);
329 if (!tw) return OMEGA_GPU_ML_CHIP_FAIL;
330 int rc = omega_gpu_fft_twiddles(n, inverse, tw);
331 if (rc != OMEGA_GPU_ML_OK) { free(tw); return rc; }
332 uint64_t t0 = now_ns();
333 size_t bytes = floats * 4;
334 omega_gpu_session_lock();
335 Slot *s;
336 rc = begin(1, (uint64_t)lg, (uint64_t)inverse, n / 4, &s, info);
337 if (rc == OMEGA_GPU_ML_OK && (omega_gpu_session_scratch(&g_in, bytes) != 0 ||
338 omega_gpu_session_scratch(&g_out, bytes) != 0 ||
339 omega_gpu_session_scratch(&g_tw, (size_t)8 * n) != 0))
340 rc = OMEGA_GPU_ML_CHIP_FAIL;
341 if (rc == OMEGA_GPU_ML_OK) {
342 memcpy(g_in.mem.cpu, in, bytes);
343 memcpy(g_tw.mem.cpu, tw, (size_t)8 * n);
344 uint32_t *dev = (uint32_t *)g_out.mem.cpu;
345 for (size_t i = 0; i < floats; i++) dev[i] = ML_POISON32;
346 if (omega_gpu_session_sync_to_gpu(&g_in.mem) != 0 || omega_gpu_session_sync_to_gpu(&g_tw.mem) != 0 ||
347 omega_gpu_session_sync_to_gpu(&g_out.mem) != 0)
348 rc = OMEGA_GPU_ML_CHIP_FAIL;
349 for (uint32_t b0 = 0; b0 < batch && rc == OMEGA_GPU_ML_OK; b0 += OMEGA_GPU_FFT_MAX_CTAS) {
350 uint32_t grid = batch - b0 < OMEGA_GPU_FFT_MAX_CTAS ? batch - b0 : OMEGA_GPU_FFT_MAX_CTAS;
351 uint64_t off = (uint64_t)b0 * n * 8u;
352 uint32_t args[6]; /* 0x380 in, 0x388 out, 0x390 twiddles */
353 put64(&args[0], g_in.mem.va + off);
354 put64(&args[2], g_out.mem.va + off);
355 put64(&args[4], g_tw.mem.va);
356 rc = launch(s, args, 6, grid, (size_t)grid * n * 2u, 8u * n, info);
357 }
358 if (rc == OMEGA_GPU_ML_OK && omega_gpu_session_sync_to_cpu(&g_out.mem) != 0) rc = OMEGA_GPU_ML_CHIP_FAIL;
359 if (rc == OMEGA_GPU_ML_OK) {
360 memcpy(out, g_out.mem.cpu, bytes);
361 uint64_t unwritten = 0;
362 for (size_t i = 0; i < floats; i++) {
363 uint32_t u;
364 memcpy(&u, &out[i], 4);
365 unwritten += u == ML_POISON32;
366 }
367 if (info) info->unwritten_words = unwritten;
368 if (unwritten) rc = OMEGA_GPU_ML_UNWRITTEN;
369 }
370 }
371 omega_gpu_session_unlock();
372 free(tw);
373 if (info) info->call_ns = now_ns() - t0;
374 return rc;
375}
376#endif
377
378/* ------------------------------------------------------------------ Philox */
379
380int omega_gpu_philox_codegen(OmegaGpuMathInfo *info) { return codegen_only(3, 0, 0, OMEGA_GPU_PHILOX_THREADS, info); }
381
382/* Caller holds the lock: fill g_out with poison, run, read back into out. */
383static int philox_pass(const Slot *s, const uint32_t *args, uint32_t grid, size_t count, uint32_t poison, uint32_t *out,
384 OmegaGpuMathInfo *info) {
385 uint32_t *dev = (uint32_t *)g_out.mem.cpu;
386 for (size_t i = 0; i < count; i++) dev[i] = poison;
387 if (omega_gpu_session_sync_to_gpu(&g_out.mem) != 0) return OMEGA_GPU_ML_CHIP_FAIL;
388 int rc = launch(s, args, 11, grid, count, 0, info);
389 if (rc == OMEGA_GPU_ML_OK && omega_gpu_session_sync_to_cpu(&g_out.mem) != 0) rc = OMEGA_GPU_ML_CHIP_FAIL;
390 if (rc == OMEGA_GPU_ML_OK) memcpy(out, dev, count * 4u);
391 return rc;
392}
393
394#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
395int omega_gpu_philox_generate_dist(OmegaGpuPhiloxDist dist, uint64_t seed, uint64_t subseq0, uint32_t nstreams,
396 uint64_t offset, uint32_t per_stream, void *out, OmegaGpuMathInfo *info) {
397 if (!out || (unsigned)dist > OMEGA_GPU_PHILOX_NORMAL || nstreams == 0 || per_stream == 0)
398 return OMEGA_GPU_ML_BAD_ARGS;
399 if (nstreams > OMEGA_GPU_PHILOX_MAX_STREAMS || (uint64_t)nstreams * per_stream > OMEGA_GPU_RNG_MAX_OUTPUTS)
400 return OMEGA_GPU_ML_TOO_LARGE;
401 uint64_t t0 = now_ns();
402 /* G outputs per group of four words; 64-bit outputs are two words each */
403 uint32_t g = dist == OMEGA_GPU_PHILOX_UNIFORM2_DOUBLE ? 2u : 4u;
404 uint32_t esz = dist == OMEGA_GPU_PHILOX_UNIFORM_DOUBLE || dist == OMEGA_GPU_PHILOX_UNIFORM2_DOUBLE ? 2u : 1u;
405 size_t count = (size_t)nstreams * per_stream * esz;
406 uint32_t *words = (uint32_t *)out;
407 omega_gpu_session_lock();
408 Slot *s;
409 int rc = begin(3, (uint64_t)dist, offset % 4u, OMEGA_GPU_PHILOX_THREADS, &s, info);
410 if (rc == OMEGA_GPU_ML_OK && omega_gpu_session_scratch(&g_out, count * 4u) != 0) rc = OMEGA_GPU_ML_CHIP_FAIL;
411 if (rc == OMEGA_GPU_ML_OK) {
412 /* 0x380 out, 0x388 key, 0x390 subsequence of stream 0, 0x398 first counter block (offset / 4), 0x3a0 groups
413 * T = ceil(per / G), 0x3a4 streams, 0x3a8 tail delta (per % G - G mod 2^32, 0 without a partial group) */
414 uint32_t args[11];
415 put64(&args[0], g_out.mem.va);
416 put64(&args[2], seed);
417 put64(&args[4], subseq0);
418 put64(&args[6], offset / 4u);
419 uint32_t r = per_stream % g;
420 args[8] = per_stream / g + (r != 0);
421 args[9] = nstreams;
422 args[10] = r ? r - g : 0u;
423 uint32_t grid = (nstreams + OMEGA_GPU_PHILOX_THREADS - 1) / OMEGA_GPU_PHILOX_THREADS;
424 rc = philox_pass(s, args, grid, count, ML_POISON32, words, info);
425 uint64_t unwritten = 0;
426 if (dist == OMEGA_GPU_PHILOX_RAW) {
427 /* Every 32-bit value is a possible output: a word equal to the poison is unwritten only if a second
428 * run with another poison leaves that poison there too. */
429 uint64_t hits = 0;
430 for (size_t i = 0; rc == OMEGA_GPU_ML_OK && i < count; i++) hits += words[i] == ML_POISON32;
431 if (rc == OMEGA_GPU_ML_OK && hits) {
432 uint32_t *first = (uint32_t *)malloc(count * 4u);
433 if (!first) rc = OMEGA_GPU_ML_CHIP_FAIL;
434 if (first) {
435 memcpy(first, words, count * 4u);
436 rc = philox_pass(s, args, grid, count, ML_POISON32_ALT, words, info);
437 for (size_t i = 0; rc == OMEGA_GPU_ML_OK && i < count; i++)
438 unwritten += first[i] == ML_POISON32 && words[i] == ML_POISON32_ALT;
439 free(first);
440 }
441 }
442 } else if (esz == 1) {
443 /* the poison is a NaN: no f32 output of these distributions */
444 for (size_t i = 0; rc == OMEGA_GPU_ML_OK && i < count; i++) unwritten += words[i] == ML_POISON32;
445 } else {
446 /* the poison pair is a NaN: no f64 output */
447 const uint64_t *d = (const uint64_t *)out;
448 for (size_t i = 0; rc == OMEGA_GPU_ML_OK && i < count / 2; i++) unwritten += d[i] == ML_POISON64;
449 }
450 if (info) info->unwritten_words = unwritten;
451 if (rc == OMEGA_GPU_ML_OK && unwritten) rc = OMEGA_GPU_ML_UNWRITTEN;
452 }
453 omega_gpu_session_unlock();
454 if (info) info->call_ns = now_ns() - t0;
455 return rc;
456}
457#endif
458
459int omega_gpu_philox_generate(uint64_t seed, uint64_t subseq0, uint32_t nstreams, uint64_t offset, uint32_t per_stream,
460 uint32_t *out, OmegaGpuMathInfo *info) {
461 return omega_gpu_philox_generate_dist(OMEGA_GPU_PHILOX_RAW, seed, subseq0, nstreams, offset, per_stream, out, info);
462}
463
464/* ------------------------------------------------------------------ FFT plans (R2C, C2R, 2-D, 3-D) */
465
466/* Caller holds the lock. Validates, sizes the buffers, asks the unit for the pass plan (kind 6) and, when fn is set,
467 * turns each step into launches of at most OMEGA_GPU_FFT_MAX_CTAS CTAs. */
468static int walk_locked(OmegaGpuFftPlanMode mode, uint32_t ndim, const uint32_t *dims, uint32_t batch, int inverse,
469 size_t bytes[3], OmegaGpuFftLaunchFn fn, void *ctx);
470#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
471static int walk_locked(OmegaGpuFftPlanMode mode, uint32_t ndim, const uint32_t *dims, uint32_t batch, int inverse,
472 size_t bytes[3], OmegaGpuFftLaunchFn fn, void *ctx) {
473 if (!dims || !bytes || batch == 0 || (inverse != 0 && inverse != 1)) return OMEGA_GPU_ML_BAD_ARGS;
474 uint64_t lgs = 0;
475 uint32_t sum = 0;
476 if (mode == OMEGA_GPU_FFT_C2C_ND) {
477 if (ndim < 1 || ndim > 3) return OMEGA_GPU_ML_BAD_ARGS;
478 for (uint32_t i = 0; i < ndim; i++) {
479 int k = fft_log2(dims[i]);
480 if (k < 0) return OMEGA_GPU_ML_BAD_ARGS;
481 lgs |= (uint64_t)k << (4 * i);
482 sum += (uint32_t)k;
483 }
484 } else if (mode == OMEGA_GPU_FFT_R2C || mode == OMEGA_GPU_FFT_C2R) {
485 if (ndim != 1 || inverse != 0) return OMEGA_GPU_ML_BAD_ARGS;
486 int k = lg_in(dims[0], OMEGA_GPU_FFT_REAL_MIN_N, OMEGA_GPU_FFT_REAL_MAX_N);
487 if (k < 0) return OMEGA_GPU_ML_BAD_ARGS;
488 lgs = (uint64_t)k;
489 sum = (uint32_t)k;
490 } else {
491 return OMEGA_GPU_ML_BAD_ARGS;
492 }
493 if (sum > 27 || ((uint64_t)batch << sum) > OMEGA_GPU_FFT_MAX_ELEMENTS) return OMEGA_GPU_ML_TOO_LARGE;
494 size_t elems = (size_t)batch << sum, half = ((size_t)1 << sum) / 2u + 1u;
495 bytes[0] = bytes[1] = bytes[2] = elems * 8u;
496 if (mode == OMEGA_GPU_FFT_R2C) { bytes[0] = elems * 4u; bytes[1] = half * 8u * batch; bytes[2] = elems * 4u; }
497 if (mode == OMEGA_GPU_FFT_C2R) { bytes[0] = half * 8u * batch; bytes[1] = elems * 4u; bytes[2] = elems * 4u; }
498 int rc = omega_gpu_om_ir_data(6, (uint64_t)mode | (uint64_t)ndim << 4, lgs, (uint64_t)inverse, g_words);
499 if (rc == OMEGA_OM_IR_REFUSED) return OMEGA_GPU_ML_BAD_ARGS;
500 if (rc != OMEGA_OM_IR_OK || g_words[0] == 0 || g_words[0] > 6) return OMEGA_GPU_ML_CODEGEN_FAIL;
501 uint64_t steps[6];
502 uint32_t nsteps = (uint32_t)g_words[0];
503 memcpy(steps, &g_words[1], nsteps * sizeof steps[0]);
504 for (uint32_t i = 0; fn && i < nsteps; i++) {
505 OmegaGpuFftLaunch L;
506 memset(&L, 0, sizeof L);
507 L.kind = (uint32_t)(steps[i] & 255u);
508 L.a = (uint32_t)(steps[i] >> 8 & 255u);
509 L.b = (uint32_t)(steps[i] >> 16 & 255u);
510 L.src = (uint32_t)(steps[i] >> 24 & 255u);
511 L.dst = (uint32_t)(steps[i] >> 32 & 255u);
512 uint64_t rows = (uint64_t)batch << (steps[i] >> 40 & 255u);
513 if (L.src > 2 || L.dst > 2) return OMEGA_GPU_ML_CODEGEN_FAIL;
514 if (L.kind == 1 || L.kind == 4) {
515 rc = twiddles_locked(L.a, L.b);
516 if (rc != OMEGA_GPU_ML_OK) return rc;
517 uint64_t n = 1ull << L.a, si = n, so = n;
518 L.tw = g_twf;
519 L.tw_n = (uint32_t)n;
520 L.tw_inverse = L.b;
521 L.threads = (uint32_t)n / 4u;
522 L.shared_bytes = L.kind == 1 ? (uint32_t)(8u * n) : 0u;
523 if (L.kind == 4) { si = n / 2u + L.b; so = n / 2u + 1u - L.b; }
524 for (uint64_t r0 = 0; r0 < rows; r0 += OMEGA_GPU_FFT_MAX_CTAS) {
525 L.grid = (uint32_t)(rows - r0 < OMEGA_GPU_FFT_MAX_CTAS ? rows - r0 : OMEGA_GPU_FFT_MAX_CTAS);
526 L.src_off = r0 * si * 8u;
527 L.dst_off = r0 * so * 8u;
528 L.elements = (size_t)2u * so * L.grid;
529 rc = fn(ctx, &L);
530 if (rc != OMEGA_GPU_ML_OK) return rc;
531 }
532 } else if (L.kind == 5) {
533 uint64_t total = rows << (L.a + L.b);
534 uint64_t ctas = (total + 255u) / 256u;
535 L.threads = 256;
536 L.total = (uint32_t)total;
537 for (uint64_t c0 = 0; c0 < ctas; c0 += OMEGA_GPU_FFT_MAX_CTAS) {
538 L.grid = (uint32_t)(ctas - c0 < OMEGA_GPU_FFT_MAX_CTAS ? ctas - c0 : OMEGA_GPU_FFT_MAX_CTAS);
539 L.first = (uint32_t)(c0 * 256u);
540 uint64_t left = total - L.first;
541 L.elements = (size_t)2u * ((uint64_t)L.grid * 256u < left ? (uint64_t)L.grid * 256u : left);
542 rc = fn(ctx, &L);
543 if (rc != OMEGA_GPU_ML_OK) return rc;
544 }
545 } else {
546 return OMEGA_GPU_ML_CODEGEN_FAIL;
547 }
548 }
549 return OMEGA_GPU_ML_OK;
550}
551#endif
552
553/* The launches of a walk with private copies of their twiddle tables, so the caller's fn runs unlocked. */
554typedef struct {
555 OmegaGpuFftLaunch *l;
556 size_t n, cap;
557 float **tw;
558 size_t ntw, twcap;
559} Collect;
560
561static int collect(void *ctx, const OmegaGpuFftLaunch *L) {
562 Collect *c = (Collect *)ctx;
563 if (c->n == c->cap) {
564 size_t cap = c->cap ? 2u * c->cap : 32u;
565 OmegaGpuFftLaunch *l = (OmegaGpuFftLaunch *)realloc(c->l, cap * sizeof *l);
566 if (!l) return OMEGA_GPU_ML_CHIP_FAIL;
567 c->l = l;
568 c->cap = cap;
569 }
570 OmegaGpuFftLaunch x = *L;
571 if (L->tw_n) {
572 const OmegaGpuFftLaunch *p = NULL;
573 for (size_t i = c->n; i-- > 0;)
574 if (c->l[i].tw_n) { p = &c->l[i]; break; }
575 if (p && p->tw_n == L->tw_n && p->tw_inverse == L->tw_inverse) {
576 x.tw = p->tw;
577 } else {
578 if (c->ntw == c->twcap) {
579 size_t cap = c->twcap ? 2u * c->twcap : 8u;
580 float **t = (float **)realloc(c->tw, cap * sizeof *t);
581 if (!t) return OMEGA_GPU_ML_CHIP_FAIL;
582 c->tw = t;
583 c->twcap = cap;
584 }
585 float *copy = (float *)malloc((size_t)8 * L->tw_n);
586 if (!copy) return OMEGA_GPU_ML_CHIP_FAIL;
587 memcpy(copy, L->tw, (size_t)8 * L->tw_n);
588 c->tw[c->ntw++] = copy;
589 x.tw = copy;
590 }
591 }
592 c->l[c->n++] = x;
593 return OMEGA_GPU_ML_OK;
594}
595
596int omega_gpu_fft_plan_walk(OmegaGpuFftPlanMode mode, uint32_t ndim, const uint32_t *dims, uint32_t batch, int inverse,
597 size_t bytes[3], OmegaGpuFftLaunchFn fn, void *ctx) {
598 Collect c;
599 memset(&c, 0, sizeof c);
600 omega_gpu_session_lock();
601 int rc = walk_locked(mode, ndim, dims, batch, inverse, bytes, fn ? collect : NULL, &c);
602 omega_gpu_session_unlock();
603 for (size_t i = 0; rc == OMEGA_GPU_ML_OK && i < c.n; i++) rc = fn(ctx, &c.l[i]);
604 for (size_t i = 0; i < c.ntw; i++) free(c.tw[i]);
605 free(c.tw);
606 free(c.l);
607 return rc;
608}
609
610typedef struct {
611 OmegaGpuMathInfo *info;
612 OmegaGpuScratch *buf[3];
613 uint32_t tw_n, tw_inverse;
614} ChipRun;
615
616/* Caller holds the lock: one launch of a plan step on the chip. */
617static int chip_launch(void *ctx, const OmegaGpuFftLaunch *l) {
618 ChipRun *r = (ChipRun *)ctx;
619 Slot *s;
620 bool hit;
621 int rc = kernel_for(l->kind, l->a, l->b, l->threads, true, &s, &hit);
622 if (rc != OMEGA_GPU_ML_OK) return rc;
623 info_kernel(r->info, s, hit);
624 if (l->tw_n && (l->tw_n != r->tw_n || l->tw_inverse != r->tw_inverse)) {
625 memcpy(g_tw.mem.cpu, l->tw, (size_t)8 * l->tw_n);
626 if (omega_gpu_session_sync_to_gpu(&g_tw.mem) != 0) return OMEGA_GPU_ML_CHIP_FAIL;
627 r->tw_n = l->tw_n;
628 r->tw_inverse = l->tw_inverse;
629 }
630 uint32_t args[6]; /* 0x380 in, 0x388 out, 0x390 twiddles (kind 5: element count, first element) */
631 put64(&args[0], r->buf[l->src]->mem.va + l->src_off);
632 put64(&args[2], r->buf[l->dst]->mem.va + l->dst_off);
633 if (l->kind == 5) {
634 args[4] = l->total;
635 args[5] = l->first;
636 } else {
637 put64(&args[4], g_tw.mem.va);
638 }
639 return launch(s, args, 6, l->grid, l->elements, l->shared_bytes, r->info);
640}
641
642static int run_plan(OmegaGpuFftPlanMode mode, uint32_t ndim, const uint32_t *dims, uint32_t batch, int inverse,
643 const float *in, float *out, OmegaGpuMathInfo *info) {
644 if (!in || !out) return OMEGA_GPU_ML_BAD_ARGS;
645 if (info) memset(info, 0, sizeof *info);
646 uint64_t t0 = now_ns();
647 size_t bytes[3];
648 omega_gpu_session_lock();
649 int rc = walk_locked(mode, ndim, dims, batch, inverse, bytes, NULL, NULL);
650 const char *pi = (const char *)in, *po = (const char *)out;
651 if (rc == OMEGA_GPU_ML_OK && ml_overlap(pi, bytes[0], po, bytes[1])) rc = OMEGA_GPU_ML_BAD_ARGS;
652 if (rc == OMEGA_GPU_ML_OK) {
653 if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; }
654 if (!omega_gpu_session_open()) rc = OMEGA_GPU_ML_CHIP_FAIL;
655 }
656 if (rc == OMEGA_GPU_ML_OK && (omega_gpu_session_scratch(&g_in, bytes[0]) != 0 ||
657 omega_gpu_session_scratch(&g_out, bytes[1]) != 0 ||
658 omega_gpu_session_scratch(&g_scr, bytes[2]) != 0 ||
659 omega_gpu_session_scratch(&g_tw, (size_t)8 * OMEGA_GPU_FFT_REAL_MAX_N) != 0))
660 rc = OMEGA_GPU_ML_CHIP_FAIL;
661 if (rc == OMEGA_GPU_ML_OK) {
662 memcpy(g_in.mem.cpu, in, bytes[0]);
663 uint32_t *dev = (uint32_t *)g_out.mem.cpu, *scr = (uint32_t *)g_scr.mem.cpu;
664 for (size_t i = 0; i < bytes[1] / 4u; i++) dev[i] = ML_POISON32;
665 for (size_t i = 0; i < bytes[2] / 4u; i++) scr[i] = ML_POISON32;
666 if (omega_gpu_session_sync_to_gpu(&g_in.mem) != 0 || omega_gpu_session_sync_to_gpu(&g_out.mem) != 0 ||
667 omega_gpu_session_sync_to_gpu(&g_scr.mem) != 0)
668 rc = OMEGA_GPU_ML_CHIP_FAIL;
669 ChipRun r = {info, {&g_in, &g_out, &g_scr}, 0, 0};
670 if (rc == OMEGA_GPU_ML_OK) rc = walk_locked(mode, ndim, dims, batch, inverse, bytes, chip_launch, &r);
671 if (rc == OMEGA_GPU_ML_OK && omega_gpu_session_sync_to_cpu(&g_out.mem) != 0) rc = OMEGA_GPU_ML_CHIP_FAIL;
672 if (rc == OMEGA_GPU_ML_OK) {
673 memcpy(out, dev, bytes[1]);
674 uint64_t unwritten = 0;
675 for (size_t i = 0; i < bytes[1] / 4u; i++) unwritten = ml_scan(unwritten, dev[i]);
676 if (info) info->unwritten_words = unwritten;
677 if (unwritten) rc = OMEGA_GPU_ML_UNWRITTEN;
678 }
679 }
680 omega_gpu_session_unlock();
681 if (info) info->call_ns = ml_sub(now_ns(), t0);
682 return rc;
683}
684
685int omega_gpu_fft_r2c(const float *in, float *out, uint32_t n, uint32_t batch, OmegaGpuMathInfo *info) {
686 if (lg_in(n, OMEGA_GPU_FFT_REAL_MIN_N, OMEGA_GPU_FFT_REAL_MAX_N) < 0 && n >= 2 && n <= OMEGA_GPU_FFT_MANY_MAX_N)
687 return many_basic(OMEGA_GPU_FFT_TYPE_R2C, 1, &n, batch, 0, in, out, info);
688 return run_plan(OMEGA_GPU_FFT_R2C, 1, &n, batch, 0, in, out, info);
689}
690
691int omega_gpu_fft_c2r(const float *in, float *out, uint32_t n, uint32_t batch, OmegaGpuMathInfo *info) {
692 if (lg_in(n, OMEGA_GPU_FFT_REAL_MIN_N, OMEGA_GPU_FFT_REAL_MAX_N) < 0 && n >= 2 && n <= OMEGA_GPU_FFT_MANY_MAX_N)
693 return many_basic(OMEGA_GPU_FFT_TYPE_C2R, 1, &n, batch, 0, in, out, info);
694 return run_plan(OMEGA_GPU_FFT_C2R, 1, &n, batch, 0, in, out, info);
695}
696
697int omega_gpu_fft_c2c_nd(const float *in, float *out, uint32_t ndim, const uint32_t *dims, uint32_t batch, int inverse,
698 OmegaGpuMathInfo *info) {
699 if (dims && ndim >= 1 && ndim <= 3) {
700 int legacy = 1, any = 1;
701 for (uint32_t i = 0; i < ndim; i++) {
702 legacy &= fft_log2(dims[i]) >= 0;
703 any &= dims[i] >= 2 && dims[i] <= OMEGA_GPU_FFT_MANY_MAX_N;
704 }
705 if (!legacy && any) return many_basic(OMEGA_GPU_FFT_TYPE_C2C, ndim, dims, batch, inverse, in, out, info);
706 }
707 return run_plan(OMEGA_GPU_FFT_C2C_ND, ndim, dims, batch, inverse, in, out, info);
708}
709
710/* ------------------------------------------------------------------ FFT many (cufftPlanMany layouts) */
711
712/* A layout: element (b, x) at base + b * bs + sum x[i] * st[i] (mod 2^32), i the logical axis. */
713typedef struct { uint32_t base, bs, st[3]; } Lay;
714
715typedef struct {
716 OmegaGpuFftManyLaunch *l;
717 size_t n, cap;
718 uint64_t *tab; /* the constant table: the unit's u64 (re, im) words */
719#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
720 size_t ntab, tabcap;
721 struct { uint32_t kind, a, b; size_t at; } tk[16];
722 uint32_t ntk;
723 int alias;
724 uint64_t w_bytes, b_bytes;
725#endif
726} Many;
727typedef struct {
728 uint32_t rank, batch, oext[3], owords; /* output: extents, f32 words per element */
729 Lay olay;
730 uint64_t oextent;
731 int obasic;
732} ManyOut;
733
734static void many_free(Many *m) {
735 free(m->l);
736 free(m->tab);
737 memset(m, 0, sizeof *m);
738}
739
740static int many_build(const OmegaGpuFftMany *p, int in_place, int alias, Many *m, size_t bytes[OMEGA_GPU_FFT_MANY_BUFS],
741 ManyOut *o);
742static int many_each(uint32_t rank, const Lay *l, const uint32_t *ext, uint32_t batch,
743 int (*f)(void *, uint32_t), void *ctx);
744#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
745/* Caller holds the lock: byte offset of the unit's table (kind 10 twiddles, 11 chirp) of a points, appended once. */
746static int many_table(Many *m, uint32_t kind, uint32_t a, uint32_t b, uint64_t *off) {
747 for (uint32_t i = 0; i < m->ntk; i++)
748 if (m->tk[i].kind == kind && m->tk[i].a == a && m->tk[i].b == b) { *off = 8u * m->tk[i].at; return OMEGA_GPU_ML_OK; }
749 if (m->ntk == sizeof m->tk / sizeof m->tk[0]) return OMEGA_GPU_ML_CODEGEN_FAIL;
750 if (omega_gpu_om_ir_data(kind, a, b, 0, g_words) != OMEGA_OM_IR_OK) return OMEGA_GPU_ML_CODEGEN_FAIL;
751 if (m->ntab + a > m->tabcap) {
752 size_t cap = (m->ntab + a) * 2u;
753 uint64_t *t = (uint64_t *)realloc(m->tab, cap * sizeof *t);
754 if (!t) return OMEGA_GPU_ML_CHIP_FAIL;
755 m->tab = t;
756 m->tabcap = cap;
757 }
758 memcpy(&m->tab[m->ntab], g_words, (size_t)a * 8u);
759 m->tk[m->ntk].kind = kind; m->tk[m->ntk].a = a; m->tk[m->ntk].b = b; m->tk[m->ntk].at = m->ntab;
760 m->ntk++;
761 *off = 8u * m->ntab;
762 m->ntab += a;
763 return OMEGA_GPU_ML_OK;
764}
765
766/* Caller holds the lock: the unit's magic divisor of d (kind 13). */
767static int many_magic(uint32_t d, uint32_t *mg) {
768 if (omega_gpu_om_ir_data(13, d, 0, 0, g_words) != OMEGA_OM_IR_OK) return OMEGA_GPU_ML_CODEGEN_FAIL;
769 *mg = (uint32_t)g_words[0];
770 return OMEGA_GPU_ML_OK;
771}
772
773/* One step of `total` threads as launches of at most OMEGA_GPU_FFT_MAX_CTAS CTAs (word 1: first thread). */
774static int many_step(Many *m, uint32_t kind, uint32_t a, uint32_t b, uint32_t nptr, const uint32_t *buf,
775 const uint64_t *off, uint32_t nw, const uint32_t *w, uint64_t total, uint32_t out_words) {
776 OmegaGpuFftManyLaunch L;
777 memset(&L, 0, sizeof L);
778 L.kind = kind; L.a = a; L.b = b; L.nptr = nptr; L.nwords = nw;
779 for (uint32_t i = 0; i < nptr; i++) {
780 L.buf[i] = m->alias && buf[i] == OMEGA_GPU_FFT_BUF_OUT ? (uint32_t)OMEGA_GPU_FFT_BUF_IN : buf[i];
781 L.off[i] = off[i];
782 }
783 memcpy(L.words, w, nw * sizeof w[0]);
784 L.words[0] = (uint32_t)total;
785 L.threads = OMEGA_GPU_FFT_MANY_THREADS;
786 uint64_t ctas = (total + OMEGA_GPU_FFT_MANY_THREADS - 1u) / OMEGA_GPU_FFT_MANY_THREADS;
787 for (uint64_t c0 = 0; c0 < ctas; c0 += OMEGA_GPU_FFT_MAX_CTAS) {
788 L.grid = (uint32_t)(ctas - c0 < OMEGA_GPU_FFT_MAX_CTAS ? ctas - c0 : OMEGA_GPU_FFT_MAX_CTAS);
789 L.words[1] = (uint32_t)(c0 * OMEGA_GPU_FFT_MANY_THREADS);
790 uint64_t left = total - L.words[1], here = (uint64_t)L.grid * OMEGA_GPU_FFT_MANY_THREADS;
791 L.elements = (size_t)out_words * (here < left ? here : left);
792 if (m->n == m->cap) {
793 size_t cap = m->cap ? 2u * m->cap : 64u;
794 OmegaGpuFftManyLaunch *l = (OmegaGpuFftManyLaunch *)realloc(m->l, cap * sizeof *l);
795 if (!l) return OMEGA_GPU_ML_CHIP_FAIL;
796 m->l = l;
797 m->cap = cap;
798 }
799 m->l[m->n++] = L;
800 }
801 return OMEGA_GPU_ML_OK;
802}
803
804/* Stockham passes (kind 7) over rows of n points, radices rad[0..np-1], from buffer src ping-ponging with alt; the
805 * last pass writes fin when fin >= 0. *res: the buffer holding the result. */
806static int many_stockham(Many *m, uint32_t n, uint64_t rows, uint32_t dir, uint32_t np, const uint32_t *rad,
807 uint32_t src, uint32_t alt, int fin, uint64_t tw_off, uint32_t *res) {
808 uint32_t cur = src, ns = 1;
809 for (uint32_t i = 0; i < np; i++) {
810 uint32_t R = rad[i], nr = n / R, dst = (i + 1 == np && fin >= 0) ? (uint32_t)fin : cur == src ? alt : src;
811 uint32_t w[8] = { 0, 0, nr, 0, ns, 0, n / (ns * R), n };
812 int rc = many_magic(nr, &w[3]);
813 if (rc == OMEGA_GPU_ML_OK) rc = many_magic(ns, &w[5]);
814 uint32_t buf[3] = { cur, dst, OMEGA_GPU_FFT_BUF_TABLE };
815 uint64_t off[3] = { 0, 0, tw_off };
816 if (rc == OMEGA_GPU_ML_OK) rc = many_step(m, 7, R, dir, 3, buf, off, 8, w, rows * nr, 2u * R);
817 if (rc != OMEGA_GPU_ML_OK) return rc;
818 cur = dst;
819 ns *= R;
820 }
821 *res = cur;
822 return OMEGA_GPU_ML_OK;
823}
824
825/* One 1-D transform along contiguous rows of n points in buffer *cur (other: the second scratch array). The result
826 * lands in *cur (Bluestein, or an even pass count) or in other; *cur is updated. */
827static int many_axis(Many *m, uint32_t n, uint64_t rows, uint32_t dir, uint32_t *cur, uint32_t other) {
828 if (omega_gpu_om_ir_data(12, n, 0, 0, g_words) != OMEGA_OM_IR_OK) return OMEGA_GPU_ML_CODEGEN_FAIL;
829 uint32_t M = (uint32_t)g_words[0], blue = (uint32_t)g_words[1], np = (uint32_t)g_words[2], rad[16];
830 if (np == 0 || np > 16) return OMEGA_GPU_ML_CODEGEN_FAIL;
831 for (uint32_t i = 0; i < np; i++) rad[i] = (uint32_t)g_words[3 + i];
832 uint64_t tw, twi, ch;
833 int rc;
834 if (!blue) {
835 rc = many_table(m, 10, n, dir, &tw);
836 if (rc == OMEGA_GPU_ML_OK) rc = many_stockham(m, n, rows, dir, np, rad, *cur, other, -1, tw, cur);
837 return rc;
838 }
839 if (rows * M > ((uint64_t)1 << 28)) return OMEGA_GPU_ML_TOO_LARGE;
840 if (rows * M * 8u > m->w_bytes) m->w_bytes = rows * M * 8u;
841 if ((uint64_t)M * 8u > m->b_bytes) m->b_bytes = (uint64_t)M * 8u;
842 uint32_t lgm = 0;
843 while ((1u << lgm) < M) lgm++;
844 rc = many_table(m, 11, n, dir, &ch);
845 if (rc == OMEGA_GPU_ML_OK) rc = many_table(m, 10, M, 0, &tw);
846 if (rc == OMEGA_GPU_ML_OK) rc = many_table(m, 10, M, 1, &twi);
847 if (rc != OMEGA_GPU_ML_OK) return rc;
848 const uint32_t W = OMEGA_GPU_FFT_BUF_W, W2 = OMEGA_GPU_FFT_BUF_W2, B = OMEGA_GPU_FFT_BUF_B, T = OMEGA_GPU_FFT_BUF_TABLE;
849 uint32_t w[3] = { 0, 0, n }, x, y;
850 /* the filter: b[m] = conj chirp[min(m, M - m)] (0 past n), forward FFT into B */
851 uint32_t bb[3] = { T, W, T };
852 uint64_t ob[3] = { ch, 0, ch };
853 rc = many_step(m, 8, 3, lgm, 3, bb, ob, 3, w, M, 2);
854 if (rc == OMEGA_GPU_ML_OK) rc = many_stockham(m, M, 1, 0, np, rad, W, W2, (int)B, tw, &x);
855 /* a = x chirp zero-padded to M, A = FFT(a), A B, inverse FFT, chirp / M on the first n points */
856 uint32_t pb[3] = { *cur, W, T };
857 uint64_t po[3] = { 0, 0, ch };
858 if (rc == OMEGA_GPU_ML_OK) rc = many_step(m, 8, 0, lgm, 3, pb, po, 3, w, rows * M, 2);
859 if (rc == OMEGA_GPU_ML_OK) rc = many_stockham(m, M, rows, 0, np, rad, W, W2, -1, tw, &x);
860 uint32_t mb[3] = { x, x, B };
861 uint64_t mo[3] = { 0, 0, 0 };
862 if (rc == OMEGA_GPU_ML_OK) rc = many_step(m, 8, 1, lgm, 3, mb, mo, 3, w, rows * M, 2);
863 if (rc == OMEGA_GPU_ML_OK) rc = many_stockham(m, M, rows, 1, np, rad, x, x == W ? W2 : W, -1, twi, &y);
864 uint32_t qb[3] = { y, *cur, T };
865 if (rc == OMEGA_GPU_ML_OK) rc = many_step(m, 8, 2, lgm, 3, qb, po, 3, w, rows * M, 2);
866 return rc;
867}
868
869/* A strided copy (kind 9): slot k (i0 fastest) walks logical axis ord[k] (-1: none) over ext[k] points. */
870static int many_copy(Many *m, uint32_t mode, uint32_t sbuf, const Lay *s, uint32_t dbuf, const Lay *d, const int ord[3],
871 const uint32_t ext[3], uint32_t batch) {
872 uint32_t w[18];
873 memset(w, 0, sizeof w);
874 uint64_t total = batch;
875 for (int k = 0; k < 3; k++) {
876 uint32_t e = ord[k] < 0 ? 1u : ext[k];
877 if (e == 0) return OMEGA_GPU_ML_OK;
878 total *= e;
879 w[2 + 2 * k] = e;
880 int rc = many_magic(e, &w[3 + 2 * k]);
881 if (rc != OMEGA_GPU_ML_OK) return rc;
882 w[10 + k] = ord[k] < 0 ? 0u : s->st[ord[k]];
883 w[15 + k] = ord[k] < 0 ? 0u : d->st[ord[k]];
884 }
885 w[8] = s->base; w[9] = s->bs; w[13] = d->base; w[14] = d->bs;
886 uint32_t buf[2] = { sbuf, dbuf };
887 uint64_t off[2] = { 0, 0 };
888 return many_step(m, 9, mode, 0, 2, buf, off, 18, w, total, mode == 2 ? 1u : 2u);
889}
890
891/* The contiguous layout of extents e with axis perm[0] slowest. */
892static Lay many_dense(uint32_t rank, const uint32_t *perm, const uint32_t *e) {
893 Lay l;
894 memset(&l, 0, sizeof l);
895 uint32_t p = 1;
896 for (int k = (int)rank - 1; k >= 0; k--) { l.st[perm[k]] = p; p *= e[perm[k]]; }
897 l.bs = p;
898 return l;
899}
900
901/* Rows of a transform along axis ax: batch times the other extents. */
902static uint64_t many_rows(uint32_t rank, const uint32_t *e, uint32_t ax, uint32_t batch) {
903 uint64_t rows = batch;
904 for (uint32_t i = 0; i < rank; i++)
905 if (i != ax) rows *= e[i];
906 return rows;
907}
908
909/* Slots of a copy that walks the destination order perm (fastest first). */
910static void many_slots(uint32_t rank, const uint32_t *perm, const uint32_t *e, int ord[3], uint32_t ext[3]) {
911 for (uint32_t k = 0; k < 3; k++) {
912 ord[k] = k < rank ? (int)perm[rank - 1 - k] : -1;
913 ext[k] = k < rank ? e[perm[rank - 1 - k]] : 1u;
914 }
915}
916
917/* A user side: extents ext, embed (all zero: basic), stride, dist; real pads the basic last axis to pad points. */
918static int many_side(uint32_t rank, const uint32_t *ext, const uint32_t *emb, uint32_t stride, uint32_t dist,
919 uint32_t batch, int basic, uint32_t pad, Lay *l, uint64_t *extent) {
920 uint32_t e[3];
921 uint64_t st[3], prod = 1;
922 for (uint32_t i = 0; i < rank; i++) e[i] = basic ? ext[i] : emb[i];
923 if (basic) {
924 if (pad) e[rank - 1] = pad;
925 stride = 1;
926 for (uint32_t i = 0; i < rank; i++) prod *= e[i];
927 if (prod >= ((uint64_t)1 << 32)) return OMEGA_GPU_ML_TOO_LARGE;
928 dist = (uint32_t)prod;
929 } else {
930 if (stride == 0) return OMEGA_GPU_ML_BAD_ARGS;
931 for (uint32_t i = 1; i < rank; i++)
932 if (e[i] < ext[i]) return OMEGA_GPU_ML_BAD_ARGS;
933 }
934 uint64_t p = stride, hi = (uint64_t)dist * (batch - 1u);
935 for (int i = (int)rank - 1; i >= 0; i--) {
936 if (p >= ((uint64_t)1 << 32)) return OMEGA_GPU_ML_TOO_LARGE;
937 st[i] = p;
938 hi += (ext[i] - 1u) * p;
939 p *= e[i];
940 }
941 if (hi >= ((uint64_t)1 << 32) || hi + 1u > ((uint64_t)1 << 28)) return OMEGA_GPU_ML_TOO_LARGE;
942 memset(l, 0, sizeof *l);
943 l->bs = dist;
944 for (uint32_t i = 0; i < rank; i++) l->st[i] = (uint32_t)st[i];
945 *extent = hi + 1u;
946 return OMEGA_GPU_ML_OK;
947}
948
949/* Visits every element index of a side (batch major, last axis fastest); stops on a nonzero return. */
950static int many_each(uint32_t rank, const Lay *l, const uint32_t *ext, uint32_t batch,
951 int (*f)(void *ctx, uint32_t idx), void *ctx) {
952 uint32_t e[3] = { 1, 1, 1 }, s[3] = { 0, 0, 0 };
953 for (uint32_t i = 0; i < rank; i++) { e[3 - rank + i] = ext[i]; s[3 - rank + i] = l->st[i]; }
954 for (uint32_t b = 0; b < batch; b++)
955 for (uint32_t x0 = 0; x0 < e[0]; x0++)
956 for (uint32_t x1 = 0; x1 < e[1]; x1++)
957 for (uint32_t x2 = 0; x2 < e[2]; x2++) {
958 int rc = f(ctx, l->base + b * l->bs + x0 * s[0] + x1 * s[1] + x2 * s[2]);
959 if (rc) return rc;
960 }
961 return 0;
962}
963
964static int many_mark(void *ctx, uint32_t idx) {
965 uint8_t *bits = (uint8_t *)ctx;
966 uint8_t bit = (uint8_t)(1u << (idx & 7u));
967 if (bits[idx >> 3] & bit) return 1;
968 bits[idx >> 3] |= bit;
969 return 0;
970}
971
972
973/* Caller holds the lock. Validates p, fills bytes[] and m's launches and table; *o describes the output side. */
974static int many_build(const OmegaGpuFftMany *p, int in_place, int alias, Many *m, size_t bytes[OMEGA_GPU_FFT_MANY_BUFS],
975 ManyOut *o) {
976 memset(m, 0, sizeof *m);
977 m->alias = alias;
978 if (!p || !bytes || p->rank < 1 || p->rank > 3 || p->batch == 0 || (unsigned)p->type > OMEGA_GPU_FFT_TYPE_C2R)
979 return OMEGA_GPU_ML_BAD_ARGS;
980 if (p->type == OMEGA_GPU_FFT_TYPE_C2C ? (p->inverse != 0 && p->inverse != 1) : p->inverse != 0)
981 return OMEGA_GPU_ML_BAD_ARGS;
982 const uint32_t r = p->rank, last = r - 1u, L = p->n[last], h = L / 2u + 1u;
983 uint64_t P = 1;
984 int ibasic = 1, obasic = 1;
985 for (uint32_t i = 0; i < r; i++) {
986 if (p->n[i] < 2 || p->n[i] > OMEGA_GPU_FFT_MANY_MAX_N) return OMEGA_GPU_ML_BAD_ARGS;
987 P *= p->n[i];
988 ibasic &= p->inembed[i] == 0;
989 obasic &= p->onembed[i] == 0;
990 }
991 if (ibasic != obasic) return OMEGA_GPU_ML_BAD_ARGS;
992 if (P * p->batch > OMEGA_GPU_FFT_MAX_ELEMENTS) return OMEGA_GPU_ML_TOO_LARGE;
993 uint32_t iext[3], oext[3];
994 memcpy(iext, p->n, sizeof iext);
995 memcpy(oext, p->n, sizeof oext);
996 int r2c = p->type == OMEGA_GPU_FFT_TYPE_R2C, c2r = p->type == OMEGA_GPU_FFT_TYPE_C2R;
997 if (c2r) iext[last] = h;
998 if (r2c) oext[last] = h;
999 Lay il, ol;
1000 uint64_t iextent, oextent;
1001 int rc = many_side(r, iext, p->inembed, p->istride, p->idist, p->batch, ibasic, r2c && in_place ? 2u * h : 0u, &il,
1002 &iextent);
1003 if (rc == OMEGA_GPU_ML_OK)
1004 rc = many_side(r, oext, p->onembed, p->ostride, p->odist, p->batch, obasic, c2r && in_place ? 2u * h : 0u, &ol,
1005 &oextent);
1006 if (rc != OMEGA_GPU_ML_OK) return rc;
1007 if (!obasic) {
1008 uint8_t *bits = (uint8_t *)calloc((size_t)(oextent + 7u) / 8u, 1);
1009 if (!bits) return OMEGA_GPU_ML_CHIP_FAIL;
1010 int dup = many_each(r, &ol, oext, p->batch, many_mark, bits);
1011 free(bits);
1012 if (dup) return OMEGA_GPU_ML_BAD_ARGS;
1013 }
1014 const uint32_t S0 = OMEGA_GPU_FFT_BUF_S0, S1 = OMEGA_GPU_FFT_BUF_S1;
1015 const uint32_t dir = c2r ? 1u : (uint32_t)p->inverse;
1016 uint32_t perm[3] = { 0, 1, 2 }, e[3], cur = S0, ext[3];
1017 int ord[3];
1018 memcpy(e, iext, sizeof e);
1019 if (r2c) e[last] = L;
1020 if (c2r) /* the last axis slowest: the outer inverses come first */
1021 for (uint32_t k = 0; k < r; k++) perm[k] = k == 0 ? last : k - 1u;
1022 Lay cl = many_dense(r, perm, e);
1023 many_slots(r, perm, e, ord, ext);
1024 rc = many_copy(m, r2c ? 1u : 0u, OMEGA_GPU_FFT_BUF_IN, &il, S0, &cl, ord, ext, p->batch);
1025 uint32_t nax = c2r ? r - 1u : r;
1026 for (uint32_t t = 0; t < nax && rc == OMEGA_GPU_ML_OK; t++) {
1027 uint32_t ax = perm[r - 1u], other = cur == S0 ? S1 : S0;
1028 rc = many_axis(m, p->n[ax], many_rows(r, e, ax, p->batch), dir, &cur, other);
1029 if (r2c && ax == last) e[last] = h;
1030 if (rc != OMEGA_GPU_ML_OK || (t + 1u == nax && !c2r)) break;
1031 /* rotate: the fastest axis becomes the slowest */
1032 uint32_t np[3];
1033 for (uint32_t k = 0; k < r; k++) np[k] = k == 0 ? perm[r - 1u] : perm[k - 1u];
1034 Lay nl = many_dense(r, np, e);
1035 many_slots(r, np, e, ord, ext);
1036 other = cur == S0 ? S1 : S0;
1037 rc = many_copy(m, 0, cur, &cl, other, &nl, ord, ext, p->batch);
1038 memcpy(perm, np, sizeof perm);
1039 cl = nl;
1040 cur = other;
1041 }
1042 if (rc == OMEGA_GPU_ML_OK && c2r) {
1043 /* Hermitian extension of the last axis: k < h as it is, k in [h, L) the conjugate of point L - k */
1044 uint32_t other = cur == S0 ? S1 : S0, fe[3];
1045 memcpy(fe, e, sizeof fe);
1046 fe[last] = L;
1047 Lay fl = many_dense(r, perm, fe);
1048 many_slots(r, perm, e, ord, ext);
1049 rc = many_copy(m, 0, cur, &cl, other, &fl, ord, ext, p->batch);
1050 if (rc == OMEGA_GPU_ML_OK && L > h) {
1051 Lay sl = cl, dl = fl;
1052 sl.base = L - h;
1053 sl.st[last] = 0xFFFFFFFFu;
1054 dl.base = h;
1055 ext[0] = L - h;
1056 rc = many_copy(m, 3, cur, &sl, other, &dl, ord, ext, p->batch);
1057 }
1058 cl = fl;
1059 cur = other;
1060 e[last] = L;
1061 if (rc == OMEGA_GPU_ML_OK) rc = many_axis(m, L, many_rows(r, e, last, p->batch), dir, &cur, cur == S0 ? S1 : S0);
1062 }
1063 if (rc == OMEGA_GPU_ML_OK) {
1064 uint32_t id[3] = { 0, 1, 2 };
1065 many_slots(r, id, e, ord, ext);
1066 rc = many_copy(m, c2r ? 2u : 0u, cur, &cl, OMEGA_GPU_FFT_BUF_OUT, &ol, ord, ext, p->batch);
1067 }
1068 if (rc != OMEGA_GPU_ML_OK) return rc;
1069 uint64_t ib = iextent * (r2c ? 4u : 8u), ob = oextent * (c2r ? 4u : 8u);
1070 bytes[OMEGA_GPU_FFT_BUF_IN] = alias ? (ib > ob ? ib : ob) : ib;
1071 bytes[OMEGA_GPU_FFT_BUF_OUT] = alias ? 0u : ob;
1072 bytes[OMEGA_GPU_FFT_BUF_S0] = bytes[OMEGA_GPU_FFT_BUF_S1] = (size_t)(P * p->batch * 8u);
1073 bytes[OMEGA_GPU_FFT_BUF_W] = bytes[OMEGA_GPU_FFT_BUF_W2] = (size_t)m->w_bytes;
1074 bytes[OMEGA_GPU_FFT_BUF_B] = (size_t)m->b_bytes;
1075 bytes[OMEGA_GPU_FFT_BUF_TABLE] = m->ntab * 8u;
1076 if (o) {
1077 o->rank = r;
1078 o->batch = p->batch;
1079 memcpy(o->oext, oext, sizeof oext);
1080 o->owords = c2r ? 1u : 2u;
1081 o->olay = ol;
1082 o->oextent = oextent;
1083 o->obasic = obasic;
1084 }
1085 return OMEGA_GPU_ML_OK;
1086}
1087#endif
1088
1089int omega_gpu_fft_many_walk(const OmegaGpuFftMany *plan, int in_place, size_t bytes[OMEGA_GPU_FFT_MANY_BUFS],
1090 OmegaGpuFftManyLaunchFn fn, void *ctx) {
1091 if (in_place != 0 && in_place != 1) return OMEGA_GPU_ML_BAD_ARGS;
1092 Many m;
1093 omega_gpu_session_lock();
1094 int rc = many_build(plan, in_place, in_place, &m, bytes, NULL);
1095 omega_gpu_session_unlock();
1096 for (size_t i = 0; fn && rc == OMEGA_GPU_ML_OK && i < m.n; i++) {
1097 m.l[i].table = m.tab;
1098 rc = fn(ctx, &m.l[i]);
1099 }
1100 many_free(&m);
1101 return rc;
1102}
1103
1104typedef struct { const uint32_t *dev; uint32_t *out; uint32_t words; uint64_t unwritten; } ManyScan;
1105#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
1106static int many_scan(void *ctx, uint32_t idx) {
1107 ManyScan *c = (ManyScan *)ctx;
1108 for (uint32_t k = 0; k < c->words; k++) c->unwritten += c->dev[(size_t)idx * c->words + k] == ML_POISON32;
1109 return 0;
1110}
1111#else
1112static int many_scan(void *ctx, uint32_t idx) {
1113 ManyScan *c = (ManyScan *)ctx;
1114 size_t first = omega_mh_mul(idx, c->words);
1115 for (uint32_t k = 0; k < c->words; k++)
1116 c->unwritten = omega_mh_add(c->unwritten, omega_mh_poison(c->dev[first + k], 0, 0, 0));
1117 return 0;
1118}
1119#endif
1120static int many_copy_back(void *ctx, uint32_t idx) {
1121 ManyScan *c = (ManyScan *)ctx;
1122 size_t at = ml_mul(idx, c->words);
1123 memcpy(&c->out[at], &c->dev[at], ml_mul(4, c->words));
1124 return 0;
1125}
1126
1127int omega_gpu_fft_many(const OmegaGpuFftMany *plan, const float *in, float *out, OmegaGpuMathInfo *info) {
1128 if (!plan || !in || !out) return OMEGA_GPU_ML_BAD_ARGS;
1129 if (info) memset(info, 0, sizeof *info);
1130 uint64_t t0 = now_ns();
1131 int in_place = (const void *)in == (const void *)out;
1132 size_t bytes[OMEGA_GPU_FFT_MANY_BUFS];
1133 Many m;
1134 ManyOut o;
1135 omega_gpu_session_lock();
1136 int rc = many_build(plan, in_place, 0, &m, bytes, &o);
1137 const char *pi = (const char *)in, *po = (const char *)out;
1138 if (rc == OMEGA_GPU_ML_OK && !in_place &&
1139 ml_overlap(pi, bytes[OMEGA_GPU_FFT_BUF_IN], po, bytes[OMEGA_GPU_FFT_BUF_OUT]))
1140 rc = OMEGA_GPU_ML_BAD_ARGS;
1141 if (rc == OMEGA_GPU_ML_OK) {
1142 if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; }
1143 if (!omega_gpu_session_open()) rc = OMEGA_GPU_ML_CHIP_FAIL;
1144 }
1145 OmegaGpuScratch *buf[OMEGA_GPU_FFT_MANY_BUFS] = { &g_in, &g_out, &g_scr, &g_s1, &g_w, &g_w2, &g_b, &g_tw };
1146 for (int i = 0; rc == OMEGA_GPU_ML_OK && i < OMEGA_GPU_FFT_MANY_BUFS; i++)
1147 if (omega_gpu_session_scratch(buf[i], bytes[i] ? bytes[i] : 8u) != 0) rc = OMEGA_GPU_ML_CHIP_FAIL;
1148 if (rc == OMEGA_GPU_ML_OK) {
1149 memcpy(g_in.mem.cpu, in, bytes[OMEGA_GPU_FFT_BUF_IN]);
1150 memcpy(g_tw.mem.cpu, m.tab, bytes[OMEGA_GPU_FFT_BUF_TABLE]);
1151 uint32_t *dev = (uint32_t *)g_out.mem.cpu;
1152 for (size_t i = 0; i < bytes[OMEGA_GPU_FFT_BUF_OUT] / 4u; i++) dev[i] = ML_POISON32;
1153 if (omega_gpu_session_sync_to_gpu(&g_in.mem) != 0 || omega_gpu_session_sync_to_gpu(&g_tw.mem) != 0 ||
1154 omega_gpu_session_sync_to_gpu(&g_out.mem) != 0)
1155 rc = OMEGA_GPU_ML_CHIP_FAIL;
1156 for (size_t i = 0; rc == OMEGA_GPU_ML_OK && i < m.n; i++) {
1157 const OmegaGpuFftManyLaunch *l = &m.l[i];
1158 Slot *s;
1159 bool hit;
1160 rc = kernel_for(l->kind, l->a, l->b, l->threads, true, &s, &hit);
1161 if (rc != OMEGA_GPU_ML_OK) break;
1162 info_kernel(info, s, hit);
1163 uint32_t args[OMEGA_GPU_SESSION_MAX_ARGS];
1164 for (uint32_t k = 0; k < l->nptr; k++) put64(&args[2 * k], buf[l->buf[k]]->mem.va + l->off[k]);
1165 memcpy(&args[2 * l->nptr], l->words, l->nwords * sizeof l->words[0]);
1166 rc = launch(s, args, 2 * l->nptr + l->nwords, l->grid, l->elements, 0, info);
1167 }
1168 if (rc == OMEGA_GPU_ML_OK && omega_gpu_session_sync_to_cpu(&g_out.mem) != 0) rc = OMEGA_GPU_ML_CHIP_FAIL;
1169 if (rc == OMEGA_GPU_ML_OK) {
1170 /* only the addressed output elements are checked and copied back */
1171 ManyScan c = { dev, (uint32_t *)(void *)out, o.owords, 0 };
1172 (void)many_each(o.rank, &o.olay, o.oext, o.batch, many_scan, &c);
1173 if (info) info->unwritten_words = c.unwritten;
1174 if (c.unwritten) rc = OMEGA_GPU_ML_UNWRITTEN;
1175 else (void)many_each(o.rank, &o.olay, o.oext, o.batch, many_copy_back, &c);
1176 }
1177 }
1178 omega_gpu_session_unlock();
1179 many_free(&m);
1180 if (info) info->call_ns = ml_sub(now_ns(), t0);
1181 return rc;
1182}
1183
1184/* The legacy entry points' other lengths: basic layout, in and out may not overlap. */
1185static int many_basic(OmegaGpuFftType type, uint32_t rank, const uint32_t *n, uint32_t batch, int inverse,
1186 const float *in, float *out, OmegaGpuMathInfo *info) {
1187#if CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
1188 if (!in || !out || in == out || !n || rank < 1 || rank > 3) return OMEGA_GPU_ML_BAD_ARGS;
1189#else
1190 if (omega_mh_basic(rank, (uintptr_t)in, (uintptr_t)out, (uintptr_t)n)) return OMEGA_GPU_ML_BAD_ARGS;
1191#endif
1192 OmegaGpuFftMany p;
1193 memset(&p, 0, sizeof p);
1194 p.rank = rank;
1195 memcpy(p.n, n, rank * sizeof n[0]);
1196 p.batch = batch;
1197 p.type = type;
1198 p.inverse = inverse;
1199 return omega_gpu_fft_many(&p, in, out, info);
1200}
1201
1202#if !CUDAPP_MIGRATION_PENDING_C_MATHLIBS_HOST
1203#include "omega_gpu_mathlibs_host.inc"
1204#endif