Skip to content

Krylov: stop adaptive-path allocation thrash (expcache wipe + lanczos slice copy) - #259

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:krylov-view-slices
Jul 23, 2026
Merged

Krylov: stop adaptive-path allocation thrash (expcache wipe + lanczos slice copy)#259
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:krylov-view-slices

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

Removes the remaining per-step allocations from the adaptive Krylov path
(phiv_timestep! / expv_timestep! with adaptive_krylov), which the
exponential Rosenbrock integrators in OrdinaryDiffEq (Exp4/EPIRK/EXPRB) drive
every step. Two sources:

  1. get_expcache! cache-wipe thrash. The exp-workspace store was reset with
    empty! once it held more than 16 distinct extended-matrix sizes. The
    adaptive path visits a bounded-but-varying set of subspace sizes as it adapts
    m; when that set exceeds 16, the wipe discarded still-hot workspaces and the
    entire working set (each workspace is 7 dense matrices + a LinearSolve.init)
    was reallocated periodically. Each workspace is O((m+p)^2) — tied to the
    subspace dimension, not the state size n — so keeping the seen sizes
    cached is cheap. Now evicts only the oldest entry (bound raised 16 → 64), so a
    repeat size is a hit instead of a reallocation.

  2. lanczos! slice copy. copyto!(@diagview(H, 1), v[1:(end - 1)])
    materialized a fresh vector each call. @view makes it a view; source
    (sub-diagonal) and destination (super-diagonal) are disjoint, so the result is
    unchanged.

Measured effect

Per-step allocations via the step! interface on a 1D semilinear
reaction–diffusion ODEProblem, well warmed (adaptive Krylov has settling
transients + rare spikes, so this must be measured over a long window):

method / size master this PR
Exp4, N=512 ~864 B/step 16 B/step (median 0)
EPIRK4s3B, N=512 ~32.8 KB/step ~2.5 KB/step (median 0)

The quiet-state per-step allocation is now zero; the residual amortized cost
is the occasional genuinely-new subspace size (one unavoidable alloc_mem +
KrylovSubspace resize! when adaptive m reaches a new maximum).

Correctness

Final solutions are bit-identical to master (max|Δu| = 0 across
Exp4/EPIRK4s3A/EPIRK4s3B/EXPRB53s3 on a 50-step N=256 solve). Both changes are
semantics-preserving by construction (view vs. copy of disjoint memory; the
workspace store is scratch that is fully overwritten on each use, so which sizes
are cached never affects results).

Test

Adds a regression test: a size seen earlier must still be cached after >16
further distinct sizes are requested (the old wipe bound), and a cache hit must
not allocate. The Core suite passes locally.

Pairs with SciML/OrdinaryDiffEq.jl#3993 (the stepper-side @views slice fix);
together they take the exponential Rosenbrock steppers to zero quiet-state
per-step allocations.


Draft — please ignore until reviewed by @ChrisRackauckas.

🤖 Generated with Claude Code

… slice copy)

Two per-step allocation sources remained in the adaptive Krylov path
(phiv_timestep!/expv_timestep! with adaptive_krylov), which the exponential
Rosenbrock integrators (Exp4/EPIRK/EXPRB) drive every step:

1. `get_expcache!` reset its whole workspace store with `empty!` once it held
   more than 16 distinct extended-matrix sizes. The adaptive path visits a
   bounded-but-varying set of subspace sizes as it adapts `m`, and when that set
   exceeds 16 the wipe discarded the still-hot workspaces, forcing the entire
   working set to be reallocated (each workspace is 7 dense matrices +
   LinearSolve.init) periodically. Each workspace is O((m+p)^2) -- tied to the
   subspace dimension, not the state size -- so keeping the seen sizes cached is
   cheap. Evict only the oldest entry (and raise the bound to 64) so a repeat
   size is a hit instead of a reallocation.

2. `lanczos!` copied the sub-diagonal into the super-diagonal via
   `v[1:(end - 1)]`, which materializes a fresh vector each call. `@view` makes
   it a view; source (sub-diagonal) and destination (super-diagonal) are
   disjoint, so the result is unchanged.

Measured on a 1D semilinear reaction-diffusion ODEProblem via the `step!`
interface, well warmed (adaptive Krylov has settling transients + rare spikes,
so per-step allocation must be measured over a long window, not a few steps):

  Exp4       N=512:  ~864  -> 16   B/step   (median 0)
  EPIRK4s3B  N=512:  ~32.8 KB -> ~2.5 KB/step amortized (median 0)

The residual is the occasional genuinely-new subspace size (one unavoidable
alloc_mem + KrylovSubspace resize when adaptive `m` reaches a new maximum),
which amortizes away; the quiet-state per-step allocation is now zero.

Final solutions are bit-identical to before (max|Δu| = 0 across
Exp4/EPIRK4s3A/EPIRK4s3B/EXPRB53s3 on a 50-step N=256 solve).

Adds a regression test that a size seen before is still cached after >16 further
distinct sizes are requested, and that a cache hit does not allocate.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 23, 2026 16:07
@ChrisRackauckas
ChrisRackauckas merged commit 3d1e43f into SciML:master Jul 23, 2026
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants