Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_gpu_engine.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_gpu_engine.c` 705 lines · 29091 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #include "omega_fixture_rest.h" 3 /* 4 * Omega GPU Engine core (A3a). See src/omega_gpu_engine.h and 5 * docs/numeric/OMEGA_GPU_ENGINE.md. 6 * 7 * omega_gpu_execute drives the backend function table in one fixed order: 8 * open, channel, alloc x5, copy-in x3, poison fill, build -> PREPARED 9 * submit -> SUBMITTED 10 * marker wait, marker2 wait (unless NO_C3), release wait -> GPU_COMPLETE 11 * barrier, copy-out -> OUTPUT_VISIBLE 12 * compare every output word with its poison word -> OUTPUT_PRODUCED 13 * free every buffer (reverse order), close -> SUCCESS 14 * Every exit runs cleanup EXCEPT an uncertain completion (any non-zero result 15 * from one of the three completion waits), which keeps the context and every 16 * allocation, sets the process-wide block and reports retained = 1. 17 * The engine never judges the math: it only checks that output words changed. 18 * 19 * Marker comments of the form MUT: label the lines that 20 * tools/gpu_engine_mutations.sh breaks one at a time. 21 */ 22 #include "omega_gpu_engine.h" 23 #if defined(CUDAPP_MIGRATION_PENDING_C_ENGINE) 24 #define ENG_C_PART 1 25 #endif 26 #include "omega_eng_dec.h" 27 28 #include 29 #include 30 #ifdef OMEGA_GPU_NO_POSIX 31 /* Freestanding link target (native v1 P07 cut A, aienos#286 cut A): no errno 32 * and no POSIX clock. err_no is always 0, and the millisecond clock must be 33 * installed with omega_gpu_engine_set_clock before any job runs; without one 34 * omega_gpu_execute refuses at STEP_CLOCK_CHECK instead of guessing. */ 35 #define ERRNO_CLEAR() ((void)0) 36 #define ERRNO_READ() 0 37 #else 38 #include 39 #include 40 #define ERRNO_CLEAR() (errno = 0) 41 #define ERRNO_READ() (errno) 42 #endif 43 44 static const char *const FAILURE_NAMES[OMEGA_GPU_ENGINE_FAILURE_COUNT] = { 45 "OK", 46 "DEVICE_OPEN", 47 "CHANNEL_CREATE", 48 "ALLOC", 49 "PREPARE", 50 "SUBMIT", 51 "COMPLETION_WAIT", 52 "RELEASE_WAIT", 53 "VISIBILITY_WAIT", 54 "OUTPUT_UNCHANGED", 55 "INVALID_ARGS", 56 "INTERNAL_INVARIANT", 57 "CLEANUP", 58 "UNCERTAIN_COMPLETION_BLOCKED", 59 }; 60 61 static const char *const STATE_NAMES[OMEGA_GPU_ENGINE_STATE_COUNT] = { 62 "INITIAL", 63 "PREPARED", 64 "SUBMITTED", 65 "GPU_COMPLETE", 66 "OUTPUT_VISIBLE", 67 "OUTPUT_PRODUCED", 68 "SUCCESS", 69 }; 70 71 static const char *const WAIT_NAMES[OMEGA_GPU_ENGINE_WAIT_COUNT] = { 72 "NONE", 73 "MARKER", 74 "RELEASE_SEMAPHORE", 75 "MARKER2", 76 }; 77 78 static const char *const STEP_NAMES[OMEGA_GPU_ENGINE_STEP_COUNT] = { 79 "NONE", 80 "VALIDATE", 81 "BACKEND_CHECK", 82 "BLOCKED", 83 "NOT_IMPLEMENTED", 84 "OPEN_DEVICE", 85 "CREATE_CHANNEL", 86 "ALLOC", 87 "COPY_IN", 88 "FILL_POISON", 89 "BUILD", 90 "SUBMIT", 91 "WAIT_MARKER", 92 "WAIT_MARKER2", 93 "WAIT_RELEASE_SEMAPHORE", 94 "BARRIER", 95 "COPY_OUT", 96 "SCAN", 97 "FREE", 98 "CLOSE", 99 "CLOCK_CHECK", 100 }; 101 102 static _Atomic(const OmegaGpuBackend *) g_backend; 103 static atomic_int g_blocked; 104 static uint64_t (*g_clock)(void); /* installed clock; NULL means CLOCK_MONOTONIC on the host, none when freestanding */ 105 106 const char *omega_gpu_engine_failure_name(int failure) 107 { 108 if (failure < 0 || failure >= (int)OMEGA_GPU_ENGINE_FAILURE_COUNT) return "UNKNOWN"; 109 return FAILURE_NAMES[failure]; 110 } 111 112 const char *omega_gpu_engine_state_name(int state) 113 { 114 if (state < 0 || state >= (int)OMEGA_GPU_ENGINE_STATE_COUNT) return "UNKNOWN"; 115 return STATE_NAMES[state]; 116 } 117 118 const char *omega_gpu_engine_wait_name(int wait) 119 { 120 if (wait < 0 || wait >= (int)OMEGA_GPU_ENGINE_WAIT_COUNT) return "UNKNOWN"; 121 return WAIT_NAMES[wait]; 122 } 123 124 const char *omega_gpu_engine_step_name(int step) 125 { 126 if (step < 0 || step >= (int)OMEGA_GPU_ENGINE_STEP_COUNT) return "UNKNOWN"; 127 return STEP_NAMES[step]; 128 } 129 130 void omega_gpu_engine_set_backend(const OmegaGpuBackend *backend) 131 { 132 atomic_store(&g_backend, backend); 133 } 134 135 int omega_gpu_engine_is_blocked(void) 136 { 137 return atomic_load(&g_blocked) ? 1 : 0; 138 } 139 140 void omega_gpu_engine_test_reset_block(void) 141 { 142 atomic_store(&g_blocked, 0); 143 } 144 145 void omega_gpu_engine_test_force_block(void) 146 { 147 atomic_store(&g_blocked, 1); 148 } 149 150 void omega_gpu_engine_set_clock(uint64_t (*now_ms)(void)) 151 { 152 g_clock = now_ms; 153 } 154 155 void omega_gpu_engine_test_set_clock(uint64_t (*now_ms)(void)) 156 { 157 g_clock = now_ms; 158 } 159 160 #ifdef OMEGA_GPU_NO_POSIX 161 static uint64_t now_ms(void) 162 { 163 return g_clock ? g_clock() : 0u; /* unreachable without a clock: omega_gpu_execute refuses first */ 164 } 165 #else 166 static uint64_t mono_ms(void) 167 { 168 struct timespec ts; 169 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return 0u; 170 return omega_fixture_calc(FR_MS, (uint64_t)ts.tv_sec, (uint64_t)ts.tv_nsec, 0); 171 } 172 173 static uint64_t now_ms(void) 174 { 175 return g_clock ? g_clock() : mono_ms(); 176 } 177 #endif 178 179 static int backend_complete(const OmegaGpuBackend *b) 180 { 181 return b->open_device && b->create_channel && b->alloc && b->copy_in && b->fill_poison && 182 b->build && b->submit && b->wait_marker && b->wait_marker2 && 183 b->wait_release_semaphore && b->barrier && b->copy_out && b->diagnostics && 184 b->free_buf && b->close; 185 } 186 187 /* Returns 1 when the job is well formed for the VECTOR_1D layout. */ 188 static int job_valid(const OmegaGpuJob *job) 189 { 190 uint64_t bytes; 191 if (!job->program || job->program_len == 0) return 0; /* MUT:VALID_PROGRAM */ 192 if (job->layout != OMEGA_GPU_LAYOUT_VECTOR_1D) return 0; /* MUT:VALID_LAYOUT */ 193 if (job->element_count == 0) return 0; /* MUT:VALID_COUNT_ZERO */ 194 bytes = (uint64_t)job->element_count * 4u; 195 if (!job->input_a || (uint64_t)job->input_a_len != bytes) return 0; 196 if (!job->input_b || (uint64_t)job->input_b_len != bytes) return 0; 197 if (!job->output || (uint64_t)job->output_len != bytes) return 0; 198 if (((uintptr_t)job->output & 3u) != 0) return 0; /* MUT:VALID_ALIGN */ 199 if (!job->poison || (uint64_t)job->poison_count != (uint64_t)job->element_count) return 0; /* MUT:VALID_POISON_COUNT */ 200 if ((job->flags & ~(uint32_t)OMEGA_GPU_ENGINE_FLAG_NO_C3) != 0) return 0; /* MUT:VALID_FLAGS */ 201 if (job->timeouts.marker_ms == 0) return 0; /* MUT:VALID_TIMEOUT_MARKER1 */ 202 if (job->timeouts.release_semaphore_ms == 0) return 0; 203 if (job->timeouts.visibility_ms == 0) return 0; 204 if ((job->flags & OMEGA_GPU_ENGINE_FLAG_NO_C3) == 0 && job->timeouts.marker2_ms == 0) return 0; /* MUT:VALID_TIMEOUT_MARKER2 */ 205 return 1; 206 } 207 208 /* What the engine still owes the device: close if opened, free each allocated buffer. */ 209 typedef struct { 210 int opened; 211 int allocated[OMEGA_GPU_BUF_ROLE_COUNT]; 212 } Run; 213 214 /* Failure before any backend call: nothing to capture, nothing to clean. */ 215 static int stop(OmegaGpuResult *result, int failure, int step) 216 { 217 result->failure = failure; 218 result->failed_step = step; 219 return failure; 220 } 221 222 /* Copies the backend diagnostics (driver code, text, observed words) into the result. */ 223 static void capture(const OmegaGpuBackend *b, OmegaGpuResult *result) 224 { 225 OmegaGpuBackendDiag d; 226 memset(&d, 0, sizeof d); 227 b->diagnostics(b->ctx, &d); 228 result->drv_rc = d.drv_rc; 229 memcpy(result->drv_text, d.drv_text, sizeof result->drv_text); 230 result->drv_text[sizeof result->drv_text - 1u] = '\0'; 231 result->sync_valid = d.sync_valid; 232 result->marker = d.marker; 233 result->marker2 = d.marker2; 234 memcpy(result->semaphore, d.semaphore, sizeof result->semaphore); 235 } 236 237 /* Records the failure of a backend call. err is errno captured right after that call. */ 238 static int fail(const OmegaGpuBackend *b, OmegaGpuResult *result, int failure, int step, int err) 239 { 240 capture(b, result); 241 result->failure = failure; 242 result->failed_step = step; 243 result->err_no = err; 244 return failure; 245 } 246 247 /* A cleanup call failed. The first failure of the run is never overwritten. */ 248 static void cleanup_fault(const OmegaGpuBackend *b, OmegaGpuResult *result, int step, int err) 249 { 250 if (result->failure == OMEGA_GPU_ENGINE_OK) /* MUT:CLEANUP_OVERWRITES_FIRST */ 251 (void)fail(b, result, OMEGA_GPU_ENGINE_CLEANUP, step, err); 252 result->cleanup_failed = 1; /* MUT:CLEANUP_FLAG_NOT_SET */ 253 } 254 255 /* Frees every allocated buffer (reverse of allocation order), then closes the 256 * device. A failing call does not stop the rest. Returns result->failure. */ 257 static int leave(const OmegaGpuBackend *b, const Run *run, OmegaGpuResult *result) 258 { 259 int role; 260 int rc; 261 int e; 262 for (role = (int)OMEGA_GPU_BUF_ROLE_COUNT - 1; role >= 0; role--) { 263 if (!run->allocated[role]) continue; /* MUT:FREE_UNALLOCATED */ 264 ERRNO_CLEAR(); 265 rc = b->free_buf(b->ctx, role); /* MUT:FREE_SKIPPED */ 266 e = ERRNO_READ(); 267 if (rc != 0) cleanup_fault(b, result, OMEGA_GPU_ENGINE_STEP_FREE, e); 268 } 269 if (run->opened) { 270 ERRNO_CLEAR(); 271 rc = b->close(b->ctx); /* MUT:CLOSE_SKIPPED */ 272 e = ERRNO_READ(); 273 if (rc != 0) cleanup_fault(b, result, OMEGA_GPU_ENGINE_STEP_CLOSE, e); 274 } 275 return result->failure; 276 } 277 278 /* A completion wait gave a non-zero result: completion is uncertain. Keep the 279 * context and every allocation (no free, no close), say so, and block the process. */ 280 static int uncertain(const OmegaGpuBackend *b, OmegaGpuResult *result, int failure, int step, 281 int wait, uint64_t limit_ms, uint64_t waited_ms, int err) 282 { 283 (void)fail(b, result, failure, step, err); 284 result->wait = wait; 285 result->wait_timeout_ms = limit_ms; 286 result->waited_ms = waited_ms; 287 result->retained = 1; /* MUT:RETAINED_FLAG_NOT_SET */ 288 atomic_store(&g_blocked, 1); /* MUT:NO_BLOCK */ 289 return failure; 290 } 291 292 /* Calls a timed backend callback; captures errno right after it and measures the wait. */ 293 static int timed_call(int (*fn)(void *, uint64_t), void *ctx, uint64_t ms, uint64_t *waited_ms, 294 int *err) 295 { 296 uint64_t t0 = now_ms(); 297 uint64_t t1; 298 int rc; 299 ERRNO_CLEAR(); 300 rc = fn(ctx, ms); 301 *err = ERRNO_READ(); 302 t1 = now_ms(); 303 *waited_ms = t1 >= t0 ? t1 - t0 : 0u; /* MUT:WAITED_NOT_A_DIFFERENCE */ 304 return rc; 305 } 306 307 static int copy_in_role(const OmegaGpuBackend *b, int role, const void *src, size_t bytes, int *err) 308 { 309 int rc; 310 ERRNO_CLEAR(); 311 rc = b->copy_in(b->ctx, role, src, bytes); 312 *err = ERRNO_READ(); 313 return rc; 314 } 315 316 /* Device bytes for a buffer role. SCRATCH (pushbuffer, marker page, QMD) is the 317 * backend's own business, so the engine passes 0 and the backend sizes it. */ 318 static size_t buf_bytes(const OmegaGpuJob *job, int role) 319 { 320 switch (role) { 321 case OMEGA_GPU_BUF_PROGRAM: return job->program_len; 322 case OMEGA_GPU_BUF_INPUT_A: return job->input_a_len; 323 case OMEGA_GPU_BUF_INPUT_B: return job->input_b_len; 324 case OMEGA_GPU_BUF_OUTPUT: return job->output_len; 325 default: return 0u; 326 } 327 } 328 329 static int eng_phase(int phase, uint64_t rc, uint64_t flags, uint64_t bit, uint64_t done, 330 uint64_t w[16]) { 331 uint64_t in[8]; 332 memset(in, 0, sizeof in); 333 in[0] = (uint64_t)phase; 334 in[1] = rc; 335 in[2] = flags; 336 in[3] = bit; 337 in[4] = done; 338 return eng_ask(ENG_OP_PHASE, in, 5, w); 339 } 340 341 static void cleanup_om(const OmegaGpuBackend *b, OmegaGpuResult *result, int step, int err) { 342 uint64_t in[1]; 343 uint64_t w[16]; 344 in[0] = (uint64_t)result->failure; 345 if (eng_ask(ENG_OP_CLEAN, in, 1, w) != 0) return; 346 if (w[0]) (void)fail(b, result, OMEGA_GPU_ENGINE_CLEANUP, step, err); /* MUT:OM_CLEANUP_FAIL_SKIPPED */ 347 if (w[1]) result->cleanup_failed = 1; /* MUT:OM_CLEANUP_FLAG_NOT_SET */ 348 } 349 350 static int leave_om(const OmegaGpuBackend *b, const Run *run, OmegaGpuResult *result) { 351 int role, rc, e; 352 for (role = (int)OMEGA_GPU_BUF_ROLE_COUNT - 1; role >= 0; role--) { 353 if (!run->allocated[role]) continue; /* MUT:OM_FREE_UNALLOCATED */ 354 ERRNO_CLEAR(); 355 rc = b->free_buf(b->ctx, role); /* MUT:OM_FREE_SKIPPED */ 356 e = ERRNO_READ(); 357 if (rc != 0) cleanup_om(b, result, OMEGA_GPU_ENGINE_STEP_FREE, e); 358 } 359 if (run->opened) { 360 ERRNO_CLEAR(); 361 rc = b->close(b->ctx); /* MUT:OM_CLOSE_SKIPPED */ 362 e = ERRNO_READ(); 363 if (rc != 0) cleanup_om(b, result, OMEGA_GPU_ENGINE_STEP_CLOSE, e); 364 } 365 return result->failure; 366 } 367 368 static int apply_act(const OmegaGpuBackend *b, Run *run, OmegaGpuResult *result, const uint64_t w[16], 369 int err, uint64_t limit, uint64_t waited) { 370 int act = (int)w[0]; 371 /* every action the unit returns carries state 255 (eng.om phfail, phunc), so an 372 * action never changes last_state */ 373 if (act == 0 || act == 4) return 0; 374 if (act == 1) return stop(result, (int)w[1], (int)w[2]); 375 if (act == 3) { 376 capture(b, result); 377 result->failure = (int)w[1]; 378 result->failed_step = (int)w[2]; 379 result->err_no = err; 380 result->wait = (int)w[4]; 381 result->wait_timeout_ms = limit; 382 result->waited_ms = waited; 383 if (w[5]) result->retained = 1; /* MUT:OM_RETAINED_NOT_SET */ 384 if (w[6]) atomic_store(&g_blocked, 1); /* MUT:OM_NO_BLOCK */ 385 return (int)w[1]; 386 } 387 (void)fail(b, result, (int)w[1], (int)w[2], err); 388 return leave_om(b, run, result); /* MUT:OM_UNWIND_SKIPPED */ 389 } 390 391 static size_t role_bytes(const OmegaGpuJob *job, int role) { 392 uint64_t in[5]; 393 uint64_t w[16]; 394 memset(in, 0, sizeof in); 395 in[0] = (uint64_t)role; 396 in[1] = job->program_len; 397 in[2] = job->input_a_len; 398 in[3] = job->input_b_len; 399 in[4] = job->output_len; 400 if (eng_ask(ENG_OP_ROLE, in, 5, w) != 0) return 0u; 401 return (size_t)w[0]; 402 } 403 404 static uint64_t scan_poison(const OmegaGpuJob *job) { 405 uint64_t n = 0; 406 size_t i; 407 for (i = 0; i < (size_t)job->element_count; i++) { 408 uint32_t word; 409 uint64_t in[2]; 410 uint64_t w[16]; 411 memcpy(&word, (const unsigned char *)job->output + i * 4u, sizeof word); 412 in[0] = word; 413 in[1] = job->poison[i]; /* MUT:OM_POISON_INDEX_FIXED */ 414 if (eng_ask(ENG_OP_POIS, in, 2, w) == 0 && w[0]) n++; 415 } 416 return n; 417 } 418 419 static int timed_om(int (*fn)(void *, uint64_t), void *ctx, uint64_t ms, uint64_t *waited_ms, int *err) { 420 uint64_t t0 = now_ms(); 421 uint64_t in[2]; 422 uint64_t w[16]; 423 int rc; 424 ERRNO_CLEAR(); 425 rc = fn(ctx, ms); 426 *err = ERRNO_READ(); 427 in[0] = now_ms(); 428 in[1] = t0; /* MUT:OM_WAITED_NOT_A_DIFFERENCE */ 429 *waited_ms = eng_ask(ENG_OP_WAITED, in, 2, w) == 0 ? w[0] : 0u; 430 return rc; 431 } 432 433 static int execute_om(const OmegaGpuJob *job, OmegaGpuResult *result) { 434 const OmegaGpuBackend *b; 435 Run run; 436 uint64_t in[23]; 437 uint64_t w[16]; 438 uint64_t waited = 0; 439 int rc, e = 0, role, valid = 0; 440 441 memset(result, 0, sizeof *result); 442 result->last_state = OMEGA_GPU_ENGINE_STATE_INITIAL; 443 if (eng_phase(0, 0, 0, (uint64_t)omega_gpu_engine_is_blocked(), 0, w) != 0) 444 return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED); 445 if (w[0] != 0) return apply_act(NULL, NULL, result, w, 0, 0, 0); 446 if (job) { 447 memset(in, 0, sizeof in); 448 in[0] = job->program ? 1u : 0u; 449 in[1] = job->program_len; 450 in[2] = (uint64_t)job->layout; 451 in[3] = job->element_count; 452 in[4] = job->input_a ? 1u : 0u; 453 in[5] = job->input_a_len; 454 in[6] = job->input_b ? 1u : 0u; 455 in[7] = job->input_b_len; 456 in[8] = job->output ? 1u : 0u; 457 in[9] = job->output_len; 458 in[10] = (uint64_t)((uintptr_t)job->output & 3u); 459 in[11] = job->poison ? 1u : 0u; 460 in[12] = job->poison_count; 461 in[13] = job->flags; 462 in[14] = job->timeouts.marker_ms; 463 in[15] = job->timeouts.release_semaphore_ms; 464 in[16] = job->timeouts.visibility_ms; 465 in[17] = job->timeouts.marker2_ms; 466 if (eng_ask(ENG_OP_JOBV, in, 18, w) == 0) valid = (int)w[0]; 467 } 468 if (eng_phase(1, 0, 0, job ? 1u : 0u, (uint64_t)valid, w) != 0) 469 return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_VALIDATE); 470 if (w[0] != 0) return apply_act(NULL, NULL, result, w, 0, 0, 0); 471 b = atomic_load(&g_backend); 472 if (!b) { 473 if (eng_phase(2, 0, 0, 0, 0, w) != 0) 474 return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED); 475 return apply_act(NULL, NULL, result, w, 0, 0, 0); 476 } 477 if (eng_phase(3, 0, 0, (uint64_t)backend_complete(b), 0, w) != 0) 478 return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_BACKEND_CHECK); 479 if (w[0] != 0) return apply_act(b, NULL, result, w, 0, 0, 0); 480 #ifdef OMEGA_GPU_NO_POSIX 481 if (eng_phase(4, 0, 0, g_clock ? 1u : 0u, 0, w) != 0) 482 return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_CLOCK_CHECK); 483 if (w[0] != 0) return apply_act(b, NULL, result, w, 0, 0, 0); 484 #endif 485 memset(&run, 0, sizeof run); 486 ERRNO_CLEAR(); 487 rc = b->open_device(b->ctx); 488 e = ERRNO_READ(); 489 if (eng_phase(5, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 490 if (w[0] != 0) return apply_act(b, &run, result, w, e, 0, 0); 491 run.opened = 1; /* MUT:OM_OPEN_NOT_TRACKED */ 492 ERRNO_CLEAR(); 493 rc = b->create_channel(b->ctx); 494 e = ERRNO_READ(); 495 if (eng_phase(6, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 496 if (w[0] != 0) return apply_act(b, &run, result, w, e, 0, 0); 497 for (role = 0; role < (int)OMEGA_GPU_BUF_ROLE_COUNT; role++) { 498 ERRNO_CLEAR(); 499 rc = b->alloc(b->ctx, role, role_bytes(job, role)); 500 e = ERRNO_READ(); 501 if (eng_phase(7, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 502 if (w[0] != 0) return apply_act(b, &run, result, w, e, 0, 0); 503 run.allocated[role] = 1; /* MUT:OM_ALLOC_NOT_TRACKED */ 504 } 505 rc = copy_in_role(b, OMEGA_GPU_BUF_PROGRAM, job->program, job->program_len, &e); 506 if (rc == 0) rc = copy_in_role(b, OMEGA_GPU_BUF_INPUT_A, job->input_a, job->input_a_len, &e); /* MUT:OM_COPY_IN_CHAIN */ 507 if (rc == 0) rc = copy_in_role(b, OMEGA_GPU_BUF_INPUT_B, job->input_b, job->input_b_len, &e); 508 if (eng_phase(8, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 509 if (w[0] != 0) return apply_act(b, &run, result, w, e, 0, 0); 510 ERRNO_CLEAR(); 511 rc = b->fill_poison(b->ctx, job->poison, job->poison_count); /* MUT:OM_POISON_FILL_SKIPPED */ 512 e = ERRNO_READ(); 513 if (eng_phase(9, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 514 if (w[0] != 0) return apply_act(b, &run, result, w, e, 0, 0); 515 ERRNO_CLEAR(); 516 rc = b->build(b->ctx, job); 517 e = ERRNO_READ(); 518 if (eng_phase(10, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 519 if (w[0] != 0) return apply_act(b, &run, result, w, e, 0, 0); 520 if (w[3] != 255) result->last_state = (int)w[3]; 521 ERRNO_CLEAR(); 522 rc = b->submit(b->ctx); 523 e = ERRNO_READ(); 524 if (eng_phase(11, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 525 if (w[0] != 0) return apply_act(b, &run, result, w, e, 0, 0); 526 if (w[3] != 255) result->last_state = (int)w[3]; 527 rc = timed_om(b->wait_marker, b->ctx, job->timeouts.marker_ms, &waited, &e); /* MUT:OM_MARKER1_WAIT_SKIPPED */ 528 if (eng_phase(12, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 529 if (w[0] != 0) return apply_act(b, &run, result, w, e, job->timeouts.marker_ms, waited); /* MUT:OM_MARKER1_LIMIT_WRONG */ 530 if (eng_phase(13, 0, job->flags, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; /* MUT:OM_C3_FLAG_IGNORED */ 531 if (w[0] != 4) { /* MUT:OM_MARKER2_WAIT_SKIPPED */ 532 rc = timed_om(b->wait_marker2, b->ctx, job->timeouts.marker2_ms, &waited, &e); 533 if (eng_phase(13, (uint64_t)rc, job->flags, 0, 1, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 534 if (w[0] != 0) return apply_act(b, &run, result, w, e, job->timeouts.marker2_ms, waited); 535 } 536 rc = timed_om(b->wait_release_semaphore, b->ctx, job->timeouts.release_semaphore_ms, &waited, &e); /* MUT:OM_RELEASE_WAIT_SKIPPED */ 537 if (eng_phase(14, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 538 if (w[0] != 0) return apply_act(b, &run, result, w, e, job->timeouts.release_semaphore_ms, waited); 539 if (w[3] != 255) result->last_state = (int)w[3]; 540 rc = timed_om(b->barrier, b->ctx, job->timeouts.visibility_ms, &waited, &e); /* MUT:OM_BARRIER_SKIPPED */ 541 if (eng_phase(15, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 542 if (w[0] != 0) return apply_act(b, &run, result, w, e, 0, 0); 543 ERRNO_CLEAR(); 544 rc = b->copy_out(b->ctx, job->output, job->output_len); 545 e = ERRNO_READ(); 546 if (eng_phase(16, (uint64_t)rc, 0, 0, 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 547 if (w[0] != 0) return apply_act(b, &run, result, w, e, 0, 0); 548 if (w[3] != 255) result->last_state = (int)w[3]; 549 waited = scan_poison(job); 550 result->n_outputs = 1u; 551 result->output_unchanged_words[0] = waited; /* MUT:OM_UNCHANGED_NOT_REPORTED */ 552 in[0] = waited; 553 if (eng_ask(ENG_OP_SCAN, in, 1, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 554 if (eng_phase(17, 0, 0, w[0], 0, w) != 0) return OMEGA_GPU_ENGINE_INTERNAL_INVARIANT; 555 if (w[0] != 0) return apply_act(b, &run, result, w, 0, 0, 0); 556 if (w[3] != 255) result->last_state = (int)w[3]; 557 rc = leave_om(b, &run, result); 558 if (eng_phase(18, (uint64_t)(result->failure != OMEGA_GPU_ENGINE_OK), 0, 0, 0, w) != 0) return rc; 559 if (w[3] != 255) result->last_state = (int)w[3]; 560 return rc; 561 } 562 563 int omega_gpu_execute(const OmegaGpuJob *job, OmegaGpuResult *result) 564 { 565 const OmegaGpuBackend *b; 566 Run run; 567 uint64_t waited = 0u; 568 uint64_t unchanged = 0u; 569 size_t i; 570 int role; 571 int rc; 572 int e = 0; 573 574 if (!result) return OMEGA_GPU_ENGINE_INVALID_ARGS; 575 if (eng_on()) return execute_om(job, result); 576 memset(result, 0, sizeof *result); 577 result->failure = OMEGA_GPU_ENGINE_OK; 578 result->last_state = OMEGA_GPU_ENGINE_STATE_INITIAL; 579 result->failed_step = OMEGA_GPU_ENGINE_STEP_NONE; 580 result->wait = OMEGA_GPU_ENGINE_WAIT_NONE; 581 if (omega_gpu_engine_is_blocked()) /* MUT:BLOCK_CHECK_SKIPPED */ 582 return stop(result, OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED, 583 OMEGA_GPU_ENGINE_STEP_BLOCKED); 584 if (!job || !job_valid(job)) /* MUT:VALIDATE_SKIPPED */ 585 return stop(result, OMEGA_GPU_ENGINE_INVALID_ARGS, OMEGA_GPU_ENGINE_STEP_VALIDATE); 586 b = atomic_load(&g_backend); 587 if (!b) 588 return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED); 589 if (!backend_complete(b)) /* MUT:BACKEND_CHECK_SKIPPED */ 590 return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, 591 OMEGA_GPU_ENGINE_STEP_BACKEND_CHECK); 592 #ifdef OMEGA_GPU_NO_POSIX 593 if (!g_clock) /* no platform clock: waited_ms could not be measured; refuse before touching the device */ 594 return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_CLOCK_CHECK); 595 #endif 596 memset(&run, 0, sizeof run); 597 598 /* INITIAL -> PREPARED */ 599 ERRNO_CLEAR(); 600 rc = b->open_device(b->ctx); 601 e = ERRNO_READ(); 602 if (rc != 0) { 603 (void)fail(b, result, OMEGA_GPU_ENGINE_DEVICE_OPEN, OMEGA_GPU_ENGINE_STEP_OPEN_DEVICE, e); 604 return leave(b, &run, result); 605 } 606 run.opened = 1; /* MUT:OPEN_NOT_TRACKED */ 607 608 ERRNO_CLEAR(); 609 rc = b->create_channel(b->ctx); 610 e = ERRNO_READ(); 611 if (rc != 0) { 612 (void)fail(b, result, OMEGA_GPU_ENGINE_CHANNEL_CREATE, OMEGA_GPU_ENGINE_STEP_CREATE_CHANNEL, e); 613 return leave(b, &run, result); /* MUT:CLEANUP_SKIPPED_CHANNEL */ 614 } 615 616 for (role = 0; role < (int)OMEGA_GPU_BUF_ROLE_COUNT; role++) { 617 ERRNO_CLEAR(); 618 rc = b->alloc(b->ctx, role, buf_bytes(job, role)); 619 e = ERRNO_READ(); 620 if (rc != 0) { 621 (void)fail(b, result, OMEGA_GPU_ENGINE_ALLOC, OMEGA_GPU_ENGINE_STEP_ALLOC, e); 622 return leave(b, &run, result); /* MUT:CLEANUP_SKIPPED_ALLOC */ 623 } 624 run.allocated[role] = 1; /* MUT:ALLOC_NOT_TRACKED */ 625 } 626 627 rc = copy_in_role(b, OMEGA_GPU_BUF_PROGRAM, job->program, job->program_len, &e); 628 if (rc == 0) rc = copy_in_role(b, OMEGA_GPU_BUF_INPUT_A, job->input_a, job->input_a_len, &e); 629 if (rc == 0) rc = copy_in_role(b, OMEGA_GPU_BUF_INPUT_B, job->input_b, job->input_b_len, &e); 630 if (rc != 0) { 631 (void)fail(b, result, OMEGA_GPU_ENGINE_PREPARE, OMEGA_GPU_ENGINE_STEP_COPY_IN, e); 632 return leave(b, &run, result); /* MUT:CLEANUP_SKIPPED_COPY_IN */ 633 } 634 635 ERRNO_CLEAR(); rc = b->fill_poison(b->ctx, job->poison, job->poison_count); e = ERRNO_READ(); /* MUT:POISON_FILL_SKIPPED */ 636 if (rc != 0) { 637 (void)fail(b, result, OMEGA_GPU_ENGINE_PREPARE, OMEGA_GPU_ENGINE_STEP_FILL_POISON, e); 638 return leave(b, &run, result); 639 } 640 641 ERRNO_CLEAR(); 642 rc = b->build(b->ctx, job); 643 e = ERRNO_READ(); 644 if (rc != 0) { 645 (void)fail(b, result, OMEGA_GPU_ENGINE_PREPARE, OMEGA_GPU_ENGINE_STEP_BUILD, e); 646 return leave(b, &run, result); 647 } 648 result->last_state = OMEGA_GPU_ENGINE_STATE_PREPARED; /* MUT:STATE_PREPARED_SKIPPED */ 649 650 /* PREPARED -> SUBMITTED. A failed submit is a known failure, not an uncertain completion. */ 651 ERRNO_CLEAR(); 652 rc = b->submit(b->ctx); 653 e = ERRNO_READ(); 654 if (rc != 0) { 655 (void)fail(b, result, OMEGA_GPU_ENGINE_SUBMIT, OMEGA_GPU_ENGINE_STEP_SUBMIT, e); 656 return leave(b, &run, result); /* MUT:CLEANUP_SKIPPED_SUBMIT */ 657 } 658 result->last_state = OMEGA_GPU_ENGINE_STATE_SUBMITTED; /* MUT:STATE_SUBMITTED_SKIPPED */ 659 660 /* SUBMITTED -> GPU_COMPLETE: marker, marker2 (unless NO_C3), release semaphore. 661 * Any non-zero result is an uncertain completion. */ 662 rc = timed_call(b->wait_marker, b->ctx, job->timeouts.marker_ms, &waited, &e); /* MUT:MARKER1_WAIT_SKIPPED MUT:MARKER1_TIMEOUT_WRONG */ 663 if (rc != 0) return uncertain(b, result, OMEGA_GPU_ENGINE_COMPLETION_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_MARKER, OMEGA_GPU_ENGINE_WAIT_MARKER, job->timeouts.marker_ms, waited, e); /* MUT:MARKER1_RESULT_IGNORED MUT:UNCERTAIN_FREES_MARKER1 */ 664 if (!(job->flags & OMEGA_GPU_ENGINE_FLAG_NO_C3)) { /* MUT:MARKER2_WAIT_SKIPPED MUT:C3_FLAG_IGNORED */ 665 rc = timed_call(b->wait_marker2, b->ctx, job->timeouts.marker2_ms, &waited, &e); 666 if (rc != 0) return uncertain(b, result, OMEGA_GPU_ENGINE_COMPLETION_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_MARKER2, OMEGA_GPU_ENGINE_WAIT_MARKER2, job->timeouts.marker2_ms, waited, e); /* MUT:MARKER2_RESULT_IGNORED MUT:UNCERTAIN_FREES_MARKER2 */ 667 } 668 rc = timed_call(b->wait_release_semaphore, b->ctx, job->timeouts.release_semaphore_ms, &waited, &e); /* MUT:RELEASE_WAIT_SKIPPED */ 669 if (rc != 0) return uncertain(b, result, OMEGA_GPU_ENGINE_RELEASE_WAIT, OMEGA_GPU_ENGINE_STEP_WAIT_RELEASE_SEMAPHORE, OMEGA_GPU_ENGINE_WAIT_RELEASE_SEMAPHORE, job->timeouts.release_semaphore_ms, waited, e); /* MUT:RELEASE_RESULT_IGNORED MUT:UNCERTAIN_FREES_RELEASE */ 670 result->last_state = OMEGA_GPU_ENGINE_STATE_GPU_COMPLETE; /* MUT:STATE_GPU_COMPLETE_SKIPPED */ 671 672 /* GPU_COMPLETE -> OUTPUT_VISIBLE: completion is known here, so a failure cleans up. */ 673 rc = timed_call(b->barrier, b->ctx, job->timeouts.visibility_ms, &waited, &e); /* MUT:BARRIER_SKIPPED */ 674 if (rc != 0) { /* MUT:BARRIER_RESULT_IGNORED */ 675 (void)fail(b, result, OMEGA_GPU_ENGINE_VISIBILITY_WAIT, OMEGA_GPU_ENGINE_STEP_BARRIER, e); 676 return leave(b, &run, result); 677 } 678 ERRNO_CLEAR(); 679 rc = b->copy_out(b->ctx, job->output, job->output_len); 680 e = ERRNO_READ(); 681 if (rc != 0) { 682 (void)fail(b, result, OMEGA_GPU_ENGINE_VISIBILITY_WAIT, OMEGA_GPU_ENGINE_STEP_COPY_OUT, e); 683 return leave(b, &run, result); 684 } 685 result->last_state = OMEGA_GPU_ENGINE_STATE_OUTPUT_VISIBLE; /* MUT:STATE_OUTPUT_VISIBLE_SKIPPED */ 686 687 /* OUTPUT_VISIBLE -> OUTPUT_PRODUCED: no output word may still equal its poison word. */ 688 for (i = 0; i < (size_t)job->element_count; i++) { 689 uint32_t w; 690 memcpy(&w, (const unsigned char *)job->output + i * 4u, sizeof w); 691 if (w == job->poison[i]) unchanged++; /* MUT:POISON_INDEX_FIXED MUT:UNCHANGED_COUNT_SATURATES */ 692 } 693 result->n_outputs = 1u; 694 result->output_unchanged_words[0] = unchanged; 695 if (unchanged != 0u) { /* MUT:UNCHANGED_CHECK_SKIPPED */ 696 (void)fail(b, result, OMEGA_GPU_ENGINE_OUTPUT_UNCHANGED, OMEGA_GPU_ENGINE_STEP_SCAN, 0); 697 return leave(b, &run, result); 698 } 699 result->last_state = OMEGA_GPU_ENGINE_STATE_OUTPUT_PRODUCED; /* MUT:STATE_OUTPUT_PRODUCED_SKIPPED */ 700 701 /* OUTPUT_PRODUCED -> SUCCESS, only when cleanup also went well. */ 702 rc = leave(b, &run, result); 703 if (rc == OMEGA_GPU_ENGINE_OK) result->last_state = OMEGA_GPU_ENGINE_STATE_SUCCESS; /* MUT:STATE_SUCCESS_SKIPPED MUT:SUCCESS_AFTER_CLEANUP_FAIL */ 704 return rc; 705 }