Skip to content

Use @generated perform_step! to eliminate per-step regression - #3643

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
singhharsh1708:sdirk-generated-perform-step
May 18, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
singhharsh1708:sdirk-generated-perform-step

Conversation

@singhharsh1708

Copy link
Copy Markdown
Member

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

@singhharsh1708

Copy link
Copy Markdown
Member Author

making fixes

@singhharsh1708

Copy link
Copy Markdown
Member Author

@ChrisRackauckas Tried the SArray/SVector refactor (with α as NTuple{S, SVector{S, T2}} to preserve the vector-of-vectors structure) but it actually made things worse — both init and per-step regressed compared to the current @generated-only state. Per-step went from 0 → 16 allocs/call for KenCarp4 because SMatrix/NTuple field access through the mutable cache struct didn't constant-fold even with literal indices in the @generated body. Init went from 67 → 111 allocs for KenCarp4 because the inline SArray fields blow up the cache struct size. The current PR state (just @generated perform_step!) is the best we can get without a fundamentally different cache architecture.

@ChrisRackauckas

Copy link
Copy Markdown
Member

What's the timing vs the pre tableau now?

@singhharsh1708

singhharsh1708 commented May 18, 2026 •

Copy link
Copy Markdown
Member Author

@ChrisRackauckas Fresh apples-to-apples benchmark vs pre-tableau master (Pollu / Van der Pol):

Algorithm           Pre-tab    Current    Δt      Δallocs
----------------------------------------------------------------
KenCarp3            1.327ms    1.322ms    -0.4%   +39
KenCarp4            1.114ms    1.100ms    -1.3%   +116
KenCarp5            1.530ms    1.503ms    -1.8%   +161
KenCarp47           1.003ms    0.996ms    -0.7%   +139
KenCarp58           1.299ms    1.275ms    -1.8%   +165
Kvaerno3            2.273ms    2.221ms    -2.3%   +71
Kvaerno4            1.730ms    1.701ms    -1.7%   +45
Kvaerno5            1.493ms    1.440ms    -3.5%   +53
Cash4               4.185ms    4.079ms    -2.5%   +81
Hairer4             2.674ms    2.624ms    -1.9%   +79
Hairer42            2.991ms    2.930ms    -2.0%   +79
TRBDF2              1.318ms    1.289ms    -2.2%   +57
SDIRK2              8.674ms    8.638ms    -0.4%   +27
ESDIRK54I8L2SA      2.704ms    2.678ms    -1.0%   +78
ESDIRK436L2SA2      2.357ms    2.353ms    -0.2%   +60
ESDIRK437L2SA       1.934ms    1.912ms    -1.1%   +69
ESDIRK547L2SA2      2.630ms    2.622ms    -0.3%   +69
ESDIRK659L2SA       5414ms     5518ms     +1.9%   +85
ImplicitMidpoint    2.417ms    2.503ms    +3.6%   +25
SSPSDIRK2           5.431ms    5.448ms    +0.3%   +29
SFSDIRK4            7.911ms    7.829ms    -1.0%   +37
SFSDIRK5            9.339ms    9.498ms    +1.7%   +44
SFSDIRK6            10.467ms   9.728ms    -7.1%   +53
SFSDIRK7            11.690ms   11.676ms   -0.1%   +63
SFSDIRK8            12.134ms   12.409ms   +2.3%   +73

Summary:

  • 19/25 algorithms are now equal-to-faster than pre-tableau master
  • Remaining 6/25 are within +1.7% to +3.6%
  • Per-step allocations are back to 0 bytes (matching pre-tableau exactly)

@ChrisRackauckas

Copy link
Copy Markdown
Member

Fixes @inferred solve failure where Tab couldn't be inferred concretely.
@singhharsh1708

singhharsh1708 commented May 18, 2026 •

Copy link
Copy Markdown
Member Author

@ChrisRackauckas CI failure was an inference regression: s::Int was lifted into ESDIRKIMEXTableau{s,...} at runtime, making the stage count non-concrete under @inferred.
Fixed by switching the constructor path to Val{S}.

@ChrisRackauckas
ChrisRackauckas merged commit ae08008 into SciML:master May 18, 2026
93 of 100 checks passed
@singhharsh1708
singhharsh1708 deleted the sdirk-generated-perform-step branch May 18, 2026 14:09
ChrisRackauckas added a commit that referenced this pull request May 27, 2026
…3690)

The OOP path of `_perform_step` in `generic_imex_perform_step.jl` sets
`nlsolver.c = c[1]` before the stage-1 `nlsolve!` (line 1404); the IIP
path was missing this assignment. `nlsolver.c` determines
`cache.tstep = integrator.t + nlsolver.c * dt` inside the Newton solver,
which is the time argument passed to `f` during the implicit solve.

Without this assignment, the IIP path's stage-1 implicit solve carried
over the stale `c` value left behind by the previous outer step's
stage 2 (= `c[2]`). For `SDIRK2` (`c = [1, 0]`), this means stage 1 ran
at `t` instead of `t + dt`, and the cancellation in the SDIRK2
stage-2 algebra reduces both stages to the same fixed point
`zs[1] == zs[2] == dt*f(t, uprev)`. The embedded error estimate
`btilde[1]*zs[1] + btilde[2]*zs[2] = (zs[1] - zs[2])/2` then collapses
to 0, letting the PI controller grow `dt` unboundedly.

Reproduces as `DimensionMismatch` in
`lib/DelayDiffEq/test/interface/multi_algorithm.jl`'s `Constant delays`
testset: iip solves of `prob_dde_constant_2delays_ip` produce 11
timesteps while scalar solves produce 36 (and the iip trajectory is
visibly wrong — `u` jumps linearly between discontinuity tstops).

Regression bisected to PR #3641 (commit 99187d0, 2026-05-17). That
commit dropped `nlsolver.c = c[1]` from both IIP and OOP. A follow-up
restored it in OOP only; the @generated rewrite in #3643 and the
inline in #3676 preserved the asymmetry.

After this patch the `Constant delays` testset goes 27/27 green for
all nine algorithms (BS3, Tsit5, RK4, Vern6, SDIRK2, TRBDF2, KenCarp4,
Rosenbrock23, Rodas4). SDIRK2 sol.errors[:l∞] = 0.0035, well under
the 3.8e-1 threshold. No other algorithm's iip-vs-scalar step counts
or l∞ errors changed.

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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