Adaptive overlap: tell each layer's landing with a stream-ordered copy into pinned memory, not a host function

The per-layer host function made the overlap depend on the driver's callback thread being scheduled: with the pool's
workers spinning on every other CPU it still cost 7 ms per window on a 20-core desktop even with the thread on all CPUs
(RTX 3060, Core Ultra 7 265: 46.5 tok/s with the warmed callbacks against 56.5 with the overlap off). The copy stream now
ends each layer's copies with a 4-byte device-to-host copy of the layer number into a pinned int the gate polls; same stream
order, no host thread. STRATA_ADAPT_SIGNAL=host brings the host functions back. Swaps, residency and answers are unchanged.
This commit is contained in:
Niko1221 committed 2026-10-11 23:00:41 +02:00
1 parent 0e20d9aee9
commit 25e0a2ef79
3 files changed
+29 -3

No files matched your search

+20 -2
View File
@@ -8311,6 +8311,21 @@ int main(int argc, char** argv) {
for (int64_t l = 0; l <= g.n_layers; ++l) agate.ticks[(size_t) l] = {&agate, (int32_t) l};
agate.has.assign((size_t) g.n_layers, 0);
if (!ver.set_adapt_gate(&agate)) why = "the device-planned layers (STRATA_VERIFY_DEVICE_PLAN) are on";
if (why == nullptr && !(std::getenv("STRATA_ADAPT_SIGNAL") && std::getenv("STRATA_ADAPT_SIGNAL")[0] == 'h')) {
// the landing of each layer's copies is told by a stream-ordered D2H copy of the layer number into pinned memory
// (STRATA_ADAPT_SIGNAL=host: a host function instead). No callback thread has to be scheduled while the pool's workers spin on every other CPU
int32_t* vals = nullptr; void* hp = nullptr;
std::vector<int32_t> iota((size_t) g.n_layers + 1);
for (size_t i = 0; i < iota.size(); ++i) iota[i] = (int32_t) i;
if (cudaMalloc((void**) &vals, iota.size() * 4) == cudaSuccess &&
cudaMemcpy(vals, iota.data(), iota.size() * 4, cudaMemcpyHostToDevice) == cudaSuccess &&
cudaHostAlloc(&hp, 64, cudaHostAllocPortable) == cudaSuccess) {
*(volatile int32_t*) hp = -1;
agate.hland = (volatile int32_t*) hp;
agate.dvals = vals;
std::fprintf(stderr, "strata serve: adaptive overlap signal: stream-ordered copies (STRATA_ADAPT_SIGNAL=host: a host function)\n");
} else { (void) cudaGetLastError(); }
}
}
aov = why == nullptr;
if (aov)
@@ -8384,6 +8399,7 @@ int main(int argc, char** argv) {
if (aov) { // 0.1.43: the copies go in layer order and each layer's landing is told to the verify window
std::stable_sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.layer < b.layer; });
agate.landed.store(-1, std::memory_order_release);
if (agate.hland != nullptr) *agate.hland = -1;
agate.has.assign((size_t) g.n_layers, 0);
for (const Swap& s : swaps) agate.has[(size_t) s.layer] = 1;
}
@@ -8464,11 +8480,13 @@ int main(int argc, char** argv) {
srcp->prefetch(s.layer, s.out); // a file-backed arena released its pages: read them back ahead
pending.emplace_back((int32_t) in, slot); // resident once the copy has landed
if (aov && (si + 1 == swaps.size() || swaps[si + 1].layer != s.layer) &&
cudaLaunchHostFunc(adapt_stream, [](void* p) {
(agate.hland != nullptr
? cudaMemcpyAsync((void*) agate.hland, agate.dvals + s.layer, 4, cudaMemcpyDeviceToHost, adapt_stream)
: cudaLaunchHostFunc(adapt_stream, [](void* p) {
auto* t = (strata::core::AdaptGate::Tick*) p;
int32_t cur = t->gate->landed.load(std::memory_order_relaxed);
while (cur < t->layer && !t->gate->landed.compare_exchange_weak(cur, t->layer, std::memory_order_release)) {}
}, &agate.ticks[(size_t) s.layer]) != cudaSuccess) {
}, &agate.ticks[(size_t) s.layer])) != cudaSuccess) {
(void) cudaGetLastError();
cudaStreamSynchronize(adapt_stream); // no callback: wait here, the gate opens at once
agate.landed.store((int32_t) g.n_layers, std::memory_order_release);