interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / m16 / m16_gpu_wait.c

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