Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

pool: STRATA_POOL_UMWAIT=<cycles> - parked workers wait with UMONITOR/UMWAIT instead of spinning (opt-in, x86 WAITPKG, MSVC x64)

zsaribogacaf1856bd985
diff --git a/src/kernels/cpu/pool.cpp b/src/kernels/cpu/pool.cpp
index af1eb5a59..840b4f818 100644
--- a/src/kernels/cpu/pool.cpp
+++ b/src/kernels/cpu/pool.cpp
@@ -9,6 +9,9 @@
#include <cmath>
#include <chrono>
#include <immintrin.h>
+#if defined(_MSC_VER)
+#include <intrin.h> // __cpuidex, __rdtsc (STRATA_POOL_UMWAIT)
+#endif
#include <cstdio>
#include <cstdlib>
@@ -27,6 +30,41 @@
namespace strata::kernels::cpu {
namespace {
+// STRATA_POOL_UMWAIT=<TSC cycles> (experimental, x86 WAITPKG only): a parked worker waits with UMONITOR/UMWAIT on the
+// epoch's cache line instead of spinning on _mm_pause. It wakes when the host publishes (a write to that line) or
+// after the cycles, in the light C0.1 state; on a laptop the CPU then gives back the power the spin took, which the
+// GPU's share of the budget gets. 0 or unset (or no WAITPKG): the spin, unchanged. STRATA_POOL_UMWAIT_C02=1: the
+// deeper C0.2 state (slower wake). MSVC x64 only for now (the intrinsics need no target flag there); other builds spin.
+uint64_t umwait_cycles() {
+ static const uint64_t c = [] {
+ const char* e = std::getenv("STRATA_POOL_UMWAIT");
+ if (e == nullptr || std::atoll(e) <= 0) return (uint64_t) 0;
+#if defined(_MSC_VER) && defined(_M_X64)
+ int r[4] = {0, 0, 0, 0};
+ __cpuidex(r, 7, 0);
+ if (((r[2] >> 5) & 1) == 0) {
+ std::fprintf(stderr, "strata: STRATA_POOL_UMWAIT: this CPU has no WAITPKG, the workers spin\n");
+ return (uint64_t) 0;
+ }
+ std::fprintf(stderr, "strata: STRATA_POOL_UMWAIT: parked workers wait with UMWAIT (%lld cycles)\n", std::atoll(e));
+ return (uint64_t) std::atoll(e);
+#else
+ return (uint64_t) 0;
+#endif
+ }();
+ return c;
+}
+unsigned umwait_state() {
+ static const unsigned s = [] { const char* e = std::getenv("STRATA_POOL_UMWAIT_C02"); return e && *e == '1' ? 0u : 1u; }();
+ return s; // 1 = C0.1 (light), 0 = C0.2
+}
+// STRATA_POOL_UMWAIT_ESC=<TSC cycles> (experimental): an escalating wait - C0.1 for the first cycles of a park, C0.2 after
+// (a wait that has lasted that long is a GPU phase, a draft or a prompt read, where a slower wake costs little).
+uint64_t umwait_escalate() {
+ static const uint64_t c = [] { const char* e = std::getenv("STRATA_POOL_UMWAIT_ESC"); return e ? (uint64_t) (std::max)(0LL, std::atoll(e)) : (uint64_t) 0; }();
+ return c;
+}
+
constexpr uint64_t pack_head(uint32_t epoch, uint32_t n, uint32_t i) {
return ((uint64_t) epoch << 32) | ((uint64_t) n << 16) | (uint64_t) i;
}
@@ -509,10 +547,27 @@ void ExpertPool::worker(int id) {
// every 1024 pauses, so the spin itself is unchanged.
const auto parked_at = std::chrono::steady_clock::now();
uint32_t spins = 0;
+ const uint64_t um = umwait_cycles();
+#if defined(_MSC_VER) && defined(_M_X64)
+ const uint64_t um_esc = umwait_escalate();
+ const uint64_t um_t0 = um != 0 && um_esc != 0 ? __rdtsc() : 0;
+#endif
while (epoch_.load(std::memory_order_acquire) == seen) {
if (stop_.load(std::memory_order_relaxed)) return;
- _mm_pause();
- if ((++spins & 1023u) != 0) continue;
+#if defined(_MSC_VER) && defined(_M_X64)
+ if (um != 0) { // STRATA_POOL_UMWAIT: wake on the publish's write to epoch_, or after `um` cycles
+ _umonitor((void*) &epoch_);
+ if (epoch_.load(std::memory_order_acquire) != seen) break;
+ const uint64_t now = __rdtsc();
+ const unsigned st = um_esc != 0 ? (now - um_t0 < um_esc ? 1u : 0u) : umwait_state();
+ _umwait(st, now + um);
+ if ((++spins & 15u) != 0) continue; // each wait is ~um cycles: look at the clock more often
+ } else
+#endif
+ {
+ _mm_pause();
+ if ((++spins & 1023u) != 0) continue;
+ }
if (std::chrono::steady_clock::now() - parked_at < spin_before_sleep_) continue;
std::unique_lock<std::mutex> lk(sleep_mu_);
wstate_[(size_t) id].store(kSleeping, std::memory_order_relaxed);