This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/device/host.cpp
T
Deadcode 769b4c64a6 radiance 1.16.0
New models:
- Hunyuan3D-2 (hunyuan3d2): an untextured 3-D mesh from one picture of an object. DINOv2-giant
  over the picture, a FLUX-shaped flow-matching DiT (16 double-stream and 32 single-stream blocks
  of 1024) over 3072 latents with both guidance branches in one step, and the shape VAE's
  geometry decoder. Converted from tencent/Hunyuan3D-2's hunyuan3d-dit-v2-0 folder with
  data/recipes/hunyuan3d-2-bf16.recipe (fp16 cast to bf16, 4.4 GiB, one card). The licence
  (Tencent Hunyuan 3D 2.0 Community License) does not cover the EU, the UK or South Korea
- Served on Hunyuan3D-2's own API server routes, so its Blender add-on and scripts work
  unchanged: POST /generate answers with the mesh, POST /send runs it in the background and
  GET /status/{uid} reports steps_done and then the file. GLB, OBJ, PLY or STL; the field is
  sampled coarse to fine as FlashVDM's hierarchical decoding does, or densely with
  "volume_decoding": "dense"; seed, steps, guidance and octree_resolution as the reference.
  Texturing (Hunyuan3D-Paint) is refused for now (docs/GUIDE.md §5.17)

Merged:
- libr3d rewritten for RDNA3's wave matrix (@RikshaDriver): bf16, f16, int8-activation and vocab
  GEMMs retiled to 16x128 blocks with a double-buffered k-walk; fp8, mxfp4a8, w2a8, w4a16, w5a16
  and w8a8 quantised GEMMs and the gated form; grouped MoE GEMMs (bf16, fp8a8f32, mxfp4a8); the
  norms and quantisers, the QSA state ops and the MoE router, scatter and gather. Brought onto the
  op contract before release (13 schemas, the MoE GEMMs on weight tables with a_order and bf16
  output, moe_gather's gate pitch, moe_scatter above 1024 slots, the mxfp4 m16 dispatch) and its
  gated-delta-net kernels replaced by libhalo's; every op it serves now passes rad-kbench against
  libref on a gfx1151, and r3d_selftest checks its MoE GEMMs by value
- Gemma 4 31B: asymmetric int5 linears (w5a8i8 above 64 rows, w5a16 up to 64, a scale per 128),
  the norm, GELU and two-rank all-reduce junctions at quant_act_i8g's grid (rmsnorm_quant_i8g,
  gelu_mul_quant_i8g, ar_rmsnorm_quant_i8g) so the prefill overlap runs on the i8a8, a4 and w5
  trunks, and compose files for the 31B i8g, heretic-ara-i8g and w5 on two cards (@keptn42)
- The norms' lane sum reads eight at a time: TranslateGemma 27B decode is back from 225 to 239
  tok/s a stream at four streams, bit for bit the same output (@mici81)
- The head-256 prefill softmax rescales each row by its own maximum, not by a vote of the wave, so
  a row's rounding no longer depends on which rows share its launch (@gostar)

Anthropic Messages API:
- /v1/messages takes min_p, presence_penalty, frequency_penalty, repetition_penalty and
  repeat_last_n as top-level fields with the chat path's names, ranges and meaning, so clients that
  speak only the Anthropic format (Claude Code) can set a model card's sampling, such as Qwen's
  presence_penalty 1.5. The penalties count the last repeat_last_n tokens of prompt and reply (64
  by default, --repeat-last-n), as llama.cpp does

Plugin ABI 33:
- Shape generators: rad_declare_shape_generator, RadShapeGenBatch (RadBatch::sgen_b) and
  RadArchProbe::gen_shape, with the SCOND, SSTEP, SLATENT and SQUERY passes. The layout
  fingerprint table is regenerated; out-of-tree plugins rebuild against 33
- New op point_fourier ([x | sin(x 2^j) | cos(x 2^j)], times pi on request) in libref, libavx,
  libr4d and libhalo
- A checkpoint for a model that reads no text gets a one-token vocabulary

More cards from the one image:
- stilldeadcode/radiance now carries libr3d (RDNA3: gfx11-generic, every gfx11 chip) and libhalo
  (Strix Halo: gfx1151, preferred there over libr3d) beside libr4d (gfx1201). docker/build.sh and
  the Dockerfile default to gfx1201;gfx11-generic;gfx1151
- libr3d loads only where the KFD topology lists a gfx11 card and libhalo only where it lists a
  gfx1151; a process without /dev/kfd sees neither. On an RDNA4 card, or with no card at all,
  the image therefore behaves exactly as before, and rad-convert lays weights out as before
- libhalo and libr3d now declare gemm_nt_q's w_rb, and libhalo gram_accum's upper, as libr4d
  does. Before, any build carrying them refused both at load ("9 parameters vs 8"), so a build
  for Strix Halo or RDNA3 had no device kernel library
