Skip to content

Always use QR with AlmostBandedMatrices - #1324

Merged
ChrisRackauckas merged 1 commit into
SciML:mainfrom
ErikQQY:qqy/dense_boundary
Sep 19, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:mainfrom
ErikQQY:qqy/dense_boundary

Conversation

@ErikQQY

@ErikQQY ErikQQY commented Sep 18, 2026

Copy link
Copy Markdown
Member

Need SciML/FastAlmostBandedMatrices.jl#94

Square AlmostBandedMatrix systems defaulted to DirectLdiv!, which recomputed the QR factorization on every solve.
Switching the default to QRFactorization allows LinearSolve to cache the factorization and reuse it when only the right-hand side changes. This avoids repeated factorization work and allocations, reducing runtime and memory allocation when solving multiple right-hand sides through the same cache.

n = 2048
B = BandedMatrix(-1 => -ones(n - 1), 0 => fill(2.0, n), 1 => -ones(n - 1))
L = spzeros(1, n)
L[1, 1], L[1, n] = 1.0, 0.25
A = AlmostBandedMatrix(B, L)
reference = sparse(B)
reference[1:1, :] = L
X = randn(MersenneTwister(42), n, 16)
bs = [reference * x for x in eachcol(X)]

function solve_many(A, bs)
    cache = init(LinearProblem(A, first(bs)))
    for b in bs
         cache.b = b
         solve!(cache)
    end
    return cache.u
end
println(": n = ", n, ", 16 right-hand sides (time / allocations / allocated memory)")
print("QR factorization: ")
@btime qr($A) samples=20 evals=1 seconds=3
print("Batch solve:      ")
@btime solve_many($A, $bs) samples=20 evals=1 seconds=3

before

n = 2048, 16 right-hand sides (time / allocations / allocated memory)
QR factorization:   455.750 μs (42 allocations: 242.02 KiB)
Batch solve:        11.427 ms (849 allocations: 4.20 MiB)

after

n = 2048, 16 right-hand sides (time / allocations / allocated memory)
QR factorization:   157.000 μs (15 allocations: 160.38 KiB)
Batch solve:        1.913 ms (188 allocations: 606.27 KiB)

@ChrisRackauckas
ChrisRackauckas merged commit e4078c3 into SciML:main Sep 19, 2026
64 of 70 checks passed
ChrisRackauckas added a commit that referenced this pull request Sep 21, 2026
Ships everything on main since 5.17.7:

- Refactor PureKLU with the pivots reused unless growth says otherwise (#1330) — restores the fast refactorization path; behaviour differs from 5.17.4/5.17.5, which always re-pivoted. Raises the PureKLU floor to 1.6 (registered).
- Keep a sparse Dual operand's pattern through the primal extraction (#1329) — fixes the snlu! pattern mismatch; map_stored! now throws DimensionMismatch on a size mismatch instead of silently corrupting the destination, and partial_vals! no longer prunes zero-derivative entries.
- Route triangular matrices to DirectLdiv! in the default algorithm (#1336).
- Apply InvPreconditioner through a temporary instead of aliasing its input (#1323).
- Always use QR with AlmostBandedMatrices (#1324).
- Route Hermitian like Symmetric instead of always reaching for Cholesky (#1327).
- Add the missing trailing newline to test/Core/banded.jl (#1331).

Minor rather than patch: the default-algorithm routing changes and the PureKLU re-pivot change what existing code computes or how it is factorized, and map_stored! gains an error path where it previously produced (wrong) results.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude <noreply@anthropic.com>
Agent-Harness: Claude Code 2.1.273
Agent-Model: claude-opus-5[1m]
Agent-Session: 6a01e420-3436-4518-83cc-6cd853283cfb (local transcript; this harness publishes no conversation URL)
ChrisRackauckas added a commit that referenced this pull request Sep 21, 2026
Ships everything on main since 5.17.7:

- Refactor PureKLU with the pivots reused unless growth says otherwise (#1330) — restores the fast refactorization path; behaviour differs from 5.17.4/5.17.5, which always re-pivoted. Raises the PureKLU floor to 1.6 (registered).
- Keep a sparse Dual operand's pattern through the primal extraction (#1329) — fixes the snlu! pattern mismatch; map_stored! now throws DimensionMismatch on a size mismatch instead of silently corrupting the destination, and partial_vals! no longer prunes zero-derivative entries.
- Route triangular matrices to DirectLdiv! in the default algorithm (#1336).
- Apply InvPreconditioner through a temporary instead of aliasing its input (#1323).
- Always use QR with AlmostBandedMatrices (#1324).
- Route Hermitian like Symmetric instead of always reaching for Cholesky (#1327).
- Add the missing trailing newline to test/Core/banded.jl (#1331).

Minor rather than patch: the default-algorithm routing changes and the PureKLU re-pivot change what existing code computes or how it is factorized, and map_stored! gains an error path where it previously produced (wrong) results.



Agent-Harness: Claude Code 2.1.273
Agent-Model: claude-opus-5[1m]
Agent-Session: 6a01e420-3436-4518-83cc-6cd853283cfb (local transcript; this harness publishes no conversation URL)

Co-authored-by: Claude <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