Skip to content

[Do Not Merge] Preallocate more caches #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 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.23.2"
version = "2.23.3"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
3 changes: 2 additions & 1 deletion src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ function Base.setproperty!(cache::LinearCache, name::Symbol, x)
update_cacheval!(cache, :b, x)
elseif name === :cacheval && cache.alg isa DefaultLinearSolver
@assert cache.cacheval isa DefaultLinearSolverInit
return setfield!(cache.cacheval, Symbol(cache.alg.alg), x)
return __setfield!(cache.cacheval, cache.alg, x)
# return setfield!(cache.cacheval, Symbol(cache.alg.alg), x)
end
setfield!(cache, name, x)
end
Expand Down
24 changes: 21 additions & 3 deletions src/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ mutable struct DefaultLinearSolverInit{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10,
KrylovJL_LSMR::T21
end

@generated function __setfield!(cache::DefaultLinearSolverInit, alg::DefaultLinearSolver, v)
ex = :()
for alg in first.(EnumX.symbol_map(DefaultAlgorithmChoice.T))
newex = quote
setfield!(cache, $(Meta.quot(alg)), v)
end
alg_enum = getproperty(LinearSolve.DefaultAlgorithmChoice, alg)
ex = if ex == :()
Expr(:elseif, :(alg.alg == $(alg_enum)), newex,
:(error("Algorithm Choice not Allowed")))
else
Expr(:elseif, :(alg.alg == $(alg_enum)), newex, ex)
end
end
ex = Expr(:if, ex.args...)
end

# Legacy fallback
# For SciML algorithms already using `defaultalg`, all assume square matrix.
defaultalg(A, b) = defaultalg(A, b, OperatorAssumptions(true))
Expand Down Expand Up @@ -159,7 +176,7 @@ function defaultalg(A, b, assump::OperatorAssumptions{Bool})
(__conditioning(assump) === OperatorCondition.IllConditioned ||
__conditioning(assump) === OperatorCondition.WellConditioned)
if length(b) <= 10
DefaultAlgorithmChoice.GenericLUFactorization
DefaultAlgorithmChoice.RFLUFactorization
elseif appleaccelerate_isavailable()
DefaultAlgorithmChoice.AppleAccelerateLUFactorization
elseif (length(b) <= 100 || (isopenblas() && length(b) <= 500) ||
Expand Down Expand Up @@ -345,11 +362,12 @@ end
retcode = sol.retcode,
iters = sol.iters, stats = sol.stats)
end
alg_enum = getproperty(LinearSolve.DefaultAlgorithmChoice, alg)
ex = if ex == :()
Expr(:elseif, :(Symbol(alg.alg) === $(Meta.quot(alg))), newex,
Expr(:elseif, :(alg.alg == $(alg_enum)), newex,
:(error("Algorithm Choice not Allowed")))
else
Expr(:elseif, :(Symbol(alg.alg) === $(Meta.quot(alg))), newex, ex)
Expr(:elseif, :(alg.alg == $(alg_enum)), newex, ex)
end
end
ex = Expr(:if, ex.args...)
Expand Down
22 changes: 18 additions & 4 deletions src/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,24 @@ function init_cacheval(alg::QRFactorization, A, b, u, Pl, Pr,
ArrayInterface.qr_instance(convert(AbstractMatrix, A), alg.pivot)
end

const PREALLOCATED_QR_ColumnNorm = ArrayInterface.qr_instance(rand(1, 1), ColumnNorm())

function init_cacheval(alg::QRFactorization{ColumnNorm}, A::Matrix{Float64}, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::Bool, assumptions::OperatorAssumptions)
return PREALLOCATED_QR_ColumnNorm
end

function init_cacheval(alg::QRFactorization, A::Union{<:Adjoint, <:Transpose}, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::Bool, assumptions::OperatorAssumptions)
A isa GPUArraysCore.AnyGPUArray && return qr(A)
return qr(A, alg.pivot)
end

const PREALLOCATED_QR = ArrayInterface.qr_instance(rand(1, 1))
const PREALLOCATED_QR_NoPivot = ArrayInterface.qr_instance(rand(1, 1))

function init_cacheval(alg::QRFactorization{NoPivot}, A::Matrix{Float64}, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::Bool,
assumptions::OperatorAssumptions)
PREALLOCATED_QR
maxiters::Int, abstol, reltol, verbose::Bool, assumptions::OperatorAssumptions)
return PREALLOCATED_QR_NoPivot
end

function init_cacheval(alg::QRFactorization, A::AbstractSciMLOperator, b, u, Pl, Pr,
Expand Down Expand Up @@ -1013,6 +1019,14 @@ function init_cacheval(alg::NormalCholeskyFactorization, A, b, u, Pl, Pr,
return ArrayInterface.cholesky_instance(Symmetric(Matrix{eltype(A)}(undef,0,0)), alg.pivot)
end

const PREALLOCATED_NORMALCHOLESKY_SYMMETRIC = ArrayInterface.cholesky_instance(
Symmetric(rand(1, 1)), NoPivot())

function init_cacheval(alg::NormalCholeskyFactorization, A::Matrix{Float64}, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::Bool, assumptions::OperatorAssumptions)
return PREALLOCATED_NORMALCHOLESKY_SYMMETRIC
end

function init_cacheval(alg::NormalCholeskyFactorization,
A::Union{Diagonal, AbstractSciMLOperator}, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::Bool,
Expand Down
2 changes: 1 addition & 1 deletion test/default_algs.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using LinearSolve, LinearAlgebra, SparseArrays, Test, JET
@test LinearSolve.defaultalg(nothing, zeros(3)).alg ===
LinearSolve.DefaultAlgorithmChoice.GenericLUFactorization
LinearSolve.DefaultAlgorithmChoice.RFLUFactorization
prob = LinearProblem(rand(3, 3), rand(3))
solve(prob)

Expand Down