Skip to content

Fix CompositeAlgorithm switching with 3+ algorithms (#4648) - #4652

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:composite-3plus-switch
Sep 28, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:composite-3plus-switch

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

Fix CompositeAlgorithm never switching with 3+ algorithms.

Fixes #4648

What changed and why

choose_algorithm! had a switching method only for 2-algorithm composites (CompositeCache{Tuple{T1,T2},F}) and a no-op choose_algorithm!(integrator, cache::OrdinaryDiffEqCache) = nothing. Since CompositeCache <: OrdinaryDiffEqCache, composites with 3+ algorithms matched the no-op instead of the generic switching fallback and never left their first algorithm.

Add a choose_algorithm!(integrator, cache::CompositeCache) method that handles tuples of any length: unrolled type-stable dispatch for indices 1 and 2 (fast-path parity with the 2-tuple method), a dynamic branch for 3+ (the same pattern initialize!/perform_step! already use for general CompositeCache), then the existing switching machinery (reset_alg_dependent_opts!, transfer_cache!) for the transition. The 2-tuple fast path, DefaultCache, and non-composite caches are untouched. StochasticCompositeCache is unaffected: it is not an OrdinaryDiffEqCache and still uses the is_composite_cache generic fallback.

Also add a regression test in test/InterfaceI/composite_algorithm_test.jl: a 3-algorithm composite with a cycling choice function must visit all three algorithms.

Verification

  • Runic --check on both changed files: clean.
  • typos over the diff: clean.
  • GROUP=Core julia --project -e 'using Pkg; Pkg.test()' in lib/OrdinaryDiffEqCore: all testsets pass.
  • GROUP=Core julia --project -e 'using Pkg; Pkg.test()' in lib/OrdinaryDiffEqDefault: all 56 Default Solver tests pass.
  • lib/StochasticDiffEq/test/composite_algorithm_test.jl run directly against the fixed tree: exit 0 (StochasticCompositeCache path unaffected).
  • Reproducer from the issue (master vs fix):
    • master: 2 algs: unique alg_choice = [1, 2] / 3 algs: unique alg_choice = [1]
    • fix: 2 algs: unique alg_choice = [1, 2] / 3 algs: unique alg_choice = [1, 2, 3]
  • Correctness spot check: Lorenz solved with CompositeAlgorithm((Tsit5(), Vern7(), BS3()), mod(iter,3)+1) returns Success, visits [1,2,3], final state within 0.012 of a Tsit5 reference (expected spread when a low-order method takes cyclic steps).

Failing-before/passing-after

New test CompositeAlgorithm with 3+ algorithms (#4648) in test/InterfaceI/composite_algorithm_test.jl, run directly against master sources:

CompositeAlgorithm with 3+ algorithms (#4648): Test Failed at .../test/InterfaceI/composite_algorithm_test.jl:148
  Expression: sort(unique(sol3.alg_choice)) == [1, 2, 3]
   Evaluated: [1] == [1, 2, 3]

Test Summary:                                 | Pass  Fail  Total   Time
CompositeAlgorithm with 3+ algorithms (#4648) |    1     1      2  12.9s
ERROR: LoadError: Some tests did not pass: 1 passed, 1 failed, 0 errored, 0 broken.

Same test with the fix applied:

Test Summary:                                 | Pass  Total  Time
CompositeAlgorithm with 3+ algorithms (#4648) |    2      2  4.9s

Equivalence check

2-algorithm composite (CompositeAlgorithm((Tsit5(), Vern7()), mod(iter,2)+1)) and DefaultODEAlgorithm() on prob_ode_lorenz and prob_ode_vanderpol_stiff (mu=1e6): sol.t, sol.u, sol.stats, and sol.alg_choice serialized on master and on this branch, then compared with cmp — bitwise identical.

lorenz    composite: nsteps=24     alg_choice=[1, 2]
lorenz    default:   nsteps=27     alg_choice=[1]
vanderpol composite: nsteps=428820 alg_choice=[1, 2]
vanderpol default:   nsteps=253    alg_choice=[1, 3]
BITWISE IDENTICAL

What was not verified

  • GPU/CUDA groups (no GPU available).
  • QA group (Aqua/JET) for the sublibs and top-level package.
  • Full InterfaceI/Everything top-level suite via Pkg.test() (the composite test file was run directly instead).
  • Downstream packages (DelayDiffEq composite tests exercise the same 2-tuple path, which is unchanged).

Risk assessment

  • Risk: low
  • Blast radius: one added method on CompositeCache dispatch; no existing method modified. Public API unchanged. DefaultCache, 2-tuple composites, stochastic composites, and non-composite caches keep their exact prior behavior (bitwise-identical results above).
  • Evidence: failing-before/passing-after above; bitwise equivalence for 2-alg composite and DefaultODEAlgorithm on Lorenz and Van der Pol mu=1e6; GROUP=Core passes for OrdinaryDiffEqCore and OrdinaryDiffEqDefault; stochastic composite tests pass.
  • Independent review: Cursor Agent (Fable 5.1 thinking-high): MERGE, low risk, high confidence. Bitwise-identical sol.t/u/stats/alg_choice vs master on 10 cases (AutoTsit5(Rosenbrock23), DefaultODEAlgorithm, 2-alg cyclic; Lorenz and VdP μ=1e6). The switch sequence mirrors the 2-tuple method line by line; which/morespecific confirm unambiguous dispatch; JET reports 0 runtime dispatch for n=2..5; the new test fails on master; no conflict with Unroll CompositeControllerCache dispatch (non-stiff Default step! allocation-free) #4647. One optional nit: the unrolled 1/2 arms duplicate the else arm.
  • Merge: low-risk candidate; the red CI jobs are the known master-red/infra set.

Please ignore until reviewed by @ChrisRackauckas.

🤖 Generated with opencode (model: opencode/longcat-2.5-preview-free), orchestrated by Claude Code (claude-opus-5-5[1m]) session https://claude.ai/code/session_01LPHREnnonfLg1VcE1EJovv

Risk assessment

  • Risk: low
  • Blast radius: Algorithm switching for composites with 3+ solvers, including FSAL state, initialization, and cache transfer. This restores intended public behavior; no new public API, breaking signature change, dependency-internal access, or weakened tests.
  • Evidence: Head has 252 passing checks, eight failures, and one queued GPU check. All eight failures match master; no observed test regressions: SciMLSensitivity Core1 (Mooncake/complex-adjoint errors; master); downgrade DiffEqBase and OrdinaryDiffEqDifferentiation (callback assertions and missing diag(::ScalarOperator); master); DelayDiffEq Regression, GlobalDiffEq Core, OrdinaryDiffEqDifferentiation Core, and OrdinaryDiffEqNonlinearSolve Core, all Julia 1 (respectively NaN matrix, unsupported callbacks, missing diag, and Trapezoid mass-matrix errors; master); top-level GPU Julia 1 (QNDF gpu_mismatch; master). All four InterfaceI configurations pass. No GitHub reviews or review comments exist. The description omits the still-queued OrdinaryDiffEqDefault GPU check and does not enumerate the pre-existing failures; these are actual test errors, not merely infrastructure failures.
  • Independent review: Codex CLI 0.156.1 (Mac) / gpt-6-astra rated it low, high confidence: the narrow dispatch fix follows existing switching logic, adds a discriminating regression test, and every completed failing check has matching master failure evidence.
  • Merge: needs human review (draft explicitly awaits @ChrisRackauckas; OrdinaryDiffEqDefault GPU remains queued, so complete no-regression verification is pending).

🤖 Risk assessment posted by an AI agent (fleet master) — harness: Codex CLI 0.156.1 (Mac) / gpt-6-astra; dispatched by Claude Code head, model claude-opus-5-5[1m]
Conversation: local Claude Code session 3cd6500a-1f81-46b5-ac0b-c466e15b6a53 on Chris's Mac (session ID, no URL)

Add a choose_algorithm! method for general CompositeCache so composites
with three or more algorithms switch correctly. Previously only the
2-tuple CompositeCache{Tuple{T1,T2},F} had a switching method; longer
tuples hit the no-op choose_algorithm!(::OrdinaryDiffEqCache) and never
left their first algorithm.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: opencode <[email protected]>
Agent-Harness: opencode 1.18.31
Agent-Model: opencode/longcat-2.5-preview-free
Agent-Session: local opencode session in /home/crackauc/sandbox/goals/performance/jobs/pr-4648/repo (log /home/crackauc/sandbox/goals/performance/jobs/pr-4648/log.txt)
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 28, 2026 11:54
@ChrisRackauckas
ChrisRackauckas merged commit a50e0d7 into SciML:master Sep 28, 2026
256 of 264 checks passed
ChrisRackauckas referenced this pull request Sep 29, 2026
* Bump DiffEqBase to 7.21.3

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqBDF to 2.4.12

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqSDIRK to 2.9.7

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqRosenbrock to 2.7.5

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqLowOrderRK to 2.2.6

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqTsit5 to 2.1.5

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqVerner to 2.4.2

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqSymplecticRK to 2.2.3

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.

CompositeAlgorithm with 3+ algorithms never switches (choose_algorithm! no-op wins dispatch)

2 participants