Repository navigation
Fix update_A! dispatch so reuse_A_if_factorization actually works - #1003
Merged
ChrisRackauckas merged 1 commit intoJul 6, 2026
Merged
ChrisRackauckas merged 1 commit into
ChrisRackauckas merged 1 commit into
Conversation
`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]>
This was referenced Jul 4, 2026
ChrisRackauckas
marked this pull request as ready for review
July 6, 2026 08:57
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.
Bug
update_A!inNonlinearSolveBaseLinearSolveExtdispatches onUtils.safe_getproperty(cache.linsolve, Val(:alg)). Butcache.linsolveis the user-passed LinearSolve algorithm (e.g.KLUFactorization(),LUFactorization()), which has noalgfield — so the lookup returnsmissingand every call falls through to the generic non-factorization method, which re-setsAunconditionally (marking the LinearSolve cache fresh).Consequences:
reuse_A_if_factorizationwas a no-op — factorization algorithms refactorized on every linear solve. Plainsolvehides 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'sNonlinearSolveAlgmodified-Newton path (step!(cache; recompute_jacobian = false)): profilingFBDF(linsolve = KLUFactorization(), nlsolve = NonlinearSolveAlg(NewtonRaphson()))on a sparse 512-dim 2D brusselator showed 79% of total runtime insideklu_l_refactor(~890 numeric refactorizations per solve vs ~218 actual W updates).stats.nfactorswas never incremented for factorization algorithms (always 0), so the problem was invisible in stats.Fix
Dispatch on the resolved algorithm
cache.lincache.alginstead. This preserves the existingDefaultLinearSolverspecial case (forced reset for default GMRES): whenlinsolve === nothing,lincache.algis exactly theDefaultLinearSolverthat method matches.Verification (run locally)
Before → after on a 200-dim sparse tridiagonal nonlinear system:
Modified-Newton pattern:
step!once withrecompute_jacobian = truethen 5× withrecompute_jacobian = false→nfactorsstays at 1 (before: refactorized each step).Added a regression test in
linear_solver_routing.jlassertingnfactorsmoves exactly once per freshAand not at all under reuse.NonlinearSolveBasetest suite passes locally on Julia 1.12 (exit 0), amended routing testset 12/12.🤖 Generated with Claude Code