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
CUDApp / src / omega_numeric_lifecycle.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 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 | #ifndef OMEGA_NUMERIC_LIFECYCLE_H |
| 18 | #define OMEGA_NUMERIC_LIFECYCLE_H |
| 19 | |
| 20 | #include <stdatomic.h> |
| 21 | #include "cudapp_native.h" |
| 22 | |
| 23 | /* One owner across the SIMT, reduction and DIV/SQRT launchers. Defined in |
| 24 | * omega_numeric.c, which all three already link. No reset API: an uncertain |
| 25 | * completion requires ending the process and investigating the device. This |
| 26 | * does not assert that process exit itself resets or recovers the GPU. */ |
| 27 | enum { OMEGA_NUMERIC_NATIVE_IDLE, OMEGA_NUMERIC_NATIVE_BUSY, |
| 28 | OMEGA_NUMERIC_NATIVE_UNCERTAIN }; |
| 29 | extern atomic_int omega_numeric_native_state; |
| 30 | |
| 31 | static inline int omega_numeric_native_acquire(void) { |
| 32 | int expected = OMEGA_NUMERIC_NATIVE_IDLE; |
| 33 | #if defined(CUDAPP_MIGRATION_PENDING_LEFTOVERS) && CUDAPP_MIGRATION_PENDING_LEFTOVERS |
| 34 | return atomic_compare_exchange_strong(&omega_numeric_native_state, &expected, |
| 35 | OMEGA_NUMERIC_NATIVE_BUSY) ? 0 : -1; |
| 36 | #else |
| 37 | uint64_t result = UINT64_MAX; |
| 38 | unsigned okay = atomic_compare_exchange_strong(&omega_numeric_native_state, |
| 39 | &expected, OMEGA_NUMERIC_NATIVE_BUSY); |
| 40 | (void)omega_leftovers(&result, 3, okay, 0, 0, 0); |
| 41 | return (int)(int64_t)result; |
| 42 | #endif |
| 43 | } |
| 44 | static inline int omega_numeric_native_uncertain(void) { |
| 45 | #if defined(CUDAPP_MIGRATION_PENDING_LEFTOVERS) && CUDAPP_MIGRATION_PENDING_LEFTOVERS |
| 46 | return atomic_load(&omega_numeric_native_state) == OMEGA_NUMERIC_NATIVE_UNCERTAIN; |
| 47 | #else |
| 48 | uint64_t result = 1; |
| 49 | (void)omega_leftovers(&result, 4, |
| 50 | (uint64_t)(int64_t)atomic_load(&omega_numeric_native_state), 0, 0, 0); |
| 51 | return (int)result; |
| 52 | #endif |
| 53 | } |
| 54 | static inline void omega_numeric_native_poison(void) { |
| 55 | atomic_store(&omega_numeric_native_state, OMEGA_NUMERIC_NATIVE_UNCERTAIN); |
| 56 | } |
| 57 | static inline void omega_numeric_native_release(void) { |
| 58 | int expected = OMEGA_NUMERIC_NATIVE_BUSY; |
| 59 | /* Never clear a poison latch, even if a cleanup caller arrives late. */ |
| 60 | (void)atomic_compare_exchange_strong(&omega_numeric_native_state, &expected, |
| 61 | OMEGA_NUMERIC_NATIVE_IDLE); |
| 62 | } |
| 63 | #endif |