Skip to content

Commit 241593c

Browse files
authored
Update the API for workspace accessors (#998)
1 parent fb0fece commit 241593c

File tree

10 files changed

+263
-246
lines changed

10 files changed

+263
-246
lines changed

docs/src/custom_workspaces.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ Note that `Krylov.kref!` is only required for the function `minres_qlp`.
298298

299299
```@example halo-regions
300300
using Krylov, OffsetArrays
301+
import Krylov: solution, statistics
301302
302303
# Parameters
303304
L = 1.0 # Length of the square domain
@@ -437,14 +438,14 @@ We now solve the system $Ax = b$ using `minres` with our preconditioner:
437438

438439
```@example block-arrays
439440
using Krylov
441+
import Krylov: solution, niterations
440442
441443
kc = KrylovConstructor(b)
442444
workspace = MinresWorkspace(kc)
443445
minres!(workspace, A, b; M=P)
444446
445447
x = solution(workspace)
446-
stats = statistics(workspace)
447-
niter = stats.niter
448+
niter = niterations(workspace)
448449
```
449450

450451
This example demonstrates how `BlockArrays.jl` and `Krylov.jl` can be effectively combined to solve structured saddle point systems.

docs/src/generic_interface.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ krylov_solve!
1010
krylov_solve
1111
```
1212

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-
krylov_elapsed_time
23-
```
24-
2513
## Examples
2614

2715
```julia
@@ -58,21 +46,21 @@ for method in (:bicgstab, :gmres)
5846
krylov_solve!(workspace, A, b)
5947

6048
# Get the statistics
61-
stats = statistics(workspace)
49+
stats = Krylov.statistics(workspace)
6250

6351
# Retrieve the solution
64-
x = solution(workspace)
52+
x = Krylov.solution(workspace)
6553

6654
# Check if the solver converged
67-
solved = issolved(workspace)
55+
solved = Krylov.issolved(workspace)
6856
println("Convergence of $method: ", solved)
6957

7058
# Display the number of iterations
71-
niter = niterations(workspace)
59+
niter = Krylov.niterations(workspace)
7260
println("Number of iterations for $method: ", niter)
7361

7462
# Display the elapsed timer
75-
timer = krylov_elapsed_time(workspace)
63+
timer = Krylov.elapsed_time(workspace)
7664
println("Elapsed time for $method: ", timer, " seconds")
7765

7866
println()

docs/src/inplace.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## [In-place methods](@id in-place)
22

33
All solvers in Krylov.jl have an in-place variant implemented in a method whose name ends with `!`.
4-
A workspace (`KrylovWorkspace`), 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.
4+
A workspace (`KrylovWorkspace` or `BlockKrylovWorkspace`), 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.
5+
It should also be the same number of right-hand sides in block Krylov methods.
56
The section [storage requirements](@ref storage-requirements) specifies the memory needed for each Krylov method.
67

78
Each `KrylovWorkspace` has three constructors with consistent argument patterns:
@@ -44,6 +45,26 @@ lsqr_workspace = LsqrWorkspace(m, n, CuVector{Float32})
4445
lsqr!(lsqr_workspace, A4, b4)
4546
```
4647

48+
## Workspace accessors
49+
50+
In-place solvers update the workspace, from which solutions and statistics can be retrieved.
51+
The following functions are available for post-solve analysis.
52+
53+
These functions are not exported and must be accessed using the prefix `Krylov.`, e.g. `Krylov.solution(workspace)`.
54+
55+
```@docs
56+
Krylov.results
57+
Krylov.solution
58+
Krylov.nsolution
59+
Krylov.statistics
60+
Krylov.elapsed_time
61+
Krylov.niterations
62+
Krylov.issolved
63+
Krylov.Aprod
64+
Krylov.Atprod
65+
Krylov.Bprod
66+
```
67+
4768
## Examples
4869

4970
We illustrate the use of in-place Krylov solvers with two well-known optimization methods.
@@ -53,6 +74,7 @@ The details of the optimization methods are described in the section about [Fact
5374

5475
```@newton
5576
using Krylov
77+
import Krylov: solution
5678
5779
function newton(∇f, ∇²f, x₀; itmax = 200, tol = 1e-8)
5880
@@ -63,7 +85,6 @@ function newton(∇f, ∇²f, x₀; itmax = 200, tol = 1e-8)
6385
iter = 0
6486
S = typeof(x)
6587
workspace = CgWorkspace(n, n, S)
66-
Δx = workspace.x
6788
6889
solved = false
6990
tired = false
@@ -72,6 +93,7 @@ function newton(∇f, ∇²f, x₀; itmax = 200, tol = 1e-8)
7293
7394
Hx = ∇²f(x) # Compute ∇²f(xₖ)
7495
cg!(workspace, Hx, -gx) # Solve ∇²f(xₖ)Δx = -∇f(xₖ)
96+
Δx = solution(workspace) # Recover Δx from the workspace
7597
x = x + Δx # Update xₖ₊₁ = xₖ + Δx
7698
gx = ∇f(x) # ∇f(xₖ₊₁)
7799
@@ -87,6 +109,7 @@ end
87109

88110
```@gauss_newton
89111
using Krylov
112+
import Krylov: solution
90113
91114
function gauss_newton(F, JF, x₀; itmax = 200, tol = 1e-8)
92115
@@ -98,7 +121,6 @@ function gauss_newton(F, JF, x₀; itmax = 200, tol = 1e-8)
98121
iter = 0
99122
S = typeof(x)
100123
workspace = LsmrWorkspace(m, n, S)
101-
Δx = workspace.x
102124
103125
solved = false
104126
tired = false
@@ -107,6 +129,7 @@ function gauss_newton(F, JF, x₀; itmax = 200, tol = 1e-8)
107129
108130
Jx = JF(x) # Compute J(xₖ)
109131
lsmr!(workspace, Jx, -Fx) # Minimize ‖J(xₖ)Δx + F(xₖ)‖
132+
Δx = solution(workspace) # Recover Δx from the workspace
110133
x = x + Δx # Update xₖ₊₁ = xₖ + Δx
111134
Fx_old = Fx # F(xₖ)
112135
Fx = F(x) # F(xₖ₊₁)

docs/src/reference.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
```@index
66
```
77

8+
## Internals
9+
810
```@docs
911
Krylov.FloatOrComplex
10-
Krylov.niterations
11-
Krylov.Aprod
12-
Krylov.Atprod
12+
Krylov.warm_start!
1313
Krylov.kstdout
1414
Krylov.extract_parameters
1515
Base.show

docs/src/warm-start.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# [Warm-start](@id warm-start)
22

3-
Most Krylov methods in this module accept a starting point as argument.
4-
The starting point is used as initial approximation to a solution.
3+
Most Krylov methods accept a user-provided initial guess instead of starting from zero.
54

65
```julia
76
workspace = CgWorkspace(A, b)
@@ -18,14 +17,6 @@ If the user has an initial guess `x0`, it can be provided directly.
1817
cg(A, b, x0)
1918
```
2019

21-
It is also possible to use the function `warm_start!` to feed the starting point into the workspace.
22-
23-
```julia
24-
warm_start!(workspace, x0)
25-
cg!(workspace, A, b)
26-
# the previous two lines are equivalent to cg!(workspace, A, b, x0)
27-
```
28-
2920
If a Krylov method doesn't have the option to warm start, it can still be done explicitly.
3021
We provide an example with `cg_lanczos!`.
3122

src/Krylov.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ using LinearAlgebra, SparseArrays, Printf
55
include("krylov_stats.jl")
66

77
include("krylov_utils.jl")
8-
include("krylov_processes.jl")
9-
include("krylov_workspaces.jl")
10-
118
include("block_krylov_utils.jl")
9+
10+
include("krylov_processes.jl")
1211
include("block_krylov_processes.jl")
12+
13+
include("krylov_workspaces.jl")
1314
include("block_krylov_workspaces.jl")
15+
include("workspace_accessors.jl")
1416

1517
include("krylov_show.jl")
1618

0 commit comments

Comments
 (0)