- libhalo does not yet have every op libr4d added after it was ported (the IQR rows, Pixtral and
  Ministral rope, the tree-attention and DFlash-tree rows, among others); halo_parity lists them
  and is left out of the image's test stage until they are ported

Fixed -- container and checkpoint parsing:
- A .rad expert-1 entry inside or before expert 0 is refused; a vocab section whose count names
  an array at offset 0 is refused rather than read from the file header; offset checks no longer
  wrap; header tables must sit on their element alignment
- An imatrix array of arrays (unbounded recursion) and a safetensors header naming a tensor twice
  are refused; a failed RadWriter undo says the container is damaged; a tune cache measured on
  another machine is refused, as its format contract says

Fixed -- plugins:
- Two plugins declaring one op with different parameter roles are refused; before, the core
  applied the first one's chunk/capacity roles to the other's rows
- Null tables behind nonzero counts, unknown roles, keyless parameters or constraints and more
  operands than an issue carries are refused by name at load; on the host backend device rows are
  dropped even when no fat binary was registered; rad-schemas checks the ABI layout fingerprint as
  the engine does

Fixed -- device:
- Stream slots are recycled under a new generation: before, 1024 stream create/destroy cycles
  (LoRA loads, lanes, uplinks) made every later rad_stream_create fail
- A stream whose drain fails keeps its queue and kernarg ring instead of freeing them under the
  card; the kernarg ring no longer leaks when its access grant fails; a block over 1024 work-items
  is refused
- rad_dev_free takes the allocation off the ledger the pin-cap check reads; the host backend
  charges pages to the card the range was reserved for

Fixed -- engine and server:
- Two NDEBUG-only asserts (runtime-window totals, ring blocks per group) are refusals; a
  plugin-declared lcm past int64 saturates so the quantum check refuses it; every share view rect
  and part span is bounded by its entry
- Video: the timestep regions hold the refine rungs a schedule appends, a zero-frame encode is
  refused, /v1/videos bounds each side before the area product, and a realtime session needs a
  clip length to cut a turn at

Fixed -- kernels and quantisation:
- A trellis run shorter than a state's codes (tcq_rows=1, or N % tcq_rows == 1) read its seam
  out of bounds; the encoder now refuses it as the decoder does, and tcq_rows below one state is
  refused at parse
- qsa_tail_store bounds its slot index by the state plane (libr4d and libhalo); paged attention
  refuses kv_heads <= 0; libavx row_amax skips a NaN as libref does

Fixed -- tools:
- pad_tp.py no longer runs cp through a shell with shard names from the checkpoint's index
  (quote injection, path traversal): names must be bare file names
