Skip to content

Complete allocation-free dense LU refactorization: generic-kernel pivot reuse + regression tests#1082

Merged
ChrisRackauckas merged 1 commit into
SciML:mainfrom
ChrisRackauckas-Claude:cache-ipiv-lufactorization
Jul 11, 2026
Merged

Complete allocation-free dense LU refactorization: generic-kernel pivot reuse + regression tests#1082
ChrisRackauckas merged 1 commit into
SciML:mainfrom
ChrisRackauckas-Claude:cache-ipiv-lufactorization

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

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

Motivation

SciML/NonlinearSolve.jl#1038 makes every dense Newton iteration hit the alias_A = true dense-LU refactorization path (cache.A = J; solve!(cache)), so any per-refactorization allocation there is paid on every Newton step. #1078 (merged) already eliminated the fresh ipiv/LU-wrapper allocation for the LAPACK fast path (strided BLAS matrices + RowMaximum, Julia >= 1.11) via the preallocated-ipiv LAPACK.getrf!. This PR completes the picture:

  1. Generic-kernel path pivot reuse. With alias_A = true, NoPivot/RowNonZero pivoting and non-BLAS element types still went through lu!(A, pivot; check = false), which allocates a fresh pivot vector and LU wrapper on every refactorization. They now call the vendored generic_lufact! directly with the cached ipiv (the same kernel/check = false/info semantics lu! dispatches to for these inputs, and the same pattern GenericLUFactorization already uses). Unlike the getrf! method this works on every supported Julia version, so this path is allocation-free on 1.10 (LTS) too. Size changes fall back to lu! (which produces a right-sized pivot that is then cached), exactly like the existing BLAS branch.
  2. Regression tests for the whole warm-refactorization contract, which GESVFactorization + dense LU pivot-buffer reuse (v4.2.0) #1078 shipped without: test/Core/lu_refactorization.jl (53 assertions on 1.12), registered in the Core group.

The non-aliased copy path, sparse paths, and all other algorithms are untouched.

Measured: 51×51 Matrix{Float64}, warm refactorize+solve cycle (copyto!(Awork, A); cache.A = Awork; solve!(cache) under @allocated, post-warmup), Julia 1.12.6

path (alias_A = true) pre-#1078 (v4.2.1) main (#1078) this PR
LUFactorization() (RowMaximum) 480 B 0 B 0 B
LUFactorization(pivot = NoPivot()) 480 B 480 B 0 B
default algorithm (init(prob, nothing)) 480 B 0 B 0 B

On Julia 1.10.11 the NoPivot path also measures 0 B with this PR (the BLAS paths keep the pre-existing allocating lu! there by design — getrf! has no preallocated-ipiv method before 1.11).

Tests

New test/Core/lu_refactorization.jl, run standalone on Julia 1.10.11, 1.12.6, and 1.13.0-rc1 (all pass), covering:

  • zero-allocation warm refactorization cycles for LUFactorization() (RowMaximum), LUFactorization(pivot = NoPivot()), and the default algorithm, with per-version ceilings asserted (== 0 wherever reuse is active) plus a direct cacheval.ipiv === ipiv_before identity check;
  • correctness vs A \ b across repeated refactorize+solve cycles;
  • singular refactorization returns ReturnCode.Failure without throwing (check = false semantics preserved) and the cache recovers on the next nonsingular matrix, for both RowMaximum and NoPivot;
  • DefaultLinearSolver's safetyfallback QR rescue still works when a warm alias_A = true cycle hands it a singular matrix (LU fails → column-pivoted QR least-squares, ReturnCode.Success);
  • generic (BigFloat) eltype correctness through the reuse branch;
  • pivot reallocation on size change via resize!(cache, n) and return to 0-alloc cycles at the new size.

Full local runs:

🤖 Generated with Claude Code

The alias_A = true dense LUFactorization refactorization path reuses the
cached ipiv through LAPACK.getrf! since SciML#1078, but only for strided BLAS
matrices with RowMaximum pivoting; NoPivot/RowNonZero pivoting and
non-BLAS element types still allocated a fresh pivot vector and LU
wrapper through lu! on every cache.A = X refactorization. Route those
through the vendored generic_lufact! with the cached pivot vector, which
accepts a preallocated ipiv on every supported Julia version (unlike the
getrf! method, which needs >= 1.11).

