diff --git a/include/strata/hip_compat/cuda_runtime.h b/include/strata/hip_compat/cuda_runtime.h index 8be095c1..d669f0fe 100644 --- a/include/strata/hip_compat/cuda_runtime.h +++ b/include/strata/hip_compat/cuda_runtime.h @@ -36,6 +36,7 @@ #define cudaEventCreateWithFlags hipEventCreateWithFlags #define cudaEventDestroy hipEventDestroy #define cudaEventDisableTiming hipEventDisableTiming +#define cudaEventBlockingSync hipEventBlockingSync #define cudaEventElapsedTime hipEventElapsedTime #define cudaEventQuery hipEventQuery #define cudaEventRecord hipEventRecord diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 8499a36f..3d5500a2 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -360,7 +360,7 @@ struct Stager { pageable[(size_t) i].resize(blob_bytes); buf[i] = pageable[(size_t) i].data(); } - if (cudaEventCreateWithFlags(&dma_done[i], cudaEventDisableTiming) != cudaSuccess) return false; + if (cudaEventCreateWithFlags(&dma_done[i], cudaEventDisableTiming | cudaEventBlockingSync) != cudaSuccess) return false; } cudaGetDevice(&device); for (int t = 0; t < nthreads; ++t) threads.emplace_back([this] { work(); }); @@ -391,8 +391,10 @@ struct Stager { const int j = claim(seen); if (j < 0) { active.fetch_sub(1, std::memory_order_acq_rel); break; } const int b = j % kRing; + // The copies run ahead of the DMAs, so most of these threads spend most of a prompt waiting here: sleep + // (atomic wait, and a blocking-sync event below), not a yield spin - 32 spinners took every core. if (j >= kRing) // job j - kRing's DMA from this buffer is queued - while (issued.load(std::memory_order_acquire) <= j - kRing) std::this_thread::yield(); + for (int i; (i = issued.load(std::memory_order_acquire)) <= j - kRing;) issued.wait(i); // and done - for a generation's first kRing jobs that is the previous generation's last DMA from // the buffer, which nothing else waits for when a chunk ends without a sync (no MTP) or the DMA // was a ring entry the routing skipped (an event never recorded returns at once) @@ -441,11 +443,13 @@ struct Stager { void issued_one(int j, cudaStream_t copy) { cudaEventRecord(dma_done[j % kRing], copy); issued.store(j + 1, std::memory_order_release); + issued.notify_all(); } /// No job is running after this (the end of a layer, or an early return in the middle of one). void finish() { head.store((uint64_t) gen << 32, std::memory_order_release); // n = 0: nothing more to claim issued.store(1 << 30, std::memory_order_release); + issued.notify_all(); while (active.load(std::memory_order_acquire) != 0) std::this_thread::yield(); } };