- ops_doc_check.sh runs under mawk (Ubuntu's awk); rad-kbench --max-cases/--max-bytes are checked
  integers; oot_plugins fails unless the installed replay passed a case and failed none

Thanks to @RikshaDriver, @keptn42, @mici81 and @gostar.
2026-11-02 18:35:02 +01:00

852 lines
30 KiB
C++

/* host.cpp -- the host device backend.
*
* A primary development target, not a fallback: without ROCm the whole engine (builder, planner,
* mover, scheduler, KV manager, tokeniser, sampler host reference, server) runs and is tested
* through these calls (docs/IMPLEMENTATION.md). It is also where RAD_DOMAIN_HOST kernels run when
* the planner puts a layer on the CPU (spec §5.1).
*
* The streams are real: each is an in-order queue with its own worker thread, and an event is a
* sequence number the stream reaches. Inline "async" calls would let ordering bugs (a missing
* rad_event_wait, a buffer reused before a copy landed) pass here and corrupt a real device.
*
* No allocation on the step path: the queue is a fixed-capacity ring of POD tasks, not
* std::function, which would heap-allocate inside rad_memcpy_async.
*/
#include "device.h"
#include "../rad_internal.h"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <cstring>
#include <map>
#include <mutex>
#include <thread>
#include <unordered_map>
#include <vector>
#include <sys/mman.h>
#include <unistd.h>
namespace rad {
namespace {
using Clock = std::chrono::steady_clock;
/* Every allocation kind is page-aligned host memory. 4096 is RAD_ALIGN_UNIT and the O_DIRECT
* buffer alignment (directio.cpp), so pools here are file-tier targets without a bounce. */
constexpr int64_t kAlign = 4096;
/* Per-stream queue depth: enough that a layer's mover transfers plus compute issues never hit
* backpressure, small enough for a ring of a few tens of KiB. Enqueue onto a full ring blocks, as
* on a saturated hardware queue. */
constexpr size_t kQueueDepth = 1024;
/* libr4d all-reduce supports at most 8 ranks (spec §9), so more fake devices are useless. */
constexpr int kMaxFakeDevices = 8;
/* ------------------------------------------------------------------ events */
/* An event is two sequence numbers: `recorded` advances on the host at rad_event_record,
* `completed` on the stream worker when it reaches that record. Complete when completed has caught
* up, so re-recording makes it pending again, as in HIP.
*
* `refs` counts the handle plus every queued task naming the event. HIP allows destroying an event
* whose record or wait is still queued; freeing it at destroy would leave the worker locking a
* freed mutex and wedge the stream. */
struct HostEvent {
std::mutex m;
std::condition_variable cv;
uint64_t recorded = 0;
uint64_t completed = 0;
Clock::time_point stamp{}; /* when the worker reached the most recent record */
std::atomic<int> refs{1};
bool host = false; /* RAD_EVENT_HOST_WAIT */
};
void event_retain(HostEvent* e) { e->refs.fetch_add(1, std::memory_order_relaxed); }
/* The last reference, handle or task, frees it. */
void event_release(HostEvent* e) {
if (e->refs.fetch_sub(1, std::memory_order_acq_rel) == 1) delete e;
}
/* ------------------------------------------------------------------ tasks */
struct Task {
enum Kind : uint8_t { Copy, Copy2D, Ranges, Memset, Record, Wait, Stop };
Kind kind = Stop;
void* dst = nullptr;
const void* src = nullptr;
int64_t a = 0, b = 0, c = 0, d = 0;
const void* aux = nullptr;
HostEvent* ev = nullptr;
uint64_t seq = 0;
};
/* ------------------------------------------------------------------ streams */
struct HostStream {
std::mutex m;
std::condition_variable cv_work; /* worker waits on this for a task or a stop */
std::condition_variable cv_room; /* producers wait on this when the ring is full */
std::condition_variable cv_done; /* rad_stream_sync waits on this */
std::vector<Task> ring;
size_t head = 0; /* next slot to pop */
size_t tail = 0; /* next slot to push */
size_t count = 0;
uint64_t submitted = 0; /* tasks ever pushed */
uint64_t retired = 0; /* tasks ever finished executing */
int gates = 0; /* closed gates (rad_stream_gate) queued on this stream */
std::thread worker;
};
void stream_run(HostStream* s);
/* Push one task, blocking while the ring is full. Fails only behind a closed gate: the ring cannot
* drain until the host opens it, and the host is the caller. Destroy queues its stop behind
* submitted work, so there is no closing state. */
int stream_push(HostStream* s, const Task& t) {
std::unique_lock<std::mutex> lk(s->m);
if (s->count >= s->ring.size() && s->gates > 0) {
device_set_error("a submission onto a full stream behind a closed gate: open the gate "
"(rad_gate_open) first");
return RAD_E_STATE;
}
s->cv_room.wait(lk, [s] { return s->count < s->ring.size(); });
s->ring[s->tail] = t;
s->tail = (s->tail + 1) % s->ring.size();
++s->count;
++s->submitted;
lk.unlock();
s->cv_work.notify_one();
return RAD_OK;
}
void stream_run(HostStream* s) {
for (;;) {
Task t;
{
std::unique_lock<std::mutex> lk(s->m);
s->cv_work.wait(lk, [s] { return s->count > 0; });
t = s->ring[s->head];
s->head = (s->head + 1) % s->ring.size();
--s->count;
}
s->cv_room.notify_one();
bool done = false;
switch (t.kind) {
case Task::Copy:
std::memcpy(t.dst, t.src, (size_t)t.a);
break;
case Task::Copy2D:
/* a = dpitch, b = spitch, c = width bytes, d = height rows. */
for (int64_t r = 0; r < t.d; ++r)
std::memcpy((uint8_t*)t.dst + r * t.a, (const uint8_t*)t.src + r * t.b,
(size_t)t.c);
break;
case Task::Ranges: {
/* a = prefix bytes, b = range count; the list is read now, as the device's is. */
std::memcpy(t.dst, t.src, (size_t)t.a);
const RadCopyRange* r = (const RadCopyRange*)t.aux;
for (int64_t i = 0; i < t.b; ++i)
std::memcpy((uint8_t*)t.dst + r[i].dst, (const uint8_t*)t.src + r[i].src,
(size_t)r[i].bytes);
break;
}
case Task::Memset:
std::memset(t.dst, (int)t.a, (size_t)t.b);
break;
case Task::Record: {
std::lock_guard<std::mutex> lk(t.ev->m);
if (t.seq > t.ev->completed) {
t.ev->completed = t.seq;
t.ev->stamp = Clock::now();
}
t.ev->cv.notify_all();
break;
}
case Task::Wait: {
/* The stream blocks, not a host thread (spec §5.4). Waiting on an event recorded
* later on this same stream wedges it, as it would a hardware queue. */
std::unique_lock<std::mutex> lk(t.ev->m);
t.ev->cv.wait(lk, [&t] { return t.ev->completed >= t.seq; });
break;
}
case Task::Stop:
done = true;
break;
}
/* After the event lock is released: this may be the last reference. */
if (t.ev) event_release(t.ev);
{
std::lock_guard<std::mutex> lk(s->m);
++s->retired;
}
s->cv_done.notify_all();
if (done) return;
}
}
/* ------------------------------------------------------------------ allocation bookkeeping */
struct AllocRec {
int64_t bytes = 0;
int kind = 0;
int device = 0;
};
std::mutex& alloc_mutex() { static std::mutex m; return m; }
std::unordered_map<void*, AllocRec>& alloc_table() {
static std::unordered_map<void*, AllocRec> t;
return t;
}
/* Per-device VRAM accounting, so vram_free moves and the planner's fit checks are exercised
* (spec §5.3). */
std::atomic<int64_t> g_used[kMaxFakeDevices];
/* A mutex-guarded map is fine: allocation happens at load and pool construction, never inside
* rad_arch_step. */
void alloc_note(void* p, int64_t bytes, int kind, int device) {
std::lock_guard<std::mutex> lk(alloc_mutex());
alloc_table()[p] = AllocRec{bytes, kind, device};
}
/* The record of allocation `p`, removed from the table when `take`. */
bool alloc_find(void* p, AllocRec* out, bool take = false) {
std::lock_guard<std::mutex> lk(alloc_mutex());
auto it = alloc_table().find(p);
if (it == alloc_table().end()) return false;
*out = it->second;
if (take) alloc_table().erase(it);
return true;
}
/* ------------------------------------------------------------------ devices */
int64_t env_i64(const char* key, int64_t dflt) {
const char* v = std::getenv(key);
if (!v || !*v) return dflt;
char* end = nullptr;
long long n = std::strtoll(v, &end, 10);
if (end == v || n < 0) {
RAD_WARN("%s=\"%s\" is not a non-negative integer; using %lld", key, v, (long long)dflt);
return dflt;
}
return (int64_t)n;
}
int host_count() {
/* Memoised so the count cannot change under a rank thread. RADIANCE_HOST_DEVICES fakes
* several devices in one address space for testing tensor parallel without cards (spec §9). */
static int n = [] {
int64_t v = env_i64("RADIANCE_HOST_DEVICES", 1);
if (v < 1) v = 1;
if (v > kMaxFakeDevices) {
RAD_WARN("RADIANCE_HOST_DEVICES=%lld clamped to %d", (long long)v, kMaxFakeDevices);
v = kMaxFakeDevices;
}
if (v > 1) RAD_INFO("device: host backend presenting %lld fake devices", (long long)v);
return (int)v;
}();
return n;
}
/* Per-thread, matching hipSetDevice: one thread per rank, one device per rank (spec §1). */
thread_local int t_device = 0;
int64_t physical_ram() {
long pages = sysconf(_SC_PHYS_PAGES), page = sysconf(_SC_PAGESIZE);
if (pages <= 0 || page <= 0) return 8ll << 30;
return (int64_t)pages * (int64_t)page;
}
/* The fake per-device VRAM budget. Read on every call so a test can change it. Default: half of
* physical RAM, split across the fake devices so their sum is loadable. */
int64_t host_vram_bytes() {
int64_t mib = env_i64("RADIANCE_HOST_VRAM_MIB", 0);
if (mib > 0) return mib << 20;
return (physical_ram() / 2) / host_count();
}
const char* cpu_model() {
static std::string s = [] {
std::string out = "host cpu";
FILE* f = std::fopen("/proc/cpuinfo", "r");
if (!f) return out;
char line[512];
while (std::fgets(line, sizeof line, f)) {
if (std::strncmp(line, "model name", 10) != 0) continue;
const char* c = std::strchr(line, ':');
if (!c) break;
++c;
while (*c == ' ' || *c == '\t') ++c;
out.assign(c);
while (!out.empty() && (out.back() == '\n' || out.back() == ' ')) out.pop_back();
break;
}
std::fclose(f);
return out;
}();
return s.c_str();
}
/* ------------------------------------------------------------------ the fourteen calls */
int h_count(void) { return host_count(); }
int h_set(int device) {
if (device < 0 || device >= host_count()) {
device_set_error("rad_dev_set(%d): host backend has %d device(s)", device, host_count());
return RAD_E_INVAL;
}
t_device = device;
return RAD_OK;
}
int h_props(int device, RadDeviceProps* out) {
if (!out) return RAD_E_INVAL;
if (device < 0 || device >= host_count()) {
device_set_error("rad_dev_props(%d): host backend has %d device(s)", device, host_count());
return RAD_E_INVAL;
}
std::memset(out, 0, sizeof *out);
snprintf(out->name, sizeof out->name, "%s", cpu_model());
snprintf(out->arch, sizeof out->arch, "host");
out->device_id = device;
/* Hardware threads split across fake devices, like the budget. */
unsigned hw = std::thread::hardware_concurrency();
out->n_cu = (int)((hw ? hw : 1u) / (unsigned)host_count());
if (out->n_cu < 1) out->n_cu = 1;
/* A host lane is one element wide. */
out->warp_size = 1;
out->vram_bytes = host_vram_bytes();
int64_t used = g_used[device].load(std::memory_order_relaxed);
out->vram_free = out->vram_bytes > used ? out->vram_bytes - used : 0;
/* A CPU has no LDS. Nonzero because budget arithmetic divides by it; a conventional
* per-workgroup figure, not a limit host kernels should honour. */
out->lds_bytes = 64 * 1024;
/* All fake devices share one address space. */
out->supports_p2p = 1;
out->is_host_backend = 1;
return RAD_OK;
}
int h_enable_peer(int self, int peer) {
if (self < 0 || self >= host_count() || peer < 0 || peer >= host_count()) {
device_set_error("rad_dev_enable_peer(%d, %d): host backend has %d device(s)",
self, peer, host_count());
return RAD_E_INVAL;
}
return RAD_OK; /* one address space; nothing to enable */
}
void* h_alloc(int64_t bytes, int kind) {
if (bytes <= 0) {
device_set_error("rad_dev_alloc(%lld): size must be positive", (long long)bytes);
return nullptr;
}
if (kind < RAD_MEM_DEVICE || kind > RAD_MEM_HOST_MAPPED) {
device_set_error("rad_dev_alloc: unknown memory kind %d", kind);
return nullptr;
}
void* p = nullptr;
/* All kinds are the same memory here, but the kind is recorded so mismatched frees are
* caught. The size is rounded up to whole pages so DirectFile can read an aligned superset
* into it without overrunning. */
if (posix_memalign(&p, (size_t)kAlign, (size_t)align_up(bytes, kAlign)) != 0 || !p) {
device_set_error("rad_dev_alloc(%lld, kind=%d): out of host memory",
(long long)bytes, kind);
return nullptr;
}
int dev = t_device;
alloc_note(p, bytes, kind, dev);
if (kind == RAD_MEM_DEVICE) g_used[dev].fetch_add(bytes, std::memory_order_relaxed);
return p;
}
void h_free(void* p, int kind) {
if (!p) return;
AllocRec r;
if (!alloc_find(p, &r, /*take=*/true)) {
/* Not ours: freeing it would corrupt the heap, so refuse and warn. */
RAD_WARN("rad_dev_free(%p, kind=%d): pointer was not allocated by the device layer",
p, kind);
return;
}
if (r.kind != kind)
RAD_WARN("rad_dev_free(%p): freed as kind %d, allocated as kind %d", p, kind, r.kind);
if (r.kind == RAD_MEM_DEVICE) g_used[r.device].fetch_sub(r.bytes, std::memory_order_relaxed);
std::free(p);
}
/* Host and device views are the same pointer (spec §5.1 zero-copy is trivial here). Null for a
* pointer the device layer did not allocate. */
void* h_host_ptr(void* p) {
if (!p) return nullptr;
AllocRec r;
return alloc_find(p, &r) ? p : nullptr;
}
void* h_device_ptr(void* p) { return h_host_ptr(p); }
int h_stream_create(RadStream* out, int high_priority) {
if (!out) return RAD_E_INVAL;
/* Priority is ignored: raising a thread's scheduling class needs privileges and buys nothing
* on a one-thread queue. */
(void)high_priority;
auto* s = new (std::nothrow) HostStream();
if (!s) { device_set_error("rad_stream_create: out of memory"); return RAD_E_NOMEM; }
s->ring.resize(kQueueDepth);
s->worker = std::thread(stream_run, s);
*out = (RadStream)s;
return RAD_OK;
}
void h_stream_destroy(RadStream st) {
if (!st) return;
auto* s = (HostStream*)st;
Task t;
t.kind = Task::Stop;
/* Queued behind submitted work, so destroy drains rather than discards. */
stream_push(s, t);
if (s->worker.joinable()) s->worker.join();
delete s;
}
int h_stream_sync(RadStream st) {
if (!st) {
device_set_error("rad_stream_sync: null stream");
return RAD_E_INVAL;
}
auto* s = (HostStream*)st;
std::unique_lock<std::mutex> lk(s->m);
if (s->gates > 0) {
device_set_error("rad_stream_sync: the stream waits at a closed gate, which only "
"rad_gate_open releases");
return RAD_E_STATE;
}
/* Waits only for work submitted before this call, as hipStreamSynchronize does; the
* scheduler relies on sync returning while another thread keeps pushing. */
const uint64_t target = s->submitted;
s->cv_done.wait(lk, [s, target] { return s->retired >= target; });
return RAD_OK;
}
/* RAD_EVENT_LOCAL is meaningless here; RAD_EVENT_HOST_WAIT is kept so event_sync refuses events
* created without it, as on a card. */
int h_event_create(RadEvent* out, unsigned flags) {
if (!out) return RAD_E_INVAL;
auto* e = new (std::nothrow) HostEvent();
if (!e) { device_set_error("rad_event_create: out of memory"); return RAD_E_NOMEM; }
e->host = (flags & RAD_EVENT_HOST_WAIT) != 0;
*out = (RadEvent)e;
return RAD_OK;
}
/* Drops the handle; queued tasks keep the event alive (see HostEvent). */
void h_event_destroy(RadEvent ev) { if (ev) event_release((HostEvent*)ev); }
int h_event_record(RadEvent ev, RadStream st) {
if (!ev || !st) {
device_set_error("rad_event_record: null %s", ev ? "stream" : "event");
return RAD_E_INVAL;
}
auto* e = (HostEvent*)ev;
uint64_t seq;
{
std::lock_guard<std::mutex> lk(e->m);
seq = ++e->recorded; /* pending from this instant, on the host, before the push */
}
Task t;
t.kind = Task::Record;
t.ev = e;
t.seq = seq;
event_retain(e);
return stream_push((HostStream*)st, t);
}
int h_event_wait(RadStream st, RadEvent ev) {
if (!ev || !st) {
device_set_error("rad_event_wait: null %s", ev ? "stream" : "event");
return RAD_E_INVAL;
}
auto* e = (HostEvent*)ev;
uint64_t target;
{
std::lock_guard<std::mutex> lk(e->m);
target = e->recorded;
}
/* Waiting on a never-recorded event is a no-op, matching hipStreamWaitEvent; the mover relies
* on it. */
if (target == 0) return RAD_OK;
Task t;
t.kind = Task::Wait;
t.ev = e;
t.seq = target;
event_retain(e);
return stream_push((HostStream*)st, t);
}
int h_event_query(RadEvent ev) {
if (!ev) return RAD_E_INVAL;
auto* e = (HostEvent*)ev;
std::lock_guard<std::mutex> lk(e->m);
return e->completed >= e->recorded ? 1 : 0;
}
/* Host kernels read host memory directly, so there is nothing to make visible. */
void h_host_wrote(void) {}
int h_event_sync(RadEvent ev) {
if (!ev) return RAD_E_INVAL;
auto* e = (HostEvent*)ev;
if (!e->host) {
device_set_error("rad_event_sync: the event was made without RAD_EVENT_HOST_WAIT");
return RAD_E_INVAL;
}
std::unique_lock<std::mutex> lk(e->m);
const uint64_t target = e->recorded;
e->cv.wait(lk, [e, target] { return e->completed >= target; });
return RAD_OK;
}
int h_event_elapsed_ms(RadEvent a, RadEvent b, float* out) {
if (!a || !b || !out) return RAD_E_INVAL;
/* An event's stamp, if it was recorded and has completed. */
auto stamp = [](RadEvent ev, Clock::time_point* t) {
auto* e = (HostEvent*)ev;
std::lock_guard<std::mutex> lk(e->m);
if (e->recorded == 0 || e->completed < e->recorded) return false;
*t = e->stamp;
return true;
};
Clock::time_point ta, tb;
if (!stamp(a, &ta) || !stamp(b, &tb)) return RAD_E_STATE;
*out = std::chrono::duration<float, std::milli>(tb - ta).count();
return RAD_OK;
}
/* A null stream is refused on both backends. HIP's stream 0 implicitly synchronises with every
* blocking stream, which spec §5.4 forbids for transfers; accepting it here would hide such a
* caller until it ran on a card. */
int check_stream(RadStream s, const char* who) {
if (s) return RAD_OK;
device_set_error("%s: null stream -- radiance does not use the default stream, "
"create one with rad_stream_create", who);
return RAD_E_INVAL;
}
int h_memcpy_async(void* dst, const void* src, int64_t bytes, RadStream st) {
RAD_TRY(check_stream(st, "rad_memcpy_async"));
if (bytes == 0) return RAD_OK;
if (!dst || !src || bytes < 0) {
device_set_error("rad_memcpy_async(%p <- %p, %lld): invalid argument",
dst, src, (long long)bytes);
return RAD_E_INVAL;
}
Task t;
t.kind = Task::Copy;
t.dst = dst;
t.src = src;
t.a = bytes;
return stream_push((HostStream*)st, t);
}
int h_memcpy_2d_async(void* dst, int64_t dpitch, const void* src, int64_t spitch,
int64_t width, int64_t height, RadStream st) {
RAD_TRY(check_stream(st, "rad_memcpy_2d_async"));
if (width == 0 || height == 0) return RAD_OK;
if (!dst || !src || width < 0 || height < 0 || dpitch < width || spitch < width) {
device_set_error("rad_memcpy_2d_async: w=%lld h=%lld dpitch=%lld spitch=%lld "
"-- a pitch shorter than the row is a caller bug",
(long long)width, (long long)height,
(long long)dpitch, (long long)spitch);
return RAD_E_INVAL;
}
Task t;
t.kind = Task::Copy2D;
t.dst = dst;
t.src = src;
t.a = dpitch;
t.b = spitch;
t.c = width;
t.d = height;
return stream_push((HostStream*)st, t);
}
int h_memcpy_ranges_async(void* dst, const void* src, int64_t prefix, const RadCopyRange* ranges,
int n, RadStream st) {
RAD_TRY(check_stream(st, "rad_memcpy_ranges_async"));
/* The card's copy kernel moves 16-byte pieces (aql.cpp, a_memcpy_ranges_async), so the same
* calls are refused here: a caller passing the host suite must run on a card. Whether the
* memory is pinned cannot be told on this backend. */
if (!dst || !src || prefix < 0 || (prefix & 15) || n < 0 || (n > 0 && !ranges)) {
device_set_error("rad_memcpy_ranges_async(%p <- %p, prefix %lld (a multiple of 16), %d "
"ranges): invalid argument", dst, src, (long long)prefix, n);
return RAD_E_INVAL;
}
if (prefix == 0 && n == 0) return RAD_OK;
if (((uintptr_t)dst | (uintptr_t)src) & 15) {
device_set_error("rad_memcpy_ranges_async(%p <- %p): both bases must be 16-byte aligned",
dst, src);
return RAD_E_UNSUPPORTED;
}
Task t;
t.kind = Task::Ranges;
t.dst = dst;
t.src = src;
t.a = prefix;
t.b = n;
t.aux = ranges;
return stream_push((HostStream*)st, t);
}
/* One copy task per segment, in list order. The list is read at the call, as on the device. */
int h_memcpy_list_async(const RadCopySeg* segs, int n, unsigned, RadStream st) {
RAD_TRY(check_stream(st, "rad_memcpy_list_async"));
if (n < 0 || (n > 0 && !segs)) {
device_set_error("rad_memcpy_list_async: %d segments at %p", n, (const void*)segs);
return RAD_E_INVAL;
}
for (int i = 0; i < n; ++i) RAD_TRY(h_memcpy_async(segs[i].dst, segs[i].src, segs[i].bytes, st));
return RAD_OK;
}
int h_memset_async(void* dst, int value, int64_t bytes, RadStream st) {
RAD_TRY(check_stream(st, "rad_memset_async"));
if (bytes == 0) return RAD_OK;
if (!dst || bytes < 0) {
device_set_error("rad_memset_async(%p, %lld): invalid argument", dst, (long long)bytes);
return RAD_E_INVAL;
}
Task t;
t.kind = Task::Memset;
t.dst = dst;
t.a = value & 0xff;
t.b = bytes;
return stream_push((HostStream*)st, t);
}
/* ------------------------------------------------------------------ gates */
/* A gate is a wait task on an event whose one record only the host completes: recorded is 1 from
* the start and rad_gate_open sets completed. The task holds a reference, so the event outlives
* the handle until the worker has passed it. */
struct HostGate {
HostEvent* e = nullptr;
HostStream* s = nullptr;
};
int h_stream_gate(RadStream st, void** out) {
RAD_TRY(check_stream(st, "rad_stream_gate"));
if (!out) return RAD_E_INVAL;
auto* s = (HostStream*)st;
auto* e = new (std::nothrow) HostEvent();
auto* g = new (std::nothrow) HostGate();
if (!e || !g) {
delete e;
delete g;
device_set_error("rad_stream_gate: out of memory");
return RAD_E_NOMEM;
}
e->recorded = 1;
Task t;
t.kind = Task::Wait;
t.ev = e;
t.seq = 1;
event_retain(e);
const int rc = stream_push(s, t);
if (rc < 0) {
event_release(e);
event_release(e);
delete g;
return rc;
}
{
std::lock_guard<std::mutex> lk(s->m);
++s->gates;
}
g->e = e;
g->s = s;
*out = g;
return RAD_OK;
}
int h_gate_open(void* p) {
auto* g = (HostGate*)p;
if (!g || !g->e) return RAD_E_INVAL;
{
std::lock_guard<std::mutex> lk(g->e->m);
g->e->completed = 1;
}
g->e->cv.notify_all();
{
std::lock_guard<std::mutex> lk(g->s->m);
--g->s->gates;
}
event_release(g->e);
delete g;
return RAD_OK;
}
int64_t h_gate_room(RadStream st) {
if (!st) return 0;
auto* s = (HostStream*)st;
std::lock_guard<std::mutex> lk(s->m);
return (int64_t)(s->ring.size() - s->count);
}
/* ------------------------------------------------------------------ elastic memory */
/* Elastic memory via mmap, the same mechanism as the card's VMM: PROT_NONE reserves address space
* without pages, MAP_FIXED read/write backs a granule, and PROT_NONE over it returns the pages.
* Decommitted ranges come back zeroed and addresses never move, as on the HIP path.
*
* The granule is the pool alignment (the card's 2 MiB), not the 4 KiB page, so pool sizing that
* passes here does not strand memory on a card. */
constexpr int64_t kVmemGranule = RAD_ALIGN_POOL;
int64_t h_vmem_granularity(void) { return kVmemGranule; }
/* Each reservation's card, recorded at reserve: the thread that later commits into a pool, or
* releases it at teardown, may have selected another card, and the pages count against the card
* the range was reserved for (as the AQL backend's VRes does). Keyed by base. */
struct VRange { char* end; int device; };
std::mutex g_vres_mu;
std::map<const char*, VRange> g_vres;
int vmem_device_of(const void* addr) {
std::lock_guard<std::mutex> lk(g_vres_mu);
auto it = g_vres.upper_bound((const char*)addr);
if (it == g_vres.begin()) return t_device;
--it;
return (const char*)addr < it->second.end ? it->second.device : t_device;
}
void* h_vmem_reserve(int64_t bytes, int64_t align) {
if (bytes <= 0) return nullptr;
if (align < kVmemGranule) align = kVmemGranule;
/* Over-reserve by one alignment and unmap the head and tail, so the range is exactly
* [base, base+bytes) and release is one munmap with no side table. */
const size_t raw_len = (size_t)(bytes + align);
void* raw = ::mmap(nullptr, raw_len, PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
if (raw == MAP_FAILED) {
device_set_error("vmem_reserve(%lld): mmap failed", (long long)bytes);
return nullptr;
}
char* base = (char*)align_up((int64_t)(uintptr_t)raw, align);
const size_t head = (size_t)(base - (char*)raw);
if (head) ::munmap(raw, head);
const size_t tail = raw_len - head - (size_t)bytes;
if (tail) ::munmap(base + bytes, tail);
{
std::lock_guard<std::mutex> lk(g_vres_mu);
g_vres[base] = VRange{ base + bytes, t_device };
}
return base;
}
void h_vmem_release(void* base, int64_t bytes) {
if (!base || bytes <= 0) return;
{
std::lock_guard<std::mutex> lk(g_vres_mu);
g_vres.erase((const char*)base);
}
::munmap(base, (size_t)bytes);
}
int h_vmem_commit(void* addr, int64_t bytes) {
if (!addr || bytes <= 0) return RAD_E_INVAL;
void* p = ::mmap(addr, (size_t)bytes, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (p != addr) {
device_set_error("vmem_commit(%p, %lld): mmap failed", addr, (long long)bytes);
return RAD_E_NOMEM;
}
g_used[vmem_device_of(addr)].fetch_add(bytes, std::memory_order_relaxed);
return RAD_OK;
}
int h_vmem_decommit(void* addr, int64_t bytes) {
if (!addr || bytes <= 0) return RAD_E_INVAL;
/* MAP_FIXED rather than munmap: a hole in the reservation could be handed to an unrelated
* mmap. */
void* p = ::mmap(addr, (size_t)bytes, PROT_NONE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
if (p != addr) {
device_set_error("vmem_decommit(%p, %lld): mmap failed", addr, (long long)bytes);
return RAD_E_INVAL;
}
g_used[vmem_device_of(addr)].fetch_sub(bytes, std::memory_order_relaxed);
return RAD_OK;
}
/* Commit already maps read/write, so this is a no-op mprotect. It is implemented anyway so the
* host suite exercises the ranges VMem computes for the card. */
int h_vmem_protect(void* addr, int64_t bytes) {
if (!addr || bytes <= 0) return RAD_E_INVAL;
if (::mprotect(addr, (size_t)bytes, PROT_READ | PROT_WRITE) != 0) {
device_set_error("vmem_protect(%p, %lld): mprotect failed", addr, (long long)bytes);
return RAD_E_INVAL;
}
return RAD_OK;
}
int h_vram_used(int device, int64_t* out) {
if (!out || device < 0 || device >= host_count()) return RAD_E_INVAL;
*out = g_used[device].load(std::memory_order_relaxed);
return RAD_OK;
}
const DeviceBackend g_host = {
"host",
h_count, h_set, h_props, h_enable_peer,
h_alloc, h_free, h_host_ptr, h_device_ptr,
h_stream_create, h_stream_destroy, h_stream_sync,
h_event_create, h_event_destroy, h_event_record, h_event_wait,
h_event_query, h_event_sync, h_event_elapsed_ms,
h_host_wrote,
h_memcpy_async, h_memcpy_2d_async, h_memset_async, h_memcpy_ranges_async,
h_memcpy_list_async,
nullptr, nullptr, /* no DMA engine, so no link to hold */
/* No recorded passes: host streams run work as issued. */
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
/* Elastic memory: see above. */
h_vmem_granularity, h_vmem_reserve, h_vmem_release, h_vmem_commit, h_vmem_decommit,
h_vmem_protect,
h_vram_used,
nullptr, nullptr, /* no device code to register */
nullptr, nullptr, /* not integrated graphics; one address space */
nullptr, nullptr, nullptr, /* no PCI address, runtime or DMA engine */
/* No expert prefetch. */
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 */
const DeviceBackend* host_backend(void) { return &g_host; }
} /* namespace rad */