T-011 — Spike: can the modal strings run in real time, and with which precision and SIMD?
Status: done · Area: rust, piano · Depends on: T-003
Read for that: Piano 13 (key states, complex one-pole, cost estimate, SIMD options), P-004.
What: A throwaway benchmark in spikes/modal-bench/ (workspace member, not shipped) — no fitted data needed, use synthetic partial tables (inharmonic from curves, plausible decays):
-
Precision: the 27.5 Hz / 20 s T60 test in
f32andf64, complex one-pole vs textbook two-pole, at 44.1/48/96 kHz: frequency error (cents) and decay error after 20 s. -
Throughput with criterion: banks of N complex one-poles (N = 1k … 40k) in SoA layout, for (a) plain loops relying on auto-vectorisation, (b)
wide, (c)pulp;f32, andf64for the lowest octave only. -
Scenario: 88 key states with realistic partial counts, 50 active keys with 2 strings × 2 polarisations, 48 kHz, block 64: real-time factor on one core.
-
Raspberry Pi 3 (aarch64, NEON; Standalone and remote): the same scenario, on one core and on four; which quality level (partials per string cap) fits with headroom. If no Pi is at hand in the session, cross-build the benchmark and leave the run to the maintainer, noting the command here.
-
Calibrate the cost model: measure the scenario of Piano 14 at each quality level; update
tools/piano_cost_model.py(FLOPS_PER_RES, assumptions) and the estimates on Piano 14 with measured numbers.
Done when: a results table on this page (CPU model, numbers, chosen option); Piano 13 and P-004 updated with the decision (precision rule, SIMD option, whether T-024 multithreading is needed, quality levels the Pi needs). The spike code may then be deleted or kept as a benchmark.
Results
Code: spikes/modal-bench (workspace member, publish = false; kept as a benchmark). Run:
cargo run --release -p modal-bench --bin precisioncargo run --release -p modal-bench --bin scenariocargo bench -p modal-bench --bench banksThe scenario binary prints all tables below as markdown (about 2 minutes; -- --quick for a shorter run); banks is the same throughput measurement under criterion. The RUSTFLAGS were not changed: the build uses the baseline x86-64 target (SSE2), as a distributed plugin would, and everything faster comes from runtime dispatch.
Machine: cloud VM, Intel Xeon @ 2.1 GHz (family 6 model 207, Emerald Rapids; AVX-512), 4 vCPUs, shared host, so numbers vary by ±20 % from run to run. Rust 1.100.1, release profile (thin LTO, one codegen unit). All runs with FTZ/DAZ set (see Subnormals).
1. Precision (27.5 Hz, T60 20 s)
f32, error after the run; f64 is exact to < 1e-6 in every form (not shown).
| Form | fs (kHz) | Frequency (cent) | Amplitude after 20 s | T60 | Amplitude after 60 s |
|---|---|---|---|---|---|
| complex one-pole, stored | 44.1 / 48 / 96 | ≤ 0.0001 | +0.9 % / +2.4 % / −5.1 % | +0.13 % / +0.35 % / −0.76 % | +2.8 % / +7.5 % / −14.7 % |
| complex one-pole, delta form ( stored) | 44.1 / 48 / 96 | ≤ 0.0001 | ≤ 0.003 % | ≤ 0.001 % | ≤ 0.006 % |
| two-pole (textbook) | 44.1 / 48 / 96 | −1.9 / −1.6 / +10.1 | −0.3 % / +1.5 % / −2.0 % | ≤ 0.3 % | ≤ 4.5 % |
- The frequency of the complex one-pole is exact in
f32, as Piano 13 predicted; the two-pole misses A0 by 1.6–10 cents, all from rounding (the coefficient error column of the binary says the same). - The decay of the plain complex one-pole is not: near DC, and
f32resolves numbers near 1 only to , so the decay rate is off by up to 0.8 % at 96 kHz — 5 % of amplitude after 20 s, which breaks the 1 % rule, although it is inaudible (a T60 error of 0.8 %). - The delta form stores the small number with full relative precision and is as accurate as
f64. With FMA it costs the same: is two FMAs, as is in the pole form (measured below: equal within noise at 4 vectors in flight).
2. Throughput of one bank (ns per resonator-sample, one core, block 64)
Sustain kernel (no excitation); a resonator-sample is one partial advanced by one sample. “Baseline” = compiled for SSE2, no dispatch; “AVX2”/“AVX-512” = selected at run time by pulp. L: partials per group in the plain loops; K: vectors in flight.
| Kernel | N = 1k | 4k | 10k | 40k | Resonators in real time (N = 10k) |
|---|---|---|---|---|---|
| plain loops, L=8 · baseline | 0.33 | 0.29 | 0.29 | 0.29 | 71k |
| plain loops, L=16 · baseline | 0.21 | 0.20 | 0.27 | 0.33 | 76k |
| plain loops + pulp dispatch, L=16 · AVX2 | 0.21 | 0.30 | 0.19 | 0.19 | 111k |
| plain loops + pulp dispatch, L=16 · AVX-512 | 0.20 | 0.20 | 0.20 | 0.20 | 106k |
wide f32x8, K=2 · baseline | 0.16 | 0.16 | 0.16 | 0.16 | 131k |
| pulp, K=1 · AVX-512 | 0.11 | 0.10 | 0.11 | 0.10 | 199k |
| pulp, K=4 · AVX2 | 0.14 | 0.12 | 0.10 | 0.10 | 203k |
| pulp, K=4 · AVX-512 | 0.067 | 0.064 | 0.061 | 0.080 | 343k |
| pulp delta form, K=4 · AVX2 | 0.11 | 0.10 | 0.10 | 0.11 | 202k |
| pulp delta form, K=4 · AVX-512 | 0.072 | 0.067 | 0.066 | 0.066 | 315k |
pulp, K=2, f64 · AVX-512 | 0.22 | 0.20 | 0.20 | 0.20 | 103k |
pulp, K=2, f64 · AVX2 | 0.31 | 0.31 | 0.31 | 0.31 | 67k |
- With excitation (hammer contact) the pulp kernels cost about the same; the plain loops at baseline get 4× slower.
- The plain loops are latency-bound: every sample of a group depends on the previous one, so a group needs several independent vectors in flight to fill the FMA pipelines; the compiler does not reorder across the groups, and wider groups spill registers. The explicit pulp kernel with K = 4 vectors (64 partials on AVX-512, 32 on AVX2) is 3× faster than the plain loops and 2.5× faster than
wideat the baseline target. f64costs 2–3×f32.
3. Scenario: 50 keys, 2 strings × 2 polarisations
50 keys spread over A0..C8, every partial below 10 kHz (Piano 14’s partial counts), 2 strings × 2 polarisations each: 8,840 resonators, 48 kHz, block 64, hammer pulses every second per key. Real-time factor (RTF) = wall time / audio time.
| Kernel | RTF 1 core | 2 threads | 4 threads |
|---|---|---|---|
| plain loops, L=8 · baseline | 0.137 | 0.071 | 0.039 |
wide f32x8, K=2 · baseline | 0.094 | – | – |
| pulp, K=4 · AVX2 | 0.055 | 0.031 | 0.019 |
| pulp, K=4 · AVX-512 | 0.028 | 0.025 | 0.019 |
| pulp delta form, K=4 · AVX-512 | 0.040 | 0.024 | 0.018 |
pulp, K=4, lowest octave (A0..G#1) in f64 · AVX-512 | 0.057 | – | – |
So the 50-key case of Piano 13’s estimate uses 3–6 % of one core. Threads split the keys by cost and meet at a spinning barrier after every block; at this load the barrier, not the work, limits the speed-up.
5. Piano 14’s quality levels (dense pedalled passage)
40 keys over C2..C6, sustain pedal down, every other key sympathetic; “average” cuts each struck key to the level’s sustain fraction (lowest partials kept), as the cost model does. Resonator counts equal tools/piano_cost_model.py’s (a unit test checks it). Kernel: pulp, K=4, delta form; RTF on one core (best of 5 runs).
| Level | Resonators, peak / average | AVX-512, peak / average | AVX2, peak / average | ns per resonator-sample (AVX-512, peak) |
|---|---|---|---|---|
| Ultra | 11,822 / 6,268 | 0.060 / 0.041 | 0.081 / 0.041 | 0.11 |
| High | 4,916 / 2,167 | 0.024 / 0.024 | 0.061 / 0.028 | 0.10 |
| Medium | 2,683 / 967 | 0.023 / 0.018 | 0.024 / 0.014 | 0.18 |
| Low | 1,030 / 312 | 0.014 / 0.010 | 0.014 / 0.014 | 0.28 |
On 4 threads Ultra’s peak drops to 0.019–0.026. At small loads the time per resonator rises (padding of short treble banks to 16, per-key overhead), which matters little because the total is small. The plain loops at baseline need 3× the time (Ultra peak 0.18).
Subnormals
Decaying states pass through subnormal numbers; without flush-to-zero the CPU takes a slow path for each operation. 10k partials held at , ns per resonator-sample:
| Kernel | FTZ/DAZ off | FTZ/DAZ on |
|---|---|---|
| plain loops, L=8 · baseline | 39.2 | 0.29 |
| pulp, K=4 · AVX2 | 19.5 | 0.11 |
| pulp, K=4 · AVX-512 | 11.1 | 0.05 |
130–220× slower. Culling at −90 dB removes most partials before they get there, but not all (a long release, sympathetic banks), so the audio thread must set FTZ/DAZ (x86: MXCSR bits 15 and 6; aarch64: FPCR.FZ), per thread, restoring them on return. modal_bench::ftz does it with inline asm!.
4. Raspberry Pi 3 — measured 2026-12-05
Run on the maintainer’s Pi 3 Model B Rev 1.2 (4 × Cortex-A53 @ 1.2 GHz, 1 GB RAM, Raspberry Pi OS, aarch64, idle) with the binaries from T-028’s cloud session (target/release/scenario, precision; baseline aarch64 target, pulp dispatches to NEON). The scenario takes ~25 min on the Pi (the cloud VM’s 2). The precision binary reproduces the table in §1 exactly — IEEE f32 rounding is not CPU-specific — so the delta-form decision is confirmed on aarch64.
Bank throughput (ns per resonator-sample, one core, block 64; the Pi’s table condensed to the kernels that matter, N = 10k):
| Kernel | ns/res-sample | Resonators in real time |
|---|---|---|
| plain loops, L=16 · baseline | 2.25 | 9.3k |
wide f32x8, K=2 · baseline | 2.31 | 9.0k |
| pulp, K=4 · NEON | 2.25 | 9.3k |
| pulp delta form, K=4 · NEON | 2.32 | 9.0k |
pulp, K=2, f64 · NEON | 5.5 | 3.8k |
- ~2.25 ns per resonator-sample, ~33× the AVX-512 cloud core (0.067 ns). The old ~5× projection was derived from the Pi 5’s A76 (2.4 GHz, two FMA pipes) and is wrong for the in-order A53 @ 1.2 GHz;
NS_PER_RES_SAMPLEintools/piano_cost_model.pyis updated. - The plain loops match the explicit pulp kernel (2.25 vs 2.25): the in-order A53 is latency-bound either way, so pulp’s advantage is an out-of-order-core thing. Pulp stays — one kernel everywhere, and it is what the engine uses. More vectors in flight do not help either (T-055 part A: delta pulp K=6 4.02, K=8 4.39 ns per resonator-sample — probably register spills, not checked in the disassembly; K = 4 stays).
- Subnormals are free on the A53: with FTZ/DAZ off vs on, 2.437 vs 2.439 ns (plain) and 2.282 vs 2.277 (pulp) — no x86-style slow path. The audio thread still sets FPCR.FZ (harmless, and required on x86).
Scenario and quality levels (pulp, delta form, K=4; RTF, best of the kernel variants):
| Case | RTF 1 core | 2 threads | 4 threads |
|---|---|---|---|
| 50-key scenario (8,840 resonators) | 1.05 | 0.52 | 0.30 |
| Ultra peak / average | 1.43 / 0.80 | – | 0.40 / 0.23 |
| High peak / average | 0.65 / 0.34 | – | 0.18 / 0.10 |
| Medium peak / average | 0.37 / 0.17 | – | 0.11 / 0.05 |
| Low peak / average | 0.16 / 0.09 | – | 0.04 / 0.03 |
- Clock and throttling were not logged during that run; T-055 part A re-ran the levels in 2026-12-05 with a clock log (
scripts/pi-clock-log.sh): the Pi stayed at 1.2 GHz,get_throttled = 0x0throughout (peak 62.3 °C) — the numbers above are confirmed, none needed correcting. - The 50-key worst case is one full core. No level above High runs on one core at peak, and Ultra’s peak needs 1.4 cores — a single audio thread cannot do Ultra on the Pi. On four threads every level fits (Ultra peak 0.40), but one core is lost to the OS and the audio callback anyway.
- Consequences: T-024 (multithreading) is needed on the Pi for Ultra; without it the bare banks allow High (peak 65 %, average 34 % of one core) — but the whole engine costs ~2× per resonator (T-055), which leaves Medium as the highest single-thread level. That default is T-028 step 4’s decision.
Decision
- Precision: complex one-pole in
f32, delta form (store , update ). It meets the A0 rule at 44.1/48/96 kHz with a margin of 100×; nof64octave is needed (it would cost 2× there). - SIMD: pulp with runtime dispatch (feature
x86-v4on, so AVX-512 is used where present; AVX2+FMA otherwise; NEON on aarch64), an explicit kernel with K = 4 vectors in flight per block. Plain loops andwideare 2.5–3× slower at the baseline target;widecannot dispatch at run time. - Threads: not needed on the desktop: Ultra’s peak takes 6–8 % of one core. The Pi run fell far short of the old projection: T-024 (multithreading) is needed on the Pi for Ultra (peak 1.4 cores, 0.40 on four threads); without threads the bare banks cap at High (peak 0.65 of one core), the whole engine at Medium (T-055).
- Quality levels: on the desktop the strings are no longer the cost driver. The levels stay as quality settings (and for the Pi, the soundboard and the coupled hammer), not as a CPU necessity on the desktop — but on the Pi they are one: Medium or lower without T-024 (whole engine, T-055).
- Learned on the way, for T-008: (1) inside pulp’s
with_simd, helper code must be generic#[inline(always)]fns, not closures called twice — otherwise it is not inlined into the#[target_feature]context and every SIMD op becomes a call (measured: 25× slower); (2) pulp’s AVX-512 path needs thex86-v4feature; (3) set FTZ/DAZ on the audio thread.