Skip to main content

cortiq_engine/
pool.rs

1//! Persistent worker pool for row-parallel matvecs.
2//!
3//! Threads are spawned once and spin-then-park between calls — vmfcore
4//! measured spawn-per-matvec at ~+27% decode cost versus a persistent
5//! pool. Parallelism is by disjoint row ranges, so results are
6//! bit-identical to the serial path (each row's dot product is computed
7//! the same way).
8//!
9//! Dispatch is a single shared job slot + atomic epoch (roadmap §3 P0):
10//! the caller publishes one pointer, bumps the epoch and JOINS THE WORK
11//! as the extra worker instead of blocking on a latch. The previous
12//! design allocated an `Arc<Latch>` and pushed a message into every
13//! worker's mpsc channel for every matvec (~200 dispatches/token) —
14//! with decode-grade matvecs that synchronization was its own budget.
15//! Workers spin for `CMF_POOL_SPIN` iterations before parking.
16//! Default 4000: at ~39 dispatches/token, park-immediately pays the
17//! unpark syscall on every worker for every dispatch — measured on an
18//! M4 (interleaved A/B, current epoch dispatch + parked-flag design):
19//! Qwen-0.5B q8 decode 101→115 tok/s, q4t 117→149, the 50M bench model
20//! 549→954 at spin=4000 vs spin=0. An early measurement that showed
21//! spinning LOSING (−25% on q8) predates the parked-flag skip and the
22//! multi-matrix dispatch cuts; it no longer reproduces. Over-spinning
23//! still hurts (200k: −15% vs 4k — spinners steal the caller's serial
24//! cycles), so the budget stays bounded. `CMF_POOL_SPIN=0` restores
25//! park-immediately for share-the-box serving.
26//!
27//! `CMF_THREADS` env: 0/1 = serial, N = worker count
28//! (default: available_parallelism − 1, capped at 8).
29
30use std::cell::UnsafeCell;
31use std::sync::Arc;
32use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
33
34/// Embedder override for the pool size (C ABI `cortiq_set_threads`):
35/// 0 = unset, consult CMF_THREADS / topology as before. Read once at
36/// pool construction, so set it before the load.
37pub static FORCED_THREADS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
38
39/// Kernel thread ids of the CURRENT pool's workers (Android/Linux) —
40/// what ADPF's PerformanceHintManager needs to attribute work to the
41/// governor. Refilled on every pool construction; empty elsewhere.
42pub static WORKER_TIDS: std::sync::Mutex<Vec<i32>> = std::sync::Mutex::new(Vec::new());
43
44/// A `*const dyn Fn` that may cross a thread boundary. Safety is
45/// provided by `Pool::run`: the caller blocks until every worker has
46/// finished, so the borrow outlives all uses.
47#[derive(Clone, Copy)]
48struct TaskPtr(*const (dyn Fn(usize, usize) + Sync));
49unsafe impl Send for TaskPtr {}
50
51struct Inner {
52    /// Bumped once per published job; workers watch it.
53    epoch: AtomicUsize,
54    /// Workers still running the current job (excludes the caller).
55    remaining: AtomicUsize,
56    /// The published job: closure pointer + total participant count.
57    /// Written by the caller BEFORE the epoch bump, read by workers
58    /// AFTER they observe the new epoch (acquire/release pairing).
59    /// (task, worker count, publisher's GPU device, worker limit). The
60    /// device rides along because a dispatch begun on card 1 must not
61    /// finish on card 0: worker threads have their own thread-locals,
62    /// and the engine resolves its wgpu context through one. The limit
63    /// is how many workers PARTICIPATE: a job with eight grains has no
64    /// use for three hundred workers — the unpark syscalls and the
65    /// remaining-drain would BE the job (measured: 361 pool dispatches
66    /// per DeepSeek-V4 token, and CMF_THREADS=64 vs 380 was 1.3 vs 2.4
67    /// tok/s with no other change). Workers at or past the limit skip
68    /// the job entirely and never touch `remaining`.
69    slot: UnsafeCell<Option<(TaskPtr, usize, usize, usize)>>,
70    shutdown: AtomicBool,
71    /// Spin iterations before a worker parks (0 = park immediately).
72    spin_budget: usize,
73    /// Per-worker "I am parked" flags — lets the caller skip the unpark
74    /// syscall for workers that are still spinning.
75    parked: Box<[AtomicBool]>,
76}
77
78// SAFETY: `slot` is only written while no job is in flight (run()
79// returns after `remaining` hits 0) and only read after the epoch
80// publication that follows the write.
81unsafe impl Sync for Inner {}
82
83/// Process-wide dispatch counter (roadmap §3 P0 «измерения»): one tick
84/// per published job. `bench --json` reports dispatches/token from it.
85static DISPATCHES: AtomicUsize = AtomicUsize::new(0);
86
87/// Total pool jobs published since process start (all pools).
88pub fn dispatch_count() -> usize {
89    DISPATCHES.load(Ordering::Relaxed)
90}
91
92/// Persistent thread pool: shared job slot, epoch dispatch, caller
93/// participation.
94pub struct Pool {
95    inner: Arc<Inner>,
96    /// Thread handles for `unpark` (same order as `parked`).
97    threads: Vec<std::thread::Thread>,
98    joins: Vec<std::thread::JoinHandle<()>>,
99}
100
101fn spin_budget_from_env() -> usize {
102    std::env::var("CMF_POOL_SPIN")
103        .ok()
104        .and_then(|v| v.parse::<usize>().ok())
105        .unwrap_or(4000)
106}
107
108/// Rows per chunk: enough chunks to balance, large enough to keep the SDOT
109/// inner loop and the prefetcher in their stride — and never so coarse that
110/// ONE worker takes the whole job.
111///
112/// That last clause was missing. The floor was a flat 32, so any job with
113/// fewer than 32 rows went entirely to whichever worker grabbed the cursor
114/// first while the other 48 were woken, found nothing, and left. The
115/// hyper-connection projection has 24 rows and is called 86 times a token:
116/// it paid the full price of a fan-out and ran single-threaded.
117pub(crate) fn grain_for(rows: usize, workers: usize) -> usize {
118    if rows == 0 || workers <= 1 {
119        return rows.max(1);
120    }
121    let balanced = (rows / (workers * 8)).max(32);
122    // One chunk per worker at the very least.
123    balanced.min(rows.div_ceil(workers)).max(1)
124}
125
126impl Pool {
127    pub fn new(n_workers: usize) -> Self {
128        Self::with_spin(n_workers, spin_budget_from_env())
129    }
130
131    /// Explicit spin budget (tests pin it without touching the env).
132    pub fn with_spin(n_workers: usize, spin_budget: usize) -> Self {
133        let inner = Arc::new(Inner {
134            epoch: AtomicUsize::new(0),
135            remaining: AtomicUsize::new(0),
136            slot: UnsafeCell::new(None),
137            shutdown: AtomicBool::new(false),
138            spin_budget,
139            parked: (0..n_workers).map(|_| AtomicBool::new(false)).collect(),
140        });
141        let mut joins = Vec::with_capacity(n_workers);
142        if let Ok(mut tids) = WORKER_TIDS.lock() {
143            tids.clear();
144        }
145        for w in 0..n_workers {
146            let inner = inner.clone();
147            let h = std::thread::Builder::new()
148                .name(format!("cmf-pool-{w}"))
149                .spawn(move || {
150                    #[cfg(any(target_os = "android", target_os = "linux"))]
151                    if let Ok(mut tids) = WORKER_TIDS.lock() {
152                        tids.push(unsafe { libc::gettid() } as i32);
153                    }
154                    worker_loop(&inner, w)
155                })
156                .expect("spawn pool worker");
157            joins.push(h);
158        }
159        // Registration barrier: `spawn` returns before the closure runs,
160        // and the embedder reads `cortiq_worker_tids` right after load —
161        // on a phone only the first worker had registered by then (the
162        // '· 1 threads' About line that misled the cmfmobile device
163        // investigation twice). Thread start is milliseconds; wait for
164        // every tid before construction returns.
165        #[cfg(any(target_os = "android", target_os = "linux"))]
166        while WORKER_TIDS.lock().map(|t| t.len()).unwrap_or(n_workers) < n_workers {
167            std::thread::yield_now();
168        }
169        let threads = joins.iter().map(|h| h.thread().clone()).collect();
170        Self {
171            inner,
172            threads,
173            joins,
174        }
175    }
176
177    /// Big-core count on heterogeneous ARM (big.LITTLE): the kernel
178    /// exposes per-core capacity on Android and most ARM Linux; efficiency
179    /// cores in the pool DRAG the big ones on our row-parallel jobs (the
180    /// same cliff llama.cpp hits at -t 10 on an M4: 163 → 112 tok/s).
181    /// None = capacities absent or homogeneous.
182    #[cfg(all(
183        target_arch = "aarch64",
184        any(target_os = "linux", target_os = "android")
185    ))]
186    fn big_cores() -> Option<usize> {
187        Self::cores_from_capacities(&core_capacities())
188    }
189
190    /// How many cores the pool should use, from the kernel's per-core
191    /// capacity values. Capacity folds µarch × clock into one number,
192    /// and the two need different treatment: cores of ANOTHER µarch
193    /// (A5xx efficiency cluster next to A7xx/X: capacity ratio ≥ ~2)
194    /// drag row-parallel work down and are excluded; cores of the SAME
195    /// µarch merely clock-binned (JLQ JR510: 8×A55 as 4×2.0 + 4×1.5 GHz,
196    /// ratio 1.33) pull their weight and must ALL be used. The 1.6
197    /// threshold splits the two regimes: on a Snapdragon 8-class part
198    /// it keeps X + A7xx mid cores and drops A5xx.
199    #[cfg_attr(
200        not(all(
201            target_arch = "aarch64",
202            any(target_os = "linux", target_os = "android")
203        )),
204        allow(dead_code)
205    )]
206    fn cores_from_capacities(caps: &[u64]) -> Option<usize> {
207        let max = *caps.iter().max()?;
208        let min = *caps.iter().min()?;
209        if caps.len() < 2 || max == min {
210            return None;
211        }
212        Some(caps.iter().filter(|&&c| c * 8 >= max * 5).count())
213    }
214
215    #[cfg(target_os = "macos")]
216    fn big_cores() -> Option<usize> {
217        // Apple silicon: the P-only default measured WORSE than mixing the
218        // efficiency cores in — the grain-pulling dispatch absorbs the
219        // speed skew exactly as designed, and decode is memory-bound
220        // enough that E-cores add real serviceable work (M4, dense 3B:
221        // 4 threads 8.4 tok/s, 6-9 threads 9.6-10.7). Fall through to
222        // available_parallelism - 1; CMF_THREADS still pins by hand.
223        // The sysctl probe stays for introspection tooling.
224        if true {
225            return None;
226        }
227        #[allow(unreachable_code)]
228        unsafe extern "C" {
229            fn sysctlbyname(
230                name: *const std::ffi::c_char,
231                oldp: *mut std::ffi::c_void,
232                oldlenp: *mut usize,
233                newp: *mut std::ffi::c_void,
234                newlen: usize,
235            ) -> std::ffi::c_int;
236        }
237        unsafe {
238            let name = std::ffi::CString::new("hw.perflevel0.physicalcpu").ok()?;
239            let mut count: i32 = 0;
240            let mut size = std::mem::size_of::<i32>();
241            let ret = sysctlbyname(
242                name.as_ptr(),
243                &mut count as *mut i32 as *mut std::ffi::c_void,
244                &mut size,
245                std::ptr::null_mut(),
246                0,
247            );
248            if ret == 0 && count > 0 {
249                Some(count as usize)
250            } else {
251                None
252            }
253        }
254    }
255
256    #[cfg(not(any(
257        all(
258            target_arch = "aarch64",
259            any(target_os = "linux", target_os = "android")
260        ),
261        target_os = "macos"
262    )))]
263    fn big_cores() -> Option<usize> {
264        None
265    }
266
267    /// The thread count `from_env` would use RIGHT NOW: forced (C ABI)
268    /// > CMF_THREADS > big-core topology > available_parallelism−1.
269    /// > ≤1 means the model runs serial (no pool). Introspection
270    /// > (`execution_mode`, status endpoints) must report THIS, not
271    /// > available_parallelism.
272    pub fn effective_threads() -> usize {
273        let forced = FORCED_THREADS.load(std::sync::atomic::Ordering::Relaxed);
274        if forced > 0 {
275            return forced;
276        }
277        match std::env::var("CMF_THREADS") {
278            Ok(v) => v.parse::<usize>().unwrap_or(0),
279            Err(_) => match Self::big_cores() {
280                Some(big) => big,
281                None => {
282                    // The cap was 8, which left big machines idle: on a
283                    // 256-core EPYC, Nanbeige 4.2 decoded at 7.4 tok/s on
284                    // the default 8 threads and 14.8 at 32, with prefill
285                    // 12 -> ~16 over the same move. Past ~32 it falls off
286                    // hard (5.5 at 64, 1.6 at 256) — decode is
287                    // memory-bound and the extra threads only add
288                    // dispatch barriers — so 32 is a ceiling, not a
289                    // target. Machines with 9 cores or fewer are
290                    // unaffected: avail-1 already bounds them.
291                    let avail = std::thread::available_parallelism()
292                        .map(|n| n.get())
293                        .unwrap_or(1);
294                    avail.saturating_sub(1).min(32)
295                }
296            },
297        }
298    }
299
300    /// Pool sized from `CMF_THREADS` (see module docs). `None` = serial.
301    /// Without the env, heterogeneous ARM defaults to its BIG cores.
302    pub fn from_env() -> Option<Arc<Self>> {
303        let n = Self::effective_threads();
304        if n <= 1 {
305            None
306        } else {
307            Some(Arc::new(Self::new(n)))
308        }
309    }
310
311    /// Spawned worker threads (the caller joins each job on top).
312    pub fn n_workers(&self) -> usize {
313        self.threads.len()
314    }
315
316    /// Run `f(row_start, row_end)` over `0..rows`, self-balancing.
317    ///
318    /// One dispatch, but workers pull row-ranges from a shared cursor
319    /// instead of each taking a fixed 1/n slice. On a heterogeneous CPU
320    /// (Apple Silicon: 4 P-cores + 6 E-cores here) a static split makes
321    /// every matvec end at the SLOWEST core's pace while the fast ones
322    /// idle at the barrier; pulling by grain lets a P-core take several
323    /// chunks for each one an E-core takes, so skew collapses to a
324    /// single grain. Row ranges stay disjoint and each row's dot is
325    /// computed exactly as in the serial path → bit-identical output.
326    pub fn run_rows(&self, rows: usize, f: &(dyn Fn(usize, usize) + Sync)) {
327        let grain = grain_for(rows, self.threads.len() + 1);
328        let chunks = rows.div_ceil(grain.max(1));
329        let next = AtomicUsize::new(0);
330        self.run_limited(chunks, &|_w, _n| loop {
331            let start = next.fetch_add(grain, Ordering::Relaxed);
332            if start >= rows {
333                break;
334            }
335            f(start, (start + grain).min(rows));
336        });
337    }
338
339    /// `run`, waking at most `max_workers` workers. Same grain, same
340    /// row split, bit-identical results — only the number of threads
341    /// woken changes, so an 8-grain job stops paying 380 unparks. Only
342    /// cursor-style closures (which ignore their (idx, n) arguments)
343    /// come through here: the caller identifies itself as `limit`,
344    /// which under a cap is NOT `n_workers()`.
345    fn run_limited(&self, max_workers: usize, f: &(dyn Fn(usize, usize) + Sync)) {
346        let nw = self.threads.len().min(max_workers);
347        if nw == self.threads.len() {
348            return self.run(f);
349        }
350        DISPATCHES.fetch_add(1, Ordering::Relaxed);
351        let ptr: *const (dyn Fn(usize, usize) + Sync) = f;
352        let ptr: *const (dyn Fn(usize, usize) + Sync + 'static) =
353            unsafe { std::mem::transmute(ptr) };
354        let dev = crate::gpu::current_device();
355        // SAFETY: same contract as `run` — no job in flight, and the
356        // wait below outlives every borrow of `f`.
357        unsafe { *self.inner.slot.get() = Some((TaskPtr(ptr), nw + 1, dev, nw)) };
358        self.inner.remaining.store(nw, Ordering::Relaxed);
359        self.inner.epoch.fetch_add(1, Ordering::SeqCst);
360        for (i, t) in self.threads.iter().enumerate().take(nw) {
361            if self.inner.parked[i].load(Ordering::SeqCst) {
362                t.unpark();
363            }
364        }
365        f(nw, nw + 1);
366        let mut spins = 0usize;
367        while self.inner.remaining.load(Ordering::Acquire) != 0 {
368            spins += 1;
369            if spins < 10_000 {
370                std::hint::spin_loop();
371            } else {
372                std::thread::yield_now();
373            }
374        }
375    }
376
377    /// Multi-matrix job: one dispatch serves SEVERAL row spaces
378    /// (roadmap §3 P0 — «одна внешняя публикация job на слой»). Parts
379    /// are laid out back-to-back in a virtual row space and pulled by
380    /// grain from one shared cursor, so QKV or gate+up cost a single
381    /// barrier instead of one each. Each part's `f(start, end)` sees its
382    /// OWN row indices — per-row math and outputs are bit-identical to
383    /// separate `run_rows` calls.
384    pub fn run_many(&self, parts: &[(usize, &(dyn Fn(usize, usize) + Sync))]) {
385        let total: usize = parts.iter().map(|p| p.0).sum();
386        if total == 0 {
387            return;
388        }
389        let grain = grain_for(total, self.threads.len() + 1);
390        let chunks = total.div_ceil(grain.max(1));
391        let next = AtomicUsize::new(0);
392        self.run_limited(chunks, &|_w, _n| loop {
393            let s = next.fetch_add(grain, Ordering::Relaxed);
394            if s >= total {
395                break;
396            }
397            let e = (s + grain).min(total);
398            let mut base = 0usize;
399            for &(rows, f) in parts {
400                let a = s.max(base);
401                let b = e.min(base + rows);
402                if a < b {
403                    f(a - base, b - base);
404                }
405                base += rows;
406                if base >= e {
407                    break;
408                }
409            }
410        });
411    }
412
413    /// Run `f(worker_idx, n_participants)` on every worker AND the
414    /// calling thread (`worker_idx = n_workers()` for the caller);
415    /// returns when all participants have finished.
416    pub fn run(&self, f: &(dyn Fn(usize, usize) + Sync)) {
417        DISPATCHES.fetch_add(1, Ordering::Relaxed);
418        let nw = self.threads.len();
419        let n = nw + 1; // caller participates
420        // SAFETY: the wait loop below blocks until every worker is done,
421        // so extending the borrow to 'static never outlives the call.
422        let ptr: *const (dyn Fn(usize, usize) + Sync) = f;
423        let ptr: *const (dyn Fn(usize, usize) + Sync + 'static) =
424            unsafe { std::mem::transmute(ptr) };
425        // SAFETY: no job in flight (previous run() drained `remaining`),
426        // so the slot is not being read.
427        let dev = crate::gpu::current_device();
428        unsafe { *self.inner.slot.get() = Some((TaskPtr(ptr), n, dev, nw)) };
429        self.inner.remaining.store(nw, Ordering::Relaxed);
430        self.inner.epoch.fetch_add(1, Ordering::SeqCst);
431        for (i, t) in self.threads.iter().enumerate() {
432            if self.inner.parked[i].load(Ordering::SeqCst) {
433                t.unpark();
434            }
435        }
436
437        // The caller's share — the barrier costs nothing while there is
438        // real work to do.
439        f(nw, n);
440
441        // Wait for the stragglers (bounded by one worker's chunk).
442        let mut spins = 0usize;
443        while self.inner.remaining.load(Ordering::Acquire) != 0 {
444            spins += 1;
445            if spins < 10_000 {
446                std::hint::spin_loop();
447            } else {
448                std::thread::yield_now();
449            }
450        }
451    }
452}
453
454impl Drop for Pool {
455    fn drop(&mut self) {
456        self.inner.shutdown.store(true, Ordering::SeqCst);
457        for t in &self.threads {
458            t.unpark();
459        }
460        for h in self.joins.drain(..) {
461            let _ = h.join();
462        }
463    }
464}
465
466/// Per-core capacity: the kernel's `cpu_capacity` (µarch × clock) when
467/// EAS exposes it, else `cpufreq/cpuinfo_max_freq` — same cluster
468/// ordering, so the 62.5% big-core rule keeps working on EAS-less
469/// kernels (TUNING.md open item: pinning silently did nothing there).
470#[cfg(any(
471    target_os = "android",
472    all(target_arch = "aarch64", target_os = "linux")
473))]
474fn core_capacities() -> Vec<u64> {
475    let read_all = |leaf: &str| -> Vec<u64> {
476        let mut vals = Vec::new();
477        for cpu in 0.. {
478            let path = format!("/sys/devices/system/cpu/cpu{cpu}/{leaf}");
479            match std::fs::read_to_string(&path) {
480                Ok(v) => match v.trim().parse() {
481                    Ok(x) => vals.push(x),
482                    Err(_) => break,
483                },
484                Err(_) => break,
485            }
486        }
487        vals
488    };
489    let caps = read_all("cpu_capacity");
490    if caps.len() >= 2 {
491        return caps;
492    }
493    read_all("cpufreq/cpuinfo_max_freq")
494}
495
496#[cfg(target_os = "android")]
497fn pin_thread_to_big_cores() {
498    use std::mem;
499    let caps = core_capacities();
500    let max = caps.iter().copied().max().unwrap_or(0);
501    let min = caps.iter().copied().min().unwrap_or(0);
502
503    // Only pin if heterogeneous
504    if caps.len() < 2 || max == min {
505        return;
506    }
507
508    unsafe {
509        let mut set: libc::cpu_set_t = mem::zeroed();
510        for (i, &c) in caps.iter().enumerate() {
511            if c * 8 >= max * 5 {
512                libc::CPU_SET(i, &mut set);
513            }
514        }
515        libc::sched_setaffinity(0, mem::size_of::<libc::cpu_set_t>(), &set);
516    }
517}
518
519fn worker_loop(inner: &Inner, idx: usize) {
520    #[cfg(target_os = "android")]
521    pin_thread_to_big_cores();
522    // Apple silicon: ask for the performance cores. Threads spawned
523    // without a QoS class land on the efficiency cores when the
524    // scheduler feels like it — a user's video-VAE encode on an M4 sat
525    // on the E-cores at 100% with the P-cores asleep for 140 s (HF
526    // discussion #4). USER_INITIATED is the class an interactive tool's
527    // work belongs to; the ~4 P-cores then take the pool's grains.
528    #[cfg(target_os = "macos")]
529    unsafe {
530        libc::pthread_set_qos_class_self_np(libc::qos_class_t::QOS_CLASS_USER_INITIATED, 0);
531    }
532
533    // The pool is created at epoch 0; baseline MUST be 0, not a fresh
534    // epoch read — if the caller publishes a job before the OS actually
535    // starts this thread, reading the live epoch would adopt that job's
536    // epoch as "already seen", skip it, and deadlock the caller's wait.
537    let mut seen = 0usize;
538    loop {
539        // Wait for a new epoch: spin first (decode publishes the next
540        // matvec within microseconds), park only when idle for real.
541        let mut spins = 0usize;
542        loop {
543            let e = inner.epoch.load(Ordering::Acquire);
544            if e != seen {
545                seen = e;
546                break;
547            }
548            if inner.shutdown.load(Ordering::Relaxed) {
549                return;
550            }
551            if spins < inner.spin_budget {
552                spins += 1;
553                std::hint::spin_loop();
554            } else {
555                inner.parked[idx].store(true, Ordering::SeqCst);
556                // Re-check under SeqCst: the caller bumps the epoch
557                // BEFORE reading `parked`, so either it sees our flag
558                // (and unparks) or we see its epoch here — a missed
559                // wakeup is impossible. Spurious unparks just loop.
560                if inner.epoch.load(Ordering::SeqCst) == seen
561                    && !inner.shutdown.load(Ordering::Relaxed)
562                {
563                    std::thread::park();
564                }
565                inner.parked[idx].store(false, Ordering::SeqCst);
566            }
567        }
568        // SAFETY: the slot was written before the epoch bump we just
569        // observed (release/acquire), and stays valid until `remaining`
570        // drops to zero — which happens only after `f` returns below.
571        let (task, n, dev, limit) =
572            unsafe { (*inner.slot.get()).expect("job published with epoch") };
573        if idx >= limit {
574            // Not invited: a bounded dispatch (run_rows with few grains)
575            // counted only `limit` workers into `remaining`. Executing —
576            // or decrementing — here would corrupt the barrier.
577            continue;
578        }
579        let f = unsafe { &*task.0 };
580        crate::gpu::set_current_device(dev);
581        f(idx, n);
582        inner.remaining.fetch_sub(1, Ordering::AcqRel);
583    }
584}
585
586/// Row-parallel dense matvec: `out[o] = Σ_j w[o·in + j]·x[j]`.
587/// Bit-identical to the serial loop (row order does not change math).
588pub fn matvec_rows(pool: Option<&Pool>, w: &[f32], x: &[f32], out: &mut [f32]) {
589    let in_dim = x.len();
590    let out_dim = out.len();
591    debug_assert!(w.len() >= out_dim * in_dim);
592
593    let row_dot = |o: usize| -> f32 {
594        let row = &w[o * in_dim..(o + 1) * in_dim];
595        let mut sum = 0.0f32;
596        for j in 0..in_dim {
597            sum += row[j] * x[j];
598        }
599        sum
600    };
601
602    match pool {
603        Some(pool) if out_dim >= 256 => {
604            let out_addr = SendMut(out.as_mut_ptr());
605            let run_range = move |start: usize, end: usize| {
606                for o in start..end {
607                    unsafe { *out_addr.at(o) = row_dot(o) };
608                }
609            };
610            pool.run_rows(out_dim, &run_range);
611        }
612        _ => {
613            for (o, dst) in out.iter_mut().enumerate() {
614                *dst = row_dot(o);
615            }
616        }
617    }
618}
619
620/// Two-input row matvec: one pass over the weight rows serves BOTH
621/// inputs — CPU decode is memory-bound, so the second position costs a
622/// fraction of the first (this is where MTP speculative verify wins).
623/// Per-output accumulation order matches the single-input path exactly
624/// → bit-identical results.
625pub fn matvec_rows2(
626    pool: Option<&Pool>,
627    w: &[f32],
628    x1: &[f32],
629    x2: &[f32],
630    out1: &mut [f32],
631    out2: &mut [f32],
632) {
633    let in_dim = x1.len();
634    debug_assert_eq!(x2.len(), in_dim);
635    let out_dim = out1.len();
636    debug_assert_eq!(out2.len(), out_dim);
637    debug_assert!(w.len() >= out_dim * in_dim);
638
639    let row_dots = |o: usize| -> (f32, f32) {
640        let row = &w[o * in_dim..(o + 1) * in_dim];
641        let (mut s1, mut s2) = (0.0f32, 0.0f32);
642        for j in 0..in_dim {
643            s1 += row[j] * x1[j];
644            s2 += row[j] * x2[j];
645        }
646        (s1, s2)
647    };
648
649    match pool {
650        Some(pool) if out_dim >= 256 => {
651            let o1 = SendMut(out1.as_mut_ptr());
652            let o2 = SendMut(out2.as_mut_ptr());
653            let run_range = move |start: usize, end: usize| {
654                for o in start..end {
655                    let (s1, s2) = row_dots(o);
656                    unsafe {
657                        *o1.at(o) = s1;
658                        *o2.at(o) = s2;
659                    }
660                }
661            };
662            pool.run_rows(out_dim, &run_range);
663        }
664        _ => {
665            for o in 0..out_dim {
666                let (s1, s2) = row_dots(o);
667                out1[o] = s1;
668                out2[o] = s2;
669            }
670        }
671    }
672}
673
674/// `SendMut` for any element type — the sampler's sparse chain writes
675/// per-grain candidate lists.
676pub(crate) struct SendMutT<T>(*mut T);
677unsafe impl<T> Send for SendMutT<T> {}
678unsafe impl<T> Sync for SendMutT<T> {}
679impl<T> Clone for SendMutT<T> {
680    fn clone(&self) -> Self {
681        *self
682    }
683}
684impl<T> Copy for SendMutT<T> {}
685impl<T> SendMutT<T> {
686    #[inline]
687    pub(crate) fn new(p: *mut T) -> Self {
688        Self(p)
689    }
690    /// Same contract as `SendMut::at`: disjoint indices, pointee outlives
691    /// the joined dispatch.
692    #[inline]
693    pub(crate) fn at(self, i: usize) -> *mut T {
694        unsafe { self.0.add(i) }
695    }
696}
697
698#[derive(Clone, Copy)]
699pub(crate) struct SendMut(*mut f32);
700unsafe impl Send for SendMut {}
701unsafe impl Sync for SendMut {}
702
703impl SendMut {
704    /// The caller promises the threads it hands this to write disjoint
705    /// indices, and that the pointee outlives them.
706    #[inline]
707    pub(crate) fn new(p: *mut f32) -> Self {
708        Self(p)
709    }
710
711    /// Method receiver forces the closure to capture the whole (Sync)
712    /// wrapper, not the bare `*mut f32` field (edition-2021 precise capture).
713    #[inline]
714    pub(crate) fn at(self, i: usize) -> *mut f32 {
715        unsafe { self.0.add(i) }
716    }
717}
718
719#[cfg(test)]
720mod tests {
721    #[test]
722    #[cfg(any(target_os = "android", target_os = "linux"))]
723    fn worker_tids_registered_before_new_returns() {
724        // WORKER_TIDS is a process-global registry, and the test harness
725        // runs suites in parallel — other tests' pools add their tids to
726        // the same list (19 showed up on a 48-core box where the old
727        // `== 3` held on a laptop by timing luck). Assert on the DELTA:
728        // our pool's three workers must be there the moment new returns.
729        // Counting LENGTHS raced: a parallel suite dropping its pool
730        // shrinks the same registry between the two reads, and the delta
731        // goes negative through no fault of ours (this flake failed two
732        // releases). Compare SETS instead — removals elsewhere cannot
733        // take away tids that were not there before.
734        use std::collections::HashSet;
735        let before: HashSet<_> = super::WORKER_TIDS.lock().unwrap().iter().copied().collect();
736        let _p = super::Pool::new(3);
737        let after: HashSet<_> = super::WORKER_TIDS.lock().unwrap().iter().copied().collect();
738        let fresh = after.difference(&before).count();
739        assert!(
740            fresh >= 3,
741            "all worker tids must be visible the moment the pool exists \
742             (fresh {fresh}, before {}, after {})",
743            before.len(),
744            after.len()
745        );
746    }
747
748    #[test]
749    fn forced_threads_overrides_env_and_topology() {
750        use std::sync::atomic::Ordering;
751        super::FORCED_THREADS.store(3, Ordering::Relaxed);
752        let pool = super::Pool::from_env().expect("forced 3 → pool");
753        assert_eq!(pool.n_workers(), 3);
754        super::FORCED_THREADS.store(1, Ordering::Relaxed);
755        assert!(super::Pool::from_env().is_none(), "forced 1 → serial");
756        super::FORCED_THREADS.store(0, Ordering::Relaxed);
757    }
758
759    #[test]
760    fn capacity_split_clock_bins_vs_microarch() {
761        type P = super::Pool;
762        // JR510: all-A55, two clock bins — use every core.
763        assert_eq!(
764            P::cores_from_capacities(&[1024, 1024, 1024, 1024, 768, 768, 768, 768]),
765            Some(8)
766        );
767        // Classic big.LITTLE (A78 + A55) — big only.
768        assert_eq!(
769            P::cores_from_capacities(&[1024, 1024, 1024, 1024, 350, 350, 350, 350]),
770            Some(4)
771        );
772        // Three-tier flagship: X + A7xx mids stay, A5xx littles go.
773        assert_eq!(
774            P::cores_from_capacities(&[1024, 800, 800, 800, 800, 300, 300, 300]),
775            Some(5)
776        );
777        // Uniform: no signal, caller falls back.
778        assert_eq!(P::cores_from_capacities(&[1024; 8]), None);
779        assert_eq!(P::cores_from_capacities(&[]), None);
780    }
781
782    use super::*;
783
784    #[test]
785    fn parallel_matvec_equals_serial_bitexact() {
786        let (out_dim, in_dim) = (512, 64);
787        let w: Vec<f32> = (0..out_dim * in_dim)
788            .map(|i| (i as f32 * 0.013).sin())
789            .collect();
790        let x: Vec<f32> = (0..in_dim).map(|i| (i as f32 * 0.07).cos()).collect();
791
792        let mut serial = vec![0.0f32; out_dim];
793        matvec_rows(None, &w, &x, &mut serial);
794
795        let pool = Pool::new(4);
796        let mut parallel = vec![0.0f32; out_dim];
797        matvec_rows(Some(&pool), &w, &x, &mut parallel);
798
799        assert_eq!(serial, parallel, "row-parallel must be bit-identical");
800    }
801
802    #[test]
803    fn fused_pair_equals_two_singles_bitexact() {
804        let (out_dim, in_dim) = (300, 48);
805        let w: Vec<f32> = (0..out_dim * in_dim)
806            .map(|i| (i as f32 * 0.011).sin())
807            .collect();
808        let x1: Vec<f32> = (0..in_dim).map(|i| (i as f32 * 0.03).cos()).collect();
809        let x2: Vec<f32> = (0..in_dim).map(|i| (i as f32 * 0.09).sin()).collect();
810
811        let mut a1 = vec![0.0f32; out_dim];
812        let mut a2 = vec![0.0f32; out_dim];
813        matvec_rows(None, &w, &x1, &mut a1);
814        matvec_rows(None, &w, &x2, &mut a2);
815
816        for pool in [None, Some(Pool::new(3))] {
817            let mut b1 = vec![0.0f32; out_dim];
818            let mut b2 = vec![0.0f32; out_dim];
819            matvec_rows2(pool.as_ref(), &w, &x1, &x2, &mut b1, &mut b2);
820            assert_eq!(a1, b1, "fused lane 1 must be bit-identical");
821            assert_eq!(a2, b2, "fused lane 2 must be bit-identical");
822        }
823    }
824
825    #[test]
826    fn pool_survives_many_runs() {
827        let pool = Pool::new(3);
828        let counter = AtomicUsize::new(0);
829        for _ in 0..100 {
830            pool.run(&|_, _| {
831                counter.fetch_add(1, Ordering::Relaxed);
832            });
833        }
834        // 3 workers + the participating caller = 4 executions per run.
835        assert_eq!(counter.load(Ordering::Relaxed), 400);
836    }
837
838    #[test]
839    fn pool_wakes_after_park() {
840        // Force immediate parking (no spin) — the epoch/parked handshake
841        // must still never miss a wakeup.
842        let pool = Pool::with_spin(2, 0);
843        let counter = AtomicUsize::new(0);
844        for _ in 0..50 {
845            pool.run(&|_, _| {
846                counter.fetch_add(1, Ordering::Relaxed);
847            });
848            // Give workers time to actually park between jobs.
849            std::thread::sleep(std::time::Duration::from_micros(200));
850        }
851        assert_eq!(counter.load(Ordering::Relaxed), 150);
852    }
853
854    #[test]
855    fn worker_indices_are_distinct_and_cover_range() {
856        let pool = Pool::new(3);
857        let hits: Vec<AtomicUsize> = (0..4).map(|_| AtomicUsize::new(0)).collect();
858        for _ in 0..20 {
859            pool.run(&|widx, n| {
860                assert_eq!(n, 4);
861                hits[widx].fetch_add(1, Ordering::Relaxed);
862            });
863        }
864        for (i, h) in hits.iter().enumerate() {
865            assert_eq!(h.load(Ordering::Relaxed), 20, "participant {i} missed runs");
866        }
867    }
868}
869
870#[cfg(test)]
871mod grain_tests {
872    use super::grain_for;
873
874    #[test]
875    fn a_short_job_still_reaches_every_worker() {
876        // 24 rows, 49 workers: the old flat floor of 32 handed all 24 to the
877        // first worker and woke the rest for nothing.
878        assert_eq!(grain_for(24, 49), 1);
879        // Wide jobs keep the stride the SDOT loop wants.
880        assert_eq!(grain_for(4096, 49), 32);
881        assert_eq!(grain_for(32768, 49), 83);
882        // Degenerate shapes must not divide by zero or return zero.
883        assert_eq!(grain_for(0, 49), 1);
884        assert_eq!(grain_for(7, 1), 7);
885        assert!(grain_for(1, 49) >= 1);
886    }
887}