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 / src / omega_gpu_wait.h

101 lines · 4876 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#ifndef OMEGA_GPU_WAIT_H
3#define OMEGA_GPU_WAIT_H
4
5/* Shared completion wait for words the GPU writes to host-visible memory.
6 *
7 * Pure C, no NVRM dependency, so the host unit test can drive it with fake
8 * memory and a fake clock.
9 *
10 * Two wait kinds, never one ambiguous wait:
11 * omega_gpu_wait_fixed the word must equal `expected` exactly. A value
12 * that overshoots is NOT success.
13 * omega_gpu_wait_sequence the word is a free-running u32 counter (for
14 * example dispatch ids). Success when it has reached
15 * or passed `target` in serial-number order, i.e.
16 * (int32_t)(observed - target) >= 0, so it is
17 * correct across the 0xffffffff -> 0 wrap. Valid
18 * while the counter is less than 2^31 ahead.
19 *
20 * Two bounds, both on CLOCK_MONOTONIC in nanoseconds:
21 * total_timeout_ms hard deadline from the start; never extended. A device
22 * that advances occasionally still hits it (TIMEOUT).
23 * progress_timeout_ms stall deadline; restarts only when a watched word
24 * (word, marker2, snapshot words) changes (STALLED).
25 *
26 * Memory visibility: a barrier (dsb sy on AArch64, a full fence elsewhere) runs
27 * before every read of the watched words and once more, after the success
28 * observation, before returning PASS. After PASS, the CPU loads issued by the
29 * caller are ordered after the marker load. That is ordering only. It does not
30 * make a cached CPU mapping coherent with GPU writes and it does not flush the
31 * GPU L2: the marker page must still be GPU-uncached and the producing work must
32 * flush (the omega_numeric_*_gb10.c pattern). The timer does not fix visibility.
33 *
34 * If cfg->marker2 is set, PASS also requires *marker2 == marker2_want (read
35 * output only after both). */
36
37#include <stdbool.h>
38#include <stdint.h>
39
40typedef enum {
41 OMEGA_GPU_WAIT_FIXED = 1,
42 OMEGA_GPU_WAIT_SEQUENCE = 2
43} omega_gpu_wait_kind_t;
44
45typedef enum {
46 OMEGA_GPU_WAIT_PASS = 0,
47 OMEGA_GPU_WAIT_TIMEOUT = 1, /* hard total deadline */
48 OMEGA_GPU_WAIT_STALLED = 2, /* no progress for progress_timeout_ms */
49 OMEGA_GPU_WAIT_INVALID_STATE = 3, /* cfg->valid rejected an observed value */
50 OMEGA_GPU_WAIT_ABORTED = 4 /* cfg->abort asked to stop */
51} omega_gpu_wait_result_t;
52
53typedef struct {
54 omega_gpu_wait_kind_t wait_kind;
55 omega_gpu_wait_result_t result;
56 uint32_t expected; /* fixed value or sequence target */
57 uint32_t last_observed; /* last value read from the watched word */
58 uint32_t last_marker2; /* last value read from marker2 (0 when unused) */
59 uint32_t snap[2]; /* last values from the snapshot callback */
60 uint64_t start_ns;
61 uint64_t elapsed_ns;
62 uint64_t last_progress_ns; /* time of the last observed change (start if none) */
63 uint64_t progress_count; /* observed changes after the first read */
64 uint64_t polls;
65} omega_gpu_wait_report_t;
66
67typedef struct {
68 uint32_t total_timeout_ms; /* 0 means the default, 600000 */
69 uint32_t progress_timeout_ms; /* 0 means the default, 5000 */
70 volatile uint32_t *marker2;
71 uint32_t marker2_want;
72 /* Optional: fill two extra words that count as progress when they change
73 * (for example GP_PUT and a semaphore). Called once per poll. */
74 void (*snapshot)(void *ctx, uint32_t out[2]);
75 void *snapshot_ctx;
76 /* Optional: return false to declare the observed word out of order. */
77 bool (*valid)(void *ctx, uint32_t observed);
78 /* Optional: return true to stop the wait (ABORTED). */
79 bool (*abort)(void *ctx);
80 /* Test hooks. NULL means the real ones (CLOCK_MONOTONIC, dsb, yield).
81 * hook_ctx is passed to now_ns, barrier, relax, valid and abort. The freestanding build (OMEGA_GPU_NO_POSIX)
82 * has no default clock: now_ns is required there (the platform clock) and a wait without it is INVALID_STATE. */
83 uint64_t (*now_ns)(void *ctx);
84 void (*barrier)(void *ctx);
85 void (*relax)(void *ctx);
86 void *hook_ctx;
87} omega_gpu_wait_cfg_t;
88
89#define OMEGA_GPU_WAIT_DEFAULT_TOTAL_MS 600000u
90#define OMEGA_GPU_WAIT_DEFAULT_PROGRESS_MS 5000u
91
92/* Both return true on PASS. rpt and cfg may be NULL (cfg NULL means defaults). */
93bool omega_gpu_wait_fixed(volatile uint32_t *word, uint32_t expected,
94 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg);
95bool omega_gpu_wait_sequence(volatile uint32_t *word, uint32_t target,
96 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg);
97
98const char *omega_gpu_wait_result_name(omega_gpu_wait_result_t r);
99const char *omega_gpu_wait_kind_name(omega_gpu_wait_kind_t k);
100
101#endif