Skip to content

Fix update_A! dispatch so reuse_A_if_factorization actually works - #1003

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix-lincache-reuse-dispatch
Jul 6, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix-lincache-reuse-dispatch

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas. Draft opened by an agent.

Bug

update_A! in NonlinearSolveBaseLinearSolveExt dispatches on Utils.safe_getproperty(cache.linsolve, Val(:alg)). But cache.linsolve is the user-passed LinearSolve algorithm (e.g. KLUFactorization(), LUFactorization()), which has no alg field — so the lookup returns missing and every call falls through to the generic non-factorization method, which re-sets A unconditionally (marking the LinearSolve cache fresh).

Consequences:

  1. reuse_A_if_factorization was a no-op — factorization algorithms refactorized on every linear solve. Plain solve hides this (each Newton iteration has a new J anyway, so a refactorization is due regardless), but any driver that holds J fixed across iterations pays a full refactorization per iteration. The big consumer is OrdinaryDiffEq's NonlinearSolveAlg modified-Newton path (step!(cache; recompute_jacobian = false)): profiling FBDF(linsolve = KLUFactorization(), nlsolve = NonlinearSolveAlg(NewtonRaphson())) on a sparse 512-dim 2D brusselator showed 79% of total runtime inside klu_l_refactor (~890 numeric refactorizations per solve vs ~218 actual W updates).
  2. stats.nfactors was never incremented for factorization algorithms (always 0), so the problem was invisible in stats.

Fix

Dispatch on the resolved algorithm cache.lincache.alg instead. This preserves the existing DefaultLinearSolver special case (forced reset for default GMRES): when linsolve === nothing, lincache.alg is exactly the DefaultLinearSolver that method matches.

Verification (run locally)

Before → after on a 200-dim sparse tridiagonal nonlinear system:

NR+KLU       iters=27 njacs=27 nfactors=0  (before)   nfactors=27  (after)
NR+LU        iters=27 njacs=27 nfactors=0  (before)   nfactors=27  (after)
NR+default   iters=27 njacs=27 nfactors=0  (before)   nfactors=27  (after)
NR+GMRES     iters=27 njacs=0  nfactors=0  (unchanged, correct — matrix-free)

Modified-Newton pattern: step! once with recompute_jacobian = true then 5× with recompute_jacobian = false → nfactors stays at 1 (before: refactorized each step).

Added a regression test in linear_solver_routing.jl asserting nfactors moves exactly once per fresh A and not at all under reuse. NonlinearSolveBase test suite passes locally on Julia 1.12 (exit 0), amended routing testset 12/12.

🤖 Generated with Claude Code

`update_A!` dispatched on `safe_getproperty(cache.linsolve, Val(:alg))`, but
`cache.linsolve` is the user-passed LinearSolve algorithm (e.g.
`KLUFactorization()`), which has no `alg` field — so the lookup returned
`missing` and every call fell through to the generic non-factorization method,
which re-sets `A` unconditionally. Consequences:

- `reuse_A_if_factorization` was a no-op: factorization algorithms
  refactorized on every linear solve. Plain `solve` hides this (each Newton
  iteration has a new J anyway), but drivers that hold J fixed across
  iterations — e.g. OrdinaryDiffEq's modified-Newton `NonlinearSolveAlg` path
  via `step!(cache; recompute_jacobian = false)` — paid a full numeric
  refactorization per iteration. Profiling FBDF+NonlinearSolveAlg+KLU on a
  sparse 512-dim brusselator showed 79% of runtime in klu_l_refactor.
- `stats.nfactors` was never incremented for factorization algorithms.

Dispatch on the resolved `cache.lincache.alg` instead. The
`DefaultLinearSolver` special case (forced reset for default GMRES) is
preserved: `lincache.alg` is exactly a `DefaultLinearSolver` in that setup.

After the fix, a frozen-J `step!` sequence keeps `nfactors` constant and
NR+KLU/LU report `nfactors == njacs`.

Co-Authored-By: Chris Rackauckas <[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