diff --git a/.claude/knowledge/pm-stats-probe.md b/.claude/knowledge/pm-stats-probe.md index b8b0f2ab..842a2697 100644 --- a/.claude/knowledge/pm-stats-probe.md +++ b/.claude/knowledge/pm-stats-probe.md @@ -106,4 +106,17 @@ from existing words; no new primitive. - On AVX-512 vertical accumulation saves about 0.8 ns per 64 lanes (20–27%). - On AVX2 it is no better and usually worse: the `U16x32` widen and add are two halves each, which costs more than the `vpsadbw` it replaces. - So a vertical accumulator is an AVX-512 realization choice, not a portable rule. A polyfill word for it would need a per-backend body (on AVX2 the horizontal form stays). -- Not built: a `vpsadbw` that returns its `U64x8` partials (no such word exists), which would avoid the widening on both tiers. +- ⊘ CORRECTED 2026-11-25: this line said a `vpsadbw` that returns its `U64x8` partials did not exist. It does: `U64x8::sum8_u8_add_masked(a, keep)` (ndarray #407), on every backend, and it takes the selection mask directly, so no blend is needed either. + +### The existing masked SAD word (`sum_abs_sad`) + +Same population, same two runs per tier: + +| | horizontal | vertical (`U16x32`) | `sum8_u8_add_masked` | +|---|---|---|---| +| AVX-512, L2-resident | 2.94–2.97 | 2.15 | **1.25–1.33** | +| AVX-512, 16M lanes | 3.8–5.9 | 2.8–5.0 | **2.7–4.4** | +| AVX2, L2-resident | 6.28–6.31 | 7.8–7.9 | **4.16–4.17** | +| AVX2, 16M lanes | 11.3–12.0 | 16.3–17.9 | **6.0–7.5** | + +It is the fastest of the three on both tiers: 2.3× the horizontal form on AVX-512, 1.5× on AVX2, where the widening accumulator lost. So the portable rule is "accumulate with `sum8_u8_add_masked`", and the widening accumulator above is superseded. The disable run (negatives masked by `P` instead of `!P`) is red. diff --git a/examples/pm_stats/main.rs b/examples/pm_stats/main.rs index 79235053..caf302d0 100644 --- a/examples/pm_stats/main.rs +++ b/examples/pm_stats/main.rs @@ -233,7 +233,7 @@ fn main() { } println!("\n[1b] S and A together: horizontal per block vs vertical per tile, mixed, 50% selection"); - println!("{:<12} {:>10} {:>10}", "lanes", "horiz", "vertical"); + println!("{:<12} {:>10} {:>10} {:>10}", "lanes", "horiz", "vertical", "sad-word"); for blocks in [128usize, 1024, 262_144] { let d = data(blocks, 50, Mix::Mixed, 13); let h = fold_soa( @@ -248,8 +248,9 @@ fn main() { }, ); assert_eq!(sum_abs_vertical(&d.mags, &d.pos, &d.sel), (h.s, h.a)); + assert_eq!(sum_abs_sad(&d.mags, &d.pos, &d.sel), (h.s, h.a)); println!( - "{:<12} {:>10.2} {:>10.2}", + "{:<12} {:>10.2} {:>10.2} {:>10.2}", blocks * 64, time(blocks, || fold_soa( &d.mags, @@ -263,6 +264,7 @@ fn main() { } )), time(blocks, || sum_abs_vertical(&d.mags, &d.pos, &d.sel)), + time(blocks, || sum_abs_sad(&d.mags, &d.pos, &d.sel)), ); } diff --git a/examples/pm_stats/stats.rs b/examples/pm_stats/stats.rs index 3e8d4e2f..af86b62f 100644 --- a/examples/pm_stats/stats.rs +++ b/examples/pm_stats/stats.rs @@ -10,7 +10,7 @@ //! filter. use ndarray::hpc::abakus::pm::{touched, Pm}; -use ndarray::simd::{U16x32, U8x64}; +use ndarray::simd::{U16x32, U64x8, U8x64}; /// Sum of the magnitudes in `mask`'s lanes (`vpsadbw` on AVX-512). #[inline(always)] @@ -163,6 +163,20 @@ pub fn sum_abs_vertical(mags: &[u8], pos: &[u64], sel: &[u64]) -> (i64, u64) { (s, a) } +/// `S` and `A` with the existing masked SAD word: `U64x8::sum8_u8_add_masked` +/// keeps its eight partial sums, so nothing is widened and nothing reduced +/// until the end. Its lanes hold more than 9 × 10^15 calls, so there is no tile. +pub fn sum_abs_sad(mags: &[u8], pos: &[u64], sel: &[u64]) -> (i64, u64) { + let (mut all, mut neg) = (U64x8::splat(0), U64x8::splat(0)); + for (b, &m) in sel.iter().enumerate() { + let x = block_at(mags, pos, b); + all = all.sum8_u8_add_masked(x.mag, m); + neg = neg.sum8_u8_add_masked(x.mag, m & !x.pos); + } + let (a, n) = (all.reduce_sum(), neg.reduce_sum()); + (a as i64 - 2 * n as i64, a) +} + /// Population variance as an exact rational `(N·Q − S², N²)`; `None` for an /// empty population. Exact in `i128` for any population this probe folds. pub fn variance_parts(st: Stats) -> Option<(i128, i128)> { diff --git a/tests/pm_stats.rs b/tests/pm_stats.rs index 904c5526..541f546b 100644 --- a/tests/pm_stats.rs +++ b/tests/pm_stats.rs @@ -274,6 +274,7 @@ fn vertical_accumulation_matches_the_oracle() { } let want = oracle(&sel, &codes); assert_eq!(sum_abs_vertical(&mags, &pos, &sel), (want.s, want.a), "{n} blocks"); + assert_eq!(sum_abs_sad(&mags, &pos, &sel), (want.s, want.a), "sad {n} blocks"); } // Every lane at -255, fully selected, for a whole tile plus one block. let n = VERTICAL_TILE + 1; @@ -282,4 +283,5 @@ fn vertical_accumulation_matches_the_oracle() { let sel = vec![u64::MAX; n]; let a = (n * 64 * 255) as u64; assert_eq!(sum_abs_vertical(&mags, &pos, &sel), (-(a as i64), a)); + assert_eq!(sum_abs_sad(&mags, &pos, &sel), (-(a as i64), a)); }