51x51 Matrix{Float64} warm refactorization (cache.A = X; solve!) with
alias_A = true, Julia 1.12.6: NoPivot drops 480 B -> 0 B per
refactorization (RowMaximum and the default algorithm were already 0 B
via SciML#1078; pre-SciML#1078 all paths allocated 480 B). On Julia 1.10 the
NoPivot path drops to 0 B as well, while the BLAS paths keep the
previous allocating lu! there by design.

Adds test/Core/lu_refactorization.jl covering: zero-allocation warm
refactorization cycles (RowMaximum, NoPivot, and the default algorithm),
pivot-vector identity across refactorizations, correctness against A \ b
across cycles, singular refactorization returning ReturnCode.Failure
without throwing and recovering on the next nonsingular matrix, the
default algorithm's QR safety fallback surviving a warm singular
refactorization, BigFloat (generic eltype) correctness, and pivot
reallocation after resize!.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 11, 2026 09:20
@ChrisRackauckas
ChrisRackauckas merged commit dd826e5 into SciML:main Jul 11, 2026
56 of 59 checks passed
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/LinearSolve.jl that referenced this pull request Jul 11, 2026
The zero-allocation assertions introduced in SciML#1082 assume the returned
LinearSolution wrapper is elided at testset scope, which holds on 1.12+
but not on 1.11 (which CI never runs: lts/1/pre = 1.10/1.12/pre). The
failure reproduces identically on unmodified main under 1.11 (48 bytes
there; 32 with the 5.0 lightweight solution). Allow the boxed wrapper
(<= 48 bytes) on 1.11 only; a leaked pivot vector would add
>= n * sizeof(Int) on top, so the reuse assertions remain meaningful.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
ChrisRackauckas added a commit that referenced this pull request Jul 11, 2026
…on.cache in solve! returns (#1083)

* Stop populating LinearSolution.cache in solve! returns

Every build_linear_solution call in src/, ext/, and lib/ now passes
nothing for the cache slot, and the direct LinearSolution construction
in the HYPRE extension does the same. The returned solution is a
lightweight value type: anyone needing the cache after solve!(cache)
already holds it.

The cleanup_petsc_cache!/cleanup_mumps_cache!/cleanup_superludist_cache!
methods taking a LinearSolution are removed since they read sol.cache;
use the LinearCache methods from an init/solve! cycle instead. Extension
docstrings are migrated accordingly.

With the cache no longer captured, the concretely-typed nothing-cache
wrapper is fully elided by escape analysis on Julia >= 1.11: the warm
aliased refactorize+solve loop is 0 bytes/call, and escaping returns
shrink from 48 to 32 bytes/call.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>

* Bump to 5.0.0 with sublibrary and test env compat updates

Breaking: reading .cache off the return of solve!/solve no longer works
(always nothing). LinearSolveAutotune -> 1.13.0 and LinearSolvePyAMG ->
1.3.0 widen their LinearSolve compat to 5.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>

* Add lightweight-solution regression tests; migrate tests off sol.cache

New test/Core/lightweight_solution.jl asserts cache === nothing across
solver paths (including failure and QR safety fallback returns), a
0-byte warm aliased refactorize+solve loop on Julia >= 1.11 (with the
documented pre-existing getrf! ipiv floor on 1.10), and correctness
across refactorizations.

Migrates remaining sol.cache readers to held-cache init/solve! cycles:
basictests Reuse precs, forwarddiff_overloads reinit!, and the PETSc/
MUMPS/SuperLUDIST/HYPRE extension suites (cleanup and reltol reads).
The KLU JET optimization test now passes with the cache dropped from
the solution, so its broken flag is removed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>

* Update docs to 5.0 lightweight-solution semantics

The caching interface tutorial no longer teaches retrieving the cache
via sol.cache, the preconditioner example keeps the cache from init,
and the PETSc/MUMPS memory-management examples clean up through the
held LinearCache.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>

* Gate lu_refactorization wrapper-elision ceilings on Julia 1.12

The zero-allocation assertions introduced in #1082 assume the returned
LinearSolution wrapper is elided at testset scope, which holds on 1.12+
but not on 1.11 (which CI never runs: lts/1/pre = 1.10/1.12/pre). The
failure reproduces identically on unmodified main under 1.11 (48 bytes
there; 32 with the 5.0 lightweight solution). Allow the boxed wrapper
(<= 48 bytes) on 1.11 only; a leaked pivot vector would add
>= n * sizeof(Int) on top, so the reuse assertions remain meaningful.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>

* Restore relative [sources] paths leaked from scratch dev environments

The AD and qa test Project.tomls had absolute scratch paths spliced in
by a Pkg.develop during this PR's work; the GPU one carried an even
older leak already on main. All three restored to {path = "../.."}.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>

---------

Co-authored-by: ChrisRackauckas-Claude <accounts@chrisrackauckas.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
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