Skip to content

Commit

Permalink
Defaults for Banded Matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Oct 16, 2023
1 parent 0843b53 commit 6fe3b2a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LinearSolve"
uuid = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae"
authors = ["SciML"]
version = "2.10.0"
version = "2.11.0"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down Expand Up @@ -31,6 +31,7 @@ SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"

[weakdeps]
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
BlockDiagonals = "0a1fb500-61f7-11e9-3c65-f5ef3456f9f0"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand All @@ -42,6 +43,7 @@ Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
Pardiso = "46dd5b70-b6fb-5a00-ae2d-e8fea33afaf2"

[extensions]
LinearSolveBandedMatricesExt = "BandedMatrices"
LinearSolveBlockDiagonalsExt = "BlockDiagonals"
LinearSolveCUDAExt = "CUDA"
LinearSolveEnzymeExt = "Enzyme"
Expand All @@ -54,6 +56,7 @@ LinearSolvePardisoExt = "Pardiso"

[compat]
ArrayInterface = "7.4.11"
BandedMatrices = "1"
BlockDiagonals = "0.1"
ConcreteStructs = "0.2"
DocStringExtensions = "0.8, 0.9"
Expand All @@ -80,6 +83,7 @@ UnPack = "1"
julia = "1.6"

[extras]
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
BlockDiagonals = "0a1fb500-61f7-11e9-3c65-f5ef3456f9f0"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
Expand Down
58 changes: 58 additions & 0 deletions ext/LinearSolveBandedMatricesExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module LinearSolveBandedMatricesExt

using BandedMatrices, LinearAlgebra, LinearSolve
import LinearSolve: defaultalg,
do_factorization, init_cacheval, DefaultLinearSolver, DefaultAlgorithmChoice

# Defaults for BandedMatrices
function defaultalg(A::BandedMatrix, b, ::OperatorAssumptions)
return DefaultLinearSolver(DefaultAlgorithmChoice.QRFactorization)

Check warning on line 9 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L8-L9

Added lines #L8 - L9 were not covered by tests
end

function defaultalg(A::Symmetric{<:Number, <:BandedMatrix}, b, ::OperatorAssumptions)
return DefaultLinearSolver(DefaultAlgorithmChoice.CholeskyFactorization)

Check warning on line 13 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L12-L13

Added lines #L12 - L13 were not covered by tests
end

# BandedMatrices `qr` doesn't allow other args without causing an ambiguity
do_factorization(alg::QRFactorization, A::BandedMatrix, b, u) = alg.inplace ? qr!(A) : qr(A)

Check warning on line 17 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L17

Added line #L17 was not covered by tests

function do_factorization(alg::LUFactorization, A::BandedMatrix, b, u)
_pivot = alg.pivot isa NoPivot ? Val(false) : Val(true)
return lu!(A, _pivot; check = false)

Check warning on line 21 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L19-L21

Added lines #L19 - L21 were not covered by tests
end

# For BandedMatrix
for alg in (:SVDFactorization, :MKLLUFactorization, :DiagonalFactorization,
:SparspakFactorization, :KLUFactorization, :UMFPACKFactorization,
:GenericLUFactorization, :RFLUFactorization, :BunchKaufmanFactorization,
:CHOLMODFactorization, :NormalCholeskyFactorization, :LDLtFactorization,
:AppleAccelerateLUFactorization, :CholeskyFactorization)
@eval begin
function init_cacheval(::$(alg), ::BandedMatrix, b, u, Pl, Pr, maxiters::Int,

Check warning on line 31 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L31

Added line #L31 was not covered by tests
abstol, reltol, verbose::Bool, assumptions::OperatorAssumptions)
return nothing

Check warning on line 33 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L33

Added line #L33 was not covered by tests
end
end
end

function init_cacheval(::LUFactorization, A::BandedMatrix, b, u, Pl, Pr, maxiters::Int,

Check warning on line 38 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L38

Added line #L38 was not covered by tests
abstol, reltol, verbose::Bool, assumptions::OperatorAssumptions)
return lu(similar(A, 0, 0))

Check warning on line 40 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L40

Added line #L40 was not covered by tests
end

# For Symmetric BandedMatrix
for alg in (:SVDFactorization, :MKLLUFactorization, :DiagonalFactorization,
:SparspakFactorization, :KLUFactorization, :UMFPACKFactorization,
:GenericLUFactorization, :RFLUFactorization, :BunchKaufmanFactorization,
:CHOLMODFactorization, :NormalCholeskyFactorization,
:AppleAccelerateLUFactorization, :QRFactorization, :LUFactorization)
@eval begin
function init_cacheval(::$(alg), ::Symmetric{<:Number, <:BandedMatrix}, b, u, Pl,

Check warning on line 50 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L50

Added line #L50 was not covered by tests
Pr, maxiters::Int, abstol, reltol, verbose::Bool,
assumptions::OperatorAssumptions)
return nothing

Check warning on line 53 in ext/LinearSolveBandedMatricesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/LinearSolveBandedMatricesExt.jl#L53

Added line #L53 was not covered by tests
end
end
end

end
2 changes: 1 addition & 1 deletion src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ However, in practice this computation is very expensive and thus not possible fo
Therefore, OperatorCondition lets one share to LinearSolve the expected conditioning. The higher the
expected condition number, the safer the algorithm needs to be and thus there is a trade-off between
numerical performance and stability. By default the method assumes the operator may be ill-conditioned
for the standard linear solvers to converge (such as LU-factorization), though more extreme
for the standard linear solvers to converge (such as LU-factorization), though more extreme
ill-conditioning or well-conditioning could be the case and specified through this assumption.
"""
EnumX.@enumx OperatorCondition begin
Expand Down
2 changes: 1 addition & 1 deletion src/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function defaultalg(A, b, assump::OperatorAssumptions)
DefaultAlgorithmChoice.GenericLUFactorization
elseif VERSION >= v"1.8" && appleaccelerate_isavailable()
DefaultAlgorithmChoice.AppleAccelerateLUFactorization
elseif (length(b) <= 100 || (isopenblas() && length(b) <= 500) ||
elseif (length(b) <= 100 || (isopenblas() && length(b) <= 500) ||
(usemkl && length(b) <= 200)) &&
(A === nothing ? eltype(b) <: Union{Float32, Float64} :
eltype(A) <: Union{Float32, Float64})
Expand Down

0 comments on commit 6fe3b2a

Please sign in to comment.