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_wait.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_wait.c` 314 lines · 10453 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #include "omega_fixture_rest.h" 3 #include "omega_gpu_wait.h" 4 5 #include 6 #if defined(CUDAPP_MIGRATION_PENDING_C_WAIT) 7 #define ENG_C_PART 1 8 #endif 9 #include "omega_eng_dec.h" 10 11 #ifndef OMEGA_GPU_NO_POSIX 12 #include 13 static uint64_t real_now_ns(void *ctx) { 14 (void)ctx; 15 struct timespec ts; 16 clock_gettime(CLOCK_MONOTONIC, &ts); 17 return omega_fixture_calc(FR_NS, (uint64_t)ts.tv_sec, (uint64_t)ts.tv_nsec, 0); 18 } 19 #define DEFAULT_NOW_NS real_now_ns 20 #else 21 /* P07 cut C: the freestanding build has no clock of its own; the caller passes the platform clock 22 * (cfg->now_ns, e.g. OmegaGpuPlatform.time_now_ns) and a wait without one is refused (INVALID_STATE). */ 23 #define DEFAULT_NOW_NS NULL 24 #endif 25 26 static void real_barrier(void *ctx) { 27 (void)ctx; 28 #if defined(__aarch64__) 29 __asm__ volatile("dsb sy" ::: "memory"); 30 #else 31 __atomic_thread_fence(__ATOMIC_SEQ_CST); 32 #endif 33 } 34 35 static void real_relax(void *ctx) { 36 (void)ctx; 37 #if defined(__aarch64__) 38 __asm__ volatile("yield"); 39 #else 40 __asm__ volatile("" ::: "memory"); 41 #endif 42 } 43 44 const char *omega_gpu_wait_result_name(omega_gpu_wait_result_t r) { 45 switch (r) { 46 case OMEGA_GPU_WAIT_PASS: return "PASS"; 47 case OMEGA_GPU_WAIT_TIMEOUT: return "TIMEOUT"; 48 case OMEGA_GPU_WAIT_STALLED: return "STALLED"; 49 case OMEGA_GPU_WAIT_INVALID_STATE: return "INVALID_STATE"; 50 case OMEGA_GPU_WAIT_ABORTED: return "ABORTED"; 51 default: return "UNKNOWN"; 52 } 53 } 54 55 const char *omega_gpu_wait_kind_name(omega_gpu_wait_kind_t k) { 56 return k == OMEGA_GPU_WAIT_FIXED ? "fixed" : k == OMEGA_GPU_WAIT_SEQUENCE ? "sequence" : "unknown"; 57 } 58 59 /* Decisions come from om/engine/eng.om. This function only calls the clock, 60 * the barrier, the hooks and the watched words. */ 61 static bool wait_om(omega_gpu_wait_kind_t kind, volatile uint32_t *word, uint32_t expected, 62 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { 63 uint64_t (*now_ns)(void *) = cfg->now_ns ? cfg->now_ns : DEFAULT_NOW_NS; 64 void (*barrier)(void *) = cfg->barrier ? cfg->barrier : real_barrier; 65 void (*relax)(void *) = cfg->relax ? cfg->relax : real_relax; 66 uint64_t in[23]; 67 uint64_t w[16]; 68 uint64_t start, now, hard, stall, total_ns, stall_ns, flags; 69 int first; 70 71 memset(in, 0, sizeof in); 72 in[0] = cfg->total_timeout_ms; 73 in[1] = cfg->progress_timeout_ms; 74 in[3] = word ? 1u : 0u; 75 in[4] = now_ns ? 1u : 0u; 76 if (eng_ask(ENG_OP_WINIT, in, 5, w) != 0) { 77 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 78 return false; 79 } 80 if (w[5]) { 81 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 82 return false; 83 } 84 start = now_ns(cfg->hook_ctx); 85 in[2] = start; 86 if (eng_ask(ENG_OP_WINIT, in, 5, w) != 0) { 87 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 88 return false; 89 } 90 /* The C oracle records the start before it refuses a null word. */ 91 rpt->start_ns = start; 92 rpt->last_progress_ns = start; 93 if (w[4]) { 94 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 95 return false; 96 } 97 total_ns = w[0]; 98 stall_ns = w[1]; 99 hard = w[2]; 100 stall = w[3]; 101 now = start; 102 first = 1; 103 flags = 0; 104 #ifdef OMEGA_GPU_WAIT_MUTANT_STALL_NO_RESET 105 flags |= 2u; 106 #endif 107 #ifdef OMEGA_GPU_WAIT_MUTANT_PROGRESS_EXTENDS_HARD 108 flags |= 1u; 109 #endif 110 for (;;) { 111 uint32_t m, m2, snap[2]; 112 int ask; 113 114 memset(in, 0, sizeof in); 115 in[0] = now; 116 in[1] = hard; 117 in[2] = start; 118 if (eng_ask(ENG_OP_WENTER, in, 3, w) != 0) { 119 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 120 return false; 121 } 122 if (w[0] == 0) { 123 rpt->elapsed_ns = w[6]; 124 rpt->result = OMEGA_GPU_WAIT_TIMEOUT; 125 return false; 126 } 127 if (cfg->abort && cfg->abort(cfg->hook_ctx)) { 128 memset(in, 0, sizeof in); 129 in[8] = 1u; 130 in[9] = now; 131 in[10] = start; 132 if (eng_ask(ENG_OP_WPOLL, in, 11, w) != 0) { 133 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 134 return false; 135 } 136 rpt->result = OMEGA_GPU_WAIT_ABORTED; 137 rpt->elapsed_ns = w[6]; 138 return false; 139 } 140 #ifndef OMEGA_GPU_WAIT_MUTANT_NO_READ_BARRIER 141 barrier(cfg->hook_ctx); 142 #endif 143 m = *word; 144 m2 = 0; 145 snap[0] = 0; 146 snap[1] = 0; 147 if (cfg->marker2) m2 = *cfg->marker2; 148 if (cfg->snapshot) cfg->snapshot(cfg->snapshot_ctx, snap); 149 rpt->polls++; 150 now = now_ns(cfg->hook_ctx); 151 memset(in, 0, sizeof in); 152 in[0] = (uint64_t)kind; 153 in[1] = expected; 154 in[2] = m; 155 in[3] = m2; 156 in[4] = cfg->marker2 ? 1u : 0u; 157 in[5] = cfg->marker2_want; 158 in[9] = now; 159 in[10] = start; 160 in[11] = stall; 161 in[12] = stall_ns; 162 in[13] = hard; 163 in[14] = total_ns; 164 in[15] = first ? 1u : 0u; 165 in[16] = rpt->last_observed; 166 in[17] = rpt->last_marker2; 167 in[18] = rpt->snap[0]; 168 in[19] = rpt->snap[1]; 169 in[20] = snap[0]; 170 in[21] = snap[1]; 171 in[22] = flags; 172 /* First ask without the valid hook. As in the C oracle, the hook runs 173 * only when the poll did not pass, and then the unit decides again. */ 174 ask = eng_ask(ENG_OP_WPOLL, in, 23, w); 175 if (ask == 0 && w[1] != 1 && cfg->valid) { 176 in[6] = 1u; 177 in[7] = cfg->valid(cfg->hook_ctx, m) ? 1u : 0u; 178 ask = eng_ask(ENG_OP_WPOLL, in, 23, w); 179 } 180 if (ask != 0) { 181 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 182 return false; 183 } 184 rpt->last_observed = m; 185 rpt->last_marker2 = m2; 186 rpt->snap[0] = snap[0]; 187 rpt->snap[1] = snap[1]; 188 rpt->elapsed_ns = now - start; 189 if (w[4]) { 190 rpt->progress_count++; 191 rpt->last_progress_ns = w[5]; 192 } 193 stall = w[2]; 194 hard = w[3]; 195 if (w[1] == 1) { 196 barrier(cfg->hook_ctx); 197 rpt->result = OMEGA_GPU_WAIT_PASS; 198 return true; 199 } 200 if (w[1] == 2) { 201 rpt->result = (omega_gpu_wait_result_t)w[0]; 202 return false; 203 } 204 first = 0; 205 relax(cfg->hook_ctx); 206 now = now_ns(cfg->hook_ctx); 207 } 208 } 209 210 static bool wait_core(omega_gpu_wait_kind_t kind, volatile uint32_t *word, uint32_t expected, 211 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { 212 omega_gpu_wait_report_t local; 213 omega_gpu_wait_cfg_t zero; 214 if (!rpt) rpt = &local; 215 memset(rpt, 0, sizeof(*rpt)); 216 rpt->wait_kind = kind; 217 rpt->expected = expected; 218 if (!cfg) { 219 memset(&zero, 0, sizeof(zero)); 220 cfg = &zero; 221 } 222 if (eng_on()) return wait_om(kind, word, expected, rpt, cfg); 223 224 225 uint64_t (*now_ns)(void *) = cfg->now_ns ? cfg->now_ns : DEFAULT_NOW_NS; 226 if (!now_ns) { 227 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; /* no clock: a bounded wait cannot be bounded */ 228 return false; 229 } 230 void (*barrier)(void *) = cfg->barrier ? cfg->barrier : real_barrier; 231 void (*relax)(void *) = cfg->relax ? cfg->relax : real_relax; 232 uint64_t total_ns = 1000000ull * (cfg->total_timeout_ms ? cfg->total_timeout_ms : OMEGA_GPU_WAIT_DEFAULT_TOTAL_MS); 233 uint64_t stall_ns = 1000000ull * (cfg->progress_timeout_ms ? cfg->progress_timeout_ms : OMEGA_GPU_WAIT_DEFAULT_PROGRESS_MS); 234 235 uint64_t start = now_ns(cfg->hook_ctx); 236 uint64_t hard_deadline = start + total_ns; 237 uint64_t stall_deadline = start + stall_ns; 238 rpt->start_ns = start; 239 rpt->last_progress_ns = start; 240 uint64_t now = start; 241 bool first = true; 242 243 /* A null word is a caller bug; report it as an invalid state, not a hang. */ 244 if (!word) { 245 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 246 return false; 247 } 248 249 while (now < hard_deadline) { 250 uint32_t m, m2 = 0, snap[2] = {0, 0}; 251 if (cfg->abort && cfg->abort(cfg->hook_ctx)) { 252 rpt->result = OMEGA_GPU_WAIT_ABORTED; 253 rpt->elapsed_ns = now - start; 254 return false; 255 } 256 #ifndef OMEGA_GPU_WAIT_MUTANT_NO_READ_BARRIER 257 barrier(cfg->hook_ctx); 258 #endif 259 m = *word; 260 if (cfg->marker2) m2 = *cfg->marker2; 261 if (cfg->snapshot) cfg->snapshot(cfg->snapshot_ctx, snap); 262 rpt->polls++; 263 now = now_ns(cfg->hook_ctx); 264 265 bool changed = m != rpt->last_observed || m2 != rpt->last_marker2 || 266 snap[0] != rpt->snap[0] || snap[1] != rpt->snap[1]; 267 rpt->last_observed = m; 268 rpt->last_marker2 = m2; 269 rpt->snap[0] = snap[0]; 270 rpt->snap[1] = snap[1]; 271 rpt->elapsed_ns = now - start; 272 273 if (changed && !first) { 274 rpt->progress_count++; 275 rpt->last_progress_ns = now; 276 #ifndef OMEGA_GPU_WAIT_MUTANT_STALL_NO_RESET 277 stall_deadline = now + stall_ns; 278 #endif 279 #ifdef OMEGA_GPU_WAIT_MUTANT_PROGRESS_EXTENDS_HARD 280 hard_deadline = now + total_ns; 281 #endif 282 } 283 bool ok = kind == OMEGA_GPU_WAIT_FIXED ? (m == expected) : ((int32_t)(m - expected) >= 0); 284 if (ok && (!cfg->marker2 || m2 == cfg->marker2_want)) { 285 barrier(cfg->hook_ctx); /* acquire: later CPU loads are ordered after the marker load */ 286 rpt->result = OMEGA_GPU_WAIT_PASS; 287 return true; 288 } 289 if (cfg->valid && !cfg->valid(cfg->hook_ctx, m)) { 290 rpt->result = OMEGA_GPU_WAIT_INVALID_STATE; 291 return false; 292 } 293 first = false; 294 if (now >= stall_deadline) { 295 rpt->result = OMEGA_GPU_WAIT_STALLED; 296 return false; 297 } 298 relax(cfg->hook_ctx); 299 now = now_ns(cfg->hook_ctx); 300 } 301 rpt->elapsed_ns = now - start; 302 rpt->result = OMEGA_GPU_WAIT_TIMEOUT; 303 return false; 304 } 305 306 bool omega_gpu_wait_fixed(volatile uint32_t *word, uint32_t expected, 307 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { 308 return wait_core(OMEGA_GPU_WAIT_FIXED, word, expected, rpt, cfg); 309 } 310 311 bool omega_gpu_wait_sequence(volatile uint32_t *word, uint32_t target, 312 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg) { 313 return wait_core(OMEGA_GPU_WAIT_SEQUENCE, word, target, rpt, cfg); 314 }