Archived
radiance 1.7.0
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
b068190326
commit
1e968f6e1d
739 files changed
+128447
-84632
No files matched your search
+110
-31
@@ -2,6 +2,9 @@
|
||||
#include "rad_plugin.h"
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* ---- the context switch: callee-saved registers and the stack pointer (x86-64 SysV) */
|
||||
extern "C" void emu_switch(void** save_sp, void* new_sp);
|
||||
@@ -31,7 +34,9 @@ namespace {
|
||||
constexpr size_t kStack = 512 * 1024;
|
||||
struct Fiber {
|
||||
void* sp = nullptr;
|
||||
std::vector<char> stack;
|
||||
/* left uninitialised so the OS commits only the pages a fiber touches: a 1024-thread block
|
||||
* would otherwise zero half a GiB */
|
||||
std::unique_ptr<char[]> stack;
|
||||
EmuIdx tid{};
|
||||
bool done = false;
|
||||
unsigned bar_gen = 0; /* block barrier generation this fiber waits on */
|
||||
@@ -50,6 +55,7 @@ struct Sched {
|
||||
/* wave barriers and exchange slots, double-buffered by parity */
|
||||
int warrive[64] = {}; unsigned wgen[64] = {};
|
||||
int slot[64][2][32] = {};
|
||||
int tag[64][2][32] = {}; /* which op, and its uniform operand, each lane posted */
|
||||
unsigned xparity[1024] = {};
|
||||
} S;
|
||||
|
||||
@@ -95,74 +101,148 @@ static void wave_barrier(int w) {
|
||||
me.waiting = 0;
|
||||
}
|
||||
|
||||
int emu_bpermute(int addr, int v) {
|
||||
const int t = (int)S.f[S.cur].tid.x, w = t >> 5, l = t & 31;
|
||||
/* Every lane posts `v` to the wave's exchange slots and passes the wave barrier; the slots of this
|
||||
* exchange are returned for the caller to read. `tag` names the op and its wave-uniform operand:
|
||||
* lanes that met at different ops (a cross-lane op called under divergence, each side reaching
|
||||
* one) are reported, not combined. */
|
||||
enum { kOpBpermute = 1 << 24, kOpReadlane = 2 << 24, kOpBallot = 3 << 24, kOpDpp = 4 << 24,
|
||||
kOpPermlanex16 = 5 << 24 };
|
||||
static const int* exchange(int v, int* lane, int tag) {
|
||||
const int t = (int)S.f[S.cur].tid.x, w = t >> 5;
|
||||
const unsigned par = S.xparity[t]++ & 1u;
|
||||
S.slot[w][par][l] = v;
|
||||
*lane = t & 31;
|
||||
S.slot[w][par][*lane] = v;
|
||||
S.tag[w][par][*lane] = tag;
|
||||
wave_barrier(w);
|
||||
return S.slot[w][par][(addr >> 2) & 31];
|
||||
for (int i = 0; i < 32; ++i)
|
||||
if (S.tag[w][par][i] != tag) {
|
||||
std::fprintf(stderr, "emu: DIVERGENCE in block (%u,%u,%u), wave %d: lanes %d and %d met at different "
|
||||
"cross-lane ops (0x%x, 0x%x)\n", S.bid.x, S.bid.y, S.bid.z, w, *lane, i, tag,
|
||||
S.tag[w][par][i]);
|
||||
std::abort();
|
||||
}
|
||||
return S.slot[w][par];
|
||||
}
|
||||
int emu_bpermute(int addr, int v) {
|
||||
int l;
|
||||
return exchange(v, &l, kOpBpermute)[(addr >> 2) & 31];
|
||||
}
|
||||
/* v_readlane_b32 takes a wave-uniform lane, so a lane-varying one is reported */
|
||||
int emu_readlane(int v, int lane) {
|
||||
int l;
|
||||
return exchange(v, &l, kOpReadlane | (lane & 31))[lane & 31];
|
||||
}
|
||||
int emu_readlane(int v, int lane) { return emu_bpermute(lane << 2, v); }
|
||||
int emu_cvt_pk_fp8(float a, float b, int old, bool hi) {
|
||||
const unsigned pk = (unsigned)rad_f32_to_fp8e4m3(a) | ((unsigned)rad_f32_to_fp8e4m3(b) << 8);
|
||||
const unsigned o = (unsigned)old;
|
||||
return (int)(hi ? ((o & 0xffffu) | (pk << 16)) : ((o & 0xffff0000u) | pk));
|
||||
}
|
||||
static short g_wa[64][2][32][8], g_wb[64][2][32][8];
|
||||
emu_v8f emu_wmma_bf16(emu_v8s a, emu_v8s b, emu_v8f c) {
|
||||
/* One 16x16x16 WMMA: each lane posts its A and B elements, then reads the whole tile back. `ga`
|
||||
* and `gb` are the exchange slots of one element type, [wave][parity][lane][element]. */
|
||||
template <class T, class V, class Cvt>
|
||||
static emu_v8f emu_wmma(T (&ga)[64][2][32][8], T (&gb)[64][2][32][8], V a, V b, emu_v8f c,
|
||||
Cvt cvt) {
|
||||
const int t = (int)S.f[S.cur].tid.x, w = t >> 5, l = t & 31;
|
||||
const unsigned par = S.xparity[t]++ & 1u;
|
||||
for (int e = 0; e < 8; ++e) { g_wa[w][par][l][e] = a[e]; g_wb[w][par][l][e] = b[e]; }
|
||||
for (int e = 0; e < 8; ++e) { ga[w][par][l][e] = a[e]; gb[w][par][l][e] = b[e]; }
|
||||
wave_barrier(w);
|
||||
auto bf = [](short v) { unsigned u = (unsigned)(unsigned short)v << 16; float f; std::memcpy(&f, &u, 4); return f; };
|
||||
const int N = l & 15, h = l >> 4;
|
||||
emu_v8f d;
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const int M = 8 * h + e;
|
||||
float acc = c[e];
|
||||
for (int k = 0; k < 16; ++k)
|
||||
acc += bf(g_wa[w][par][M + 16 * (k >> 3)][k & 7]) * bf(g_wb[w][par][N + 16 * (k >> 3)][k & 7]);
|
||||
acc += cvt(ga[w][par][M + 16 * (k >> 3)][k & 7]) * cvt(gb[w][par][N + 16 * (k >> 3)][k & 7]);
|
||||
d[e] = acc;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
static short g_wa[64][2][32][8], g_wb[64][2][32][8];
|
||||
emu_v8f emu_wmma_bf16(emu_v8s a, emu_v8s b, emu_v8f c) {
|
||||
return emu_wmma(g_wa, g_wb, a, b, c, [](short v) { return rad_bf16_to_f32((uint16_t)v); });
|
||||
}
|
||||
static _Float16 g_ha[64][2][32][8], g_hb[64][2][32][8];
|
||||
emu_v8f emu_wmma_f16(emu_v8h a, emu_v8h b, emu_v8f c) {
|
||||
const int t = (int)S.f[S.cur].tid.x, w = t >> 5, l = t & 31;
|
||||
const unsigned par = S.xparity[t]++ & 1u;
|
||||
for (int e = 0; e < 8; ++e) { g_ha[w][par][l][e] = a[e]; g_hb[w][par][l][e] = b[e]; }
|
||||
wave_barrier(w);
|
||||
const int N = l & 15, h = l >> 4;
|
||||
emu_v8f d;
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const int M = 8 * h + e;
|
||||
float acc = c[e];
|
||||
for (int k = 0; k < 16; ++k)
|
||||
acc += (float)g_ha[w][par][M + 16 * (k >> 3)][k & 7] * (float)g_hb[w][par][N + 16 * (k >> 3)][k & 7];
|
||||
d[e] = acc;
|
||||
}
|
||||
return d;
|
||||
return emu_wmma(g_ha, g_hb, a, b, c, [](_Float16 v) { return (float)v; });
|
||||
}
|
||||
unsigned emu_ballot(bool p) {
|
||||
int l;
|
||||
const int* s = exchange(p ? 1 : 0, &l, kOpBallot);
|
||||
unsigned m = 0;
|
||||
for (int l = 0; l < 32; ++l) m |= (unsigned)(emu_readlane(p ? 1 : 0, l) & 1) << l;
|
||||
for (int i = 0; i < 32; ++i) m |= (unsigned)(s[i] & 1) << i;
|
||||
return m;
|
||||
}
|
||||
int emu_update_dpp(int old, int src, int ctrl, int row_mask, int bank_mask, bool bound_ctrl) {
|
||||
int l;
|
||||
const int* s = exchange(src, &l, kOpDpp | (ctrl & 0xffff));
|
||||
const int r = l & 15, k = ctrl & 15;
|
||||
int from;
|
||||
if (ctrl > 0x100 && ctrl < 0x110) from = r + k < 16 ? l + k : -1; /* row_shl */
|
||||
else if (ctrl > 0x110 && ctrl < 0x120) from = r - k >= 0 ? l - k : -1; /* row_shr */
|
||||
else if (ctrl >= 0x160 && ctrl < 0x170) from = (l & 16) | (r ^ k); /* row_xmask */
|
||||
else { std::fprintf(stderr, "emu: dpp_ctrl 0x%x is not emulated\n", ctrl); std::abort(); }
|
||||
if ((row_mask & 3) != 3 || (bank_mask & 15) != 15) {
|
||||
std::fprintf(stderr, "emu: dpp row/bank masks 0x%x/0x%x are not emulated\n", row_mask, bank_mask);
|
||||
std::abort();
|
||||
}
|
||||
return from < 0 ? (bound_ctrl ? 0 : old) : s[from];
|
||||
}
|
||||
int emu_permlanex16(int, int src, unsigned sel_lo, unsigned sel_hi, bool, bool) {
|
||||
int l;
|
||||
const int* s = exchange(src, &l, kOpPermlanex16);
|
||||
const int r = l & 15;
|
||||
const unsigned sel = (r < 8 ? sel_lo >> (4 * r) : sel_hi >> (4 * (r - 8))) & 15u;
|
||||
return s[(16 - (l & 16)) | (int)sel];
|
||||
}
|
||||
emu_v8s emu_load_tr_b128(const void*) {
|
||||
std::fprintf(stderr, "emu: global_load_tr_b128 is not emulated\n");
|
||||
std::abort();
|
||||
}
|
||||
|
||||
/* Dynamic LDS: a mapping whose last page is inaccessible, the buffer placed against it with its
|
||||
* start 16-byte aligned, refilled with a poison pattern for every block. */
|
||||
static char* g_lds_map = nullptr;
|
||||
static size_t g_lds_map_len = 0;
|
||||
static char* g_lds = nullptr;
|
||||
void* emu_dyn_lds() { return g_lds; }
|
||||
static void lds_reserve(size_t bytes) {
|
||||
const size_t page = (size_t)sysconf(_SC_PAGESIZE);
|
||||
const size_t len = (bytes + 15 + page - 1) / page * page + page;
|
||||
if (len > g_lds_map_len) {
|
||||
if (g_lds_map) munmap(g_lds_map, g_lds_map_len);
|
||||
void* m = mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (m == MAP_FAILED || mprotect((char*)m + len - page, page, PROT_NONE) != 0) {
|
||||
std::fprintf(stderr, "emu: no dynamic LDS mapping\n");
|
||||
std::abort();
|
||||
}
|
||||
g_lds_map = (char*)m;
|
||||
g_lds_map_len = len;
|
||||
}
|
||||
const uintptr_t end = (uintptr_t)(g_lds_map + g_lds_map_len - page);
|
||||
g_lds = (char*)((end - bytes) & ~(uintptr_t)15);
|
||||
}
|
||||
|
||||
int emu_grids = 0;
|
||||
int emu_last_block = 0;
|
||||
void emu_run_grid(dim3 grid, dim3 block, void (*body)(void*), void* ctx) {
|
||||
const char* emu_last_kernel = "";
|
||||
void emu_run_grid(dim3 grid, dim3 block, void (*body)(void*), void* ctx, size_t dyn_lds) {
|
||||
++emu_grids;
|
||||
const int n = (int)(block.x * block.y * block.z);
|
||||
emu_last_block = n;
|
||||
if (block.y != 1 || block.z != 1 || n % 32 || n > 1024) { std::fprintf(stderr, "emu: block shape\n"); std::abort(); }
|
||||
if (dyn_lds > 64 * 1024) {
|
||||
std::fprintf(stderr, "emu: %zu bytes of dynamic LDS, past the 64 KiB a workgroup has\n", dyn_lds);
|
||||
std::abort();
|
||||
}
|
||||
lds_reserve(dyn_lds);
|
||||
S.body = body; S.ctx = ctx; S.nthreads = n;
|
||||
S.f.resize(n);
|
||||
for (auto& f : S.f) if (f.stack.size() != kStack) f.stack.assign(kStack, 0);
|
||||
for (auto& f : S.f) if (!f.stack) f.stack.reset(new char[kStack]);
|
||||
for (unsigned bz = 0; bz < grid.z; ++bz)
|
||||
for (unsigned by = 0; by < grid.y; ++by)
|
||||
for (unsigned bx = 0; bx < grid.x; ++bx) {
|
||||
S.bid = EmuIdx{bx, by, bz};
|
||||
std::memset(g_lds, 0xA5, dyn_lds);
|
||||
S.arrive = 0; S.gen = 0;
|
||||
for (int w = 0; w < 64; ++w) { S.warrive[w] = 0; S.wgen[w] = 0; }
|
||||
for (int t = 0; t < n; ++t) {
|
||||
@@ -170,14 +250,13 @@ void emu_run_grid(dim3 grid, dim3 block, void (*body)(void*), void* ctx) {
|
||||
f.done = false; f.waiting = 0; f.tid = EmuIdx{(unsigned)t, 0, 0};
|
||||
S.xparity[t] = 0;
|
||||
/* a fresh stack: six callee-saved slots, then the entry as the return address */
|
||||
void** top = (void**)(f.stack.data() + kStack - 64);
|
||||
void** top = (void**)(f.stack.get() + kStack - 64);
|
||||
*--top = (void*)&emu_entry;
|
||||
for (int r = 0; r < 6; ++r) *--top = nullptr;
|
||||
f.sp = top;
|
||||
}
|
||||
int live = n;
|
||||
while (live > 0) {
|
||||
int progressed = 0;
|
||||
const int live_before = live;
|
||||
const unsigned g0 = S.gen; unsigned wg0[64]; for (int w = 0; w < 64; ++w) wg0[w] = S.wgen[w];
|
||||
int arr0 = S.arrive; int warr0[64]; for (int w = 0; w < 64; ++w) warr0[w] = S.warrive[w];
|
||||
@@ -189,7 +268,7 @@ void emu_run_grid(dim3 grid, dim3 block, void (*body)(void*), void* ctx) {
|
||||
S.cur = -1;
|
||||
if (!S.f[t].done) ++live;
|
||||
}
|
||||
progressed = S.gen != g0 || S.arrive != arr0 || live != live_before;
|
||||
bool progressed = S.gen != g0 || S.arrive != arr0 || live != live_before;
|
||||
for (int w = 0; w < 64; ++w) progressed |= (S.wgen[w] != wg0[w]) || (S.warrive[w] != warr0[w]);
|
||||
if (live > 0 && !progressed) {
|
||||
int done = 0, wb = 0, bb = 0;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
/* tests/r4d_emu: SIMT EMULATOR SHIM -- runs libr4d kernel source on the host. Every GPU thread is a fiber on
|
||||
* one OS thread; __syncthreads and every cross-lane op are barriers the scheduler enforces, and a
|
||||
* barrier some threads never reach is reported as a deadlock rather than hung. Test-only. */
|
||||
/* tests/r4d_emu: SIMT emulator shim that runs libr4d kernel source on the host. Each GPU thread
|
||||
* is a fiber on one OS thread; __syncthreads and cross-lane ops are barriers the scheduler
|
||||
* enforces, a barrier some threads never reach is reported as a deadlock, and lanes of a wave
|
||||
* that meet at different cross-lane ops as a divergence. Test-only. */
|
||||
#pragma once
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#define __global__
|
||||
@@ -23,6 +25,14 @@ typedef void* hipStream_t;
|
||||
typedef int hipError_t;
|
||||
enum { hipSuccess = 0 };
|
||||
inline hipError_t hipGetLastError() { return 0; }
|
||||
/* one device, with the R9700's 64 KiB of LDS a workgroup, which launchers size dynamic LDS by */
|
||||
struct hipDeviceProp_t { size_t sharedMemPerBlock; };
|
||||
inline hipError_t hipGetDevice(int* d) { *d = 0; return hipSuccess; }
|
||||
inline hipError_t hipGetDeviceProperties(hipDeviceProp_t* p, int) {
|
||||
*p = hipDeviceProp_t{};
|
||||
p->sharedMemPerBlock = 64 * 1024;
|
||||
return hipSuccess;
|
||||
}
|
||||
|
||||
struct EmuIdx { unsigned x, y, z; };
|
||||
EmuIdx& emu_thread_idx();
|
||||
@@ -36,6 +46,40 @@ int emu_readlane(int v, int lane);
|
||||
#define __builtin_amdgcn_readlane(v, l) emu_readlane((v), (l))
|
||||
unsigned emu_ballot(bool p);
|
||||
#define __builtin_amdgcn_ballot_w32(p) emu_ballot((p))
|
||||
/* HIP's __ballot: the wave32 mask, zero-extended */
|
||||
inline unsigned long long __ballot(int p) { return emu_ballot(p != 0); }
|
||||
inline int __popcll(unsigned long long v) { return __builtin_popcountll(v); }
|
||||
/* v_readfirstlane_b32: a cross-lane op is a wave barrier here, so every lane is active, lane 0 first */
|
||||
#define __builtin_amdgcn_readfirstlane(v) emu_readlane((v), 0)
|
||||
/* DPP16 row_shl, row_shr and row_xmask with every row and bank written; a source outside the row
|
||||
* reads 0 under bound_ctrl, else leaves `old`. Other controls and masks are reported. */
|
||||
int emu_update_dpp(int old, int src, int ctrl, int row_mask, int bank_mask, bool bound_ctrl);
|
||||
#define __builtin_amdgcn_update_dpp(o, s, c, rm, bm, bc) emu_update_dpp((o), (s), (c), (rm), (bm), (bc))
|
||||
/* v_permlanex16_b32: lane L reads the lane of the other row of sixteen that L's selector names */
|
||||
int emu_permlanex16(int old, int src, unsigned sel_lo, unsigned sel_hi, bool fi, bool bc);
|
||||
#define __builtin_amdgcn_permlanex16(o, s, lo, hi, fi, bc) emu_permlanex16((o), (s), (lo), (hi), (fi), (bc))
|
||||
/* v_perm_b32: result byte i is the byte of {a, b} (b low) that selector byte i names; 8-11 a sign
|
||||
* byte (bit 15 or 31 of b, then of a), 12 zero, 13 and up 0xff */
|
||||
inline unsigned emu_perm(unsigned a, unsigned b, unsigned sel) {
|
||||
const unsigned long long d = ((unsigned long long)a << 32) | b;
|
||||
unsigned r = 0;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const unsigned s = (sel >> (8 * i)) & 0xffu;
|
||||
unsigned v = s >= 13 ? 0xffu : s == 12 ? 0u : s >= 8 ? ((d >> (16 * (s - 8) + 15)) & 1u) * 0xffu
|
||||
: (unsigned)(d >> (8 * s)) & 0xffu;
|
||||
r |= v << (8 * i);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
#define __builtin_amdgcn_perm(a, b, s) emu_perm((a), (b), (s))
|
||||
/* s_barrier is the workgroup barrier. Fibers share one memory and switch only at barriers, so a
|
||||
* fence or a scheduling barrier orders nothing more. */
|
||||
#define __builtin_amdgcn_s_barrier() __syncthreads()
|
||||
#define __builtin_amdgcn_fence(...) ((void)0)
|
||||
#define __builtin_amdgcn_sched_barrier(m) ((void)0)
|
||||
/* LDS atomics: fibers switch only at barriers, so a plain read-modify-write is indivisible */
|
||||
template <class T>
|
||||
inline T atomicAdd(T* p, T v) { const T o = *p; *p = o + v; return o; }
|
||||
/* v_wmma_f32_16x16x16_bf16 (gfx12, wave32): A[M][K] lane M + 16 * (K / 8) element K % 8; B[K][N]
|
||||
* lane N + 16 * (K / 8) element K % 8; D[M][N] lane N + 16 * (M / 8) element M % 8 */
|
||||
typedef short emu_v8s __attribute__((ext_vector_type(8)));
|
||||
@@ -45,15 +89,19 @@ emu_v8f emu_wmma_bf16(emu_v8s a, emu_v8s b, emu_v8f c);
|
||||
typedef _Float16 emu_v8h __attribute__((ext_vector_type(8)));
|
||||
emu_v8f emu_wmma_f16(emu_v8h a, emu_v8h b, emu_v8f c);
|
||||
#define __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a, b, c) emu_wmma_f16((a), (b), (c))
|
||||
/* v_cvt_pk_fp8_f32 (gfx12, OCP E4M3): a and b as two codes into the low half of `old` (hi false)
|
||||
* or its high half, the other half kept; round to nearest even. The project's saturating encoder,
|
||||
* rad_f32_to_fp8e4m3, is the same function on the +-448 a kernel clamps to first. */
|
||||
/* global_load_tr_b128: not emulated; reported when reached */
|
||||
emu_v8s emu_load_tr_b128(const void* p);
|
||||
#define __builtin_amdgcn_global_load_tr_b128_v8i16(p) emu_load_tr_b128((const void*)(p))
|
||||
/* v_cvt_pk_fp8_f32 (gfx12, OCP E4M3): converts a and b into the low half of `old` (hi false) or
|
||||
* its high half, keeping the other half; round to nearest even. Matches the saturating encoder
|
||||
* rad_f32_to_fp8e4m3 on inputs a kernel has already clamped to +-448. */
|
||||
int emu_cvt_pk_fp8(float a, float b, int old, bool hi);
|
||||
#define __builtin_amdgcn_cvt_pk_fp8_f32(a, b, old, hi) emu_cvt_pk_fp8((a), (b), (old), (hi))
|
||||
extern int emu_last_block; /* threads a block of the last grid had: which kernel ran */
|
||||
|
||||
struct alignas(8) uint2 { unsigned x, y; };
|
||||
struct alignas(16) uint4 { unsigned x, y, z, w; };
|
||||
struct alignas(16) int4 { int x, y, z, w; };
|
||||
struct alignas(16) float4 { float x, y, z, w; };
|
||||
inline uint4 make_uint4(unsigned a, unsigned b, unsigned c, unsigned d) { return uint4{a, b, c, d}; }
|
||||
inline uint2 make_uint2(unsigned a, unsigned b) { return uint2{a, b}; }
|
||||
@@ -64,8 +112,8 @@ inline unsigned __float_as_uint(float f) { unsigned u; std::memcpy(&u, &f, 4); r
|
||||
inline float __int_as_float(int u) { float f; std::memcpy(&f, &u, 4); return f; }
|
||||
inline int __float_as_int(float f) { int u; std::memcpy(&u, &f, 4); return u; }
|
||||
|
||||
/* one block at a time, every thread a fiber */
|
||||
void emu_run_grid(dim3 grid, dim3 block, void (*body)(void*), void* ctx);
|
||||
/* one block at a time, every thread a fiber; `dyn_lds` bytes of dynamic LDS */
|
||||
void emu_run_grid(dim3 grid, dim3 block, void (*body)(void*), void* ctx, size_t dyn_lds = 0);
|
||||
template <typename K, typename A>
|
||||
struct EmuCall { K k; A a; static void run(void* p) { auto* c = (EmuCall*)p; c->k(c->a); } };
|
||||
template <typename A>
|
||||
@@ -74,3 +122,28 @@ void emu_launch(void (*k)(A), dim3 g, dim3 b, A a) {
|
||||
emu_run_grid(g, b, &EmuCall<void (*)(A), A>::run, &c);
|
||||
}
|
||||
#define R4D_KLAUNCH(kernel, grid, block, stream, ...) emu_launch(&kernel, grid, block, __VA_ARGS__)
|
||||
|
||||
/* R4D_EMIT (r4d_emit.h): every argument converted to its parameter's type, as r4d_emit does, and
|
||||
* `lds` bytes of dynamic LDS; the kernel's name is kept in emu_last_kernel. Returns RAD_OK. */
|
||||
extern const char* emu_last_kernel;
|
||||
template <class... P, class... A>
|
||||
int emu_emit(const char* name, void (*k)(P...), dim3 g, dim3 b, size_t lds, const A&... a) {
|
||||
static_assert(sizeof...(P) == sizeof...(A),
|
||||
"a kernel argument list must match the kernel's parameter list");
|
||||
struct Call {
|
||||
void (*k)(P...);
|
||||
std::tuple<P...> v;
|
||||
static void run(void* p) { Call* c = (Call*)p; std::apply(c->k, c->v); }
|
||||
};
|
||||
Call c{k, std::tuple<P...>(static_cast<P>(a)...)};
|
||||
emu_last_kernel = name;
|
||||
emu_run_grid(g, b, &Call::run, &c, lds);
|
||||
return 0;
|
||||
}
|
||||
#define R4D_EMIT(stream, kernel, grid, block, lds, ...) \
|
||||
emu_emit(#kernel, (kernel), (grid), (block), (size_t)(lds), __VA_ARGS__)
|
||||
/* R4D_DYN_LDS (r4d_launch.h): the launch's dynamic LDS, poisoned at each block's start and ending
|
||||
* at an inaccessible page (its start 16-byte aligned), so a read before any write shows and one
|
||||
* past the end faults */
|
||||
void* emu_dyn_lds();
|
||||
#define R4D_DYN_LDS(type, name) type* const name = (type*)emu_dyn_lds()
|
||||
Reference in new issue
Block a user