interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_gpu_engine.h

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