diff --git a/.claude/knowledge/pm-stats-probe.md b/.claude/knowledge/pm-stats-probe.md index c8566b61..b8b0f2ab 100644 --- a/.claude/knowledge/pm-stats-probe.md +++ b/.claude/knowledge/pm-stats-probe.md @@ -87,6 +87,23 @@ pack_saturate_u8}` (lane-order preserving), `abakus::pm::touched`, `I16x32::cmp_gt`. `masked_group_bounded_power_sums_u8` is a scalar keyed fold (`group_walk`, one row at a time), not a SIMD kernel. -Missing and worth naming: a vertical byte-sum accumulator. `sum_bytes_u64` -reduces horizontally on every call; a fold that kept the `U64x8` partials -across blocks would pay one horizontal reduction per tile. +## Vertical byte accumulation (follow-up, 2026-11-25) + +`sum_abs_vertical` widens each masked magnitude block into `U16x32` partials +and reduces horizontally once per 128 blocks (`2 × 255 × 128 = 65_280`; a +129-block tile wraps, and the disable run with 129 is red). It is composed +from existing words; no new primitive. + +`S` and `A` together, 50% selection, ns per 64 lanes, two runs: + +| | horizontal per block | vertical per tile | +|---|---|---| +| AVX-512, L2-resident | 2.97–3.56 | 2.15–2.73 | +| AVX-512, 16M lanes | 5.59–6.41 | 4.83–5.49 | +| AVX2, L2-resident | 7.6–9.6 | 8.0–10.9 | +| AVX2, 16M lanes | 7.7–11.0 | 9.7–16.6 | + +- 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. diff --git a/examples/pm_stats/main.rs b/examples/pm_stats/main.rs index 9a254bb7..79235053 100644 --- a/examples/pm_stats/main.rs +++ b/examples/pm_stats/main.rs @@ -232,6 +232,40 @@ 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"); + for blocks in [128usize, 1024, 262_144] { + let d = data(blocks, 50, Mix::Mixed, 13); + let h = fold_soa( + &d.mags, + &d.pos, + &d.sel, + Demand { + n: false, + s: true, + q: false, + a: true, + }, + ); + assert_eq!(sum_abs_vertical(&d.mags, &d.pos, &d.sel), (h.s, h.a)); + println!( + "{:<12} {:>10.2} {:>10.2}", + blocks * 64, + time(blocks, || fold_soa( + &d.mags, + &d.pos, + &d.sel, + Demand { + n: false, + s: true, + q: false, + a: true + } + )), + time(blocks, || sum_abs_vertical(&d.mags, &d.pos, &d.sel)), + ); + } + println!("\n[1] all four statistics (N, S, Q, A), mixed, 50% selection, by working set"); println!("{:<12} {:>10} {:>10}", "lanes", "fold", "i16"); for blocks in [1usize, 1024, 262_144] { diff --git a/examples/pm_stats/stats.rs b/examples/pm_stats/stats.rs index 27010fea..3e8d4e2f 100644 --- a/examples/pm_stats/stats.rs +++ b/examples/pm_stats/stats.rs @@ -136,6 +136,33 @@ pub fn fold_soa(mags: &[u8], pos: &[u64], sel: &[u64], d: Demand) -> Stats { st } +/// Blocks a `u16` vertical accumulator can absorb before it may wrap: each +/// block adds two bytes to every `u16` lane, and `2 × 255 × 128 = 65_280`. +pub const VERTICAL_TILE: usize = 128; + +/// `S` and `A` with vertical accumulation: masked magnitudes are widened into +/// `U16x32` partials and reduced horizontally once per [`VERTICAL_TILE`] +/// blocks, instead of once per block as [`fold_block`] does. +pub fn sum_abs_vertical(mags: &[u8], pos: &[u64], sel: &[u64]) -> (i64, u64) { + let z = U8x64::splat(0); + let hsum = |v: U16x32| v.to_array().iter().map(|&x| u64::from(x)).sum::(); + let (mut s, mut a) = (0i64, 0u64); + for (t, tile) in sel.chunks(VERTICAL_TILE).enumerate() { + let (mut all, mut neg) = (U16x32::splat(0), U16x32::splat(0)); + for (k, &m) in tile.iter().enumerate() { + let x = block_at(mags, pos, t * VERTICAL_TILE + k); + let ma = U8x64::mask_blend(m, z, x.mag); + let mn = U8x64::mask_blend(m & !x.pos, z, x.mag); + all += U16x32::from_u8x64_lo(ma) + U16x32::from_u8x64_hi(ma); + neg += U16x32::from_u8x64_lo(mn) + U16x32::from_u8x64_hi(mn); + } + let (ta, tn) = (hsum(all), hsum(neg)); + a += ta; + s += ta as i64 - 2 * tn as i64; + } + (s, 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 5b6f190d..904c5526 100644 --- a/tests/pm_stats.rs +++ b/tests/pm_stats.rs @@ -259,3 +259,27 @@ fn selection_density_extremes() { assert_eq!(fold(&xs, &sel, Demand::ALL), oracle(&sel, &codes)); } } + +/// The vertical accumulator agrees with the oracle, including across a tile +/// boundary and at the magnitude bound where a too-large tile would wrap. +#[test] +fn vertical_accumulation_matches_the_oracle() { + for (n, seed) in [(1usize, 2u64), (VERTICAL_TILE, 3), (3 * VERTICAL_TILE + 5, 4)] { + let (xs, sel, codes) = random_blocks(n, seed); + let mut mags = Vec::new(); + let mut pos = Vec::new(); + for x in &xs { + mags.extend_from_slice(&x.mag.to_array()); + pos.push(x.pos); + } + let want = oracle(&sel, &codes); + assert_eq!(sum_abs_vertical(&mags, &pos, &sel), (want.s, want.a), "{n} blocks"); + } + // Every lane at -255, fully selected, for a whole tile plus one block. + let n = VERTICAL_TILE + 1; + let mags = vec![255u8; n * 64]; + let pos = vec![0u64; n]; + 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)); +}