Krylov: stop adaptive-path allocation thrash (expcache wipe + lanczos slice copy) - #259
Merged
ChrisRackauckas merged 1 commit intoJul 23, 2026
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Removes the remaining per-step allocations from the adaptive Krylov path
(
phiv_timestep!/expv_timestep!withadaptive_krylov), which theexponential Rosenbrock integrators in OrdinaryDiffEq (Exp4/EPIRK/EXPRB) drive
every step. Two sources:
get_expcache!cache-wipe thrash. The exp-workspace store was reset withempty!once it held more than 16 distinct extended-matrix sizes. Theadaptive 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 theentire working set (each workspace is 7 dense matrices + a
LinearSolve.init)was reallocated periodically. Each workspace is
O((m+p)^2)— tied to thesubspace dimension, not the state size
n— so keeping the seen sizescached is cheap. Now evicts only the oldest entry (bound raised 16 → 64), so a
repeat size is a hit instead of a reallocation.
lanczos!slice copy.copyto!(@diagview(H, 1), v[1:(end - 1)])materialized a fresh vector each call.
@viewmakes 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 semilinearreaction–diffusion
ODEProblem, well warmed (adaptive Krylov has settlingtransients + rare spikes, so this must be measured over a long window):
The quiet-state per-step allocation is now zero; the residual amortized cost
is the occasional genuinely-new subspace size (one unavoidable
alloc_mem+KrylovSubspaceresize!when adaptivemreaches a new maximum).Correctness
Final solutions are bit-identical to master (
max|Δu| = 0acrossExp4/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
Coresuite passes locally.Pairs with SciML/OrdinaryDiffEq.jl#3993 (the stepper-side
@viewsslice 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