Skip to content

Support Krylov.jl v0.10 #156

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
[compat]
CodecBzip2 = "0.7.2, 0.8"
CodecZlib = "0.7.0"
Krylov = "0.8, 0.9"
LDLFactorizations = "0.8, 0.9, 0.10"
Krylov = "0.10"
LDLFactorizations = "0.10"
LinearOperators = "2.0"
MathOptInterface = "1"
QPSReader = "0.2"
Expand Down
49 changes: 15 additions & 34 deletions src/KKT/Krylov/defs.jl
Original file line number Diff line number Diff line change
@@ -1,48 +1,29 @@
const _KRYLOV_SPD = Union{
Krylov.CgSolver,
Krylov.CrSolver,
Krylov.CgWorkspace,
Krylov.CrWorkspace,
Krylov.CarWorkspace,
}

const _KRYLOV_SID = Union{
Krylov.MinresSolver,
Krylov.MinresQlpSolver,
Krylov.SymmlqSolver
Krylov.MinresWorkspace,
Krylov.MinaresWorkspace,
Krylov.MinresQlpWorkspace,
Krylov.SymmlqWorkspace
}

const _KRYLOV_SQD = Union{
Krylov.TricgSolver,
Krylov.TrimrSolver,
Krylov.TricgWorkspace,
Krylov.TrimrWorkspace,
}

const _KRYLOV_LN = Union{
Krylov.LnlqSolver,
Krylov.CraigSolver,
Krylov.CraigmrSolver,
Krylov.LnlqWorkspace,
Krylov.CraigWorkspace,
Krylov.CraigmrWorkspace,
}

const _KRYLOV_LS = Union{
Krylov.LslqSolver,
Krylov.LsqrSolver,
Krylov.LsmrSolver,
Krylov.LslqWorkspace,
Krylov.LsqrWorkspace,
Krylov.LsmrWorkspace,
}

# Helper functions
for (KS, fun) in [
(Krylov.CgSolver,Krylov.cg!)
(Krylov.CrSolver,Krylov.cr!)
(Krylov.MinresSolver,Krylov.minres!)
(Krylov.MinresQlpSolver,Krylov.minres_qlp!)
(Krylov.SymmlqSolver,Krylov.symmlq!)
(Krylov.TricgSolver,Krylov.tricg!)
(Krylov.TrimrSolver,Krylov.trimr!)
(Krylov.LnlqSolver,Krylov.lnlq!)
(Krylov.CraigSolver,Krylov.craig!)
(Krylov.CraigmrSolver,Krylov.craigmr!)
(Krylov.LslqSolver,Krylov.lslq!)
(Krylov.LsqrSolver,Krylov.lsqr!)
(Krylov.LsmrSolver,Krylov.lsmr!)
]
@eval begin
@inline _krylov!(solver::$KS, args...; kwargs...) = $(fun)(solver, args...; kwargs...)
end
end
12 changes: 6 additions & 6 deletions src/KKT/Krylov/krylov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import ..KKT: setup, update!, solve!
include("defs.jl")

"""
Backend{KS<:Krylov.KrylovSolver,V<:AbstractVector}
Backend{KS<:Krylov.KrylovWorkspace,V<:AbstractVector}

[Krylov.jl](https://github.com/JuliaSmoothOptimizers/Krylov.jl)-based backend for solving linear systems.

The type is parametrized by:
* `KS<:Krylov.KrylovSolver`: workspace type for the Krylov method.
* `KS<:Krylov.KrylovWorkspace`: workspace type for the Krylov method.
Also defines the Krylov method to be used.
* `V<:AbstractVector`: the vector storage type used within the Krylov method.
This should be set to `Vector{T}` (for arithmetic `T`) unless, e.g., one uses a GPU.
Expand All @@ -31,15 +31,15 @@ See the [Krylov.jl documentation](https://juliasmoothoptimizers.github.io/Krylov
All the following examples assume everything runs on a CPU in `Float64` arithmetic.
* To use the conjugate gradient:
```julia
backend = KKT.TlpKrylov.Backend(Krylov.CgSolver, Vector{Float64})
backend = KKT.TlpKrylov.Backend(Krylov.CgWorkspace, Vector{Float64})
```
* To use MINRES:
```julia
backend = KKT.TlpKrylov.Backend(Krylov.MinresSolver, Vector{Float64})
backend = KKT.TlpKrylov.Backend(Krylov.MinresWorkspace, Vector{Float64})
```
"""
struct Backend{KS,V} <: AbstractKKTBackend
krylov_solver::Type{KS}
struct Backend{KW,V} <: AbstractKKTBackend
krylov_workspace::Type{KW}
vector_storage::Type{V}
end

