Skip to content

Dense LU refactorization without the per-step A copy - #1038

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:lu-refactorization-alias-a
Jul 11, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:lu-refactorization-alias-a

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

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-place lu! refactorization path, but gates it on alias_A = true at init — otherwise every refactorization does fact = lu(A, check = false), a full O(n²) copy. construct_linear_solver never 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 A with alias_A = true: one O(n²) copy at init, zero per refactorization. Safety argument:

  • set_lincache_A! already refreshes lincache.A in full via copyto! before every refactorization, so destroying it in place cannot corrupt any later read;
  • the init-time copy (instead of aliasing the caller's 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_refactorization hook: LUFactorization + Matrix, and linsolve === nothing + Matrix (DefaultLinearSolver's dense choices funnel into the same alias-gated body; its singular-LU → QR safety fallback stays intact — _copy_A_for_safety keeps a cached private backup, measured in the steady-state number below). Everything else (sparse, QR, Krylov, GPU, structured) keeps exact current behavior.

Measured

layer before after
LinearSolve refactorization (51×51, default alg) 21,516 B 560 B (ipiv inside lu! — a LinearSolve-side cacheval-reuse follow-up)
plain NewtonRaphson per iteration (n = 50 dense) ~21.5 KB 56 B
ArcLengthContinuation, Newton inner, per step 22.2 KB 1.28 KB
continuation, default inner, per step 55.8 KB 34.8 KB (remainder: Broyden maybe_pinv!! + Klement internals — separate follow-up)

Verification (all run locally, output observed)

  • New lib/NonlinearSolveBase/test/lu_refactorization_allocs.jl: gate dispatch, caller's-A-preserved after first factorization, correctness across refactorization, allocation bounds — for both explicit LUFactorization and the nothing default path (26/26 with routing tests).
  • Full NonlinearSolveFirstOrder Pkg.test against this NLSB: passed (the heaviest consumer of this path).
  • All test/Core homotopy + arclength files against this NLSB: 426/426.

🤖 Generated with Claude Code

…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
ChrisRackauckas marked this pull request as ready for review July 11, 2026 10:19
@ChrisRackauckas
ChrisRackauckas merged commit 7a620df into SciML:master Jul 11, 2026
91 of 93 checks passed
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]>
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