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_fp8_slot_table.c
| 1 | /* omega_fp8_slot_table.c: host-only slot state machine (see the header). */ |
| 2 | #include "omega_fp8_slot_table.h" |
| 3 | #include <stdlib.h> |
| 4 | #include "omega_api_shells.h" |
| 5 | #include "omega_api_views_rest.h" |
| 6 | #include <string.h> |
| 7 | |
| 8 | int omega_fp8_slot_table_init(OmegaFp8SlotTable *t, uint32_t slots) { |
| 9 | if (!t || slots == 0) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 10 | memset(t, 0, sizeof *t); |
| 11 | t->rec = calloc(slots, sizeof *t->rec); |
| 12 | if (!t->rec) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 13 | if (pthread_mutex_init(&t->mu, NULL) != 0) { free(t->rec); t->rec = NULL; return OMEGA_FP8_SLOT_BAD_ARGS; } |
| 14 | t->slots = slots; |
| 15 | t->next_version = 1; |
| 16 | return OMEGA_FP8_SLOT_OK; |
| 17 | } |
| 18 | |
| 19 | void omega_fp8_slot_table_fini(OmegaFp8SlotTable *t) { |
| 20 | if (!t || !t->rec) return; |
| 21 | pthread_mutex_destroy(&t->mu); |
| 22 | free(t->rec); |
| 23 | t->rec = NULL; |
| 24 | t->slots = 0; |
| 25 | } |
| 26 | |
| 27 | #define LOCKED(t, body) do { pthread_mutex_lock(&(t)->mu); body; pthread_mutex_unlock(&(t)->mu); } while (0) |
| 28 | |
| 29 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 30 | static void pack_rec(const OmegaFp8SlotTable *t, const OmegaFp8SlotRec *r, int ok, uint64_t rec[16]) { |
| 31 | memset(rec, 0, sizeof(*rec) * 16); |
| 32 | rec[0] = r->state; rec[1] = r->pins; rec[2] = r->key; rec[3] = r->version; |
| 33 | rec[4] = t->next_version; rec[5] = (uint64_t)t->mutant; rec[6] = (uint64_t)(ok != 0); |
| 34 | rec[8] = t->stats.loads; rec[9] = t->stats.load_failures; rec[10] = t->stats.busy_refusals; |
| 35 | rec[11] = t->stats.stale_refusals; rec[12] = t->stats.evictions; rec[13] = t->stats.pins; |
| 36 | rec[14] = t->stats.pinned_now; rec[15] = t->stats.pinned_high; |
| 37 | } |
| 38 | static void unpack_rec(OmegaFp8SlotTable *t, OmegaFp8SlotRec *r, const uint64_t rec[16]) { |
| 39 | r->state = (uint32_t)rec[0]; r->pins = (uint32_t)rec[1]; r->key = rec[2]; r->version = rec[3]; |
| 40 | t->next_version = rec[4]; |
| 41 | t->stats.loads = rec[8]; t->stats.load_failures = rec[9]; t->stats.busy_refusals = rec[10]; |
| 42 | t->stats.stale_refusals = rec[11]; t->stats.evictions = rec[12]; t->stats.pins = rec[13]; |
| 43 | t->stats.pinned_now = (uint32_t)rec[14]; t->stats.pinned_high = (uint32_t)rec[15]; |
| 44 | } |
| 45 | static int step_slot(OmegaFp8SlotTable *t, uint32_t slot, int op, uint64_t aux, int ok) { |
| 46 | uint64_t rec[16]; |
| 47 | pack_rec(t, &t->rec[slot], ok, rec); |
| 48 | int rc = omega_api_slot((uint64_t)op, aux, rec); |
| 49 | unpack_rec(t, &t->rec[slot], rec); |
| 50 | return rc; |
| 51 | } |
| 52 | #else |
| 53 | _Static_assert(sizeof(OmegaFp8SlotRec) == 3 * sizeof(uint64_t), "slot record view"); |
| 54 | _Static_assert(offsetof(OmegaFp8SlotRec, key) == 8, "slot key view"); |
| 55 | _Static_assert(offsetof(OmegaFp8SlotRec, version) == 16, "slot version view"); |
| 56 | _Static_assert(sizeof(OmegaFp8SlotStats) == 7 * sizeof(uint64_t), "slot statistics view"); |
| 57 | _Static_assert(offsetof(OmegaFp8SlotStats, pinned_now) == 48, "slot pin statistics view"); |
| 58 | static int step_slot(OmegaFp8SlotTable *t, uint32_t slot, int op, uint64_t aux, int ok) { |
| 59 | uint64_t cfg[8] = {t->next_version, (uint64_t)t->mutant, (uint64_t)ok, (uint64_t)op, aux}; |
| 60 | uint64_t rc = 0; |
| 61 | ar_check(omega_arslot(&rc, cfg, 8, &t->rec[slot], 3, &t->stats, 7)); |
| 62 | t->next_version = cfg[0]; |
| 63 | return (int)(int64_t)rc; |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | int omega_fp8_slot_begin_load(OmegaFp8SlotTable *t, uint32_t slot) { |
| 68 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 69 | int rc = OMEGA_FP8_SLOT_OK; |
| 70 | LOCKED(t, { rc = step_slot(t, slot, 1, 0, 0); }); |
| 71 | return rc; |
| 72 | } |
| 73 | |
| 74 | int omega_fp8_slot_finish_load(OmegaFp8SlotTable *t, uint32_t slot, uint64_t key, int ok, uint64_t *version) { |
| 75 | if (version) *version = 0; |
| 76 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 77 | int rc = OMEGA_FP8_SLOT_OK; |
| 78 | LOCKED(t, { rc = step_slot(t, slot, 2, key, ok); if (version && rc == 0 && ok) *version = t->rec[slot].version; }); |
| 79 | return rc; |
| 80 | } |
| 81 | |
| 82 | int omega_fp8_slot_pin(OmegaFp8SlotTable *t, uint32_t slot, uint64_t version) { |
| 83 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 84 | int rc = OMEGA_FP8_SLOT_OK; |
| 85 | LOCKED(t, { rc = step_slot(t, slot, 3, version, 0); }); |
| 86 | return rc; |
| 87 | } |
| 88 | |
| 89 | int omega_fp8_slot_unpin(OmegaFp8SlotTable *t, uint32_t slot, uint64_t version) { |
| 90 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 91 | int rc = OMEGA_FP8_SLOT_OK; |
| 92 | LOCKED(t, { rc = step_slot(t, slot, 4, version, 0); }); |
| 93 | return rc; |
| 94 | } |
| 95 | |
| 96 | int omega_fp8_slot_evict(OmegaFp8SlotTable *t, uint32_t slot) { |
| 97 | if (!t || !t->rec || slot >= t->slots) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 98 | int rc = OMEGA_FP8_SLOT_OK; |
| 99 | LOCKED(t, { rc = step_slot(t, slot, 5, 0, 0); }); |
| 100 | return rc; |
| 101 | } |
| 102 | |
| 103 | int omega_fp8_slot_query(OmegaFp8SlotTable *t, uint32_t slot, OmegaFp8SlotRec *out) { |
| 104 | if (!t || !t->rec || slot >= t->slots || !out) return OMEGA_FP8_SLOT_BAD_ARGS; |
| 105 | LOCKED(t, { ar_words(out, sizeof *out / sizeof(uint32_t), (const uint32_t *)&t->rec[slot], sizeof *out / sizeof(uint32_t)); }); |
| 106 | return OMEGA_FP8_SLOT_OK; |
| 107 | } |
| 108 | |
| 109 | int omega_fp8_slot_table_idle(OmegaFp8SlotTable *t) { |
| 110 | if (!t || !t->rec) return 1; |
| 111 | int idle = 1; |
| 112 | LOCKED(t, { |
| 113 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 114 | for (uint32_t i = 0; i < t->slots; i++) |
| 115 | if (step_slot(t, i, 6, 0, 0) == 0) { idle = 0; break; } |
| 116 | #else |
| 117 | uint64_t result = 0; |
| 118 | ar_check(omega_aridle(&result, t->rec, (uint64_t)t->slots * 6)); |
| 119 | idle = result != 0; |
| 120 | #endif |
| 121 | }); |
| 122 | return idle; |
| 123 | } |
| 124 | /* slot decisions live in om/api/apish_slot.om */ |
| 125 | |
| 126 | void omega_fp8_slot_table_stats(OmegaFp8SlotTable *t, OmegaFp8SlotStats *out) { |
| 127 | if (!out) return; |
| 128 | ar_words(out, sizeof *out / sizeof(uint32_t), NULL, 0); |
| 129 | if (!t || !t->rec) return; |
| 130 | LOCKED(t, { ar_words(out, sizeof *out / sizeof(uint32_t), (const uint32_t *)&t->stats, sizeof *out / sizeof(uint32_t)); }); |
| 131 | } |
| 132 | |
| 133 | void omega_fp8_slot_table_set_mutant(OmegaFp8SlotTable *t, int mutant) { |
| 134 | if (!t || !t->rec) return; |
| 135 | LOCKED(t, { t->mutant = mutant; }); |
| 136 | } |