Expand Down
20 changes: 10 additions & 10 deletions src/KKT/Krylov/sid.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
SIDSolver
"""
mutable struct SIDSolver{T,V,Ta,KL,KS} <: AbstractKrylovSolver{T}
mutable struct SIDSolver{T,V,Ta,KL,KW} <: AbstractKrylovSolver{T}
# Problem data
m::Int
n::Int
Expand All @@ -21,15 +21,15 @@ mutable struct SIDSolver{T,V,Ta,KL,KS} <: AbstractKrylovSolver{T}
# Krylov solver & related options
atol::T
rtol::T
krylov_solver::KS
krylov_workspace::KW

# TODO: preconditioner
end

backend(kkt::SIDSolver) = "$(typeof(kkt.krylov_solver))"
backend(kkt::SIDSolver) = "$(typeof(kkt.krylov_workspace))"
linear_system(kkt::SIDSolver) = "K2"

function setup(A, ::K2, backend::Backend{KS,V}) where{KS<:_KRYLOV_SID,V}
function setup(A, ::K2, backend::Backend{KW,V}) where{KW<:_KRYLOV_SID,V}
Ta = typeof(A)
T = eltype(A)
T == eltype(V) || error("eltype(A)=$T incompatible with eltype of Krylov vector storage $V.")
Expand Down Expand Up @@ -66,15 +66,15 @@ function setup(A, ::K2, backend::Backend{KS,V}) where{KS<:_KRYLOV_SID,V}
# Allocate Krylov solver's workspace
atol = sqrt(eps(T))
rtol = sqrt(eps(T))
krylov_solver = KS(m+n, m+n, V)
krylov_workspace = KW(m+n, m+n, V)

return SIDSolver{T,V,Ta,typeof(opK),typeof(krylov_solver)}(
return SIDSolver{T,V,Ta,typeof(opK),typeof(krylov_workspace)}(
m, n, A,
θ, regP, regD,
Θp, Θd, ξ,
opK,
atol, rtol,
krylov_solver
krylov_workspace
)
end

Expand All @@ -97,11 +97,11 @@ function solve!(dx, dy, kkt::SIDSolver{T}, ξp, ξd) where{T}
@views copyto!(kkt.ξ[(m+1):(m+n)], ξd)

# Solve the augmented system
_krylov!(kkt.krylov_solver, kkt.opK, kkt.ξ; atol=kkt.atol, rtol=kkt.rtol)
krylov_solve!(kkt.krylov_workspace, kkt.opK, kkt.ξ; atol=kkt.atol, rtol=kkt.rtol)

# Recover dx, dy
copyto!(dx, kkt.krylov_solver.x[1:n])
copyto!(dy, kkt.krylov_solver.x[(n+1):(m+n)])
copyto!(dx, kkt.krylov_workspace.x[1:n])
copyto!(dy, kkt.krylov_workspace.x[(n+1):(m+n)])

# TODO: iterative refinement (?)
return nothing
Expand Down
2 changes: 1 addition & 1 deletion src/KKT/Krylov/spd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function solve!(dx, dy, kkt::SPDSolver{T}, ξp, ξd) where{T}
mul!(kkt.ξ, kkt.A, kkt.D * ξd, true, true)

# Solve the normal equations
_krylov!(kkt.krylov_solver, kkt.opK, kkt.ξ; atol=kkt.atol, rtol=kkt.rtol)
krylov_solve!(kkt.krylov_solver, kkt.opK, kkt.ξ; atol=kkt.atol, rtol=kkt.rtol)
copyto!(dy, kkt.krylov_solver.x)

# Recover dx
Expand Down
2 changes: 1 addition & 1 deletion src/KKT/Krylov/sqd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function solve!(dx, dy, kkt::SQDSolver{T}, ξp, ξd) where{T}
copyto!(kkt.ξd, ξd)

# Solve the augmented system
_krylov!(kkt.krylov_solver, kkt.A, kkt.ξp, kkt.ξd;
krylov_solve!(kkt.krylov_solver, kkt.A, kkt.ξp, kkt.ξd;
M=kkt.Θd⁻¹,
N=kkt.Θp⁻¹,
atol=kkt.atol,
Expand Down
2 changes: 1 addition & 1 deletion test/KKT/Krylov/sid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function test_krylov_sid(T, ksolver)
end

@testset "SID" begin
for T in TvTYPES, ksolver in [MinresSolver, MinresQlpSolver, SymmlqSolver]
for T in TvTYPES, ksolver in [MinresWorkspace, MinaresWorkspace, MinresQlpWorkspace, SymmlqWorkspace]
@testset "$ksolver ($T)" begin
test_krylov_sid(T, ksolver)
end
Expand Down
2 changes: 1 addition & 1 deletion test/KKT/Krylov/spd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function test_krylov_spd(T, ksolver)
end

@testset "SPD" begin
for T in TvTYPES, ksolver in [CgSolver, MinresSolver]
for T in TvTYPES, ksolver in [CgWorkspace, CarWorkspace]
@testset "$ksolver ($T)" begin
test_krylov_spd(T, ksolver)
end
Expand Down
2 changes: 1 addition & 1 deletion test/KKT/Krylov/sqd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function test_krylov_sqd(T, ksolver)
end

@testset "SQD" begin
for T in TvTYPES, ksolver in [TricgSolver, TrimrSolver]
for T in TvTYPES, ksolver in [TricgWorkspace, TrimrWorkspace]
@testset "$ksolver ($T)" begin
test_krylov_sqd(T, ksolver)
end
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Krylov = "0.8, 0.9"
Krylov = "0.10"
MathOptInterface= "1"
Loading