Repository navigation
Dense LU refactorization without the per-step A copy - #1038
Merged
ChrisRackauckas merged 1 commit intoJul 11, 2026
Merged
ChrisRackauckas merged 1 commit into
ChrisRackauckas merged 1 commit into
Conversation
…wned buffer) LinearSolve's dense LU solve! gates its in-place lu! path on alias_A = true at init; construct_linear_solver never opted in, so every refactorization paid a full lu(A) copy (21.5 KB at n = 51). Init the lincache on a NonlinearSolve-owned copy with alias_A = true: one O(n²) copy at init, none per refactorization. Safe because set_lincache_A! refreshes the aliased buffer in full via copyto! before every refactorization, and the init-time copy keeps the first factorization from destroying the caller's Jacobian buffer. Opted in via alias_A_for_refactorization for explicit LUFactorization and for linsolve === nothing (DefaultLinearSolver's dense choices funnel into the same alias-gated body; its singular-LU → QR safety backup is cached and reused, measured 560 B/refactorization steady state vs 21,513 B before). Measured end to end (n = 50): plain NewtonRaphson 21.5 KB → 56 B per iteration; ArcLengthContinuation with Newton inner 22.2 KB → 1.28 KB per step; default-inner continuation 55.8 KB → 34.8 KB per step (the remainder is Broyden maybe_pinv!! and Klement internals). Co-Authored-By: Claude Fable 5 <[email protected]> Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas
marked this pull request as ready for review
July 11, 2026 10:19
ChrisRackauckas-Claude
pushed a commit
to ChrisRackauckas-Claude/NonlinearSolve.jl
that referenced
this pull request
Jul 11, 2026
…_for_refactorization (SciML#1038); pick up the NLSB QA fix (SciML#1035) construct_linear_solver's alias kwarg now defaults to nothing, deferring to SciML#1038's alias_A_for_refactorization trait (owned-copy refactorization buffer); an explicit LinearAliasSpecifier from a caller that owns A/b outright (the inverse-Jacobian workspace) skips the defensive copy. Co-Authored-By: Claude Fable 5 <[email protected]> Co-Authored-By: Chris Rackauckas <[email protected]>
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.
Note: This PR should be ignored until reviewed by @ChrisRackauckas.
Came out of the #1029 review question ("it should be using LinearSolve … which should make the LU factorization not allocate?"). Part of the #1020 allocation work.
Problem
LinearSolve's dense LU
solve!has an in-placelu!refactorization path, but gates it onalias_A = trueatinit— otherwise every refactorization doesfact = lu(A, check = false), a full O(n²) copy.construct_linear_solvernever opted in, so every dense-LU Newton iteration in NonlinearSolve paid a 21.5 KB copy (n = 51).Fix
Init the LinearSolve cache on a NonlinearSolve-owned copy of
Awithalias_A = true: one O(n²) copy at init, zero per refactorization. Safety argument:set_lincache_A!already refresheslincache.Ain full viacopyto!before every refactorization, so destroying it in place cannot corrupt any later read;A) keeps the first factorization from destroying the Jacobian buffer, which trust-region/line-search consumers read after the solve — asserted in the new test.Opt-in is per (algorithm, matrix) via a new
alias_A_for_refactorizationhook:LUFactorization+Matrix, andlinsolve === nothing+Matrix(DefaultLinearSolver's dense choices funnel into the same alias-gated body; its singular-LU → QR safety fallback stays intact —_copy_A_for_safetykeeps a cached private backup, measured in the steady-state number below). Everything else (sparse, QR, Krylov, GPU, structured) keeps exact current behavior.Measured
ipivinsidelu!— a LinearSolve-side cacheval-reuse follow-up)NewtonRaphsonper iteration (n = 50 dense)ArcLengthContinuation, Newton inner, per stepmaybe_pinv!!+ Klement internals — separate follow-up)Verification (all run locally, output observed)
lib/NonlinearSolveBase/test/lu_refactorization_allocs.jl: gate dispatch, caller's-A-preserved after first factorization, correctness across refactorization, allocation bounds — for both explicitLUFactorizationand thenothingdefault path (26/26 with routing tests).NonlinearSolveFirstOrderPkg.testagainst this NLSB: passed (the heaviest consumer of this path).test/Corehomotopy + arclength files against this NLSB: 426/426.🤖 Generated with Claude Code