Skip to content

[meta] Wave2D perf pipeline — autoresearch benchmark + sanity calibration + PSRAM storage + algorithmic follow-ups #3113

Open

Description

@zackees

Background

The audit family #3073 / closed meta #3098 produced an optimized scalar WaveSimulation2D_Real kernel (PRs #3083, #3099, #3101, #3103, #3104, #3105, #3110). The research doc landed in PR #3112 (agents/docs/research/wave_pde_alternatives.md) identified 5 algorithmic follow-ups that should beat SIMD on the cost/quality axis.

Everything past this point is bottlenecked on measurement. Optimizing further without a trustworthy per-platform performance benchmark means we'd be picking algorithms based on theory rather than against-hardware data — exactly the failure mode the research doc warns about for SIMD work.

This meta tracks getting that benchmark in place, capturing baselines, and then sequencing the algorithmic follow-ups against measured wins.

PSRAM concern that motivated this meta

The current WaveSimulation2D_Real stores its two i16 grids in fl::vector_psram<i16>, which on ESP32-S3 means PSRAM-backed storage. That's a design decision that needs validation:

Platform Cache PSRAM throughput (practical) Notes
ESP32-S3 no L2 ~50–80 MB/s (octal SPI PSRAM) Per-access penalty ~80 ns. No prefetcher to mask.
ESP32-P4 L2 + RVV ~500+ MB/s (HEX-mode PSRAM) Cache hides the latency for stencil-pattern access.
ESP32-C6 SRAM only n/a RISC-V single-core @ 160 MHz; no PSRAM in typical configs.
Teensy 4.0 / 4.1 Cortex-M7 L1 DTCM/OCRAM (fast); FlexSPI PSRAM is slow Internal RAM is the right home for the grid.

Concrete back-of-envelope on ESP32-S3: 100×100 i16 grid = 20 KB. The 5-point stencil update reads ~5 cells per inner cell per step ≈ 100 KB per step. At 50 MB/s practical PSRAM throughput that's ~2 ms per step on load alone, before any compute. At 30 fps that's 6% of the CPU just on grid loads — and that's before the kernel does any arithmetic. On ESP32-P4 the same workload would be ~10× faster because L2 catches the cells the next iteration needs.

The hypothesis to test: vector_psram<i16> is the wrong default on ESP32-S3 for grids that fit in SRAM. A 64×64 grid is 8 KB and fits trivially in S3's 320 KB SRAM. The perf benchmark below has to actually demonstrate the SRAM-vs-PSRAM gap before we change the default.

Platform targets

Grid sizes we expect to run smoothly at 30 fps:

Platform class Target grid Storage path Why
AVR (Uno / ATmega328) 8×8 SRAM RAM budget
Cortex-M0 (RP2040, Teensy-LC) 16×16 SRAM No FPU, software multiply for some ops
ESP32-C6 32×32 SRAM RISC-V @ 160 MHz, no PSRAM in stock config
ESP32 (original) 32×32 SRAM Dual Tensilica @ 240 MHz
ESP32-S3 64×64 SRAM (PSRAM optional, opt-in) DSP coproc + PIE SIMD, but PSRAM is slow
Teensy 4.0 / 4.1 64×64 (headroom to 128×128) DTCM/OCRAM Cortex-M7 @ 600 MHz, fast internal RAM
ESP32-P4 64×64 (headroom to 128×128) L2-cacheable, PSRAM OK RVV SIMD, L2 cache, fast PSRAM

The bold rows are the ones the user named explicitly. The non-bold rows are the platforms we already test against and need to keep working.


Sub-tasks tracked by this meta

1. Wave2D autoresearch performance benchmark — DESIGN

Goal: a repeatable, deterministic, sub-millisecond-resolution perf test for WaveSimulation2D_Real::update() that runs through the existing autoresearch RPC infrastructure on real hardware. Without this, every other item in this meta is theory.

Approach:

  • No new .ino — extend examples/AutoResearch/AutoResearch.ino per CLAUDE.md policy. Add a wave2d_perf RPC handler.
  • Invocation from the host side: bash autoresearch <board> --wave2d-perf <W>x<H> (new --wave2d-perf flag in ci/autoresearch.py).
  • The RPC handler takes (W, H, stencil_id, iterations, storage_mode) and returns a timing dict.
  • Storage mode is one of SRAM / PSRAM / default — lets us A/B the same grid in two locations on ESP32-S3.

Measurement contract:

result = {
    "platform": "esp32s3",
    "grid": [64, 64],
    "stencil": "NinePointIsotropic",
    "storage": "SRAM",
    "iterations": 1000,
    "total_cycles": 12345678,
    "cycles_per_update": 12345.678,
    "cycles_per_cell_per_update": 3.014,
    "loads_only_cycles_per_cell": 1.823,  # baseline: just sum the grid
    "wall_clock_us_per_update": 51.4,
    "fps_at_one_update_per_frame": 19450,
    "std_dev_pct": 1.2,
}

