| 1821 | } |
| 1822 | |
| 1823 | void ExpertPool::run(ExpertJob* jobs, int n) { |
| 1824 | if (n <= 0) return; |
| 1825 | if (n_ == 1) { // no workers: run inline, so a single-core machine still produces a token |
| 1826 | for (int i = 0; i < n; ++i) s2_expert_vnni_q(jobs[i].blob, *jobs[i].act, jobs[i].out, scratch_[0]); |
| 1827 | return; |
| 1828 | } |
| 1829 | // Wait for every worker to be parked BEFORE touching the batch, so the publish below is the only thing |
| 1830 | // that can move a worker into the drain loop. |
| 1831 | // |
| 1832 | // THE THREE PHASES ARE TIMED SEPARATELY. They were one number, which cannot distinguish a pool that is |
| 1833 | // slow at the WORK from one that is slow at the SYNCHRONISATION - and those need opposite fixes. |
| 1834 | const auto t_a = std::chrono::steady_clock::now(); |
| 1835 | wait_parked("before a batch"); |
| 1836 | per_worker_ = false; |
| 1837 | const auto t_b = std::chrono::steady_clock::now(); |
| 1838 | jobs_ = jobs; |
| 1839 | njobs_ = n; |
| 1840 | mode_ = 0; |
| 1841 | const uint32_t e = begin_batch(n); // done, then head (release), then the epoch: the batch is described first |
| 1842 | |
| 1843 | // ---- **THE HOST DRAINS TOO (R2.2), INSTEAD OF SPINNING ON `done_`.** |
| 1844 | // |
| 1845 | // The loop below used to be `while (done_ != n) strata_cpu_pause();`. The host is pinned to core 0 - the core |
| 1846 | // `physical_cores(true)` deliberately keeps the five workers off - so for the whole drain that core was |
| 1847 | // idle while five cores did six cores' worth of work. Measured before the change: 33.7 GB/s against |
| 1848 | // 5/6 x 44.14 = 36.8 for five workers and 44.14 for six. |
| 1849 | // |
| 1850 | // The host claims through the SAME `head_` counter, so this is not a second scheduler and nothing about |
| 1851 | // the ordering changes: `head_` is a single `fetch_add`, every job is the same size, and a thread that |
| 1852 | // arrives late simply claims nothing. `done_` is still the completion signal and the host still waits for |
| 1853 | // it - what changed is only that the host arrives at that wait having done a share of the work. |
| 1854 | // |
| 1855 | // The host's `done_.fetch_add` is a release for the same reason a worker's is: `j.out` is read by the |
| 1856 | // device after `run()` returns, so the write must be published, not merely performed. |
| 1857 | if (host_works_) { |
| 1858 | for (;;) { |
| 1859 | const int ci = claim(e); |
| 1860 | if (ci < 0) break; |
| 1861 | hstate_.store(ci, std::memory_order_relaxed); |
| 1862 | hstate_ms_.store(now_ms(), std::memory_order_relaxed); |
| 1863 | const ExpertJob& j = jobs_[ci]; |
| 1864 | s2_expert_vnni_q(j.blob, *j.act, j.out, host_scratch_); |
| 1865 | done_.fetch_add(1, std::memory_order_release); |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | wait_done(n); |
| 1870 | // And park again, so the next `run` starts from a known state. See the header for why `done` alone is |
| 1871 | // not enough. |
| 1872 | const auto t_c = std::chrono::steady_clock::now(); |
| 1873 | wait_parked("after a batch"); |
| 1874 | hstate_.store(kIdle, std::memory_order_relaxed); |
| 1875 | hstate_ms_.store(now_ms(), std::memory_order_relaxed); |
| 1876 | const auto t_d = std::chrono::steady_clock::now(); |
| 1877 | |
| 1878 | ms_wait_park_ += std::chrono::duration<double, std::milli>(t_b - t_a).count(); |
| 1879 | ms_drain_ += std::chrono::duration<double, std::milli>(t_c - t_b).count(); |
| 1880 | ms_repark_ += std::chrono::duration<double, std::milli>(t_d - t_c).count(); |