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_engine.h

259 lines · 13162 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * Omega GPU Engine: run one already-built GPU kernel job and return SUCCESS or
4 * exactly one named failure.
5 *
6 * Purpose: today several launcher files repeat the same open / alloc / copy-in /
7 * pushbuffer / submit / completion-wait / readback / close sequence with three
8 * different error styles. This module owns that sequence once. Design:
9 * docs/numeric/OMEGA_GPU_ENGINE.md (the first-cut plan is Drake's, 2026-10-02).
10 *
11 * Public entry point:
12 * int omega_gpu_execute(const OmegaGpuJob *job, OmegaGpuResult *result);
13 * It returns OMEGA_GPU_ENGINE_OK (0) on success, otherwise the failure code,
14 * which is also stored in result->failure.
15 *
16 * States: INITIAL -> PREPARED -> SUBMITTED -> GPU_COMPLETE -> OUTPUT_VISIBLE ->
17 * OUTPUT_PRODUCED -> SUCCESS. The result records the last completed state and
18 * the precise failed step. (The earlier draft of this header called the final
19 * state COMMITTED and the first one NONE.)
20 *
21 * Poison: the wrapper supplies one poison word per output word. The engine
22 * writes those words to the device output before submit and, after readback,
23 * checks only whether any word is still equal to its poison word. The engine
24 * never chooses poison and never judges whether the math is right.
25 *
26 * Uncertain completion: if the engine cannot tell whether the GPU finished (a
27 * completion wait timed out, or the device returned an unknown state), it keeps
28 * the device context and every allocation (no free, no close), records that in
29 * the result, and blocks every later omega_gpu_execute call in this process,
30 * which then returns UNCERTAIN_COMPLETION_BLOCKED.
31 *
32 * STATUS: A3a engine core. omega_gpu_execute drives the backend table below
33 * through the whole state machine; only a fake backend exists (host tests).
34 * With no backend set it still returns INTERNAL_INVARIANT at step
35 * NOT_IMPLEMENTED. The real Blackwell backend is a later cut (A3b). This cut is
36 * not chip-qualified (host NOT_RUN, chip NOT_RUN).
37 */
38#ifndef OMEGA_GPU_ENGINE_H
39#define OMEGA_GPU_ENGINE_H
40
41#include <stddef.h>
42#include <stdint.h>
43
44/* One failure name per run. OK is 0, so the return value reads as a boolean. */
45typedef enum {
46 OMEGA_GPU_ENGINE_OK = 0,
47 OMEGA_GPU_ENGINE_DEVICE_OPEN, /* device could not be opened */
48 OMEGA_GPU_ENGINE_CHANNEL_CREATE, /* channel could not be created */
49 OMEGA_GPU_ENGINE_ALLOC, /* a device buffer could not be allocated */
50 OMEGA_GPU_ENGINE_PREPARE, /* copy-in, poison fill or descriptor build failed */
51 OMEGA_GPU_ENGINE_SUBMIT, /* submitting the work failed */
52 OMEGA_GPU_ENGINE_COMPLETION_WAIT, /* marker or marker2 wait failed or timed out */
53 OMEGA_GPU_ENGINE_RELEASE_WAIT, /* release semaphore wait failed or timed out */
54 OMEGA_GPU_ENGINE_VISIBILITY_WAIT, /* barrier or readback failed or timed out */
55 OMEGA_GPU_ENGINE_OUTPUT_UNCHANGED, /* after readback, output words still equal their poison */
56 OMEGA_GPU_ENGINE_INVALID_ARGS, /* the job or the result pointer is malformed */
57 OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, /* the engine or its backend broke its own contract */
58 OMEGA_GPU_ENGINE_CLEANUP, /* free or close failed after an otherwise good run */
59 OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED, /* an earlier job left completion uncertain */
60 OMEGA_GPU_ENGINE_FAILURE_COUNT /* not a failure: number of names above */
61} OmegaGpuEngineFailure;
62
63/* How far a run got. Success is only SUCCESS. */
64typedef enum {
65 OMEGA_GPU_ENGINE_STATE_INITIAL = 0, /* nothing completed yet (device may be unopened) */
66 OMEGA_GPU_ENGINE_STATE_PREPARED,
67 OMEGA_GPU_ENGINE_STATE_SUBMITTED,
68 OMEGA_GPU_ENGINE_STATE_GPU_COMPLETE,
69 OMEGA_GPU_ENGINE_STATE_OUTPUT_VISIBLE,
70 OMEGA_GPU_ENGINE_STATE_OUTPUT_PRODUCED,
71 OMEGA_GPU_ENGINE_STATE_SUCCESS,
72 OMEGA_GPU_ENGINE_STATE_COUNT /* not a state: number of names above */
73} OmegaGpuEngineState;
74
75/* Which completion wait failed (meaningful for COMPLETION_WAIT and RELEASE_WAIT). */
76typedef enum {
77 OMEGA_GPU_ENGINE_WAIT_NONE = 0,
78 OMEGA_GPU_ENGINE_WAIT_MARKER,
79 OMEGA_GPU_ENGINE_WAIT_RELEASE_SEMAPHORE,
80 OMEGA_GPU_ENGINE_WAIT_MARKER2,
81 OMEGA_GPU_ENGINE_WAIT_COUNT /* not a wait: number of names above */
82} OmegaGpuEngineWait;
83
84/* The precise step that failed (the step the run was in when it stopped). */
85typedef enum {
86 OMEGA_GPU_ENGINE_STEP_NONE = 0, /* no step failed (success) */
87 OMEGA_GPU_ENGINE_STEP_VALIDATE,
88 OMEGA_GPU_ENGINE_STEP_BACKEND_CHECK,
89 OMEGA_GPU_ENGINE_STEP_BLOCKED,
90 OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED,
91 OMEGA_GPU_ENGINE_STEP_OPEN_DEVICE,
92 OMEGA_GPU_ENGINE_STEP_CREATE_CHANNEL,
93 OMEGA_GPU_ENGINE_STEP_ALLOC,
94 OMEGA_GPU_ENGINE_STEP_COPY_IN,
95 OMEGA_GPU_ENGINE_STEP_FILL_POISON,
96 OMEGA_GPU_ENGINE_STEP_BUILD,
97 OMEGA_GPU_ENGINE_STEP_SUBMIT,
98 OMEGA_GPU_ENGINE_STEP_WAIT_MARKER,
99 OMEGA_GPU_ENGINE_STEP_WAIT_MARKER2,
100 OMEGA_GPU_ENGINE_STEP_WAIT_RELEASE_SEMAPHORE,
101 OMEGA_GPU_ENGINE_STEP_BARRIER,
102 OMEGA_GPU_ENGINE_STEP_COPY_OUT,
103 OMEGA_GPU_ENGINE_STEP_SCAN,
104 OMEGA_GPU_ENGINE_STEP_FREE,
105 OMEGA_GPU_ENGINE_STEP_CLOSE,
106 OMEGA_GPU_ENGINE_STEP_CLOCK_CHECK, /* freestanding build only: no platform clock installed */
107 OMEGA_GPU_ENGINE_STEP_COUNT /* not a step: number of names above */
108} OmegaGpuEngineStep;
109
110/* Argument layouts the engine accepts. Later layouts are separate qualified cuts. */
111typedef enum {
112 OMEGA_GPU_LAYOUT_VECTOR_1D = 0, /* two inputs, one output, element_count 32-bit words each */
113 OMEGA_GPU_LAYOUT_COUNT /* not a layout: number of layouts above */
114} OmegaGpuLayout;
115
116/* Job flags. A zero flags word means every protection ON. */
117#define OMEGA_GPU_ENGINE_FLAG_NO_C3 0x1u /* A/B control arm only: skip L2 flush and marker2 */
118
119#define OMEGA_GPU_ENGINE_JOB_OUTPUTS 1u /* VECTOR_1D has exactly one output buffer */
120#define OMEGA_GPU_ENGINE_SEMAPHORE_WORDS 1u /* the vector path uses one semaphore word */
121#define OMEGA_GPU_ENGINE_DRV_TEXT_BYTES 128u
122
123/* Per-step timeouts in milliseconds. Never hard-coded by the engine. Each wait
124 * that is enabled needs a value greater than 0 (marker2_ms is ignored with NO_C3). */
125typedef struct {
126 uint64_t marker_ms;
127 uint64_t release_semaphore_ms;
128 uint64_t marker2_ms;
129 uint64_t visibility_ms;
130} OmegaGpuTimeouts;
131
132typedef struct {
133 const void *program; /* encoded program bytes, already built by the caller */
134 size_t program_len; /* bytes, > 0 */
135 int layout; /* OmegaGpuLayout; only VECTOR_1D today */
136 uint32_t element_count; /* > 0; each buffer below holds element_count 32-bit words */
137 const void *input_a; /* element_count * 4 bytes */
138 size_t input_a_len;
139 const void *input_b; /* element_count * 4 bytes */
140 size_t input_b_len;
141 void *output; /* host destination, 4-byte aligned, element_count * 4 bytes */
142 size_t output_len;
143 const uint32_t *poison; /* wrapper-supplied, one word per output word */
144 size_t poison_count; /* must equal element_count */
145 OmegaGpuTimeouts timeouts;
146 uint32_t flags; /* OMEGA_GPU_ENGINE_FLAG_* */
147} OmegaGpuJob;
148
149typedef struct {
150 int failure; /* OmegaGpuEngineFailure, OK on success */
151 int last_state; /* last COMPLETED OmegaGpuEngineState */
152 int failed_step; /* OmegaGpuEngineStep, NONE on success */
153 int wait; /* OmegaGpuEngineWait, NONE unless a completion or release wait failed */
154 uint64_t wait_timeout_ms; /* the configured limit of that wait */
155 uint64_t waited_ms; /* how long the engine actually waited before giving up */
156 int drv_rc; /* driver return code of the failing call, 0 if none */
157 int err_no; /* errno captured right after the failing call, 0 if none */
158 char drv_text[OMEGA_GPU_ENGINE_DRV_TEXT_BYTES]; /* driver message, NUL terminated, "" if none */
159 uint32_t sync_valid; /* OMEGA_GPU_SYNC_* mask: which observed values below are meaningful */
160 uint32_t marker; /* observed first marker word */
161 uint32_t marker2; /* observed second marker word */
162 uint32_t semaphore[OMEGA_GPU_ENGINE_SEMAPHORE_WORDS]; /* observed semaphore words */
163 int retained; /* 1 when context and allocations were kept (uncertain completion) */
164 int cleanup_failed; /* 1 when free or close failed (first failure is never overwritten) */
165 size_t n_outputs; /* number of valid entries in output_unchanged_words */
166 uint64_t output_unchanged_words[OMEGA_GPU_ENGINE_JOB_OUTPUTS]; /* words still equal to poison, per output */
167} OmegaGpuResult;
168
169#define OMEGA_GPU_SYNC_MARKER 0x1u
170#define OMEGA_GPU_SYNC_MARKER2 0x2u
171#define OMEGA_GPU_SYNC_SEMAPHORE 0x4u
172
173/*
174 * Backend seam. A fake backend implements these to drive every transition in
175 * host tests; the Blackwell GB10 backend (src/omega_blackwell_engine.c, later
176 * cut) wraps the m16_native_* calls the launchers use today. All callbacks
177 * must be non-NULL. Every callback returns 0 on success. The three wait
178 * callbacks and the barrier may also return:
179 */
180#define OMEGA_GPU_BACKEND_TIMEOUT 1 /* the wait ran out of time */
181#define OMEGA_GPU_BACKEND_UNKNOWN_STATE 2 /* the device reported a state the backend cannot classify */
182/* Any other nonzero value is a driver fault; the backend reports its raw driver code via diagnostics. */
183
184typedef enum {
185 OMEGA_GPU_BUF_SCRATCH = 0, /* backend-internal: pushbuffer, marker page, QMD */
186 OMEGA_GPU_BUF_PROGRAM,
187 OMEGA_GPU_BUF_INPUT_A,
188 OMEGA_GPU_BUF_INPUT_B,
189 OMEGA_GPU_BUF_OUTPUT,
190 OMEGA_GPU_BUF_ROLE_COUNT /* not a role: number of roles above */
191} OmegaGpuBufRole;
192
193typedef struct {
194 int drv_rc;
195 char drv_text[OMEGA_GPU_ENGINE_DRV_TEXT_BYTES];
196 uint32_t sync_valid; /* OMEGA_GPU_SYNC_* */
197 uint32_t marker;
198 uint32_t marker2;
199 uint32_t semaphore[OMEGA_GPU_ENGINE_SEMAPHORE_WORDS];
200} OmegaGpuBackendDiag;
201
202typedef struct {
203 void *ctx; /* passed to every callback; owned by the backend */
204 int (*open_device)(void *ctx);
205 int (*create_channel)(void *ctx);
206 int (*alloc)(void *ctx, int role, size_t bytes); /* SCRATCH gets bytes == 0: the backend sizes it */
207 int (*copy_in)(void *ctx, int role, const void *src, size_t bytes);
208 int (*fill_poison)(void *ctx, const uint32_t *words, size_t count); /* device-side output pre-fill */
209 int (*build)(void *ctx, const OmegaGpuJob *job); /* descriptors, QMD, pushbuffer */
210 int (*submit)(void *ctx);
211 int (*wait_marker)(void *ctx, uint64_t timeout_ms);
212 int (*wait_marker2)(void *ctx, uint64_t timeout_ms);
213 int (*wait_release_semaphore)(void *ctx, uint64_t timeout_ms);
214 int (*barrier)(void *ctx, uint64_t timeout_ms); /* visibility before readback */
215 int (*copy_out)(void *ctx, void *dst, size_t bytes);
216 void (*diagnostics)(void *ctx, OmegaGpuBackendDiag *diag); /* driver rc, text, observed words */
217 int (*free_buf)(void *ctx, int role);
218 int (*close)(void *ctx);
219} OmegaGpuBackend;
220
221/* Runs one job on the process-wide default backend. See the header comment. */
222int omega_gpu_execute(const OmegaGpuJob *job, OmegaGpuResult *result);
223
224/* Sets the process-wide default backend (NULL clears it). The pointer must stay
225 * valid while it is set. With none set, omega_gpu_execute returns the defined
226 * stub result: INTERNAL_INVARIANT at step NOT_IMPLEMENTED. Real code sets it;
227 * host tests set a fake. */
228void omega_gpu_engine_set_backend(const OmegaGpuBackend *backend);
229
230/* 1 once an uncertain completion has blocked this process, else 0. */
231int omega_gpu_engine_is_blocked(void);
232
233/* TEST ONLY. Clears the block. Production code must never call this: a blocked
234 * process stays blocked because the GPU may still be running. */
235void omega_gpu_engine_test_reset_block(void);
236
237/* TEST ONLY. Forces the blocked state so tests can exercise the blocked
238 * result without a real uncertain completion. */
239void omega_gpu_engine_test_force_block(void);
240
241/* Platform time service. Installs the monotonic millisecond clock the engine
242 * uses to measure waited_ms. Host build: NULL restores CLOCK_MONOTONIC.
243 * Freestanding build (OMEGA_GPU_NO_POSIX, see src/omega_gpu_platform.h):
244 * there is no default; omega_gpu_execute refuses with INTERNAL_INVARIANT at
245 * STEP_CLOCK_CHECK until a clock is installed. Not thread safe: set it before
246 * any job runs. */
247void omega_gpu_engine_set_clock(uint64_t (*now_ms)(void));
248
249/* TEST ONLY. Same as omega_gpu_engine_set_clock; lets host tests assert exact
250 * waited_ms with a fake clock. Kept so existing tests and mutants read as before. */
251void omega_gpu_engine_test_set_clock(uint64_t (*now_ms)(void));
252
253/* Static name strings, never NULL. Out-of-range values give "UNKNOWN". */
254const char *omega_gpu_engine_failure_name(int failure);
255const char *omega_gpu_engine_state_name(int state);
256const char *omega_gpu_engine_wait_name(int wait);
257const char *omega_gpu_engine_step_name(int step);
258
259#endif