Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Feb 5, 2025
1 parent 536611a commit 3f62e1b
Show file tree
Hide file tree
Showing 11 changed files with 761 additions and 224 deletions.
4 changes: 2 additions & 2 deletions benchmarks/applelu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ for i in 1:length(ns)
for j in 1:length(algs)
bt = @belapsed solve(prob, $(algs[j])).u setup=(prob = LinearProblem(copy(A),
copy(b);
u0 = copy(u0),
u0 = copy(u0),
alias = LinearAliasSpecifier(alias_A = true, alias_b = true)
))
))
push!(res[j], luflop(n) / bt / 1e9)
end
end
Expand Down
6 changes: 4 additions & 2 deletions docs/src/advanced/developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ basic machinery. A simplified version is:
```julia
struct MyLUFactorization{P} <: LinearSolve.SciMLLinearSolveAlgorithm end

function LinearSolve.init_cacheval(alg::MyLUFactorization, A, b, u, Pl, Pr, maxiters::Int, abstol, reltol,
function LinearSolve.init_cacheval(
alg::MyLUFactorization, A, b, u, Pl, Pr, maxiters::Int, abstol, reltol,
verbose::Bool, assump::LinearSolve.OperatorAssumptions)
lu!(convert(AbstractMatrix, A))
end
Expand All @@ -41,7 +42,8 @@ need to cache their own things, and so there's one value `cacheval` that is
for the algorithms to modify. The function:

```julia
init_cacheval(alg::MyLUFactorization, A, b, u, Pl, Pr, maxiters, abstol, reltol, verbose, assump)
init_cacheval(
alg::MyLUFactorization, A, b, u, Pl, Pr, maxiters, abstol, reltol, verbose, assump)
```

is what is called at `init` time to create the first `cacheval`. Note that this
Expand Down
3 changes: 2 additions & 1 deletion docs/src/basics/common_solver_opts.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ in order to give composability. These are also the options taken at `init` time.
The following are the options these algorithms take, along with their defaults.

## General Controls

- `alias::LinearAliasSpecifier`: Holds the fields `alias_A` and `alias_b` which specify
whether to alias the matrices `A` and `b` respectively. When these fields are `true`,
`A` and `b` can be written to and changed by the solver algorithm. When fields are `nothing`
the default behavior is used, which is to default to `true` when the algorithm is known
the default behavior is used, which is to default to `true` when the algorithm is known
not to modify the matrices, and false otherwise.
- `verbose`: Whether to print extra information. Defaults to `false`.
- `assumptions`: Sets the assumptions of the operator in order to effect the default
Expand Down
35 changes: 22 additions & 13 deletions ext/LinearSolveSparseArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ include("../src/KLU/klu.jl")

LinearSolve.issparsematrixcsc(A::AbstractSparseMatrixCSC) = true
LinearSolve.issparsematrix(A::AbstractSparseArray) = true
LinearSolve.make_SparseMatrixCSC(A::AbstractSparseArray) = SparseMatrixCSC(size(A)..., getcolptr(A), rowvals(A), nonzeros(A))
LinearSolve.makeempty_SparaseMatrixCSC(A::AbstractSparseArray) = SparseMatrixCSC(0, 0, [1], Int[], eltype(A)[])
function LinearSolve.make_SparseMatrixCSC(A::AbstractSparseArray)
SparseMatrixCSC(size(A)..., getcolptr(A), rowvals(A), nonzeros(A))
end
function LinearSolve.makeempty_SparaseMatrixCSC(A::AbstractSparseArray)
SparseMatrixCSC(0, 0, [1], Int[], eltype(A)[])
end

function LinearSolve.init_cacheval(alg::RFLUFactorization,
A::Union{AbstractSparseArray, LinearSolve.SciMLOperators.AbstractSciMLOperator}, b, u, Pl, Pr,
Expand All @@ -22,7 +26,6 @@ function LinearSolve.init_cacheval(alg::RFLUFactorization,
nothing, nothing
end


function LinearSolve.init_cacheval(
alg::QRFactorization, A::Symmetric{<:Number, <:SparseMatrixCSC}, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::Bool,
Expand All @@ -32,12 +35,12 @@ end

function LinearSolve.handle_sparsematrixcsc_lu(A::AbstractSparseMatrixCSC)
lu(SparseMatrixCSC(size(A)..., getcolptr(A), rowvals(A), nonzeros(A)),
check = false)
check = false)
end

function LinearSolve.defaultalg(
A::Symmetric{<:Number, <:SparseMatrixCSC}, b, ::OperatorAssumptions{Bool})
LinearSolve.DefaultLinearSolver(LinearSolve.DefaultAlgorithmChoice.CHOLMODFactorization)
LinearSolve.DefaultLinearSolver(LinearSolve.DefaultAlgorithmChoice.CHOLMODFactorization)
end

function LinearSolve.defaultalg(A::AbstractSparseMatrixCSC{Tv, Ti}, b,
Expand All @@ -61,14 +64,16 @@ end
const PREALLOCATED_UMFPACK = SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC(0, 0, [1],
Int[], Float64[]))

function LinearSolve.init_cacheval(alg::UMFPACKFactorization, A::SparseMatrixCSC{Float64, Int}, b, u,
function LinearSolve.init_cacheval(
alg::UMFPACKFactorization, A::SparseMatrixCSC{Float64, Int}, b, u,
Pl, Pr,
maxiters::Int, abstol, reltol,
verbose::Bool, assumptions::OperatorAssumptions)
PREALLOCATED_UMFPACK
end

function LinearSolve.init_cacheval(alg::UMFPACKFactorization, A::AbstractSparseArray, b, u, Pl, Pr,
function LinearSolve.init_cacheval(
alg::UMFPACKFactorization, A::AbstractSparseArray, b, u, Pl, Pr,
maxiters::Int, abstol,
reltol,
verbose::Bool, assumptions::OperatorAssumptions)
Expand All @@ -78,7 +83,8 @@ function LinearSolve.init_cacheval(alg::UMFPACKFactorization, A::AbstractSparseA
rowvals(A), nonzeros(A)))
end

function SciMLBase.solve!(cache::LinearSolve.LinearCache, alg::UMFPACKFactorization; kwargs...)
function SciMLBase.solve!(
cache::LinearSolve.LinearCache, alg::UMFPACKFactorization; kwargs...)
A = cache.A
A = convert(AbstractMatrix, A)
if cache.isfresh
Expand Down Expand Up @@ -116,14 +122,16 @@ end
const PREALLOCATED_KLU = KLU.KLUFactorization(SparseMatrixCSC(0, 0, [1], Int[],
Float64[]))

function LinearSolve.init_cacheval(alg::KLUFactorization, A::SparseMatrixCSC{Float64, Int}, b, u, Pl,
function LinearSolve.init_cacheval(
alg::KLUFactorization, A::SparseMatrixCSC{Float64, Int}, b, u, Pl,
Pr,
maxiters::Int, abstol, reltol,
verbose::Bool, assumptions::OperatorAssumptions)
PREALLOCATED_KLU
end

function LinearSolve.init_cacheval(alg::KLUFactorization, A::AbstractSparseArray, b, u, Pl, Pr,
function LinearSolve.init_cacheval(
alg::KLUFactorization, A::AbstractSparseArray, b, u, Pl, Pr,
maxiters::Int, abstol,
reltol,
verbose::Bool, assumptions::OperatorAssumptions)
Expand Down Expand Up @@ -182,7 +190,7 @@ function LinearSolve.init_cacheval(alg::NormalCholeskyFactorization,
Symmetric{<:Number, <:AbstractSparseArray}}, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::Bool,
assumptions::OperatorAssumptions)
LinearSolve.ArrayInterface.cholesky_instance(convert(AbstractMatrix, A))
LinearSolve.ArrayInterface.cholesky_instance(convert(AbstractMatrix, A))
end

# Specialize QR for the non-square case
Expand Down Expand Up @@ -221,7 +229,8 @@ function pattern_changed(fact, A::SparseArrays.SparseMatrixCSC)
fact.rowval)
end

function LinearSolve.defaultalg(A::AbstractSparseMatrixCSC{<:Union{Float64, ComplexF64}, Ti}, b,
function LinearSolve.defaultalg(
A::AbstractSparseMatrixCSC{<:Union{Float64, ComplexF64}, Ti}, b,
assump::OperatorAssumptions{Bool}) where {Ti}
if assump.issq
if length(b) <= 10_000 && length(nonzeros(A)) / length(A) < 2e-4
Expand All @@ -242,4 +251,4 @@ LinearSolve.PrecompileTools.@compile_workload begin
sol = solve(prob, UMFPACKFactorization())
end

end
end
11 changes: 7 additions & 4 deletions ext/LinearSolveSparspakExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ using SparseArrays: AbstractSparseMatrixCSC, nonzeros, rowvals, getcolptr
const PREALLOCATED_SPARSEPAK = sparspaklu(SparseMatrixCSC(0, 0, [1], Int[], Float64[]),
factorize = false)

function LinearSolve.init_cacheval(::SparspakFactorization, A::SparseMatrixCSC{Float64, Int}, b, u, Pl,
function LinearSolve.init_cacheval(
::SparspakFactorization, A::SparseMatrixCSC{Float64, Int}, b, u, Pl,
Pr, maxiters::Int, abstol,
reltol,
verbose::Bool, assumptions::OperatorAssumptions)
PREALLOCATED_SPARSEPAK
end

function LinearSolve.init_cacheval(::SparspakFactorization, A, b, u, Pl, Pr, maxiters::Int, abstol,
function LinearSolve.init_cacheval(
::SparspakFactorization, A, b, u, Pl, Pr, maxiters::Int, abstol,
reltol,
verbose::Bool, assumptions::OperatorAssumptions)
A = convert(AbstractMatrix, A)
Expand All @@ -30,7 +32,8 @@ function LinearSolve.init_cacheval(::SparspakFactorization, A, b, u, Pl, Pr, max
end
end

function SciMLBase.solve!(cache::LinearSolve.LinearCache, alg::SparspakFactorization; kwargs...)
function SciMLBase.solve!(
cache::LinearSolve.LinearCache, alg::SparspakFactorization; kwargs...)
A = cache.A
if cache.isfresh
if cache.cacheval !== nothing && alg.reuse_symbolic
Expand All @@ -56,4 +59,4 @@ LinearSolve.PrecompileTools.@compile_workload begin
sol = solve(prob, SparspakFactorization())
end

end
end
Loading

0 comments on commit 3f62e1b

Please sign in to comment.