Skip to content

Commit 6273703

Browse files
authored
Add a generic interface for Krylov.jl (#983)
1 parent cabd85e commit 6273703

20 files changed

+569
-396
lines changed

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ makedocs(
2323
"Generalized saddle-point and non-Hermitian partitioned systems" => "solvers/gsp.md"],
2424
"Block-Krylov methods" => "block_krylov.md",
2525
"In-place methods" => "inplace.md",
26-
"Storage requirements" => "storage.md",
26+
"Generic interface" => "generic_interface.md",
27+
"Storage requirements" => "storage.md",
2728
"Preconditioners" => "preconditioners.md",
2829
"GPU support" => "gpu.md",
2930
"Warm-start" => "warm-start.md",

docs/src/generic_interface.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## [Generic interface](@id generic-interface)
2+
3+
Krylov.jl provides a generic interface for solving linear systems using (block) Krylov methods.
4+
The interface is designed to be common for all methods and contains three routines [`krylov_workspace`](@ref krylov_workspace), [`krylov_solve`](@ref krylov_solve) and [`krylov_solve!`](@ref krylov_solve!).
5+
They allow to build Krylov workspaces and call both in-place and out-of-place variants of the solvers with a unified API.
6+
7+
```@docs
8+
krylov_workspace
9+
krylov_solve!
10+
krylov_solve
11+
```
12+
13+
In-place solvers update the workspace, from which solutions and statistics can be retrieved.
14+
The following functions are available for post-solve analysis:
15+
16+
```@docs
17+
Krylov.results
18+
Krylov.solution
19+
Krylov.statistics
20+
Krylov.nsolution
21+
Krylov.issolved
22+
```
23+
24+
## Examples
25+
26+
```julia
27+
using Krylov, Random
28+
29+
# Define a symmetric positive definite matrix A and a right-hand side vector b
30+
n = 1000
31+
A = sprandn(n, n, 0.005)
32+
A = A * A' + I
33+
b = randn(n)
34+
35+
# Out-of-place interface
36+
for method in (:cg, :cr, :car)
37+
x, stats = krylov_solve(Val{method}(), A, b)
38+
r = b - A * x
39+
println("Residual norm for $(method): ", norm(r))
40+
end
41+
```
42+
43+
```julia
44+
using Krylov, Random
45+
46+
# Define a square nonsymmetric matrix A and a right-hand side vector b
47+
n = 100
48+
A = sprand(n, n, 0.05) + I
49+
b = rand(n)
50+
51+
# In-place interface
52+
for method in (:bicgstab, :gmres)
53+
# Create a workspace for the Krylov method
54+
solver = krylov_workspace(Val(method), A, b)
55+
56+
# Solve the system in-place
57+
krylov_solve!(solver, A, b)
58+
59+
# Get the statistics
60+
stats = statistics(solver)
61+
62+
# Retrieve the solution
63+
x = solution(solver)
64+
65+
# Check if the solver converged
66+
solved = issolved(solver)
67+
println("Converged $method: ", solved)
68+
69+
# Display the number of iterations
70+
niter = niterations(solver)
71+
println("Number of iterations for $method: ", niter)
72+
end
73+
```

docs/src/inplace.md

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
All solvers in Krylov.jl have an in-place variant implemented in a method whose name ends with `!`.
44
A workspace (`KrylovSolver`), which contains the storage needed by a Krylov method, can be used to solve multiple linear systems with the same dimensions and the same floating-point precision.
55
The section [storage requirements](@ref storage-requirements) specifies the memory needed for each Krylov method.
6-
Each `KrylovSolver` has three constructors:
6+
7+
Each `KrylovSolver` has three constructors with consistent argument patterns:
78

89
```@constructors
910
XyzSolver(A, b)
1011
XyzSolver(m, n, S)
1112
XyzSolver(kc::KrylovConstructor)
1213
```
14+
The only exceptions are `CgLanczosShiftSolver` and `CglsLanczosShiftSolver`, which require an additional argument `nshifts`.
15+
Additionally, some constructors accept keyword arguments.
1316

14-
`Xyz` represents the name of the Krylov method, written in lowercase except for its first letter (e.g., `Cg`, `Minres`, `Lsmr`, `Bicgstab`, etc.).
17+
`Xyz` represents the name of the Krylov method, written in lowercase except for its first letter (such as `Cg`, `Minres`, `Lsmr` or `Bicgstab`).
1518
If the name of the Krylov method contains an underscore (e.g., `minres_qlp` or `cgls_lanczos_shift`), the workspace constructor transforms it by capitalizing each word and removing underscores, resulting in names like `MinresQlpSolver` or `CglsLanczosShiftSolver`.
1619

1720
Given an operator `A` and a right-hand side `b`, you can create a `KrylovSolver` based on the size of `A` and the type of `b`, or explicitly provide the dimensions `(m, n)` and the storage type `S`.
1821

19-
!!! note
20-
The constructors of `CgLanczosShiftSolver` and `CglsLanczosShiftSolver` require an additional argument `nshifts`.
21-
2222
We assume that `S(undef, 0)`, `S(undef, n)`, and `S(undef, m)` are well-defined for the storage type `S`.
2323
For more advanced vector types, workspaces can also be created with the help of a `KrylovConstructor`.
2424
```@docs
@@ -44,27 +44,6 @@ lsqr_solver = LsqrSolver(m, n, CuVector{Float32})
4444
lsqr!(lsqr_solver, A4, b4)
4545
```
4646

47-
A generic function `solve!` is also available and dispatches to the appropriate Krylov method.
48-
49-
```@docs
50-
Krylov.solve!
51-
```
52-
53-
!!! note
54-
The function `solve!` is not exported to prevent potential conflicts with other Julia packages.
55-
56-
In-place methods return an updated `solver` workspace.
57-
Solutions and statistics can be recovered via `solver.x`, `solver.y` and `solver.stats`.
58-
Functions `solution`, `statistics` and `results` can be also used.
59-
60-
```@docs
61-
Krylov.nsolution
62-
Krylov.issolved
63-
Krylov.solution
64-
Krylov.statistics
65-
Krylov.results
66-
```
67-
6847
## Examples
6948

7049
We illustrate the use of in-place Krylov solvers with two well-known optimization methods.

src/block_gmres.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export block_gmres, block_gmres!
77

88
"""
99
(X, stats) = block_gmres(A, b::AbstractMatrix{FC};
10-
memory::Int=20, M=I, N=I, ldiv::Bool=false,
10+
memory::Int=5, M=I, N=I, ldiv::Bool=false,
1111
restart::Bool=false, reorthogonalization::Bool=false,
1212
atol::T=√eps(T), rtol::T=√eps(T), itmax::Int=0,
1313
timemax::Float64=Inf, verbose::Int=0, history::Bool=false,
@@ -61,6 +61,10 @@ function block_gmres end
6161
6262
where `kwargs` are keyword arguments of [`block_gmres`](@ref).
6363
64+
The keyword argument `memory` is the only exception.
65+
It is only supported by `block_gmres` and is required to create a `BlockGmresSolver`.
66+
It cannot be changed later.
67+
6468
See [`BlockGmresSolver`](@ref) for more details about the `solver`.
6569
"""
6670
function block_gmres! end

src/block_krylov_solvers.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
export BlockKrylovSolver
1+
export BlockKrylovSolver, BlockMinresSolver, BlockGmresSolver
22

3-
export BlockMinresSolver, BlockGmresSolver
4-
5-
const BLOCK_KRYLOV_SOLVERS = Dict(:block_minres => :BlockMinresSolver,
6-
:block_gmres => :BlockGmresSolver )
7-
8-
"Abstract type for using block Krylov solvers in-place"
3+
"Abstract type for using block Krylov solvers in-place."
94
abstract type BlockKrylovSolver{T,FC,SV,SM} end
105

116
"""

src/diom.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ function diom end
7777
7878
where `kwargs` are keyword arguments of [`diom`](@ref).
7979
80-
Note that the `memory` keyword argument is the only exception.
81-
It's required to create a `DiomSolver` and can't be changed later.
80+
The keyword argument `memory` is the only exception.
81+
It is only supported by `diom` and is required to create a `DiomSolver`.
82+
It cannot be changed later.
8283
8384
See [`DiomSolver`](@ref) for more details about the `solver`.
8485
"""

src/dqgmres.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ function dqgmres end
7777
7878
where `kwargs` are keyword arguments of [`dqgmres`](@ref).
7979
80-
Note that the `memory` keyword argument is the only exception.
81-
It's required to create a `DqgmresSolver` and can't be changed later.
80+
The keyword argument `memory` is the only exception.
81+
It is only supported by [`dqgmres`](@ref) and is required to create a `DqgmresSolver`.
82+
It cannot be changed later.
8283
8384
See [`DqgmresSolver`](@ref) for more details about the `solver`.
8485
"""

src/fgmres.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ function fgmres end
7979
8080
where `kwargs` are keyword arguments of [`fgmres`](@ref).
8181
82-
Note that the `memory` keyword argument is the only exception.
83-
It's required to create a `FgmresSolver` and can't be changed later.
82+
The keyword argument `memory` is the only exception.
83+
It is only supported by [`fgmres`](@ref) and is required to create a `FgmresSolver`.
84+
It cannot be changed later.
8485
8586
See [`FgmresSolver`](@ref) for more details about the `solver`.
8687
"""

src/fom.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ function fom end
7272
7373
where `kwargs` are keyword arguments of [`fom`](@ref).
7474
75-
Note that the `memory` keyword argument is the only exception.
76-
It's required to create a `FomSolver` and can't be changed later.
75+
The keyword argument `memory` is the only exception.
76+
It is only supported by [`fom`](@ref) and is required to create a `FomSolver`.
77+
It cannot be changed later.
7778
7879
See [`FomSolver`](@ref) for more details about the `solver`.
7980
"""

src/gmres.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ function gmres end
7272
7373
where `kwargs` are keyword arguments of [`gmres`](@ref).
7474
75-
Note that the `memory` keyword argument is the only exception.
76-
It's required to create a `GmresSolver` and can't be changed later.
75+
The keyword argument `memory` is the only exception.
76+
It is only supported by [`gmres`](@ref) and is required to create a `GmresSolver`.
77+
It cannot be changed later.
7778
7879
See [`GmresSolver`](@ref) for more details about the `solver`.
7980
"""

0 commit comments

Comments
 (0)