-
Notifications
You must be signed in to change notification settings - Fork 106
/
bicgstabl.jl
219 lines (167 loc) · 5.99 KB
/
bicgstabl.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
export bicgstabl, bicgstabl!, bicgstabl_iterator, bicgstabl_iterator!, BiCGStabIterable
using Printf
import Base: iterate
mutable struct BiCGStabIterable{precT, matT, solT, vecT <: AbstractVector, smallMatT <: AbstractMatrix, realT <: Real, scalarT <: Number}
A::matT
l::Int
x::solT
r_shadow::vecT
rs::smallMatT
us::smallMatT
max_mv_products::Int
mv_products::Int
tol::realT
residual::realT
Pl::precT
γ::vecT
ω::scalarT
σ::scalarT
M::smallMatT
end
function bicgstabl_iterator!(x, A, b, l::Int = 2;
Pl = Identity(),
max_mv_products = size(A, 2),
abstol::Real = zero(real(eltype(b))),
reltol::Real = sqrt(eps(real(eltype(b)))),
initial_zero = false)
T = eltype(x)
n = size(A, 1)
mv_products = 0
# Large vectors.
r_shadow = rand(T, n)
rs = Matrix{T}(undef, n, l + 1)
us = zeros(T, n, l + 1)
residual = view(rs, :, 1)
# Compute the initial residual rs[:, 1] = b - A * x
# Avoid computing A * 0.
if initial_zero
copyto!(residual, b)
else
mul!(residual, A, x)
residual .= b .- residual
mv_products += 1
end
# Apply the left preconditioner
ldiv!(Pl, residual)
γ = zeros(T, l)
ω = σ = one(T)
nrm = norm(residual)
# For the least-squares problem
M = zeros(T, l + 1, l + 1)
# Stopping condition based on absolute and relative tolerance.
tolerance = max(reltol * nrm, abstol)
BiCGStabIterable(A, l, x, r_shadow, rs, us,
max_mv_products, mv_products, tolerance, nrm,
Pl,
γ, ω, σ, M
)
end
@inline converged(it::BiCGStabIterable) = it.residual ≤ it.tol
@inline start(::BiCGStabIterable) = 0
@inline done(it::BiCGStabIterable, iteration::Int) = it.mv_products ≥ it.max_mv_products || converged(it)
function iterate(it::BiCGStabIterable, iteration::Int=start(it))
if done(it, iteration) return nothing end
T = eltype(it.x)
L = 2 : it.l + 1
it.σ = -it.ω * it.σ
## BiCG part
for j = 1 : it.l
ρ = dot(it.r_shadow, view(it.rs, :, j))
β = ρ / it.σ
# us[:, 1 : j] .= rs[:, 1 : j] - β * us[:, 1 : j]
it.us[:, 1 : j] .= it.rs[:, 1 : j] .- β .* it.us[:, 1 : j]
# us[:, j + 1] = Pl \ (A * us[:, j])
next_u = view(it.us, :, j + 1)
mul!(next_u, it.A, view(it.us, :, j))
ldiv!(it.Pl, next_u)
it.σ = dot(it.r_shadow, next_u)
α = ρ / it.σ
it.rs[:, 1 : j] .-= α .* it.us[:, 2 : j + 1]
# rs[:, j + 1] = Pl \ (A * rs[:, j])
next_r = view(it.rs, :, j + 1)
mul!(next_r, it.A , view(it.rs, :, j))
ldiv!(it.Pl, next_r)
# x = x + α * us[:, 1]
it.x .+= α .* view(it.us, :, 1)
end
# Bookkeeping
it.mv_products += 2 * it.l
## MR part
# M = rs' * rs
mul!(it.M, adjoint(it.rs), it.rs)
# γ = M[L, L] \ M[L, 1]
F = lu!(view(it.M, L, L))
ldiv!(it.γ, F, view(it.M, L, 1))
mul!(view(it.us, :, 1), view(it.us, :, L), it.γ, -one(T), one(T))
mul!(it.x, view(it.rs, :, 1 : it.l), it.γ, one(T), one(T))
mul!(view(it.rs, :, 1), view(it.rs, :, L), it.γ, -one(T), one(T))
it.ω = it.γ[it.l]
it.residual = norm(view(it.rs, :, 1))
it.residual, iteration + 1
end
# Classical API
"""
bicgstabl(A, b, l; kwargs...) -> x, [history]
Same as [`bicgstabl!`](@ref), but allocates a solution vector `x` initialized with zeros.
"""
bicgstabl(A, b, l = 2; kwargs...) = bicgstabl!(zerox(A, b), A, b, l; initial_zero = true, kwargs...)
"""
bicgstabl!(x, A, b, l; kwargs...) -> x, [history]
# Arguments
- `A`: linear operator;
- `b`: right hand side (vector);
- `l::Int = 2`: Number of GMRES steps.
## Keywords
- `max_mv_products::Int = size(A, 2)`: maximum number of matrix vector products.
For BiCGStab(l) this is a less dubious term than "number of iterations";
- `Pl = Identity()`: left preconditioner of the method;
- `abstol::Real = zero(real(eltype(b)))`,
`reltol::Real = sqrt(eps(real(eltype(b))))`: absolute and relative
tolerance for the stopping condition
`|r_k| ≤ max(reltol * |r_0|, abstol)`, where `r_k ≈ A * x_k - b`
is the approximate residual in the `k`th iteration;
!!! note
1. The true residual norm is never computed during the iterations,
only an approximation;
2. If a left preconditioner is given, the stopping condition is based on the
*preconditioned residual*.
# Return values
**if `log` is `false`**
- `x`: approximate solution.
**if `log` is `true`**
- `x`: approximate solution;
- `history`: convergence history.
"""
function bicgstabl!(x, A, b, l = 2;
abstol::Real = zero(real(eltype(b))),
reltol::Real = sqrt(eps(real(eltype(b)))),
max_mv_products::Int = size(A, 2),
log::Bool = false,
verbose::Bool = false,
Pl = Identity(),
kwargs...)
history = ConvergenceHistory(partial = !log)
history[:abstol] = abstol
history[:reltol] = reltol
# This doesn't yet make sense: the number of iters is smaller.
log && reserve!(history, :resnorm, max_mv_products)
# Actually perform iterative solve
iterable = bicgstabl_iterator!(x, A, b, l; Pl = Pl,
abstol = abstol, reltol = reltol,
max_mv_products = max_mv_products, kwargs...)
if log
history.mvps = iterable.mv_products
end
for (iteration, item) = enumerate(iterable)
if log
nextiter!(history)
history.mvps = iterable.mv_products
push!(history, :resnorm, iterable.residual)
end
verbose && @printf("%3d\t%1.2e\n", iteration, iterable.residual)
end
verbose && println()
log && setconv(history, converged(iterable))
log && shrink!(history)
log ? (iterable.x, history) : iterable.x
end