Timing primitives:

  • ESP32: esp_cpu_get_cycle_count().
  • Cortex-M: ARM_DWT_CYCCNT (DWT cycle counter, enable once at boot).
  • AVR: 16-bit Timer1 prescaler 1 (1-cycle resolution but 65 K-cycle range — split long runs).
  • Host: std::chrono::steady_clock.

Two separate measurements per run, isolation matters:

  1. loads_only baseline. A helper that reads the same 5-cell stencil pattern but only sums the values, no multiply / damp / clamp. Lets us separate the memory-bound floor from the compute addition. On ESP32-S3 PSRAM-backed, the gap between loads_only and update will be small (memory dominates). On L2-cached platforms the gap will be large (compute dominates).
  2. update full kernel. What we actually care about.

Grid sizes per platform:

Run at 16×16, 32×32, and 64×64 wherever each fits in RAM. The scaling curve tells us when we hit cache / PSRAM thresholds.

Stencil sweep:

Run all currently-implemented stencils — FivePoint, NinePointIsotropic — plus any new ones as they ship (compact-O(h⁴) is the obvious next addition per #3112).

Output:

Python-side reporter formats one comparison table per platform. Cross-platform comparison table at the end.


2. Autoresearch perf-measurement sanity / calibration test

Goal: before we use autoresearch perf numbers to gate optimization decisions, prove autoresearch's timing infrastructure produces trustworthy data on each platform.

Three quick sanity probes:

2a. Known-throughput memcpy microbench

void rpc_memcpy_throughput(uint32_t bytes, uint32_t iterations) {
    static uint8_t src[8192];  // in SRAM
    static uint8_t dst[8192];
    uint32_t t0 = cycle_count();
    for (uint32_t i = 0; i < iterations; ++i) {
        memcpy(dst, src, bytes);
    }
    uint32_t t1 = cycle_count();
    // Return measured MB/s.
}

Pass criterion: measured MB/s within 30% of vendor spec for SRAM bandwidth.

Platform Vendor MB/s (SRAM) Min acceptable
ESP32-S3 ~250 175
ESP32-C6 ~200 140
Teensy 4.0 OCRAM ~600 420
AVR ~5 3.5

If measured throughput is <50% of vendor spec → autoresearch timing instrumentation is broken; bail loudly.

2b. Idle-loop calibration

void rpc_nop_calibration(uint32_t iterations) {
    uint32_t t0 = cycle_count();
    for (uint32_t i = 0; i < iterations; ++i) {
        __asm__ volatile ("nop");
    }
    uint32_t t1 = cycle_count();
    // Expected: ~iterations cycles + small constant.
}

Pass criterion: (t1 - t0) / iterations ∈ [1.0, 2.5] cycles/nop. >2.5 means there's overhead (IRQ jitter, branch predictor stalls, RPC framing leak); <1.0 means the cycle counter is broken (probably running at a different clock domain).

2c. Repeat-stability check

Run the full wave2d_perf test 10 times back-to-back at fixed (W, H, stencil). Report mean and std dev. Pass criterion: std dev < 5% of mean. Higher → IRQ noise, RPC contention, or autoresearch reporter rounding.

Fail-loud contract:

If ANY of 2a / 2b / 2c fails on a platform, the autoresearch reporter MUST stamp the result with AUTORESEARCH_PERF_UNTRUSTED=true and refuse to feed it into the cross-platform comparison table. Silent corruption of perf data is worse than no data.


3. SIMD backlog (already filed, on hold)

Remain on hold pending:

  • Task 1 (perf benchmark) merged.
  • Task 4 algorithmic candidates evaluated against perf benchmark.

Rationale: per the #3112 research doc, the algorithmic wins (compact-4 stencil, screen-space blur replacing SS, procedural waves) each beat SIMD's typical 4–8×. Pick the algorithm before tuning the constant factor.


4. Algorithmic follow-ups from #3112 research doc

To be filed as separate sub-issues once Task 1's perf benchmark is in place and baselines are captured:

  • Compact O(h⁴) 9-point Laplacian — third option on the existing LaplacianStencil enum. Same memory pattern as the isotropic 9-point from feat(wave): opt-in 9-point isotropic Laplacian for WaveSimulation2D (closes #3095) #3104, O(h⁴) accuracy. Probably ship first since the diff is smallest.
  • Output Gaussian blur as super-sample replacement — addresses super-sample's actual user motivation (LED-grid AA) at ~1.2× scalar cost vs 8–512× for SS 2X/4X/8X.
  • WaveProceduralFx — sum-of-Gerstner-wavelets — bypasses the PDE entirely. Zero grid memory. AVR / Low-memory win.
  • Tile-level active-region tracking — extends existing useChangeGrid infrastructure. Biggest win on ESP32-S3 with PSRAM-backed large grids (skip the PSRAM read entirely for idle tiles).
  • Q7/Q8 grid storage option — templated Storage = i16 | i8. Halves PSRAM bandwidth on ESP32-S3.

Each candidate's PR must report perf-benchmark numbers from Task 1 on at least ESP32-S3, ESP32-C6, Teensy 4.0, and host. Without those numbers the PR cannot be evaluated against the alternatives.


5. PSRAM storage — vector_psram<i16> default is wrong (tracked separately)

The current WaveSimulation2D_Real declares its two grids as fl::vector_psram<i16>, which silently lands them in PSRAM on any ESP32 that has it. This is the wrong default:

  • ESP32-S3 has no L2 cache. Every cell access pays full PSRAM latency (~80 ns). The 5-point stencil's interior loop touches 5 cells per inner cell, so a 64×64 update reads ~80 KB per step. At ~50 MB/s PSRAM throughput that's ~1.6 ms per step on memory alone, before any compute — borderline at 30 fps and ruins frame rate at the grid sizes users actually want.
  • ESP32-P4 has L2 cache (128 KB), which helps for grids that fit in L2. A 64×64 i16 grid is 8 KB and lives in L2 fine. A 128×128 grid is 32 KB and also fits. But the moment a user goes to 256×256 (128 KB grid + halo) the working set blows L2 and we're back to streaming PSRAM. So even on P4, "PSRAM is always fine" doesn't hold.
  • Auto-decision by the wrapper is dangerous. A grid size that's safe on P4 ruins frame rate on S3, and silently changing storage based on __has_psram() produces wildly different perf on hardware that looks identical to the user.

Fix tracked in its own focused follow-up issue (file alongside this meta). Design space for the fix:

Option Mechanism Pros Cons
A. SRAM by default, explicit PSRAM opt-in Switch WaveSimulation2D_Real grids to fl::vector<i16>; add a WaveSimulation2D_Real_Large variant (or templated Allocator) that uses PSRAM Predictable perf, simple mental model User has to know they're opting in
B. Templated Storage/Allocator parameter WaveSimulation2D_Real<Alloc>; wrapper class picks based on size Most flexible Doubles template instantiations; harder to debug perf
C. Size-gated default in wrapper WaveSimulation2D picks SRAM for W*H ≤ 64×64, PSRAM otherwise Hides decision from user Same hidden-perf-cliff problem as today, just at a different size

Default recommendation (subject to perf data from Task 1): Option A. Public API stays simple; PSRAM becomes an explicit, documented choice for users who genuinely have large grids and accept the perf cost.

This is large enough to belong in its own focused issue, not a checkbox here. Status: filed as #3114.


Acceptance criteria for closing this meta

Stop condition

This meta closes when the acceptance criteria above all hold OR when the user explicitly decides to defer remaining items. The 5 algorithmic follow-ups don't all have to land here — the meta can close after the perf infrastructure is in place and the top 2 candidates are validated.

Cross-references

Activity

  1. BitsForPeople commented on Jul 6, 2026

    @BitsForPeople

    Notice that on the ESP32-S3, PSRAM does operate via the cache.

  2. zackees commented on Jul 6, 2026

    @zackees
    MemberAuthor

    Thanks i didnt know that. This may change the calculus. I'll have to test it to see what the hit is.

  3. zackees commented on Aug 20, 2026

    @zackees
    MemberAuthor

    Burn-down disposition: DEFER ? this is a multi-platform performance program with remaining hardware and algorithm-selection work, not one bounded PR.

    Acceptance audit on current master:

    Restart criteria:

    1. Restore reliable fbuild C6/S3 deploys and attach S3, Teensy 4.x, and P4 (or explicitly waive P4 until available).
    2. Run the fail-loud calibration plus --perf-wave2d at 16?16/32?32/64?64, commit raw JSON and a versioned baseline report for all required platforms/storage modes.
    3. File and land compact O(h?) as a focused issue/PR with numerical-parity, stability, and hardware performance evidence.
    4. Implement and measure one additional algorithmic candidate, then compare it with output blur and scalar baselines.
    5. Revisit the three SIMD issues from measured frame-budget gaps and either implement or close each as not worthwhile.
    6. Close this meta only after its acceptance table is updated with the exact PRs and benchmark artifacts.

    The already-merged plumbing is valuable, but it does not satisfy the meta's own stop condition.

  4. added
    deferred
    on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    deferred

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions