…t_by_rank (#10168)
## Summary
Follow-up to #10138. Without BMI2, `intersect_by_rank` deposits the rank
bits with a loop over the set bits of each `self` chunk. That covers
every aarch64 build, plus x86 without BMI2. On masks that don't repeat,
the loop mispredicts about once per chunk and runs 3 to 8 times slower
than PDEP.
This PR uses SVE2 BDEP where the CPU has it and a branch-free deposit
elsewhere:
- **SVE2 BDEP** on Neoverse N2 and V2 (Graviton4, Cobalt 100, Axion),
detected at runtime. It is used for the deposit and for the select of
the mask-driven path, the same way BMI2 is used on x86. The kernels are
compiled with SVE2 enabled as a whole, so BDEP inlines into the loop.
Without that it costs a call and a register reload per chunk, which I
found in the assembly rather than in the design.
- **Portable network** otherwise. Masks with at most two set bits or at
most two clear bits get closed forms. For the rest, each byte takes its
source bits from the count of mask bits below it (one multiply sums
them), and inside each byte the compress network of Hacker's Delight 7-4
runs backwards. That is the `uN::extract_bits` scheme from core, with
three stages instead of six.
- **Loop below 1/16 density of `self`**, where a chunk has about four
set bits. The first version used the network everywhere and lost to the
old loop on every sparse bench, so the loop is back for sparse `self`,
chosen once per call, and now stops when the source bits run out.
The BMI2 path is unchanged.
Median µs before / after on Neoverse N2, and the speedup of the portable
path on Apple M1:
| | N2, BDEP | N2, portable | M1, portable |
|---|---|---|---|
| random, self 50 %, mask 50 % | 151 / 13.0 (11.7x) | 151 / 55.8 (2.7x)
| 3.8x to 4.6x |
| random, self 90 %, mask 90 % | 240 / 14.6 (16.5x) | 240 / 57.5 (4.2x)
| 5.4x to 6.2x |
| random, self 2 %, mask 50 % | 34.4 / 10.4 (3.3x) | 34.4 / 20.1 (1.7x)
| 1.7x to 2.4x |
| random, self 5 %, mask 50 % | 39.9 / 11.5 (3.5x) | 39.9 / 44.4 (0.90x)
| 0.79x to 0.94x |
| random, self 50 %, mask 2 % | 67.2 / 31.9 (2.1x) | 66.5 / 66.6 (1.0x)
| 0.9x to 1.16x |
| `density_matrix` dense | 24.8 / 4.6 (5.4x) | 24.8 / 19.8 (1.26x) |
1.5x to 1.9x |
| `density_matrix` self sparse | 7.3 / 3.9 (1.85x) | 7.3 / 7.8 (0.93x) |
0.92x to 1.19x |
| `intersect_by_rank` runs | 3.82 / 4.13 (0.92x) | 3.82 / 4.94 (0.77x) |
0.73x to 1.0x |
| `very_sparse_mask_cached` | 1.2x to 1.4x | unchanged | unchanged |
`random_rotating` is the bench from #10138, with extra densities added
for this measurement. The runners are GitHub's `ubuntu-24.04-arm`
(Cobalt 100) and `macos-15` (a virtualized M1), with two rounds per run.
For the portable column on N2 the portable path is forced. The M1
numbers are ranges over several runs, because a virtual M1 has opinions.
I also tried HD 7-5 through PMULL, on the theory that the Apple cores
love it. The M1 ran it at the speed of the network, so it isn't in this
PR.
Where it's slower:
- **Runs of 64 set and 64 clear bits:** 0.77x to 0.92x. Aligned blocks
of eight empty or full chunks skip the deposit, but the old per-chunk
shortcuts were cheaper.
- **The other benches' masks:** they come from `(i * 7 + 13) % 1024`,
repeat every 16 chunks, and the predictor learns the old loop on them.
With a sparse mask over a dense `self`, the portable path stays on the
network and runs 0.67x to 0.9x. With random masks at the same densities
it's 2.9x to 5.4x faster, so I didn't also pick the kernel by mask
density.
- **Random `self` at 5 %:** about 0.9x, just above the crossover. The
threshold is set for the M1, since N2 takes BDEP anyway.
---------
Signed-off-by: mightsleep <acannix@proton.me>
Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Summary
Adds
random_rotatingto theintersect_by_rankbenchmarks: independent random bits, and a different pair of masks on every iteration.The existing cases use periodic masks (
(i * 7 + 13) % 1000) and one pair per benchmark. A branch predictor learns such a pair within a few iterations, so on real hardware they measure the learned state. On mixed masks the portable kernel spends most of its time in the mispredictions this hides: on Neoverse N2, 50/50 random masks cost 35.9 ns per word.The new cases rotate through 8 pairs of 256K rows. That is 32K words of
self, twice the repeated sequence Zen 5 learns in my measurements, in about 0.5 MiB, inside L2 on the walltime legs.They carry
#[cpu_features].intersect_by_rankpicks its kernel at runtime, so the x86 legs measure the BMI2 kernel and the NEON leg the portable one. This gives a walltime baseline for the portable kernel ahead of a follow-up that rewrites it.The existing cases are unchanged, so their CodSpeed history is kept.
Checks
cargo clippy -p vortex-mask --benches -- -D warningscargo fmt -p vortex-mask --checkcargo bench -p vortex-mask --bench intersect_by_rank -- random_rotatingon Zen 5 (BMI2 kernel): 14 to 41 µs per iteration.