Skip to content

Stop threading LinearSolve cache through solution return values - #3875

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:remove-linres-cache-threading
Jul 11, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:remove-linres-cache-threading

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

What

Stops threading the LinearSolve cache through solution return values (linres.cache). OrdinaryDiffEq now holds the LinearSolve.LinearCache it builds at alg_cache time (cache.linsolve, cache.linsolve1/2/3, cache.linsolve[i]) and passes that stable reference to every dolinsolve/solve! call, instead of re-reading .cache off the previously returned LinearSolution.

Requested by @ChrisRackauckas.

Why

Modern LinearSolve.LinearCache is mutable and updated in place: solve!(cache) never replaces the cache, so linres.cache === cache always. The linres.cache threading is a relic of an era when cache updates could rebuild the cache — every deleted cache.linsolve = linres.cache writeback was assigning an object to the field that already held it.

This prepares OrdinaryDiffEq for LinearSolve 5.0, which will remove the cache from solve!'s returned LinearSolution (a parallel PR is making that change). After this PR, no code in the monorepo reads .cache off a linear solution.

This PR should merge before LinearSolve 5.0 is released.

dolinsolve (lib/OrdinaryDiffEqDifferentiation/src/linsolve_utils.jl) was audited first: it mutates the passed cache in place (linsolve.b = b, linsolve.u = linu, LinearSolve.reinit!(linsolve; A)) and returns solve!(linsolve) — no branch ever constructs a replacement cache, so this is a pure plumbing change with byte-identical semantics.

Sites changed

  • lib/OrdinaryDiffEqNonlinearSolve/src/newton.jl: deleted the cache.linsolve = linres.cache writeback in compute_step! (core path for all NLNewton-based implicit methods).
  • lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl: chained dolinsolve(integrator, linres.cache; …) calls now pass cache.linsolve; deleted 3 writebacks (Rosenbrock23/32 caches and the generic RosenbrockCache loop).
  • lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl: chained call now reuses the local linsolve (cache.linsolve).
  • lib/OrdinaryDiffEqFIRK/src/firk_perform_step.jl and firk_addsteps.jl: deleted all cache.linsolveX = linresX.cache writebacks (RadauIIA3/5/9); cache.linsolveX = dolinsolve(…).cache assignments in AdaptiveRadauCache become bare dolinsolve(…) calls; smooth_est error-estimate calls pass cache.linsolve1/local linsolve1 instead of linres1.cache; the GaussLegendre direct LinearSolve.solve! no longer binds an unused linres.
  • lib/OrdinaryDiffEqExtrapolation/src/extrapolation_perform_step.jl: deleted all 31 cache.linsolve[…] = linres.cache writebacks (serial and threaded implicit extrapolation paths).

Not changed: lib/OrdinaryDiffEqAMF/src/linsolve.jl constructs a LinearSolution via SciMLBase.build_linear_solution(alg, y, nothing, cache) — that is construction (SciMLBase API), not a read of .cache off a returned solution, and is unaffected by removing the read-side threading. It may need a follow-up when SciMLBase/LinearSolve 5.0 land, depending on the final build_linear_solution signature.

Tests run locally (Julia 1.12.4, monorepo GROUP routing via root Pkg.test())

Group Result
GROUP=OrdinaryDiffEqFIRK (Core) 105/105 pass
GROUP=OrdinaryDiffEqFIRK_QA 1 error in Aqua/ExplicitImports all_explicit_imports_are_public — reproduces identically on unmodified master (pre-existing, unrelated: flags long-standing imports like alg_cache, dolinsolve; being investigated separately)
GROUP=OrdinaryDiffEqExtrapolation (Core) 284/284 pass
GROUP=OrdinaryDiffEqExtrapolation_Multithreading (2 threads) 620/620 pass (exercises the cache.linsolve[Threads.threadid()] branches)
GROUP=OrdinaryDiffEqNonlinearSolve all pass (Newton 6/6, Linear Solver Tests 113/113, Linear Nonlinear 44/44, Mass Matrix 225 pass + 42 pre-existing broken, W-Prototype 18/18, DAE Init 19/19, …)
GROUP=OrdinaryDiffEqBDF (implicit-core stiff solvers over newton.jl) all pass (BDF Convergence 84/84, BDF Regression 32/32, DAE Convergence 26/26, DAE AD 16/16, …)
GROUP=OrdinaryDiffEqRosenbrock DAE Rosenbrock AD Tests 14/16 with 2 errors (IIP=false Brown=true AutoForwardDiff(chunksize=3) × Rodas5P/Tsit5DA) — identical 2 errors on unmodified master (pre-existing Dual-conversion failure in the external NonlinearSolve initialization path; being investigated separately). Because that safetestset aborts the group, the remaining files were run directly on this branch: Rosenbrock AD Tests 56/56 pass, Rosenbrock Convergence Tests 169/169 pass

Runic produced no changes on the touched files.

Note: This PR should be ignored until reviewed by @ChrisRackauckas.

🤖 Generated with Claude Code

Modern LinearSolve.LinearCache is mutable and updated in place:
solve! never replaces the cache, so linres.cache === cache always.
Use the cache built at init directly instead of reading .cache off
returned LinearSolutions, and drop the no-op writebacks. Prepares
for LinearSolve 5.0, which removes the cache from solve!'s returned
LinearSolution.

Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 11, 2026 20:33
@ChrisRackauckas
ChrisRackauckas merged commit a3415f4 into SciML:master Jul 11, 2026
157 of 181 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