Archived
aql: flush the host data path before every doorbell; empty the scalar cache behind a chained wait
Two latent bugs that the packet processor's acquire (about 0.9 us a launch) has been hiding by timing. 1. Kernel arguments and the chain record are written into VRAM through the BAR, and the card's host data path keeps such a write until it is flushed. Nothing ordered that against the doorbell (the sfence only orders the CPU's own buffers). A kernel the card starts a microsecond or two after its doorbell reads the slot as the launch one lap of the ring before left it: old arguments, or no chain record, in which case the launch counts nothing and the chained launch behind it waits for good. The backend now stores to the card's HDP flush register (HSA_AMD_AGENT_INFO_HDP_FLUSH) in front of every doorbell, in publish() and in a played pass's ring(). Without a register (the argument ring in host memory, or a driver that gives none) nothing changes, and a warning says so at startup. 2. rad_chain_wait()/r4d_chain_wait() end in an agent-scope acquire fence, which invalidates the vector caches. A load whose address every lane shares is a scalar load, served from the scalar data cache, and nothing but the packet processor's acquire (which runs when the kernel is launched, before the launch in front has written) empties that. A chained kernel can therefore read a stale scale, count or activation from the arena address an earlier launch read. The waits now issue s_dcache_inv behind the fence (r4d_chain_acquire in libr4d, chain_kernel.h in the core's blit kernels). Evidence on an R9700 with the packet acquire left as it is: a probe of 1024-block readers behind a chained wait saw a stale shared value in 1 launch of 1800 without the s_dcache_inv and in none with it; card-side copies issued one by one with 50 us between them stalled the queue in its first lap, four runs in four, and six runs in nine of 300000 back to back ended with a unit moved by another launch's arguments, when the packets went without their acquire and without the flush, and in none with the flush. New device tests: a chained reader sees a shared value the launch in front wrote, a chained launch reads its own arguments after the ring has lapped, and a copy issued after a pause finds the record written for it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
78ad2096a3
commit
8f2e511fcc
6 files changed
+315
-4
No files matched your search
+80
-3
@@ -21,7 +21,8 @@
|
||||
* an invalidation. A slot is rewritten one lap after it was last read, so a system-scope
|
||||
* acquire is placed at least once per half lap; without it every kernel after the first lap
|
||||
* reads its predecessor's arguments. Slots are 1 KiB so no argument fetch shares a cache line
|
||||
* with the next slot;
|
||||
* with the next slot. No fence makes a write through the BAR arrive: the card's host data
|
||||
* path is flushed in front of every doorbell (host_writes_out);
|
||||
* - host-written memory: the first dispatch after the host waits on any queue takes a
|
||||
* system-scope acquire, since host writes for the next step follow such a wait;
|
||||
* - copies touching host or peer memory carry the scope themselves, as do the barrier packets
|
||||
@@ -225,6 +226,9 @@ struct Dev {
|
||||
bool has_vram = false;
|
||||
uint32_t kfd_id = 0; /* the driver's gpu_id: its sysfs and ioctls name it so */
|
||||
bool host_kargs = false; /* no large BAR: the argument ring lives in host memory */
|
||||
/* The card's host data path flush register (HSA_AMD_AGENT_INFO_HDP_FLUSH), where the host
|
||||
* writes arguments into VRAM; see host_writes_out. */
|
||||
volatile uint32_t* hdp_flush = nullptr;
|
||||
};
|
||||
|
||||
struct Global {
|
||||
@@ -441,6 +445,18 @@ bool init_devices() {
|
||||
RAD_INFO("device: card %d (%s) keeps kernel arguments in pinned host memory, as a "
|
||||
"test asked", i, bus);
|
||||
}
|
||||
if (!g.dev[(size_t)i].host_kargs) {
|
||||
hsa_amd_hdp_flush_t hdp{};
|
||||
if (hsa_agent_get_info(g.dev[(size_t)i].agent,
|
||||
(hsa_agent_info_t)HSA_AMD_AGENT_INFO_HDP_FLUSH,
|
||||
&hdp) == HSA_STATUS_SUCCESS)
|
||||
g.dev[(size_t)i].hdp_flush = hdp.HDP_MEM_FLUSH_CNTL;
|
||||
if (!g.dev[(size_t)i].hdp_flush)
|
||||
RAD_WARN("device: card %d (%s) gives no host data path flush register, so a kernel "
|
||||
"may start before the arguments written for it through the BAR have "
|
||||
"reached VRAM; only the packet processor's acquire, which holds a launch "
|
||||
"back, leaves them the time", i, bus);
|
||||
}
|
||||
}
|
||||
/* Without the handler a fault falls back to the runtime's own handling, which the
|
||||
* memory-fault report below replaces; warn at startup. */
|
||||
@@ -600,6 +616,7 @@ struct Stream {
|
||||
hsa_queue_t* q = nullptr;
|
||||
uint64_t mask = 0;
|
||||
char* karg = nullptr; /* VRAM the CPU writes through the BAR, or host memory */
|
||||
volatile uint32_t* hdp = nullptr; /* Dev::hdp_flush where `karg` is VRAM */
|
||||
std::atomic<bool> busy{false};
|
||||
uint64_t wr = 0; /* next packet index */
|
||||
uint64_t rd = 0; /* last read index seen */
|
||||
@@ -1093,10 +1110,37 @@ inline int reserve(Stream* s, uint64_t* out) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* What the host wrote for the card, on its way before the packets that read it are announced.
|
||||
*
|
||||
* An argument slot in VRAM is written through the BAR: the bytes sit in the processor's
|
||||
* write-combining buffers until a store fence, and behind the link in the card's host data path,
|
||||
* which hands them on to memory in its own time unless it is told to flush. Nothing orders that
|
||||
* against the doorbell. A kernel the card starts soon enough after its packet was announced
|
||||
* reads the slot as the launch a lap of the ring before left it: first its chain record, which
|
||||
* it obeys (an old target, so no wait) or does not accept (so it counts nothing and the chained
|
||||
* launch behind it waits for good), then, behind its wait, its arguments.
|
||||
*
|
||||
* On an R9700 "soon enough" is a microsecond or two. What kept launches from it so far is that the
|
||||
* packet processor's acquire holds a launch about 0.9 us and an ordered launch waits for the
|
||||
* kernel in front: the margin is timing, not ordering. Without the packet's acquire (a test that
|
||||
* issues it so) runs of fourteen 256 KiB card-side copies issued one by one: back to back, six runs
|
||||
* in nine of 300 000 ended with a unit moved by another launch's arguments; with the host quiet
|
||||
* for 50 us behind each doorbell the queue stalled in its first lap, four runs in four, on either
|
||||
* card. With the flush below neither happened.
|
||||
*
|
||||
* The register is the driver's (HSA_AMD_AGENT_INFO_HDP_FLUSH, the remapped page it gives a
|
||||
* process for this), and the store travels the link behind the slot's bytes and in front of the
|
||||
* doorbell's: the order the driver's own submissions use. It goes in front of every doorbell, a
|
||||
* chained launch's or not: no launch was ordered against the write. */
|
||||
inline void host_writes_out(Stream* s) {
|
||||
_mm_sfence();
|
||||
if (s->hdp) *s->hdp = 1;
|
||||
}
|
||||
|
||||
inline void publish(Stream* s, uint64_t idx, uint16_t header, uint16_t setup) {
|
||||
s->wr = idx + 1;
|
||||
hsa_queue_store_write_index_relaxed(s->q, idx + 1);
|
||||
_mm_sfence(); /* the argument slot, written before the header */
|
||||
host_writes_out(s); /* the argument slot, in VRAM before the header */
|
||||
__atomic_store_n(reinterpret_cast<uint32_t*>(
|
||||
static_cast<hsa_kernel_dispatch_packet_t*>(s->q->base_address) +
|
||||
(idx & s->mask)),
|
||||
@@ -1372,6 +1416,9 @@ struct PlayRun {
|
||||
|
||||
void ring() {
|
||||
if (!unrung) return;
|
||||
/* The chained records' argument slots (put) are VRAM the packets read as soon as they
|
||||
* are announced. */
|
||||
host_writes_out(s);
|
||||
hsa_queue_store_write_index_relaxed(s->q, next);
|
||||
hsa_signal_store_relaxed(s->q->doorbell_signal, (hsa_signal_value_t)(next - 1));
|
||||
unrung = 0;
|
||||
@@ -1795,6 +1842,7 @@ int a_stream_create(RadStream* out, int high_priority) {
|
||||
return RAD_E_DEVICE;
|
||||
}
|
||||
s->karg = static_cast<char*>(ka);
|
||||
s->hdp = d.host_kargs ? nullptr : d.hdp_flush;
|
||||
s->chain_ctr = reinterpret_cast<uint64_t*>(s->karg + (size_t)(s->mask + 1) * kKargSlot);
|
||||
*reinterpret_cast<volatile uint64_t*>(s->chain_ctr) = 0;
|
||||
_mm_sfence();
|
||||
@@ -4651,6 +4699,32 @@ bool ride_reusable(void* token) {
|
||||
std::lock_guard<std::mutex> lk(pool().mu);
|
||||
return reusable(g);
|
||||
}
|
||||
|
||||
int chain_probe(void* stream, unsigned* x, unsigned* seen, unsigned pairs, unsigned blocks) {
|
||||
if (!ours(stream) || !x || !seen) return RAD_E_INVAL;
|
||||
for (unsigned i = 0; i < pairs; ++i)
|
||||
if (rad_aql_probe_store(x, i + 1u, stream) < 0 ||
|
||||
rad_aql_probe_load(x, seen + (size_t)i * blocks, blocks, stream) < 0)
|
||||
return RAD_E_DEVICE;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int chain_asks(void* stream, const void* src, void* dst, uint64_t bytes, unsigned* seen, unsigned n) {
|
||||
Stream* s = ours(stream);
|
||||
if (!s || !src || !dst || !seen || !s->chain_ctr || bytes == 0 || (bytes & 15)) return RAD_E_INVAL;
|
||||
for (unsigned i = 0; i < n; ++i)
|
||||
if (rad_aql_probe_ask(src, dst, bytes, seen, i + 1u,
|
||||
reinterpret_cast<unsigned long long*>(s->chain_ctr), stream) < 0)
|
||||
return RAD_E_DEVICE;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int chain_marks(void* stream, unsigned* seen, unsigned n) {
|
||||
if (!ours(stream) || !seen) return RAD_E_INVAL;
|
||||
for (unsigned i = 0; i < n; ++i)
|
||||
if (rad_aql_probe_mark(seen, i + 1u, stream) < 0) return RAD_E_DEVICE;
|
||||
return RAD_OK;
|
||||
}
|
||||
} // namespace aql
|
||||
|
||||
size_t aql_code_registered(void) { return a_code_mark(); }
|
||||
@@ -4961,8 +5035,11 @@ hipError_t hipPeekAtLastError(void) {
|
||||
|
||||
namespace rad {
|
||||
namespace aql {
|
||||
/* A build without the AQL backend hands out no ride. */
|
||||
/* A build without the AQL backend hands out no ride and launches no chain. */
|
||||
bool ride_reusable(void*) { return false; }
|
||||
int chain_probe(void*, unsigned*, unsigned*, unsigned, unsigned) { return RAD_E_UNSUPPORTED; }
|
||||
int chain_marks(void*, unsigned*, unsigned) { return RAD_E_UNSUPPORTED; }
|
||||
int chain_asks(void*, const void*, void*, uint64_t, unsigned*, unsigned) { return RAD_E_UNSUPPORTED; }
|
||||
} // namespace aql
|
||||
} // namespace rad
|
||||
|
||||
|
||||
@@ -72,6 +72,25 @@ void set_next_fence(uint8_t f);
|
||||
/* Whether the signal behind a rad_ride token could be handed out again now (tests/device_test.cpp). */
|
||||
bool ride_reusable(void* token);
|
||||
|
||||
/* `pairs` launches of two chained kernels onto `stream`, one behind the other: the first stores
|
||||
* i + 1 over the 64 words at `x`; the second loads x[0] the way a kernel loads a value all its
|
||||
* lanes share (a scalar load) and each of its `blocks` blocks stores what it saw, at
|
||||
* seen[i * blocks + block]. Every reader must have seen the store in front of it
|
||||
* (tests/device_test.cpp). RAD_E_UNSUPPORTED in a build without the backend. */
|
||||
int chain_probe(void* stream, unsigned* x, unsigned* seen, unsigned pairs, unsigned blocks);
|
||||
|
||||
/* `n` chained launches onto `stream`, launch i storing i + 1 at seen[i]: each takes where to
|
||||
* store from its own arguments, so a launch that read another's leaves a word unwritten
|
||||
* (tests/device_test.cpp). RAD_E_UNSUPPORTED in a build without the backend. */
|
||||
int chain_marks(void* stream, unsigned* seen, unsigned n);
|
||||
|
||||
/* `n` chained launches onto `stream`, each a card-side copy of `bytes` (a multiple of 16) from
|
||||
* `src` to `dst` that also stores at seen[i] whether it found a chain record in its argument
|
||||
* slot: i + 1 if every block did, that with the top bit set if one did not. A block that found
|
||||
* none still counts, so the launches behind it are not left waiting (tests/device_test.cpp).
|
||||
* RAD_E_UNSUPPORTED in a build without the backend. */
|
||||
int chain_asks(void* stream, const void* src, void* dst, uint64_t bytes, unsigned* seen, unsigned n);
|
||||
|
||||
} // namespace aql
|
||||
} // namespace rad
|
||||
|
||||
@@ -90,4 +109,10 @@ int rad_aql_blit_fill(void* dst, int value, uint64_t bytes, void* stream);
|
||||
/* rad_memcpy_ranges_async's kernel: `prefix` a multiple of 16, both bases 16-byte aligned. */
|
||||
int rad_aql_blit_ranges(void* dst, const void* src, uint64_t prefix, const RadCopyRange* ranges,
|
||||
int n, void* stream);
|
||||
/* aql::chain_probe's two kernels, aql::chain_marks's one and aql::chain_asks's one. */
|
||||
int rad_aql_probe_store(unsigned* x, unsigned v, void* stream);
|
||||
int rad_aql_probe_load(const unsigned* x, unsigned* seen, unsigned blocks, void* stream);
|
||||
int rad_aql_probe_mark(unsigned* seen, unsigned v, void* stream);
|
||||
int rad_aql_probe_ask(const void* src, void* dst, uint64_t bytes, unsigned* seen, unsigned v,
|
||||
unsigned long long* ctr, void* stream);
|
||||
}
|
||||
@@ -250,6 +250,75 @@ extern "C" int rad_aql_blit_ranges(void* dst, const void* src, uint64_t prefix,
|
||||
return launched();
|
||||
}
|
||||
|
||||
/* aql::chain_probe's pair (aql.h). The reader's load has one address for the whole wave and
|
||||
* nothing in the kernel writes there, so the compiler makes it a scalar load: served from the
|
||||
* scalar data cache, which the wait must have emptied (chain_kernel.h) for the kernel to see the
|
||||
* store in front of it. */
|
||||
namespace {
|
||||
__global__ __launch_bounds__(64) void r4dchain_probe_store(unsigned* __restrict__ x, unsigned v) {
|
||||
rad_chain_wait();
|
||||
x[threadIdx.x] = v;
|
||||
rad_chain_done();
|
||||
}
|
||||
__global__ __launch_bounds__(32) void r4dchain_probe_load(const unsigned* __restrict__ x,
|
||||
unsigned* __restrict__ seen) {
|
||||
rad_chain_wait();
|
||||
const unsigned v = x[0];
|
||||
if (threadIdx.x == 0) seen[blockIdx.x] = v;
|
||||
rad_chain_done();
|
||||
}
|
||||
/* aql::chain_marks's kernel: where it stores comes from its arguments alone. */
|
||||
__global__ __launch_bounds__(32) void r4dchain_probe_mark(unsigned* __restrict__ seen, unsigned v) {
|
||||
rad_chain_wait();
|
||||
if (threadIdx.x == 0) seen[v - 1u] = v;
|
||||
rad_chain_done();
|
||||
}
|
||||
|
||||
/* aql::chain_asks's kernel: a card-side copy like r4dchain_copy16 that says whether there was a
|
||||
* chain record in its argument slot when it started. A block that found none counts nothing in
|
||||
* rad_chain_done, so it counts by the address in its arguments instead and the launches behind
|
||||
* it do not wait for it for good. */
|
||||
__global__ __launch_bounds__(kThreads) void r4dchain_probe_ask(const u32x4* __restrict__ src,
|
||||
u32x4* __restrict__ dst, uint64_t n,
|
||||
unsigned* __restrict__ seen, unsigned v,
|
||||
unsigned long long* __restrict__ ctr) {
|
||||
const bool found = rad_chain_rec() != nullptr;
|
||||
rad_chain_wait();
|
||||
const uint64_t stride = (uint64_t)gridDim.x * kThreads;
|
||||
for (uint64_t i = (uint64_t)blockIdx.x * kThreads + threadIdx.x; i < n; i += stride) dst[i] = src[i];
|
||||
if (threadIdx.x == 0)
|
||||
__hip_atomic_fetch_or(&seen[v - 1u], found ? v : (v | 0x80000000u), __ATOMIC_RELAXED,
|
||||
__HIP_MEMORY_SCOPE_AGENT);
|
||||
rad_chain_done();
|
||||
if (!found && threadIdx.x == 0)
|
||||
__hip_atomic_fetch_add(ctr, 1ull, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_AGENT);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
extern "C" int rad_aql_probe_ask(const void* src, void* dst, uint64_t bytes, unsigned* seen, unsigned v,
|
||||
unsigned long long* ctr, void* stream) {
|
||||
const uint64_t n = bytes / 16;
|
||||
hipLaunchKernelGGL(r4dchain_probe_ask, dim3(blocks_for(n, 4 * kThreads)), dim3(kThreads), 0,
|
||||
(hipStream_t)stream, (const u32x4*)src, (u32x4*)dst, n, seen, v, ctr);
|
||||
return launched();
|
||||
}
|
||||
|
||||
extern "C" int rad_aql_probe_mark(unsigned* seen, unsigned v, void* stream) {
|
||||
hipLaunchKernelGGL(r4dchain_probe_mark, dim3(1), dim3(32), 0, (hipStream_t)stream, seen, v);
|
||||
return launched();
|
||||
}
|
||||
|
||||
extern "C" int rad_aql_probe_store(unsigned* x, unsigned v, void* stream) {
|
||||
hipLaunchKernelGGL(r4dchain_probe_store, dim3(1), dim3(64), 0, (hipStream_t)stream, x, v);
|
||||
return launched();
|
||||
}
|
||||
|
||||
extern "C" int rad_aql_probe_load(const unsigned* x, unsigned* seen, unsigned blocks, void* stream) {
|
||||
if (blocks == 0) return 0;
|
||||
hipLaunchKernelGGL(r4dchain_probe_load, dim3(blocks), dim3(32), 0, (hipStream_t)stream, x, seen);
|
||||
return launched();
|
||||
}
|
||||
|
||||
extern "C" int rad_aql_blit_fill(void* dst, int value, uint64_t bytes, void* stream) {
|
||||
if (bytes == 0) return 0;
|
||||
const hipStream_t s = (hipStream_t)stream;
|
||||
|
||||
@@ -37,6 +37,11 @@ __device__ __forceinline__ void rad_chain_wait() {
|
||||
__builtin_amdgcn_s_sleep(1);
|
||||
__syncthreads();
|
||||
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "agent");
|
||||
/* The fence empties the vector caches. A load every lane shares may be a scalar one, and the
|
||||
* scalar data cache still holds what a launch read from the address before the launch in front
|
||||
* of this one wrote it: only the packet processor's acquire empties that cache, and it runs
|
||||
* when the kernel is launched, before the launch in front has written. */
|
||||
asm volatile("s_dcache_inv" ::: "memory");
|
||||
}
|
||||
|
||||
__device__ __forceinline__ void rad_chain_done() {
|
||||
|
||||
+19
-1
@@ -10,6 +10,16 @@
|
||||
//
|
||||
// Every block must reach r4d_chain_done exactly once, so a kernel whose blocks return early keeps
|
||||
// its body in a __device__ function called from the chained wrapper.
|
||||
//
|
||||
// The wait's acquire has to cover every read behind it, and an agent-scope fence does not: it
|
||||
// invalidates the vector caches, while a load whose address every lane shares is a scalar load,
|
||||
// served from the scalar data cache. A GEMM reads its rows' scales that way, the routed kernels
|
||||
// their counts and offsets, the head its activation (r4dchain_lm_wmma, 16 words a load). What that
|
||||
// cache holds is what a launch read from the same arena address a layer earlier. The packet
|
||||
// processor's acquire empties it when it launches the kernel, which is before the kernels in front
|
||||
// have written; r4d_chain_wait therefore empties it behind the fence itself (r4d_chain_acquire).
|
||||
// With the packets' acquire on, a probe of 1024-block readers saw one launch in 1800 read a stale
|
||||
// value without it.
|
||||
#pragma once
|
||||
#include <hip/hip_runtime.h>
|
||||
#include "rad_abi.h"
|
||||
@@ -59,6 +69,14 @@ __device__ __forceinline__ const R4dChainRec* r4d_chain_rec() {
|
||||
#ifndef R4D_CHAIN_SLOW
|
||||
#define R4D_CHAIN_SLOW 16
|
||||
#endif
|
||||
// The acquire behind a chained wait: the vector caches by the fence, the scalar data cache by
|
||||
// hand, which the fence leaves (see the head of this file). Every wave, for the compute unit it
|
||||
// runs on.
|
||||
__device__ __forceinline__ void r4d_chain_acquire() {
|
||||
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "agent");
|
||||
asm volatile("s_dcache_inv" ::: "memory");
|
||||
}
|
||||
|
||||
__device__ __forceinline__ void r4d_chain_wait() {
|
||||
const R4dChainRec* r = r4d_chain_rec();
|
||||
if (!r) return;
|
||||
@@ -76,7 +94,7 @@ __device__ __forceinline__ void r4d_chain_wait() {
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "agent");
|
||||
r4d_chain_acquire();
|
||||
}
|
||||
|
||||
// After the block's last write: release, then one add a block.
|
||||
|
||||
@@ -137,6 +137,123 @@ TEST(chained_launches_follow_the_plugins_ask_on_the_aql_backend_only) {
|
||||
CHECK_EQ(rad_dev_chain_enabled(), 0);
|
||||
}
|
||||
|
||||
/* A chained kernel reads behind its wait what the launch in front of it wrote, a value all its
|
||||
* lanes share included. The compiler loads such a value through the scalar data cache, which no
|
||||
* fence it emits empties: only the packet processor's acquire does, when the kernel is launched,
|
||||
* before the launch in front has written. The wait must therefore empty that cache itself
|
||||
* (chain_kernel.h): one reader in 1800 saw the value the reader before it loaded on an R9700
|
||||
* before it did, with the packets' acquire on. Behind a gate, so the card has the readers launched
|
||||
* ahead of the stores they wait for, as a played pass has them. */
|
||||
TEST(a_chained_kernel_sees_a_shared_value_the_launch_in_front_wrote) {
|
||||
if (std::strcmp(rad::device_backend_name(), "aql") != 0) SKIP("launches chain on the AQL backend only");
|
||||
const unsigned pairs = 400, blocks = 256;
|
||||
Stream st;
|
||||
DevBuf<unsigned> x(64 * sizeof(unsigned), RAD_MEM_DEVICE);
|
||||
DevBuf<unsigned> seen((int64_t)pairs * blocks * sizeof(unsigned), RAD_MEM_DEVICE);
|
||||
REQUIRE(x && seen);
|
||||
std::vector<unsigned> got((size_t)pairs * blocks);
|
||||
{
|
||||
rad_dev_chain_infer(1);
|
||||
CHECK_OK(rad_memset_async(x, 0, 64 * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_memset_async(seen, 0, (int64_t)pairs * blocks * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
void* gate = nullptr;
|
||||
REQUIRE_EQ(rad_stream_gate(st.s, &gate), RAD_OK);
|
||||
REQUIRE(rad_gate_room(st.s) >= (int64_t)2 * pairs);
|
||||
CHECK_OK(rad::aql::chain_probe(st.s, x, seen, pairs, blocks));
|
||||
CHECK_OK(rad_gate_open(gate));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
CHECK_OK(rad_memcpy_async(got.data(), seen, (int64_t)pairs * blocks * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
int64_t stale = 0;
|
||||
for (unsigned i = 0; i < pairs; ++i)
|
||||
for (unsigned b = 0; b < blocks; ++b) stale += got[(size_t)i * blocks + b] != i + 1u;
|
||||
CHECK_EQ(stale, 0);
|
||||
}
|
||||
rad_dev_chain_infer(0);
|
||||
}
|
||||
|
||||
/* A chained launch reads its own arguments and its chain record from a slot of the argument ring
|
||||
* that the host wrote and that another launch read a lap of the ring ago; the L2 still holds what
|
||||
* that launch read. Only a system-scope acquire of the packet processor's makes the card forget
|
||||
* it: the one the ring asks for every half lap. Three laps of launches that each store where their own arguments say,
|
||||
* with no host wait between them: without that acquire every launch past the first lap stores
|
||||
* where the launch a lap before did (as they do on a prefetch stream, which never takes one and
|
||||
* counts on the pass beside it to have turned the L2 over). */
|
||||
TEST(a_chained_launch_reads_its_own_arguments_after_the_ring_has_lapped) {
|
||||
if (std::strcmp(rad::device_backend_name(), "aql") != 0) SKIP("launches chain on the AQL backend only");
|
||||
const unsigned n = 3 * 4096 + 517;
|
||||
Stream st;
|
||||
DevBuf<unsigned> seen((int64_t)n * sizeof(unsigned), RAD_MEM_DEVICE);
|
||||
REQUIRE(!!seen);
|
||||
std::vector<unsigned> got(n);
|
||||
{
|
||||
rad_dev_chain_infer(1);
|
||||
CHECK_OK(rad_memset_async(seen, 0, (int64_t)n * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
CHECK_OK(rad::aql::chain_marks(st.s, seen, n));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
CHECK_OK(rad_memcpy_async(got.data(), seen, (int64_t)n * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
unsigned wrong = 0;
|
||||
for (unsigned i = 0; i < n; ++i) wrong += got[i] != i + 1u;
|
||||
CHECK_EQ(wrong, 0u);
|
||||
}
|
||||
rad_dev_chain_infer(0);
|
||||
}
|
||||
|
||||
/* The host writes a launch's arguments and chain record into VRAM through the BAR, and the card's
|
||||
* host data path keeps such a write until it is flushed (aql.cpp, host_writes_out). A launch the
|
||||
* card starts within a microsecond or two of its doorbell otherwise reads the slot as it was. What
|
||||
* it reads first is its chain record, so that is what this asks every launch for, on every card:
|
||||
* a lap of card-side copies with chaining off leaves every slot with their arguments and a record
|
||||
* no kernel accepts; then the same copies chained and 50 us apart, the host quiet between them as
|
||||
* the mover is between the copies of a loan recall, each saying whether it found a record. One
|
||||
* that does not would count nothing and stall the queue; the probe counts for it. The packets
|
||||
* keep the packet processor's acquire here, whose 0.9 us is all that gave the slot time to arrive
|
||||
* before the flush in front of the doorbell. */
|
||||
TEST(a_copy_issued_after_a_pause_finds_the_record_written_for_it) {
|
||||
if (std::strcmp(rad::device_backend_name(), "aql") != 0) SKIP("the AQL backend's argument ring");
|
||||
const unsigned lap = 4096 + 64, words = 8192;
|
||||
const int64_t bytes = 256 * 1024;
|
||||
for (int card = 0; card < rad_dev_count(); ++card) {
|
||||
REQUIRE_EQ(rad_dev_set(card), RAD_OK);
|
||||
DevBuf<unsigned> seen((int64_t)words * sizeof(unsigned), RAD_MEM_DEVICE);
|
||||
DevBuf<uint8_t> src(bytes, RAD_MEM_DEVICE), dst(bytes, RAD_MEM_DEVICE);
|
||||
REQUIRE(seen && src && dst);
|
||||
std::vector<unsigned> got(words);
|
||||
{
|
||||
const unsigned n = 1500;
|
||||
Stream st;
|
||||
rad_dev_chain_infer(0);
|
||||
CHECK_OK(rad::aql::chain_asks(st.s, src, dst, (uint64_t)bytes, seen, lap));
|
||||
CHECK_OK(rad_memset_async(seen, 0, (int64_t)words * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
rad_dev_chain_infer(1);
|
||||
for (unsigned i = 0; i < n; ++i) {
|
||||
/* In runs of eight with a host wait between them, as a recall ends in one. */
|
||||
if (i % 8 == 0) CHECK_OK(rad_stream_sync(st.s));
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
while (std::chrono::steady_clock::now() - t0 < std::chrono::microseconds(50)) {}
|
||||
CHECK_OK(rad::aql::chain_asks(st.s, src, dst, (uint64_t)bytes, seen + i, 1));
|
||||
}
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
CHECK_OK(rad_memcpy_async(got.data(), seen, (int64_t)words * sizeof(unsigned), st.s));
|
||||
CHECK_OK(rad_stream_sync(st.s));
|
||||
/* A launch that read the slot as the lap left it stores where that launch did. */
|
||||
unsigned without = 0, missing = 0;
|
||||
for (unsigned i = 0; i < words; ++i) {
|
||||
without += (got[i] & 0x80000000u) != 0;
|
||||
missing += i < n && got[i] != 1u;
|
||||
}
|
||||
CHECK_EQ(without, 0u);
|
||||
CHECK_EQ(missing, 0u);
|
||||
}
|
||||
rad_dev_chain_infer(0);
|
||||
}
|
||||
rad_dev_set(0);
|
||||
}
|
||||
|
||||
/* An engine of more than one rank claims its cards (rad_dev_cards_claim). The claim that makes a
|
||||
* card shared returns only once everything issued on that card's queues before it has finished:
|
||||
* from then on no chained launch waits on the card behind a collective, and nothing issued under
|
||||
|
||||
Reference in new issue
Block a user