Merge upstream main (v1.12 -> v1.15.1, 408 commits) into the SYCL port

Conflicts from the old m2xfp branch take upstream's version. Two libxe2 fixes the merge needs:
- rad_plugin_abi_layout: the plugin now exports the ABI layout hash the loader checks.
- gemm_nt_q_w4a16: upstream 492d33c sends bf16 activations to the w4a16 GEMM (the separate cast
  is gone); the fast kernel took only f16, so every call fell back to the exact path (decode
  ~2.5 tok/s). A bf16 activation is narrowed to f16 (round to nearest even) into a workspace first,
  the dropped cast bit for bit; decode back to ~57 tok/s on one request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-11-02 00:25:57 +01:00
commit 7580ec3d17
937 files changed
+285776 -8914

No files matched your search

+546 -83
View File
@@ -64,6 +64,7 @@
#include <vector>
#include "aql.h"
#include "directio.h"
#include "rad_format.h"
#include "../rad_internal.h"
@@ -77,6 +78,10 @@ namespace rad {
int aql_vmem_device_of(const void* addr, int* dev);
namespace {
void stamp_hold(hsa_signal_t h, bool on);
}
namespace aql {
namespace {
@@ -598,8 +603,6 @@ struct Stream {
bool agent_only = false; /* rad_xp_stream_create: never a system-scope acquire */
std::atomic<int> gates{0}; /* closed gates (rad_stream_gate) on this queue */
std::atomic<int> fault{0};
int unordered_run = 0; /* played packets in a row without the barrier bit */
bool unordered_warned = false;
char* bounce = nullptr; /* pinned, for pageable copies */
std::mutex bounce_mu;
/* Chained launches (RadKernelChainFn): the completion counter every block of a chained kernel
@@ -633,11 +636,25 @@ inline Stream* ours(const void* s) {
return p->magic == kStreamMagic ? const_cast<Stream*>(p) : nullptr;
}
/* The device's stuck state, for dev_stuck_ms, in steady-clock ns (0: none). `g_stuck_at` is the
* last moment anything advanced, set by the stall verdict (Stall::check) with the read-progress sum
* it saw in `g_stuck_progress`; `g_fault_at` is when the first queue faulted. */
std::atomic<uint64_t> g_stuck_at{0};
std::atomic<uint64_t> g_stuck_progress{0};
std::atomic<uint64_t> g_fault_at{0};
inline uint64_t steady_ns() {
return (uint64_t)std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
void on_queue_error(hsa_status_t st, hsa_queue_t*, void* data) {
Stream* s = static_cast<Stream*>(data);
const char* m = nullptr;
hsa_status_string(st, &m);
s->fault.store((int)st, std::memory_order_release);
uint64_t none = 0;
g_fault_at.compare_exchange_strong(none, steady_ns(), std::memory_order_acq_rel);
G().faulted.store(true, std::memory_order_release);
hsa_signal_store_screlease(G().fault, 1);
RAD_ERR("device %d: the AQL queue faulted: %s", s->ord, m ? m : "?");
@@ -648,12 +665,19 @@ void on_queue_error(hsa_status_t st, hsa_queue_t*, void* data) {
* nothing will decrement, or a kernel that faulted silently); the packet at the read index says
* which. */
/* Sum of every queue's read index. It only grows, so an unchanged sum means no queue moved. */
/* How long a host wait may see no queue advance before it reports the queues; see dump_queues. */
/* How long a host wait may see no progress before it reports the queues; see dump_queues. The
* watchdog is progress-aware -- read_progress counts the drive reader's bytes beside the queues'
* read indices -- so a period that moves nothing anywhere is a hang however slow the model's
* drives are, and the threshold needs no tuning per deployment. */
constexpr uint32_t kStallMs = 5000;
uint64_t read_progress() {
/* Queue read indices plus the drive reader's byte count: the file tier's reads legitimately
* park every queue for seconds at a time (one rank spins at the all-reduce while the other is
* still reading its layer off the drive), and the bytes coming back are the progress that
* makes such a wait not a stall. */
uint64_t n = rad::dio_progress.load(std::memory_order_relaxed);
std::lock_guard<std::mutex> lk(G().streams_mu);
uint64_t n = 0;
for (Stream* s : G().streams) n += hsa_queue_load_read_index_scacquire(s->q);
return n;
}
@@ -869,7 +893,8 @@ hsa_status_t on_system_event(const hsa_amd_event_t* e, void*) {
* video generator's denoising pass for tens of them. So the queues' read indices are sampled once
* every stall period and only a period in which none advanced reports, with the packet each queue
* is on: a hung kernel, or a queue the driver evicted and could not restore. A wait that keeps
* moving says nothing. */
* moving says nothing. The drive reader's byte count is progress too (read_progress), so a wait
* parked on the file tier's drive reads never reports. */
struct Stall {
const char* where;
uint32_t n = 0;
@@ -892,8 +917,11 @@ struct Stall {
const uint64_t p = read_progress();
if (sampled && p == progress) {
told = true;
/* Nothing has advanced since the previous sample, a stall period ago. */
g_stuck_progress.store(p, std::memory_order_relaxed);
g_stuck_at.store(steady_ns() - (uint64_t)kStallMs * 1000000u, std::memory_order_release);
dump_queues(where);
RAD_ERR("device: no queue has advanced in the %u s since: the GPU is not running the "
RAD_ERR("device: nothing has advanced in the %u s since: the GPU is not running the "
"packet named above -- a hung kernel, or a queue the driver evicted and could "
"not restore (amdgpu cwsr_enable=0 is one cause)", kStallMs / 1000);
return;
@@ -903,6 +931,22 @@ struct Stall {
}
};
/* dev_stuck_ms. A standing verdict is dropped as soon as the read progress moves again. */
int64_t stuck_ms() {
const uint64_t now = steady_ns();
if (G().faulted.load(std::memory_order_acquire)) {
const uint64_t f = g_fault_at.load(std::memory_order_acquire);
return f && now > f ? (int64_t)((now - f) / 1000000u) : 0;
}
uint64_t at = g_stuck_at.load(std::memory_order_acquire);
if (!at) return 0;
if (read_progress() != g_stuck_progress.load(std::memory_order_relaxed)) {
g_stuck_at.compare_exchange_strong(at, 0, std::memory_order_acq_rel);
return 0;
}
return now > at ? (int64_t)((now - at) / 1000000u) : 0;
}
/* Reserves packet `idx` once the packet processor has consumed far enough that its slot is free.
* A launched barrier-bit packet means every packet before it has completed, which a kernel
* argument slot (read for the kernel's whole run) needs. A played tape clears the bit on at most
@@ -935,18 +979,12 @@ inline int reserve(Stream* s, uint64_t* out) {
inline void publish(Stream* s, uint64_t idx, uint16_t header, uint16_t setup) {
s->wr = idx + 1;
s->unordered_run = 0; /* an offered packet: every reclamation proof holds */
_mm_sfence(); /* the packet body and the argument slot are write-combined
VRAM: globally visible before the index below exposes
the packet to the GPU (a torn kernarg read faults on the
device with a stripped high pointer bit) */
hsa_queue_store_write_index_relaxed(s->q, idx + 1);
_mm_sfence(); /* the argument slot, written before the header */
__atomic_store_n(reinterpret_cast<uint32_t*>(
static_cast<hsa_kernel_dispatch_packet_t*>(s->q->base_address) +
(idx & s->mask)),
(uint32_t)header | ((uint32_t)setup << 16), __ATOMIC_RELEASE);
/* The index is what exposes the packet to the GPU: only after the fence and the
* header, or the packet processor can read a half-written argument block. */
hsa_queue_store_write_index_relaxed(s->q, idx + 1);
hsa_signal_store_relaxed(s->q->doorbell_signal, (hsa_signal_value_t)idx);
}
@@ -1165,23 +1203,21 @@ struct PlayLeft { uint32_t q = UINT32_MAX; uint64_t wr = 0; };
constexpr int kPlayLeft = 8;
thread_local PlayLeft t_left[kPlayLeft];
/* A dispatch played under RADIANCE_AQL_STAMP (see "dispatch timestamps"). `sig` goes in as the
* stamp's own signal and comes out as the packet's completion signal, which is a record's or a
* ride's where one rides on the dispatch. */
struct PlayStamp {
hsa_signal_t sig{};
bool ordered = false;
bool chained = false;
};
struct PlayRun {
const Tape* tape = nullptr; /* the tape played, for a chained record's argument bytes */
Stream* s = nullptr;
uint64_t next = 0; /* the write index after the last packet written */
int unrung = 0;
bool foreign = true; /* the next packet's queue has packets this thread did not play */
uint32_t seen[4] = {UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX};
int n_seen = 0;
/* True for the first dispatch this play writes on each queue, whatever its record says.
* reserve()'s argument-slot proof needs an ordered packet after any the reuse may race:
* a slot is reused once the read index passes the packet that held it, and only an
* ordered packet after that one makes the index mean "it completed". */
bool first_on(const Stream* q) {
for (int i = 0; i < n_seen; ++i) if (seen[i] == q->id) return false;
if (n_seen < 4) seen[n_seen++] = q->id;
return true;
}
void ring() {
if (!unrung) return;
@@ -1210,7 +1246,7 @@ struct PlayRun {
/* `done`, when set, is the packet's completion signal and `rel_min` its minimum release
* scope, for a record riding on this dispatch (see a_tape_play). */
int put(const TapeRec& r, uint64_t* out_idx, hsa_signal_t done = hsa_signal_t{0},
int rel_min = HSA_FENCE_SCOPE_NONE) {
int rel_min = HSA_FENCE_SCOPE_NONE, PlayStamp* stamp = nullptr) {
if (s != r.s) {
end();
s = r.s;
@@ -1235,6 +1271,12 @@ struct PlayRun {
s->ride.handle = 0;
}
if (done.handle) p->completion_signal = done;
if (stamp) {
/* Before the header: once the packet completes, the signal may return to the pool. */
if (done.handle) stamp_hold(done, true);
else p->completion_signal = stamp->sig;
stamp->sig = p->completion_signal;
}
/* A chained record plays from this queue's own argument slot, where its chain record goes:
* the tape's copy is shared by every play, and the next step's play may be written while
* this one runs. */
@@ -1254,21 +1296,15 @@ struct PlayRun {
/* A dispatch carrying a record keeps the bit: its completion must cover everything before
* it on the queue, which an unordered dispatch's does not. A chained kernel behind another chained one
* on this queue waits in its prologue instead. */
bool ordered = first_on(s) || foreign || !r.overlap || done.handle;
bool ordered = foreign || !r.overlap || done.handle;
/* Not while a plain packet may still run: a played plain kernel the overlap analysis let
* start early (it touched nothing in flight) adds nothing to the counter, so a chained
* kernel that reads its output and sits behind a chained one (a cache read beside a
* collective, say) would wait for the chained blocks only. */
if (chain && s->prev_chained && !s->plain_inflight && !foreign && !done.handle) ordered = false;
/* The bound the slot proof rests on. Say so once if it is ever crossed, rather than tear
* an argument block silently. */
if (ordered) {
s->unordered_run = 0;
} else if (++s->unordered_run > kTapeOverlapRun && !s->unordered_warned) {
s->unordered_warned = true;
RAD_WARN("queue %u: %d played packets in a row carry no barrier bit, past the %d the "
"argument-slot proof tolerates; a kernarg block may be reused while a kernel "
"still reads it", s->id, s->unordered_run, kTapeOverlapRun);
if (stamp) {
stamp->ordered = ordered;
stamp->chained = chain;
}
const uint16_t h = header(HSA_PACKET_TYPE_KERNEL_DISPATCH, acq, rel, ordered);
__atomic_store_n(reinterpret_cast<uint32_t*>(p), (uint32_t)h | ((uint32_t)r.pkt.setup << 16),
@@ -1612,12 +1648,19 @@ struct Sig {
bool irq = false; /* an interrupt signal */
hsa_agent_t agent{}; /* where it was recorded */
std::vector<std::pair<uint32_t, uint64_t>> waiters; /* queue id, packet index */
bool stamped = false; /* its times not read yet (stamp_hold) */
/* A prefetch DMA queue's list polls it (a_xp_list): the card whose queue, -1 for none, and the
* list number the queue writes to its finish word once the engine is past the poll. A DMA
* poll is no queue packet, so `waiters` cannot hold it. */
std::atomic<int32_t> xp_dev{-1};
std::atomic<uint32_t> xp_list{0};
};
struct SigPool {
std::mutex mu;
std::vector<Sig*> free[2]; /* by irq */
std::deque<Sig*> retiring; /* in the order they were retired */
std::unordered_map<uint64_t, Sig*> by_handle;
};
SigPool& pool() {
@@ -1625,15 +1668,29 @@ SigPool& pool() {
return *p;
}
bool xp_list_finished(int ord, uint32_t k);
/* A signal at 0 can still be awaited: by a queue packet not yet consumed (`waiters`), or by a
* prefetch DMA list that has not yet read it (`xp_dev`). Reused then, its next user's 1 would be
* what that wait sees, and the wait would hang behind work that waits for it. */
bool reusable(const Sig* g) {
if (hsa_signal_load_relaxed(g->h) != 0) return false;
if (g->stamped || hsa_signal_load_relaxed(g->h) != 0) return false;
for (const auto& w : g->waiters) {
hsa_queue_t* q = g_queue_by_id[w.first].load(std::memory_order_acquire);
if (q && hsa_queue_load_read_index_relaxed(q) <= w.second) return false;
}
const int32_t xd = g->xp_dev.load(std::memory_order_acquire);
if (xd >= 0 && !xp_list_finished(xd, g->xp_list.load(std::memory_order_relaxed))) return false;
return true;
}
/* Back to the free list: what held it is over. */
void sig_free(SigPool& p, Sig* g) {
g->waiters.clear();
g->xp_dev.store(-1, std::memory_order_relaxed);
p.free[g->irq].push_back(g);
}
/* A signal for one record: an interrupt signal if the host waits on the event, else GPU_ONLY.
* Only interrupt signals can end a host wait (see "host waits"), but each costs a driver event (a
* process has a few thousand) and an interrupt per completion, wasted on events recorded dozens
@@ -1648,8 +1705,7 @@ Sig* sig_get(bool irq) {
while (p.free[irq].empty() && !p.retiring.empty() && reusable(p.retiring.front())) {
Sig* g = p.retiring.front();
p.retiring.pop_front();
g->waiters.clear();
p.free[g->irq].push_back(g);
sig_free(p, g);
}
if (p.free[irq].empty()) {
/* In place and in order, so the front stays the oldest. */
@@ -1657,8 +1713,7 @@ Sig* sig_get(bool irq) {
for (size_t i = 0; i < p.retiring.size(); ++i) {
Sig* g = p.retiring[i];
if (reusable(g)) {
g->waiters.clear();
p.free[g->irq].push_back(g);
sig_free(p, g);
} else {
p.retiring[kept++] = g;
}
@@ -1675,9 +1730,19 @@ Sig* sig_get(bool irq) {
const hsa_status_t st = irq ? hsa_signal_create(0, 0, nullptr, &g->h)
: hsa_amd_signal_create(0, 0, nullptr, HSA_AMD_SIGNAL_AMD_GPU_ONLY, &g->h);
if (st != HSA_STATUS_SUCCESS || (irq && !interrupts(g->h))) return nullptr;
p.by_handle[g->h.handle] = g.get();
return g.release();
}
/* A stamp's times live in the signal until a reuse overwrites them, so a record's or a ride's
* signal a stamp shares stays out of the free list until they are read. */
void stamp_hold(hsa_signal_t h, bool on) {
SigPool& p = pool();
std::lock_guard<std::mutex> lk(p.mu);
auto it = p.by_handle.find(h.handle);
if (it != p.by_handle.end()) it->second->stamped = on;
}
void sig_retire(Sig* g) {
SigPool& p = pool();
std::lock_guard<std::mutex> lk(p.mu);
@@ -1951,6 +2016,228 @@ int a_tape_set_overlap(RadTape rt, int i, int on) {
return RAD_OK;
}
/* ---- dispatch timestamps (RADIANCE_AQL_STAMP)
*
* RADIANCE_AQL_STAMP=N gives every dispatch of every Nth pass played on card 0 a completion signal,
* into which the packet processor writes the dispatch's start and end (profiling is on on every
* queue). Nothing else changes: PlayRun::put decides the barrier bit and the chain as without the
* signal, so the queues run the pass as unstamped. A pass is the plays of one tape from its record
* 0 on: Ctx::play_pass plays a pass in stretches between its host ops, and counting plays would
* time stretches rather than passes. A dispatch a record or a ride already rides on is stamped
* from that signal, held out of the pool until its times are read (stamp_hold).
*
* Nothing waits for the card: a later play on card 0 reads the signals that reached 0, in issue
* order, and appends a line per dispatch to RADIANCE_AQL_STAMP_OUT (default
* /tmp/radiance-stamps-<pid>.csv); exit waits briefly for the rest. */
constexpr size_t kStampSignals = 8192;
struct StampRow {
hsa_signal_t h{};
bool own = false; /* the stamp's signal, not a record's or a ride's */
bool ordered = false;
bool chained = false;
uint32_t queue = 0;
int seq = 0; /* its record in the tape */
uint32_t play = 0; /* the play of the pass that issued it */
uint32_t records = 0; /* the tape's length, which tells kinds of pass apart */
uint64_t pass = 0;
uint64_t kernel = 0;
};
struct Stamps {
std::mutex mu;
int every = 0;
std::string path;
FILE* out = nullptr;
bool out_failed = false;
bool full_told = false;
uint64_t passes = 0; /* begun on card 0 */
const Tape* tape = nullptr; /* the pass being stamped, if one is */
uint32_t play = 0;
std::vector<hsa_signal_t> free;
size_t made = 0;
std::deque<StampRow> rows; /* issued, times not read yet */
std::unordered_map<uint64_t, std::string> names;
};
void stamps_at_exit();
Stamps& stamps() {
static Stamps* st = [] {
Stamps* x = new Stamps();
const char* n = std::getenv("RADIANCE_AQL_STAMP");
x->every = n ? std::max(0, std::atoi(n)) : 0;
const char* o = std::getenv("RADIANCE_AQL_STAMP_OUT");
x->path = o && *o ? std::string(o) : fmt("/tmp/radiance-stamps-%d.csv", (int)getpid());
if (x->every) {
RAD_WARN("device: RADIANCE_AQL_STAMP times every dispatch of one pass in %d on card 0, "
"into %s", x->every, x->path.c_str());
std::atexit(stamps_at_exit);
}
return x;
}();
return *st;
}
bool stamps_file(Stamps& st) {
if (st.out) return true;
if (st.out_failed) return false;
st.out = std::fopen(st.path.c_str(), "a");
if (!st.out) {
st.out_failed = true;
RAD_WARN("device: RADIANCE_AQL_STAMP: %s cannot be opened (%s); the times are dropped",
st.path.c_str(), std::strerror(errno));
return false;
}
std::fseek(st.out, 0, SEEK_END);
if (std::ftell(st.out) == 0)
std::fputs("pass,seq,queue,kernel,ordered,chained,start_ns,end_ns,play,records\n", st.out);
return true;
}
const std::string& stamps_kernel(Stamps& st, uint64_t obj) {
auto it = st.names.find(obj);
if (it != st.names.end()) return it->second;
std::string n;
{
std::lock_guard<std::mutex> nl(g_names_mu);
auto k = g_names.find(obj);
n = k != g_names.end() ? k->second : fmt("%#" PRIx64, obj);
}
if (n.find_first_of(",\"") != std::string::npos) {
std::string q = "\"";
for (char c : n) {
if (c == '"') q += '"';
q += c;
}
n = q + "\"";
}
return st.names.emplace(obj, std::move(n)).first->second;
}
uint64_t stamps_ns(uint64_t t) {
return (uint64_t)((unsigned __int128)t * 1000000000u / G().ts_freq);
}
/* With st.mu held. Rows are read in issue order, so one still running holds back the rows after
* it, which a later call reads. */
void stamps_read(Stamps& st, bool wait) {
bool wrote = false;
while (!st.rows.empty()) {
const StampRow w = st.rows.front();
if (hsa_signal_load_scacquire(w.h) != 0 &&
(!wait || G().faulted.load(std::memory_order_acquire) ||
hsa_signal_wait_scacquire(w.h, HSA_SIGNAL_CONDITION_EQ, 0, G().ts_freq / 10,
HSA_WAIT_STATE_ACTIVE) != 0))
break;
hsa_amd_profiling_dispatch_time_t t{};
const bool timed = hsa_amd_profiling_get_dispatch_time(G().dev[0].agent, w.h, &t) ==
HSA_STATUS_SUCCESS;
if (w.own) st.free.push_back(w.h);
else stamp_hold(w.h, false);
st.rows.pop_front();
if (!stamps_file(st)) continue;
std::fprintf(st.out, "%" PRIu64 ",%d,%u,%s,%d,%d,", w.pass, w.seq, w.queue,
stamps_kernel(st, w.kernel).c_str(), (int)w.ordered, (int)w.chained);
if (timed) std::fprintf(st.out, "%" PRIu64 ",%" PRIu64, stamps_ns(t.start), stamps_ns(t.end));
else std::fputc(',', st.out);
std::fprintf(st.out, ",%u,%u\n", w.play, w.records);
wrote = true;
}
if (wrote) std::fflush(st.out);
}
void stamps_at_exit() {
Stamps& st = stamps();
std::lock_guard<std::mutex> lk(st.mu);
stamps_read(st, true);
if (st.out) std::fclose(st.out);
st.out = nullptr;
st.out_failed = true;
}
/* The stamping of one play: a signal for each of its dispatches, taken up front so the play
* takes the lock twice rather than once a dispatch. */
struct StampRun {
bool on = false;
uint64_t pass = 0;
uint32_t play = 0;
uint32_t records = 0;
std::vector<hsa_signal_t> sig;
size_t used = 0;
std::vector<StampRow> rows;
void begin(const Tape* t, int from, int to) {
Stamps& st = stamps();
if (!st.every || from >= to || t->rec[(size_t)from].s->ord != 0) return;
std::lock_guard<std::mutex> lk(st.mu);
stamps_read(st, false);
if (from == 0) {
st.tape = ++st.passes % (uint64_t)st.every == 0 ? t : nullptr;
st.play = 0;
}
if (!st.tape || st.tape != t) return;
size_t n = 0;
for (int i = from; i < to; ++i) n += t->rec[(size_t)i].kind == kTapeDispatch;
while (sig.size() < n) {
hsa_signal_t h{};
if (!st.free.empty()) {
h = st.free.back();
st.free.pop_back();
} else if (st.made < kStampSignals &&
hsa_amd_signal_create(1, 0, nullptr, HSA_AMD_SIGNAL_AMD_GPU_ONLY, &h) ==
HSA_STATUS_SUCCESS) {
++st.made;
} else {
break;
}
hsa_signal_silent_store_relaxed(h, 1);
sig.push_back(h);
}
if (sig.size() < n) {
st.tape = nullptr;
if (!st.full_told) {
st.full_told = true;
RAD_WARN("device: RADIANCE_AQL_STAMP: %zu signals are in flight, pass %" PRIu64
" and any pass that finds them so are stamped in part", st.made, st.passes);
}
}
on = true;
pass = st.passes;
play = st.play++;
records = (uint32_t)t->rec.size();
}
PlayStamp* take(PlayStamp& ps) {
if (!on || used == sig.size()) return nullptr;
ps = PlayStamp{};
ps.sig = sig[used++];
return &ps;
}
void drop() { --used; }
void note(const TapeRec& r, int seq, const PlayStamp& ps) {
StampRow w;
w.h = ps.sig;
w.own = ps.sig.handle == sig[used - 1].handle;
if (!w.own) --used;
w.ordered = ps.ordered;
w.chained = ps.chained;
w.queue = r.s->id;
w.seq = seq;
w.play = play;
w.records = records;
w.pass = pass;
w.kernel = r.pkt.kernel_object;
rows.push_back(w);
}
void end() {
if (sig.empty()) return;
Stamps& st = stamps();
std::lock_guard<std::mutex> lk(st.mu);
st.free.insert(st.free.end(), sig.begin() + (std::ptrdiff_t)used, sig.end());
st.rows.insert(st.rows.end(), rows.begin(), rows.end());
}
};
int a_tape_play(RadTape rt, int from, int to) {
Tape* t = tape_of(rt);
if (!t || from < 0 || to > (int)t->rec.size() || from > to) {
@@ -1961,6 +2248,8 @@ int a_tape_play(RadTape rt, int from, int to) {
int rc = RAD_OK;
PlayRun run;
run.tape = t;
StampRun stamp;
stamp.begin(t, from, to);
for (int i = from; i < to && rc >= 0; ++i) {
const TapeRec& r = t->rec[(size_t)i];
if (r.kind == kTapeDispatch) {
@@ -1979,8 +2268,14 @@ int a_tape_play(RadTape rt, int from, int to) {
break;
}
uint64_t idx = 0;
rc = run.put(r, &idx, g ? g->h : hsa_signal_t{0}, rel);
if (rc < 0) break;
PlayStamp ps;
PlayStamp* sp = stamp.take(ps);
rc = run.put(r, &idx, g ? g->h : hsa_signal_t{0}, rel, sp);
if (rc < 0) {
if (sp) stamp.drop();
break;
}
if (sp) stamp.note(r, i, ps);
bool found = false;
for (auto& l : t->last)
if (l.first == r.s->id) { l.second = idx; found = true; break; }
@@ -2001,6 +2296,7 @@ int a_tape_play(RadTape rt, int from, int to) {
: a_event_wait((RadStream)r.s, r.ev);
}
run.end();
stamp.end();
--t_tape_pause;
return rc;
}
@@ -2272,10 +2568,10 @@ struct DmaQueue {
bool ever_held = false; /* a stream has held this card's link at least once */
std::atomic<bool> tried{false}; /* dma_open has run, for the startup report */
std::string why; /* why it failed */
/* For a queue whose lists each end by writing their number to `fin` (the expert prefetch's),
* the room check uses the end of the last finished list as a lower bound on what the engine
* has read. The engine's written-back read pointer can stop moving on such a queue while the
* engine runs on, and a ring the host thinks full would wait forever. */
/* For a queue whose lists each end by writing their number to `fin` (the lane's and the
* expert prefetch's), the room check uses the end of the last finished list as a lower bound
* on what the engine has read. The engine's written-back read pointer can stop moving on such
* a queue while the engine runs on, and a ring the host thinks full would wait forever. */
volatile uint32_t* fin = nullptr;
uint32_t lists = 0; /* lists handed over; list k ends at ends[k % kDmaEnds] */
std::unique_ptr<uint64_t[]> ends;
@@ -2462,8 +2758,9 @@ void dump_dma_lanes() {
for (int c = 0; c < kMaxDevices; ++c) {
DmaQueue& q = g_dma[c];
if (!q.tried.load(std::memory_order_acquire) || !q.ok) continue;
RAD_ERR(" device %d DMA lane: ring written %" PRIu64 " B, read %" PRIu64 " B; link gate %"
PRId64 " (%s), last hold issued %s", c, (uint64_t)q.w, (uint64_t)*q.rptr,
RAD_ERR(" device %d DMA lane: ring written %" PRIu64 " B, read %" PRIu64 " B; %u lists handed "
"over, %" PRIu32 " finished; link gate %" PRId64 " (%s), last hold issued %s",
c, (uint64_t)q.w, dma_read(&q), q.lists, q.fin ? *q.fin : 0u,
(int64_t)hsa_signal_load_relaxed(q.gate),
(hsa_signal_load_relaxed(q.gate) & 1) ? "held" : "free", q.held ? "on" : "off");
}
@@ -2475,6 +2772,11 @@ void dump_dma_lanes() {
}
}
/* Where the engine reads and writes a signal's value: the 64-bit word the CP decrements and polls. */
inline uint64_t sig_value_addr(const Sig* g) {
return (uint64_t)g->h.handle + offsetof(amd_signal_t, value);
}
/* The card's queue, opened on first use; null (after one warning) if the driver refuses. */
DmaQueue* dma_queue(int ord) {
if (ord < 0 || ord >= kMaxDevices || ord >= (int)G().dev.size()) return nullptr;
@@ -2486,6 +2788,18 @@ DmaQueue* dma_queue(int ord) {
q.why = why;
q.tried.store(true, std::memory_order_release);
if (q.ok) aql::g_dump_lanes = dump_dma_lanes;
if (q.ok) {
/* The engine's written-back read pointer can stop moving while the engine runs on (the
* expert prefetch's queue has the same quirk), and a ring the host thinks full would
* wait forever -- a large pool's load fill wedged exactly there. So every list ends by
* writing its number to a word the engine keeps advancing, which bounds the engine's
* progress from below without trusting the read pointer (dma_read). */
if (Sig* f = sig_get(false)) {
hsa_signal_silent_store_relaxed(f->h, 0);
q.ends.reset(new uint64_t[kDmaEnds]());
q.fin = (volatile uint32_t*)(uintptr_t)sig_value_addr(f);
}
}
if (q.ok)
RAD_INFO("device: card %d copy lists go to DMA engine 0 on a queue of this process's own "
"(%.1f ms to open)", ord,
@@ -2543,11 +2857,6 @@ void dma_kick(DmaQueue* q) {
*q->db = q->w;
}
/* Where the engine reads and writes a signal's value: the 64-bit word the CP decrements and polls. */
inline uint64_t sig_value_addr(const Sig* g) {
return (uint64_t)g->h.handle + offsetof(amd_signal_t, value);
}
/* ------------------------------------------------- the expert prefetch's device half (rad_xp_*) */
/* The card's prefetch queue on its second DMA engine (the lane has the first), opened on first
@@ -2576,6 +2885,15 @@ DmaQueue* xp_queue(int ord) {
return q.ok ? &q : nullptr;
}
/* Whether card `ord`'s prefetch queue has finished list `k`, or a later one: its lists finish in
* order, so the engine is then past every packet list `k` holds. */
bool xp_list_finished(int ord, uint32_t k) {
if (ord < 0 || ord >= kMaxDevices) return true;
const DmaQueue& q = g_dma_xp[ord];
if (!q.fin) return true;
return (int32_t)(*q.fin - k) >= 0;
}
int a_xp_supported(int device) { return xp_queue(device) != nullptr; }
int a_xp_stream_create(RadStream* out) {
@@ -2697,9 +3015,9 @@ int64_t a_gate_room(RadStream rs) {
/* Waits for the mark, then for the card's link gate if a collective has ever held it (under TP
* the all-reduce shares the link and the step waits on it, not on these copies), copies, writes
* `done`, then the list's number. */
* `done`, then the list's number. RAD_XP_NO_YIELD skips the gate (rad_xp_list). */
int a_xp_list(int ord, void* mark, int n, const uint64_t* src, const uint64_t* dst, const uint64_t* bytes,
uint32_t* done, uint32_t done_val) {
uint32_t* done, uint32_t done_val, unsigned flags) {
DmaQueue* dq = xp_queue(ord);
if (!dq) return RAD_E_UNSUPPORTED;
Sig* in = (Sig*)mark;
@@ -2712,8 +3030,18 @@ int a_xp_list(int ord, void* mark, int n, const uint64_t* src, const uint64_t* d
(uint32_t)ia, (uint32_t)(ia >> 32), 0u, 0xffffffffu,
10u /* interval */ | (0xfffu << 16) /* retry forever */ };
rc = dma_put(dq, poll, 6);
/* The mark stays out of the pool until the engine is past this poll: the finish word
* reaches lists + 1 only behind it, whether this list's own fence writes it or, if the
* list below fails, the next list's. Returned earlier, a mark at 0 can be handed out and
* set to 1 before the engine reads it, and the list then waits for whatever completes
* the next use, which can sit behind the stream's wait for this list. */
if (rc == RAD_OK) {
in->xp_list.store(dq->lists + 1, std::memory_order_relaxed);
in->xp_dev.store(ord, std::memory_order_release);
}
}
if (rc == RAD_OK && n > 0 && lane && lane->ever_held && lane->gate.handle) {
if (rc == RAD_OK && n > 0 && lane && lane->ever_held && lane->gate.handle &&
!(flags & RAD_XP_NO_YIELD)) {
const uint64_t ga = (uint64_t)lane->gate.handle + offsetof(amd_signal_t, value);
const uint32_t gate[6] = { kSdmaPoll | (3u << 28) /* equal */ | (1u << 31) /* memory */,
(uint32_t)ga, (uint32_t)(ga >> 32), 0u, 1u /* the low bit */,
@@ -2748,6 +3076,29 @@ int a_xp_list(int ord, void* mark, int n, const uint64_t* src, const uint64_t* d
return rc;
}
/* rad_event_dma: a fresh signal for the event, at 0. The caller arms it with a 1 written in stream
* order: a host store could land ahead of a wait still queued for the previous copy, and an event
* record completes by reaching 0, the value the wait looks for. A fresh signal each time keeps two
* uses from sharing one word. */
int a_event_dma(RadEvent re, RadStream rs, uint32_t** word) {
if (!word) { device_set_error("rad_event_dma: null word"); return RAD_E_INVAL; }
if (capturing()) {
device_set_error("rad_event_dma: a tape cannot record a DMA list; the caller must re-run "
"this call with the tape paused");
return RAD_E_STATE;
}
Event* e = ev_of(re);
Stream* s = ours(rs);
if (!e || !s) { device_set_error("rad_event_dma: null or foreign argument"); return RAD_E_INVAL; }
Sig* g = sig_get(e->host);
if (!g) { device_set_error("rad_event_dma: no signal could be made"); return RAD_E_DEVICE; }
hsa_signal_silent_store_relaxed(g->h, 0);
g->agent = s->agent;
record_adopt(e, g);
*word = (uint32_t*)(uintptr_t)sig_value_addr(g);
return RAD_OK;
}
int64_t a_xp_behind(int ord) {
DmaQueue* dq = (ord >= 0 && ord < kMaxDevices) ? &g_dma_xp[ord] : nullptr;
if (!dq || !dq->tried.load(std::memory_order_acquire) || !dq->ok) return 0;
@@ -2864,6 +3215,19 @@ int a_memcpy_list_async(const RadCopySeg* segs, int n, unsigned flags, RadStream
(uint32_t)oa, (uint32_t)(oa >> 32), 0u };
rc = dma_put(dq, fence, 4);
}
if (rc == RAD_OK && dq->fin) {
/* Name the list: the engine writes the number when everything before it is done,
* which the room check trusts over the read pointer (see dma_read). */
const uint32_t k = dq->lists + 1;
const uint64_t fa = (uint64_t)(uintptr_t)dq->fin;
const uint32_t finf[4] = { kSdmaFence | (3u << 16) /* uncached */,
(uint32_t)fa, (uint32_t)(fa >> 32), k };
rc = dma_put(dq, finf, 4);
if (rc == RAD_OK) {
dq->ends[k % kDmaEnds] = dq->w;
dq->lists = k;
}
}
dma_kick(dq);
}
/* If the ring refused the list, release `out` so the stream does not wait on it forever. */
@@ -3383,6 +3747,43 @@ void* pin_whole(int64_t bytes, const std::vector<hsa_agent_t>& cards) {
return p;
}
/* Locks [p, p + len) in pieces of at most kPinPiece for every card, each mapped on the cards at
* its host address. On failure sets the device error for `who` (asked for `bytes`), unlocks what
* was locked and returns false; the mapping stays the caller's to unmap. */
bool lock_pieces(char* p, int64_t len, int64_t bytes, std::vector<hsa_agent_t>& cards,
const char* who) {
const int pieces = (int)((len + kPinPiece - 1) / kPinPiece);
int done = 0;
for (; done < pieces; ++done) {
const int64_t off = (int64_t)done * kPinPiece;
const int64_t n = std::min(kPinPiece, len - off);
void* on_card = nullptr;
const hsa_status_t st = hsa_amd_memory_lock_to_pool(p + off, (size_t)n, cards.data(),
(int)cards.size(), G().host_fine, 0,
&on_card);
if (st != HSA_STATUS_SUCCESS) {
device_set_error("%s(%s): locking piece %d of %d (%s at +%s): %s", who,
humanb(bytes).c_str(), done + 1, pieces, humanb(n).c_str(),
humanb(off).c_str(), hsa_why(st));
break;
}
if (on_card != p + off) {
++done;
device_set_error("%s(%s): piece %d of %d is mapped on the cards at %p, not "
"at its host address %p, so the pieces are not one range there, "
"although the kernel driver reports the SVM API that maps a lock in "
"place",
who, humanb(bytes).c_str(), done, pieces, on_card, (void*)(p + off));
break;
}
}
if (done < pieces) {
for (int i = 0; i < done; ++i) (void)hsa_amd_memory_unlock(p + (int64_t)i * kPinPiece);
return false;
}
return true;
}
void* pin_pieces(int64_t bytes) {
std::vector<hsa_agent_t> cards;
for (const Dev& d : G().dev) cards.push_back(d.agent);
@@ -3411,33 +3812,61 @@ void* pin_pieces(int64_t bytes) {
}
(void)::madvise(p, (size_t)len, MADV_DONTFORK);
const int pieces = (int)((len + kPinPiece - 1) / kPinPiece);
int done = 0;
for (; done < pieces; ++done) {
const int64_t off = (int64_t)done * kPinPiece;
const int64_t n = std::min(kPinPiece, len - off);
void* on_card = nullptr;
const hsa_status_t st = hsa_amd_memory_lock_to_pool(p + off, (size_t)n, cards.data(),
(int)cards.size(), G().host_fine, 0,
&on_card);
if (st != HSA_STATUS_SUCCESS) {
device_set_error("rad_dev_alloc(%s): locking piece %d of %d (%s at +%s): %s",
humanb(bytes).c_str(), done + 1, pieces, humanb(n).c_str(),
humanb(off).c_str(), hsa_why(st));
break;
}
if (on_card != p + off) {
++done;
device_set_error("rad_dev_alloc(%s): piece %d of %d is mapped on the cards at %p, not "
"at its host address %p, so the pieces are not one range there, "
"although the kernel driver reports the SVM API that maps a lock in "
"place",
humanb(bytes).c_str(), done, pieces, on_card, (void*)(p + off));
break;
}
if (!lock_pieces(p, len, bytes, cards, "rad_dev_alloc")) {
::munmap(p, (size_t)len);
return nullptr;
}
if (done < pieces) {
for (int i = 0; i < done; ++i) (void)hsa_amd_memory_unlock(p + (int64_t)i * kPinPiece);
std::lock_guard<std::mutex> lk(g_pinned_mu);
g_pinned.emplace((uintptr_t)p, Pinned{len, nullptr});
return p;
}
/* pin_pieces over a shared file mapping instead of private anonymous memory: the pages are the
* file's (a tmpfs file other processes map too) and the lock takes them in place. The file maps
* onto a 2 MiB boundary inside a reservation made with one alignment extra, then the ends are
* trimmed. MADV_HUGEPAGE is advised but shmem follows shmem_enabled, not it. A tmpfs page is
* swappable until the lock pins it, so MLOCK_ONFAULT keeps the pages in memory meanwhile; a
* refusal (the memlock limit) costs only that. */
void* pin_pieces_file(int64_t bytes, int fd, int64_t foff) {
std::vector<hsa_agent_t> cards;
for (const Dev& d : G().dev) cards.push_back(d.agent);
if (!svm_in_place()) {
device_set_error("rad_dev_map_file(%s): a file range is pinned in place only through the "
"kernel driver's SVM API, which this host lacks, so ram=DIR needs it",
humanb(bytes).c_str());
return nullptr;
}
const int64_t align = RAD_ALIGN_POOL;
const int64_t len = align_up(bytes, 4096);
void* m = ::mmap(nullptr, (size_t)(len + align), PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
if (m == MAP_FAILED) {
device_set_error("rad_dev_map_file(%s): mmap: %s", humanb(bytes).c_str(),
std::strerror(errno));
return nullptr;
}
const uintptr_t lo = (uintptr_t)m;
const uintptr_t at = (lo + (uintptr_t)align - 1) & ~((uintptr_t)align - 1);
if (::mmap((void*)at, (size_t)len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd,
(off_t)foff) == MAP_FAILED) {
device_set_error("rad_dev_map_file(%s): mmap of the file at +%s: %s", humanb(bytes).c_str(),
humanb(foff).c_str(), std::strerror(errno));
::munmap(m, (size_t)(len + align));
return nullptr;
}
if (at > lo) ::munmap(m, at - lo);
if (lo + (uintptr_t)align > at) ::munmap((void*)(at + (uintptr_t)len), lo + (uintptr_t)align - at);
char* const p = (char*)at;
if (len >= ((int64_t)2 << 20)) (void)::madvise(p, (size_t)len, MADV_HUGEPAGE);
(void)::madvise(p, (size_t)len, MADV_DONTFORK);
if (::mlock2(p, (size_t)len, MLOCK_ONFAULT) != 0)
RAD_WARN("device: the shared file range %p (%s) could not be locked against swap (mlock2: "
"%s)", (void*)p, humanb(len).c_str(), std::strerror(errno));
prefer_numa_node(p, len, current_card_numa_node());
if (!lock_pieces(p, len, bytes, cards, "rad_dev_map_file")) {
::munmap(p, (size_t)len);
return nullptr;
}
@@ -3501,6 +3930,17 @@ void* a_alloc(int64_t bytes, int kind) {
return p;
}
void* a_map_file(int fd, int64_t foff, int64_t bytes) {
if (fd < 0 || foff < 0 || bytes <= 0) {
device_set_error("rad_dev_map_file(%d, %lld, %lld): a file, a non-negative offset and a "
"positive size are needed", fd, (long long)foff, (long long)bytes);
return nullptr;
}
void* p = pin_pieces_file(bytes, fd, foff);
note_alloc(p, bytes, RAD_MEM_HOST_MAPPED);
return p;
}
/* Every free invalidates locate()'s cache first. */
void (*g_hip_free)(void*, int) = nullptr;
void a_free(void* p, int kind) {
@@ -3553,6 +3993,15 @@ DeviceBackend g_aql;
} // namespace
namespace aql {
bool ride_reusable(void* token) {
Sig* g = static_cast<Sig*>(token);
if (!g) return false;
std::lock_guard<std::mutex> lk(pool().mu);
return reusable(g);
}
} // namespace aql
size_t aql_code_registered(void) { return a_code_mark(); }
const DeviceBackend* aql_backend_if_present(void) {
@@ -3581,6 +4030,7 @@ const DeviceBackend* aql_backend_if_present(void) {
g_hip_host_ptr = hip->host_ptr;
g_hip_device_ptr = hip->device_ptr;
g_aql.alloc = a_alloc;
g_aql.map_file = a_map_file;
g_aql.free = a_free;
g_aql.host_ptr = a_host_ptr;
g_aql.device_ptr = a_device_ptr;
@@ -3612,6 +4062,7 @@ const DeviceBackend* aql_backend_if_present(void) {
g_aql.alloc_uncached = a_alloc_uncached;
g_aql.free_uncached = a_free_uncached;
g_aql.dma_lane = a_dma_lane;
g_aql.stuck_ms = stuck_ms;
g_aql.vmem_granularity = a_vmem_granularity;
g_aql.vmem_reserve = a_vmem_reserve;
g_aql.vmem_release = a_vmem_release;
@@ -3633,6 +4084,7 @@ const DeviceBackend* aql_backend_if_present(void) {
g_aql.stream_gate = a_stream_gate;
g_aql.gate_open = a_gate_open;
g_aql.gate_room = a_gate_room;
g_aql.event_dma = a_event_dma;
return &g_aql;
}();
return b;
@@ -3834,4 +4286,15 @@ hipError_t hipPeekAtLastError(void) {
} // extern "C"
#else
#include "aql.h"
namespace rad {
namespace aql {
/* A build without the AQL backend hands out no ride. */
bool ride_reusable(void*) { return false; }
} // namespace aql
} // namespace rad
#endif /* RAD_HAVE_HIP */
+5
View File
@@ -67,6 +67,11 @@ bool code_object_kernels(const void* elf, size_t size, std::vector<KernelMeta>*
enum : uint8_t { kFenceAcquireSystem = 1, kFenceReleaseSystem = 2 };
void set_next_fence(uint8_t f);
/* ------------------------------------------------------------------ test hooks */
/* Whether the signal behind a rad_ride token could be handed out again now (tests/device_test.cpp). */
bool ride_reusable(void* token);
} // namespace aql
} // namespace rad
+41 -5
View File
@@ -104,26 +104,47 @@ extern "C" void* rad_link_gate(RadStream s);
* A ride is a signal completed by the next dispatch on a stream, instead of a separate barrier
* packet. That dispatch keeps its ordering bit, so the signal covers everything before it.
* rad_ride_wait puts a barrier waiting on it onto another stream and recycles the signal behind
* that barrier. A ride polled by the DMA engine is returned with rad_ride_release once nothing can
* poll it: immediately (`after` null) when the polling list has finished, else once `after` is
* `packets` dispatches further on.
* that barrier. A ride handed to rad_xp_list as its mark is returned with rad_ride_release once
* the caller is done with it, `after` null; the backend keeps it out of reuse until the list's
* engine is past its poll. One never handed to a list (a dropped request's) is returned with
* `after` and `packets` reaching past the dispatch it rides on: the signal is reused only once
* `after` has consumed `packets` more packets.
*
* rad_xp_list gives the card's prefetch DMA queue a list: wait for `mark` (a ride, or null), copy
* the `n` ranges, then write `done_val` to `done` (pinned host memory, or null). rad_xp_behind
* counts lists handed over and not yet finished. The prefetch ring is uncached device memory
* (rad_dev_alloc_uncached): the engine writes it and kernels read it, so no cache may hold a
* slot's previous contents. */
/* An event a DMA list completes instead of a dispatch. `*word` is its completion word, for
* rad_xp_list to write 0 to as the list's `done`; the caller arms it with a 1 in stream order (the
* weight table's patch dispatch). `rad_event_wait(s, ev)` then holds `s` on the device until the
* copy has landed. The caller waits for the previous copy before arming the next, so an older 0
* cannot release a later wait. RAD_E_STATE while a tape records: the caller re-runs it with the
* tape paused (core/kvstream/stage.cpp). */
extern "C" int rad_event_dma(RadEvent ev, RadStream s, uint32_t** word);
extern "C" int rad_xp_supported(int device);
extern "C" int rad_xp_stream_create(RadStream* out);
extern "C" int rad_ride(RadStream s, void** token);
extern "C" int rad_ride_wait(RadStream s, void* token);
extern "C" void rad_ride_release(void* token, RadStream after, int64_t packets);
/* `flags` RAD_XP_NO_YIELD: the list does not yield the link to a hold by a stream of this card. A
* copy the compute stream waits on must set it, since the hold's release is behind that wait. */
#define RAD_XP_NO_YIELD 1u
extern "C" int rad_xp_list(int device, void* mark, int n, const uint64_t* src, const uint64_t* dst,
const uint64_t* bytes, uint32_t* done, uint32_t done_val);
const uint64_t* bytes, uint32_t* done, uint32_t done_val,
unsigned flags);
extern "C" int64_t rad_xp_behind(int device);
extern "C" void* rad_dev_alloc_uncached(int64_t bytes);
extern "C" void rad_dev_free_uncached(void* p);
/* [foff, foff + bytes) of the open file `fd`, mapped MAP_SHARED and pinned for every card as a
* RAD_MEM_HOST_MAPPED allocation is: one range at the same address on host and cards. For a table
* that engines on separate cards share through a tmpfs file (format/table_share.h). `foff` is a
* multiple of 2 MiB and the file holds the whole range. Freed with rad_dev_free(p,
* RAD_MEM_HOST_MAPPED), which unpins and unmaps and leaves the file alone. Null with the reason in
* rad_dev_last_error() on a backend without it or a failure. */
extern "C" void* rad_dev_map_file(int fd, int64_t foff, int64_t bytes);
/* A gate on a stream: the stream stops at the point rad_stream_gate enqueues and goes on once the
* host calls rad_gate_open. Everything enqueued behind the gate meanwhile is then ready at once, so
* the device runs it back to back however long the host took to submit it. For timing a batch of
@@ -316,7 +337,7 @@ struct DeviceBackend {
int (*ride_wait)(RadStream s, void* token);
void (*ride_release)(void* token, RadStream after, int64_t packets);
int (*xp_list)(int device, void* mark, int n, const uint64_t* src, const uint64_t* dst,
const uint64_t* bytes, uint32_t* done, uint32_t done_val);
const uint64_t* bytes, uint32_t* done, uint32_t done_val, unsigned flags);
int64_t (*xp_behind)(int device);
void* (*alloc_uncached)(int64_t bytes);
void (*free_uncached)(void* p);
@@ -325,6 +346,15 @@ struct DeviceBackend {
int (*stream_gate)(RadStream s, void** gate);
int (*gate_open)(void* gate);
int64_t (*gate_room)(RadStream s);
/* ---- rad_event_dma; null on a backend with no DMA queue. */
int (*event_dma)(RadEvent e, RadStream s, uint32_t** word);
/* ---- see rad_dev_map_file. Null on a backend with no pinned host memory to map a file into. */
void* (*map_file)(int fd, int64_t foff, int64_t bytes);
/* ---- see dev_stuck_ms. Null on a backend that cannot tell. */
int64_t (*stuck_ms)(void);
};
@@ -363,6 +393,12 @@ std::string dev_runtime_version();
/* The current backend's DeviceBackend::dma_lane, "" where it has none. */
std::string dev_dma_lane(int device);
/* How long the device has been stuck, in ms, or 0: since the last moment anything advanced, once a
* host wait has seen nothing advance for its stall period and named the stuck packet (the queue
* dump), or since a queue faulted. Anything advancing again clears it. 0 where the backend cannot
* tell. */
int64_t dev_stuck_ms();
/* Longest run of dispatches a played tape leaves unordered. Of any kTapeOverlapRun + 1
* consecutive packets on a queue one carries the barrier bit, so a launched packet still proves
* that everything more than that many packets earlier has completed. Argument slots and tape
+3 -1
View File
@@ -40,7 +40,9 @@ int64_t pread_some(int fd, void* dst, int64_t bytes, int64_t off) {
int pread_exact(int fd, void* dst, int64_t bytes, int64_t off) {
const int64_t got = pread_some(fd, dst, bytes, off);
if (got < 0) return (int)got;
return got == bytes ? RAD_OK : RAD_E_IO;
if (got != bytes) return RAD_E_IO;
dio_progress.fetch_add((uint64_t)bytes, std::memory_order_relaxed);
return RAD_OK;
}
int pwrite_exact(int fd, const void* src, int64_t bytes, int64_t off) {
+7
View File
@@ -11,10 +11,17 @@
* (rad_format.h), so every mover read takes the unbounced path. The bounce is for other callers.
*/
#pragma once
#include <atomic>
#include <cstdint>
namespace rad {
/* Bytes the drive has returned through this reader, monotonically. The stall
* watchdog (aql.cpp) counts this alongside the queues' read indices: a host wait
* that moves nothing anywhere -- no queue advanced, no read came back -- is the
* only one that reports a stall. */
inline std::atomic<uint64_t> dio_progress{0};
enum {
RAD_DIO_READ = 1,
RAD_DIO_WRITE = 2, /* implies read: a partial block is a read-modify-write */
+30 -2
View File
@@ -538,6 +538,10 @@ std::string dev_pci_address(int device) {
if (!dev()->pci_address) return std::string();
return dev_string(64, [&](char* b, int n) { return dev()->pci_address(device, b, n); });
}
int64_t dev_stuck_ms() {
const auto f = dev()->stuck_ms;
return f ? f() : 0;
}
std::string dev_dma_lane(int device) {
if (!dev()->dma_lane) return std::string();
return dev_string(256, [&](char* b, int n) { return dev()->dma_lane(device, b, n); });
@@ -564,6 +568,7 @@ int dev_code_runs(size_t from, size_t to, int device, std::string* why) {
extern "C" {
int rad_dev_peer_access(int self, int peer) { return rad::dev_peer_access(self, peer); }
int rad_event_create_as(RadEvent* out, unsigned flags) {
const int s = dev()->event_create(out, flags);
@@ -578,6 +583,11 @@ void rad_event_destroy(RadEvent e) {
dev()->event_destroy(e);
}
int rad_event_record(RadEvent e, RadStream s) { return dev()->event_record(e, s); }
int rad_event_dma(RadEvent e, RadStream s, uint32_t** w) {
if (const auto f = dev()->event_dma) return f(e, s, w);
rad::device_set_error("rad_event_dma: the device backend has no DMA queue to complete an event");
return RAD_E_UNSUPPORTED;
}
int rad_event_wait(RadStream s, RadEvent e) { return dev()->event_wait(s, e); }
int rad_event_query(RadEvent e) { return dev()->event_query(e); }
int rad_event_sync(RadEvent e) {
@@ -612,10 +622,14 @@ int rad_event_elapsed_ms(RadEvent a, RadEvent b, float* out) {
}
int rad_memcpy_async(void* dst, const void* src, int64_t bytes, RadStream s) {
/* One address space (unified memory, or a host view of a device allocation): a copy
* onto itself is already done. */
if (dst == src) return RAD_OK;
return dev()->memcpy_async(dst, src, bytes, s);
}
int rad_memcpy_2d_async(void* dst, int64_t dpitch, const void* src, int64_t spitch,
int64_t width, int64_t height, RadStream s) {
if (dst == src && dpitch == spitch) return RAD_OK;
return dev()->memcpy_2d_async(dst, dpitch, src, spitch, width, height, s);
}
int rad_memset_async(void* dst, int value, int64_t bytes, RadStream s) {
@@ -659,8 +673,8 @@ void rad_ride_release(void* token, RadStream after, int64_t packets) {
if (const auto f = dev()->ride_release) f(token, after, packets);
}
int rad_xp_list(int device, void* mark, int n, const uint64_t* src, const uint64_t* dst, const uint64_t* bytes,
uint32_t* done, uint32_t done_val) {
if (const auto f = dev()->xp_list) return f(device, mark, n, src, dst, bytes, done, done_val);
uint32_t* done, uint32_t done_val, unsigned flags) {
if (const auto f = dev()->xp_list) return f(device, mark, n, src, dst, bytes, done, done_val, flags);
return RAD_E_UNSUPPORTED;
}
int64_t rad_xp_behind(int device) {
@@ -671,6 +685,20 @@ void* rad_dev_alloc_uncached(int64_t bytes) {
if (const auto f = dev()->alloc_uncached) return f(bytes);
return nullptr;
}
void* rad_dev_map_file(int fd, int64_t foff, int64_t bytes) {
const auto f = dev()->map_file;
if (!f) {
rad::device_set_error("rad_dev_map_file: the %s backend has no pinned host memory to map a "
"file into", dev()->name);
return nullptr;
}
void* p = f(fd, foff, bytes);
if (p) {
rad::g_ledger[RAD_MEM_HOST_MAPPED].bytes.fetch_add(bytes, std::memory_order_relaxed);
rad::g_ledger[RAD_MEM_HOST_MAPPED].count.fetch_add(1, std::memory_order_relaxed);
}
return p;
}
void rad_dev_free_uncached(void* p) {
if (const auto f = dev()->free_uncached) f(p);
}
+1
View File
@@ -182,6 +182,7 @@ int d_props(int device, RadDeviceProps* out) {
out->vram_bytes = (int64_t)p.totalGlobalMem;
out->lds_bytes = (int64_t)p.sharedMemPerBlock;
out->is_host_backend = 0;
out->unified = d_integrated(device);
/* hipMemGetInfo reports the current device, so switch to `device` and back. props is not
* called per step. */
+1
View File
@@ -816,6 +816,7 @@ const DeviceBackend g_host = {
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
h_stream_gate, h_gate_open, h_gate_room,
nullptr, /* event_dma: no DMA queue */
};
} /* namespace */