From 073d079d27d229468456604a80eeac79bab301f7 Mon Sep 17 00:00:00 2001 From: SuperDistance <43229543+SuperDistance@users.noreply.github.com> Date: Tue, 13 Oct 2026 17:16:06 -0700 Subject: [PATCH] batch-groups: the concurrent read's threads do not share the host thread's core on Linux On Linux a thread inherits its creator's CPU mask, and the host thread is pinned to the one core the pool leaves it (aux_cpus.hpp). The read's thread and the doorbell thread were created from it, so they shared that core with the host thread that pumps the windows, and every window waited there for the doorbell of each layer: 6.1 rows/s during a read on 4x RTX 5080, 4 stages (blange48 on #2326). Windows places the host with CPU Sets, which new threads do not inherit, so it did not show there. Both threads now move off the host's core and the pool workers' CPUs (on an SMT machine with the default pool: the idle sibling threads; without such CPUs, every CPU but the host's), and the threads that already run on the host's CPU (the prompt path's copy threads, started by the first prompt) move the same way at a read. --aux-cpus places them all when it is on. STRATA_PIPED_CONCURRENT_UNPIN=0 keeps the inherited CPUs (an A/B). Measured on Linux (WSL2, Ubuntu 24.04, 2x RTX 5090, 4 streams, a cold 32K prompt; same binary, UNPIN=0 / default): the slots decoded 25.9 and 23.7 rows/s during the read with the threads kept on the host's CPU (76-81 threads there), and 75.6 and 62.8 with this change; with the n-gram table in RAM (WSL's disk reads it 7x slower than Windows) 110.7 rows/s, as on Windows (107.0). piped_read_join_bench.py (cold32k, followup, two_joiners; table in RAM, 2-3 runs each): 24-37 rows/s during the 30K+ reads kept on the host's CPU, 95-127 with this change, and 925-1,065 stream tokens during the cold 32K join against 454-484. On Windows, STRATA_TEST_CONC_ON_HOST_CPU=1 (a test) puts both threads on the host thread's CPU set first: 18.6 rows/s kept there, 105.4 with this change. --- include/strata/platform/aux_cpus.hpp | 64 ++++++++++++++++++++++++++++ src/program/generate.cpp | 54 ++++++++++++++++++++++- 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/include/strata/platform/aux_cpus.hpp b/include/strata/platform/aux_cpus.hpp index 3a560316..cd90e700 100644 --- a/include/strata/platform/aux_cpus.hpp +++ b/include/strata/platform/aux_cpus.hpp @@ -159,6 +159,7 @@ struct State { std::vector allowed; ///< the process's CPUs, snapshotted before any thread is pinned std::vector plan; ///< the aux CPUs (empty: nothing to do) std::vector workers; ///< the pool's CPUs: their threads are never touched + int host_cpu = -1; ///< the CPU the host thread is pinned to (-1: unknown) cpu_set_t target; std::set owned; ///< tids of pool workers and host threads std::map original; ///< tid -> the mask it had before this module moved it @@ -286,6 +287,66 @@ inline void set_enabled(bool on) { inline bool enabled() { return detail::state().enabled.load(std::memory_order_acquire); } +namespace detail { +/// The CPUs a thread that must not share the host's core runs on: every CPU the process may use but the host's and +/// the pool workers' (on an SMT machine with the default pool, the idle sibling threads); without such CPUs, every CPU +/// but the host's; failing that, every CPU. Caller holds `mu`. False when `configure` saw no CPUs. +inline bool off_host_target_locked(const State& s, cpu_set_t& out) { + if (s.allowed.empty()) return false; + auto build = [&](bool skip_workers) { + CPU_ZERO(&out); + int n = 0; + for (int c : s.allowed) { + if (c == s.host_cpu) continue; + if (skip_workers && std::find(s.workers.begin(), s.workers.end(), c) != s.workers.end()) continue; + CPU_SET(c, &out); + ++n; + } + return n; + }; + if (build(true) > 0 || build(false) > 0) return true; + CPU_ZERO(&out); + for (int c : s.allowed) CPU_SET(c, &out); + return true; +} +} // namespace detail + +/// Puts the calling thread off the host's core and off the pool workers' CPUs (see off_host_target_locked). A thread +/// the host creates inherits the host's single CPU; one that must not share that core (the concurrent prompt read's +/// thread and its doorbell thread, STRATA_PIPED_CONCURRENT) calls this first. False when nothing changed. +inline bool place_off_host() { + detail::State& s = detail::state(); + std::lock_guard lk(s.mu); + cpu_set_t to; + if (!detail::off_host_target_locked(s, to)) return false; + return sched_setaffinity(0, sizeof to, &to) == 0; +} + +/// Called on the host thread: every other thread that runs on exactly the host's CPUs (it inherited them: the prefill +/// copy threads started by the first prompt, a helper the host started) goes where place_off_host puts a thread. The +/// pool's workers and the host threads are never moved. Returns how many moved. +inline int release_host_inheritors() { + detail::State& s = detail::state(); + std::lock_guard lk(s.mu); + cpu_set_t host, all, to; + CPU_ZERO(&host); + CPU_ZERO(&all); + if (!detail::off_host_target_locked(s, to) || sched_getaffinity(0, sizeof host, &host) != 0) return 0; + for (int c : s.allowed) CPU_SET(c, &all); + if (detail::same_mask(host, all)) return 0; // the host is not pinned: nothing inherited a single core + detail::purge_locked(s); + const int self = detail::self_tid(); + int n = 0; + for (int tid : detail::task_ids()) { + if (tid == self || s.owned.find(tid) != s.owned.end()) continue; + cpu_set_t m; + CPU_ZERO(&m); + if (sched_getaffinity(tid, sizeof m, &m) != 0 || !detail::same_mask(m, host)) continue; + if (sched_setaffinity(tid, sizeof to, &to) == 0) ++n; + } + return n; +} + /// The aux CPUs of the plan ({} when there is none). inline std::vector plan() { detail::State& s = detail::state(); @@ -324,6 +385,7 @@ inline std::string set_topology(int host_cpu, const std::vector& worker_cpu : derive(allowed, host_cpu, worker_cpus); s.plan = plan; s.workers = worker_cpus; + s.host_cpu = host_cpu; CPU_ZERO(&s.target); for (int c : plan) CPU_SET(c, &s.target); s.have_plan.store(!plan.empty(), std::memory_order_release); @@ -353,6 +415,8 @@ inline void pin_current_thread() {} inline int restore() { return 0; } inline void set_enabled(bool) {} inline bool enabled() { return false; } +inline bool place_off_host() { return false; } // (a new thread does not inherit the host's CPU set here) +inline int release_host_inheritors() { return 0; } inline std::vector plan() { return {}; } inline std::string set_topology(int, const std::vector&) { return "strata aux cpus: not available on this system"; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index a54192ae..d96ca97f 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -11684,6 +11684,56 @@ int main(int argc, char** argv) { if (!sp.reserve_uploads(C)) std::fprintf(stderr, "strata batch: the prompt path's pinned upload slots could not be " "reserved (they grow at first use)\n"); + // where the read's thread and the doorbell thread run: on Linux a thread inherits its creator's + // CPUs, and the host thread is pinned to the one core the pool leaves it, so both would share that + // core with the host thread that pumps the windows - every window then waits its turn there for + // the doorbell of each layer (4x RTX 5080, 4 stages: 6 rows/s during a read). They move off the + // host's core and the pool workers' CPUs instead (--aux-cpus places them, when it is on). + // STRATA_PIPED_CONCURRENT_UNPIN=0 keeps the inherited CPUs (an A/B); STRATA_TEST_CONC_ON_HOST_CPU=1 + // (Windows, a test) puts them on the host thread's CPUs first, as Linux does. + static const bool conc_unpin = [] { + const char* v = std::getenv("STRATA_PIPED_CONCURRENT_UNPIN"); + return v == nullptr || std::atoi(v) != 0; + }(); +#if defined(_WIN32) + static const bool conc_on_host = std::getenv("STRATA_TEST_CONC_ON_HOST_CPU") != nullptr; + std::vector host_cpu_sets; + if (conc_on_host) { + const HANDLE self = GetCurrentThread(); + ULONG count = 0; + if (GetThreadSelectedCpuSets(self, nullptr, 0, &count) || + GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + host_cpu_sets.resize(count); + if (count && !GetThreadSelectedCpuSets(self, host_cpu_sets.data(), count, &count)) + host_cpu_sets.clear(); + } + static bool said = false; + if (!said) { + said = true; + std::fprintf(stderr, "strata batch: test: the read's and the doorbell threads start on the " + "host thread's %zu CPU set(s)%s\n", host_cpu_sets.size(), + conc_unpin ? ", then leave them (the fix)" + : " and stay there (as on Linux)"); + } + } +#endif + // the threads that already inherited the host's core (the prompt path's copy threads, started by + // the first prompt on the host thread) work during the read too: off that core as well (Linux) + if (conc_unpin && !strata::aux_cpus::enabled()) + if (const int moved = strata::aux_cpus::release_host_inheritors(); moved > 0) + std::fprintf(stderr, "strata batch: %d threads that ran on the host thread's core moved off " + "it, for reads beside the slots\n", moved); + auto conc_thread_cpus = [&] { + strata::aux_cpus::pin_current_thread(); +#if defined(_WIN32) + const HANDLE self = GetCurrentThread(); + if (conc_on_host && !host_cpu_sets.empty()) + SetThreadSelectedCpuSets(self, host_cpu_sets.data(), (ULONG) host_cpu_sets.size()); + if (conc_unpin && conc_on_host) SetThreadSelectedCpuSets(self, nullptr, 0); +#else + if (conc_unpin && !strata::aux_cpus::enabled()) (void) strata::aux_cpus::place_off_host(); +#endif + }; // the slots' doorbells are served on a thread of their own for the read, which makes no CUDA // call: a call here or on the read's threads can wait for the driver's lock while its holder // waits for the GPU (a free; a module the first kernel of a shape loads), and a window on that @@ -11710,7 +11760,7 @@ int main(int argc, char** argv) { bell_cv.notify_one(); }; bells.th = std::thread([&] { - strata::aux_cpus::pin_current_thread(); + conc_thread_cpus(); auto any_busy = [&] { for (int k = 0; k < n_pipe; ++k) if (stage_verifier(k).batch_busy()) return true; @@ -11762,7 +11812,7 @@ int main(int argc, char** argv) { in_prompt_read = true; sp.stop_at_chunk = [&] { return want_yield.load(std::memory_order_acquire) >= 0; }; std::thread reader([&] { - strata::aux_cpus::pin_current_thread(); + conc_thread_cpus(); try { // (an exception leaving a std::thread ends the process: a failed read instead) read_ok = sp.run(ids.data() + q0, b0 - q0, q0, read_err); } catch (const std::exception& x) {