MCPcopy Create free account
hub / github.com/Niko1221/Strata / worker

Method worker

engine/src/kernels/cpu/pool.cpp:1126–1194  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1124}
1125
1126void ExpertPool::worker(int id) {
1127 uint32_t seen = 0;
1128 // ARRIVE at the park before the first wait, so `parked_ == n_` is true from construction. Counting only
1129 // on the RETURN from a drain leaves `parked_` at 0 until each worker has finished one batch, and the first
1130 // `run()` - which waits for `parked_ == n_` before publishing - then deadlocks. It deadlocks on the very
1131 // first call, which is the good case; a version that deadlocked on the second would be far worse.
1132 parked_of(id).fetch_add(1, std::memory_order_acq_rel);
1133 for (;;) {
1134 // Park: wait for work. `_mm_pause` rather than a bare spin because it yields the pipeline to the
1135 // sibling hyperthread; `epoch_` is bumped once per LAYER, not once per expert, so most of these
1136 // iterations are spent here with nothing to do.
1137 //
1138 // **AND NOTHING ELSE HAPPENS IN HERE.** This loop used to do `pauses_.fetch_add(1)` on every iteration
1139 // - a locked read-modify-write, five workers against one cache line - so the workers spent their wait
1140 // invalidating each other's caches and the very line the host writes to publish work. The counter was
1141 // diagnostic and nothing branched on it. See the note on the atomics in pool.hpp.
1142 //
1143 // After `kSpinBeforeSleep` with no work the worker sleeps instead (issue #4). The clock is read once
1144 // every 1024 pauses, so the spin itself is unchanged.
1145 const auto parked_at = std::chrono::steady_clock::now();
1146 uint32_t spins = 0;
1147 const uint64_t um = umwait_cycles();
1148#if defined(_MSC_VER) && defined(_M_X64) && !defined(__clang__) // MSVC proper: clang-cl (the Windows HIP build) has no WAITPKG intrinsics by default
1149 const uint64_t um_esc = umwait_escalate();
1150 const uint64_t um_t0 = um != 0 && um_esc != 0 ? __rdtsc() : 0;
1151#endif
1152 while (epoch_.load(std::memory_order_acquire) == seen) {
1153 if (stop_.load(std::memory_order_relaxed)) return;
1154#if defined(_MSC_VER) && defined(_M_X64) && !defined(__clang__) // MSVC proper: clang-cl (the Windows HIP build) has no WAITPKG intrinsics by default
1155 if (um != 0) { // STRATA_POOL_UMWAIT: wake on the publish's write to epoch_, or after `um` cycles
1156 _umonitor((void*) &epoch_);
1157 if (epoch_.load(std::memory_order_acquire) != seen) break;
1158 const uint64_t now = __rdtsc();
1159 const unsigned st = um_esc != 0 ? (now - um_t0 < um_esc ? 1u : 0u) : umwait_state();
1160 _umwait(st, now + um);
1161 if ((++spins & 15u) != 0) continue; // each wait is ~um cycles: look at the clock more often
1162 } else
1163#endif
1164 {
1165 strata_cpu_pause();
1166 if ((++spins & 1023u) != 0) continue;
1167 }
1168 if (std::chrono::steady_clock::now() - parked_at < spin_before_sleep_) continue;
1169 std::unique_lock<std::mutex> lk(sleep_mu_);
1170 wstate_[(size_t) id].store(kSleeping, std::memory_order_relaxed);
1171 sleepers_.fetch_add(1, std::memory_order_seq_cst);
1172 sleep_cv_.wait(lk, [&] {
1173 return epoch_.load(std::memory_order_seq_cst) != seen || stop_.load(std::memory_order_relaxed);
1174 });
1175 sleepers_.fetch_sub(1, std::memory_order_relaxed);
1176 wstate_[(size_t) id].store(kParked, std::memory_order_relaxed);
1177 }
1178 if (stop_.load(std::memory_order_acquire)) return;
1179 // acquire: the batch this epoch published (`head`, and the description before it) is visible from here
1180 seen = epoch_.load(std::memory_order_acquire);
1181 if (diag_pool_on() && mode_ >= 5)
1182 dg_w_[(size_t) id].wake.fetch_add(now_ns() - dg_pub_ns_.load(std::memory_order_relaxed), std::memory_order_relaxed);
1183 parked_of(id).fetch_sub(1, std::memory_order_acq_rel); // leaving the park

Callers

nothing calls this directly

Calls 13

strata_cpu_pauseFunction · 0.85
diag_pool_onFunction · 0.85
fetch_addMethod · 0.80
fetch_subMethod · 0.80
nowFunction · 0.70
umwait_cyclesFunction · 0.70
umwait_escalateFunction · 0.70
umwait_stateFunction · 0.70
now_nsFunction · 0.70
drainFunction · 0.50
loadMethod · 0.45
storeMethod · 0.45

Tested by

no test coverage detected