-
Notifications
You must be signed in to change notification settings - Fork 50
/
krylov_stats.jl
386 lines (356 loc) · 8.95 KB
/
krylov_stats.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
export KrylovStats, SimpleStats, LsmrStats, LanczosStats, LanczosShiftStats,
SymmlqStats, AdjointStats, LNLQStats, LSLQStats
import Base.copyto!
"Abstract type for statistics returned by a solver"
abstract type KrylovStats{T} end
"""
Type for statistics returned by the majority of Krylov solvers, the attributes are:
- niter
- solved
- inconsistent
- residuals
- Aresiduals
- Acond
- timer
- status
"""
mutable struct SimpleStats{T} <: KrylovStats{T}
niter :: Int
solved :: Bool
inconsistent :: Bool
residuals :: Vector{T}
Aresiduals :: Vector{T}
Acond :: Vector{T}
timer :: Float64
status :: String
end
function reset!(stats :: SimpleStats)
empty!(stats.residuals)
empty!(stats.Aresiduals)
empty!(stats.Acond)
end
function copyto!(dest :: SimpleStats, src :: SimpleStats)
dest.niter = src.niter
dest.solved = src.solved
dest.inconsistent = src.inconsistent
dest.residuals = copy(src.residuals)
dest.Aresiduals = copy(src.Aresiduals)
dest.Acond = copy(src.Acond)
dest.timer = src.timer
dest.status = src.status
return dest
end
"""
Type for statistics returned by LSMR. The attributes are:
- niter
- solved
- inconsistent
- residuals
- Aresiduals
- Acond
- Anorm
- xNorm
- timer
- status
"""
mutable struct LsmrStats{T} <: KrylovStats{T}
niter :: Int
solved :: Bool
inconsistent :: Bool
residuals :: Vector{T}
Aresiduals :: Vector{T}
residual :: T
Aresidual :: T
Acond :: T
Anorm :: T
xNorm :: T
timer :: Float64
status :: String
end
function reset!(stats :: LsmrStats)
empty!(stats.residuals)
empty!(stats.Aresiduals)
end
function copyto!(dest :: LsmrStats, src :: LsmrStats)
dest.niter = src.niter
dest.solved = src.solved
dest.inconsistent = src.inconsistent
dest.residuals = copy(src.residuals)
dest.Aresiduals = copy(src.Aresiduals)
dest.residual = src.residual
dest.Aresidual = src.Aresidual
dest.Acond = src.Acond
dest.Anorm = src.Anorm
dest.xNorm = src.xNorm
dest.timer = src.timer
dest.status = src.status
return dest
end
"""
Type for statistics returned by CG-LANCZOS, the attributes are:
- niter
- solved
- residuals
- indefinite
- Anorm
- Acond
- timer
- status
"""
mutable struct LanczosStats{T} <: KrylovStats{T}
niter :: Int
solved :: Bool
residuals :: Vector{T}
indefinite :: Bool
Anorm :: T
Acond :: T
timer :: Float64
status :: String
end
function reset!(stats :: LanczosStats)
empty!(stats.residuals)
end
function copyto!(dest :: LanczosStats, src :: LanczosStats)
dest.niter = src.niter
dest.solved = src.solved
dest.residuals = copy(src.residuals)
dest.indefinite = src.indefinite
dest.Anorm = src.Anorm
dest.Acond = src.Acond
dest.timer = src.timer
dest.status = src.status
return dest
end
"""
Type for statistics returned by CG-LANCZOS with shifts, the attributes are:
- niter
- solved
- residuals
- indefinite
- Anorm
- Acond
- timer
- status
"""
mutable struct LanczosShiftStats{T} <: KrylovStats{T}
niter :: Int
solved :: Bool
residuals :: Vector{Vector{T}}
indefinite :: BitVector
Anorm :: T
Acond :: T
timer :: Float64
status :: String
end
function reset!(stats :: LanczosShiftStats)
for vec in stats.residuals
empty!(vec)
end
end
function copyto!(dest :: LanczosShiftStats, src :: LanczosShiftStats)
dest.niter = src.niter
dest.solved = src.solved
dest.residuals = deepcopy(src.residuals)
dest.indefinite = src.indefinite
dest.Anorm = src.Anorm
dest.Acond = src.Acond
dest.timer = src.timer
dest.status = src.status
return dest
end
"""
Type for statistics returned by SYMMLQ, the attributes are:
- niter
- solved
- residuals
- residualscg
- errors
- errorscg
- Anorm
- Acond
- timer
- status
"""
mutable struct SymmlqStats{T} <: KrylovStats{T}
niter :: Int
solved :: Bool
residuals :: Vector{T}
residualscg :: Vector{Union{T, Missing}}
errors :: Vector{T}
errorscg :: Vector{Union{T, Missing}}
Anorm :: T
Acond :: T
timer :: Float64
status :: String
end
function reset!(stats :: SymmlqStats)
empty!(stats.residuals)
empty!(stats.residualscg)
empty!(stats.errors)
empty!(stats.errorscg)
end
function copyto!(dest :: SymmlqStats, src :: SymmlqStats)
dest.niter = src.niter
dest.solved = src.solved
dest.residuals = copy(src.residuals)
dest.residualscg = copy(src.residualscg)
dest.errors = copy(src.errors)
dest.errorscg = copy(src.errorscg)
dest.Anorm = src.Anorm
dest.Acond = src.Acond
dest.timer = src.timer
dest.status = src.status
return dest
end
"""
Type for statistics returned by adjoint systems solvers BiLQR and TriLQR, the attributes are:
- niter
- solved_primal
- solved_dual
- residuals_primal
- residuals_dual
- timer
- status
"""
mutable struct AdjointStats{T} <: KrylovStats{T}
niter :: Int
solved_primal :: Bool
solved_dual :: Bool
residuals_primal :: Vector{T}
residuals_dual :: Vector{T}
timer :: Float64
status :: String
end
function reset!(stats :: AdjointStats)
empty!(stats.residuals_primal)
empty!(stats.residuals_dual)
end
function copyto!(dest :: AdjointStats, src :: AdjointStats)
dest.niter = src.niter
dest.solved_primal = src.solved_primal
dest.solved_dual = src.solved_dual
dest.residuals_primal = copy(src.residuals_primal)
dest.residuals_dual = copy(src.residuals_dual)
dest.timer = src.timer
dest.status = src.status
return dest
end
"""
Type for statistics returned by the LNLQ method, the attributes are:
- niter
- solved
- residuals
- error_with_bnd
- error_bnd_x
- error_bnd_y
- timer
- status
"""
mutable struct LNLQStats{T} <: KrylovStats{T}
niter :: Int
solved :: Bool
residuals :: Vector{T}
error_with_bnd :: Bool
error_bnd_x :: Vector{T}
error_bnd_y :: Vector{T}
timer :: Float64
status :: String
end
function reset!(stats :: LNLQStats)
empty!(stats.residuals)
empty!(stats.error_bnd_x)
empty!(stats.error_bnd_y)
end
function copyto!(dest :: LNLQStats, src :: LNLQStats)
dest.niter = src.niter
dest.solved = src.solved
dest.residuals = copy(src.residuals)
dest.error_with_bnd = src.error_with_bnd
dest.error_bnd_x = copy(src.error_bnd_x)
dest.error_bnd_y = copy(src.error_bnd_y)
dest.timer = src.timer
dest.status = src.status
return dest
end
"""
Type for statistics returned by the LSLQ method, the attributes are:
- niter
- solved
- inconsistent
- residuals
- Aresiduals
- err_lbnds
- error_with_bnd
- err_ubnds_lq
- err_ubnds_cg
- timer
- status
"""
mutable struct LSLQStats{T} <: KrylovStats{T}
niter :: Int
solved :: Bool
inconsistent :: Bool
residuals :: Vector{T}
Aresiduals :: Vector{T}
err_lbnds :: Vector{T}
error_with_bnd :: Bool
err_ubnds_lq :: Vector{T}
err_ubnds_cg :: Vector{T}
timer :: Float64
status :: String
end
function reset!(stats :: LSLQStats)
empty!(stats.residuals)
empty!(stats.Aresiduals)
empty!(stats.err_lbnds)
empty!(stats.err_ubnds_lq)
empty!(stats.err_ubnds_cg)
end
function copyto!(dest :: LSLQStats, src :: LSLQStats)
dest.niter = src.niter
dest.solved = src.solved
dest.inconsistent = src.inconsistent
dest.residuals = copy(src.residuals)
dest.Aresiduals = copy(src.Aresiduals)
dest.err_lbnds = copy(src.err_lbnds)
dest.error_with_bnd = src.error_with_bnd
dest.err_ubnds_lq = copy(src.err_ubnds_lq)
dest.err_ubnds_cg = copy(src.err_ubnds_cg)
dest.timer = src.timer
dest.status = src.status
return dest
end
import Base.show
special_fields = Dict(
:residualscg => "residuals (cg)",
:errorscg => "errors (cg)",
:Anorm => "‖A‖F",
:Acond => "κ₂(A)",
:err_ubnds_lq => "error bound LQ",
:err_ubnds_cg => "error bound CG",
)
function show(io :: IO, stats :: KrylovStats)
kst = typeof(stats)
s = string(kst.name.name) * "\n"
nfield = fieldcount(kst)
for i = 1 : nfield
field = fieldname(kst, i)
field_name = if field ∈ keys(special_fields)
special_fields[field]
else
replace(string(field), "_" => " ")
end
s *= " " * field_name * ":"
statfield = getfield(stats, field)
if isa(statfield, AbstractVector) && eltype(statfield) <: Union{Missing, AbstractFloat}
s *= @sprintf " %s\n" vec2str(statfield)
elseif field_name == "timer"
(statfield < 1e-3) && (s *= @sprintf " %.2fμs\n" 1e6*statfield)
(1e-3 ≤ statfield < 1.00) && (s *= @sprintf " %.2fms\n" 1e3*statfield)
(statfield ≥ 1.00) && (s *= @sprintf " %.2fs\n" statfield)
else
s *= @sprintf " %s\n" statfield
end
end
print(io, s)
end