Skip to content

Commit

Permalink
Change LDLFactorizations to be a weakdep (#326)
Browse files Browse the repository at this point in the history
* Move LDLFactorizations to weak deps

* fix weak deps
  • Loading branch information
tmigot authored May 3, 2024
1 parent c67bb81 commit 36b496d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "2.7.0"

[deps]
FastClosures = "9aa1b823-49e4-5ca5-8b0f-3971ec8bab6a"
LDLFactorizations = "40e66cde-538c-5869-a4ad-c39174c6795b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Expand All @@ -14,10 +13,12 @@ TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
LDLFactorizations = "40e66cde-538c-5869-a4ad-c39174c6795b"

[extensions]
LinearOperatorsChainRulesCoreExt = "ChainRulesCore"
LinearOperatorsCUDAExt = "CUDA"
LinearOperatorsLDLFactorizationsExt = "LDLFactorizations"

[compat]
ChainRulesCore = "1"
Expand All @@ -34,4 +35,4 @@ julia = "^1.6.0"
[extras]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"

LDLFactorizations = "40e66cde-538c-5869-a4ad-c39174c6795b"
34 changes: 34 additions & 0 deletions ext/LinearOperatorsLDLFactorizationsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module LinearOperatorsLDLFactorizationsExt

using FastClosures, LDLFactorizations, LinearAlgebra, LinearOperators, SparseArrays

function LinearOperators.opLDL(M::AbstractMatrix; check::Bool = false)
(m, n) = size(M)
m == n || throw(LinearOperatorException("shape mismatch"))
if check
check_hermitian(M) || throw(LinearOperatorException("matrix is not Hermitian"))
end
LDL = ldlt(M)
prod! = @closure (res, v, α, β) -> LinearOperators.mulFact!(res, LDL, v, α, β)
tprod! = @closure (res, u, α, β) -> LinearOperators.tmulFact!(res, LDL, u, α, β) # M.' = conj(M)
ctprod! = @closure (res, w, α, β) -> LinearOperators.mulFact!(res, LDL, w, α, β)
S = eltype(LDL)
return LinearOperator{S}(m, m, isreal(M), true, prod!, tprod!, ctprod!)
#TODO: use iterative refinement.
end

function LinearOperators.opLDL(M::Symmetric{T, SparseMatrixCSC{T, Int}}; check::Bool = false) where {T <: Real}
(m, n) = size(M)
m == n || throw(LinearOperatorException("shape mismatch"))
if check
check_hermitian(M) || throw(LinearOperatorException("matrix is not Hermitian"))
end
LDL = ldl(M)
prod! = @closure (res, v) -> ldiv!(res, LDL, v)
tprod! = @closure (res, u) -> ldiv!(res, LDL, u) # M.' = conj(M)
ctprod! = @closure (res, w) -> ldiv!(res, LDL, w)
S = eltype(LDL)
return LinearOperator{S}(m, m, isreal(M), true, prod!, tprod!, ctprod!)
end

end # module
4 changes: 3 additions & 1 deletion src/LinearOperators.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module LinearOperators

using FastClosures, LinearAlgebra, Printf, SparseArrays
using LDLFactorizations

# Basic defitions
include("abstract.jl")
Expand Down Expand Up @@ -41,6 +40,9 @@ end
Requires.@require CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" begin
include("../ext/LinearOperatorsCUDAExt.jl")
end
Requires.@require LDLFactorizations = "40e66cde-538c-5869-a4ad-c39174c6795b" begin
include("../ext/LinearOperatorsLDLFactorizationsExt.jl")
end
end
end

Expand Down
32 changes: 3 additions & 29 deletions src/linalg.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export opInverse, opCholesky, opLDL, opHouseholder, opHermitian
export opInverse, opCholesky, opHouseholder, opLDL, opHermitian

function mulFact!(res, F, v, α, β::T) where {T}
if β == zero(T)
Expand Down Expand Up @@ -67,38 +67,12 @@ argument will perform a cheap hermicity check.
If M is sparse and real, then only the upper triangle should be stored in order to use
[`LDLFactorizations.jl`](https://github.com/JuliaSmoothOptimizers/LDLFactorizations.jl):
using LDLFactorizations
triu!(M)
opLDL(Symmetric(M, :U))
"""
function opLDL(M::AbstractMatrix; check::Bool = false)
(m, n) = size(M)
m == n || throw(LinearOperatorException("shape mismatch"))
if check
check_hermitian(M) || throw(LinearOperatorException("matrix is not Hermitian"))
end
LDL = ldlt(M)
prod! = @closure (res, v, α, β) -> mulFact!(res, LDL, v, α, β)
tprod! = @closure (res, u, α, β) -> tmulFact!(res, LDL, u, α, β) # M.' = conj(M)
ctprod! = @closure (res, w, α, β) -> mulFact!(res, LDL, w, α, β)
S = eltype(LDL)
return LinearOperator{S}(m, m, isreal(M), true, prod!, tprod!, ctprod!)
#TODO: use iterative refinement.
end

function opLDL(M::Symmetric{T, SparseMatrixCSC{T, Int}}; check::Bool = false) where {T <: Real}
(m, n) = size(M)
m == n || throw(LinearOperatorException("shape mismatch"))
if check
check_hermitian(M) || throw(LinearOperatorException("matrix is not Hermitian"))
end
LDL = ldl(M)
prod! = @closure (res, v) -> ldiv!(res, LDL, v)
tprod! = @closure (res, u) -> ldiv!(res, LDL, u) # M.' = conj(M)
ctprod! = @closure (res, w) -> ldiv!(res, LDL, w)
S = eltype(LDL)
return LinearOperator{S}(m, m, isreal(M), true, prod!, tprod!, ctprod!)
end
function opLDL end

function mulHouseholder!(res, h, v, α, β::T) where {T}
if β == zero(T)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Arpack, Test, TestSetExtensions, LinearOperators
using LinearAlgebra, SparseArrays
using LinearAlgebra, LDLFactorizations, SparseArrays
using Zygote
include("test_aux.jl")

Expand Down

0 comments on commit 36b496d

Please sign in to comment.