Complete allocation-free dense LU refactorization: generic-kernel pivot reuse + regression tests#1082
Merged
ChrisRackauckas merged 1 commit intoJul 11, 2026
Conversation
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
marked this pull request as ready for review
July 11, 2026 09:20
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>
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.
Motivation
SciML/NonlinearSolve.jl#1038 makes every dense Newton iteration hit the
alias_A = truedense-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 freshipiv/LU-wrapper allocation for the LAPACK fast path (strided BLAS matrices +RowMaximum, Julia >= 1.11) via the preallocated-ipivLAPACK.getrf!. This PR completes the picture:alias_A = true,NoPivot/RowNonZeropivoting and non-BLAS element types still went throughlu!(A, pivot; check = false), which allocates a fresh pivot vector andLUwrapper on every refactorization. They now call the vendoredgeneric_lufact!directly with the cachedipiv(the same kernel/check = false/info semanticslu!dispatches to for these inputs, and the same patternGenericLUFactorizationalready uses). Unlike thegetrf!method this works on every supported Julia version, so this path is allocation-free on 1.10 (LTS) too. Size changes fall back tolu!(which produces a right-sized pivot that is then cached), exactly like the existing BLAS branch.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.6alias_A = true)LUFactorization()(RowMaximum)LUFactorization(pivot = NoPivot())init(prob, nothing))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-ipivmethod 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:LUFactorization()(RowMaximum),LUFactorization(pivot = NoPivot()), and the default algorithm, with per-version ceilings asserted (== 0wherever reuse is active) plus a directcacheval.ipiv === ipiv_beforeidentity check;A \ bacross repeated refactorize+solve cycles;ReturnCode.Failurewithout throwing (check = falsesemantics preserved) and the cache recovers on the next nonsingular matrix, for both RowMaximum and NoPivot;DefaultLinearSolver'ssafetyfallbackQR rescue still works when a warmalias_A = truecycle hands it a singular matrix (LU fails → column-pivoted QR least-squares,ReturnCode.Success);resize!(cache, n)and return to 0-alloc cycles at the new size.Full local runs:
GROUP=Core Pkg.test()on Julia 1.12.6: passed ("Testing LinearSolve tests passed"), including the new testset (53/53).GROUP=QA Pkg.test(): fails on unmodified main with a pre-existing ExplicitImportsno_implicit_importserror (unqualifiedissuccessfrom GESVFactorization + dense LU pivot-buffer reuse (v4.2.0) #1078); reproduced on clean main and fixed separately in Qualify issuccess as LinearAlgebra.issuccess to fix QA ExplicitImports failure #1081. All other QA checks pass with this branch.🤖 Generated with Claude Code