This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
conjugate_gradient_solver.jl
490 lines (406 loc) · 14.2 KB
/
conjugate_gradient_solver.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#### Conjugate Gradient solver
export ConjugateGradient
struct ConjugateGradient{AT1, AT2, FT, RD, RT, IT} <:
AbstractIterativeSystemSolver
# tolerances (2)
rtol::FT
atol::FT
# arrays of size reshape_tuple (7)
r0::AT1
z0::AT1
p0::AT1
r1::AT1
z1::AT1
p1::AT1
Lp::AT1
# arrays of size(MPIStateArray) which are aliased to two of the previous dimensions (2)
alias_p0::AT2
alias_Lp::AT2
# reduction dimension (1)
dims::RD
# reshape dimension (1)
reshape_tuple::RT
# maximum number of iterations (1)
max_iter::IT
end
# Define the outer constructor for the ConjugateGradient struct
"""
ConjugateGradient(
Q::AT;
rtol = eps(eltype(Q)),
atol = eps(eltype(Q)),
max_iter = length(Q),
dims = :,
reshape_tuple = size(Q),
) where {AT}
# ConjugateGradient
# Description
- Outer constructor for the ConjugateGradient struct
# Arguments
- `Q`:(array). The kind of object that linearoperator! acts on.
# Keyword Arguments
- `rtol`: (float). relative tolerance
- `atol`: (float). absolute tolerance
- `dims`: (tuple or : ). the dimensions to reduce over
- `reshape_tuple`: (tuple). the dimensions that the conjugate gradient solver operators over
# Comment
- The reshape tuple is necessary in case the linearoperator! is defined over vectors of a different size as compared to what plays nicely with the dimension reduction in the ConjugateGradient. It also allows the user to define preconditioners over arrays that are more convenienently shaped.
# Return
- ConjugateGradient struct
"""
function ConjugateGradient(
Q::AT;
rtol = eps(eltype(Q)),
atol = eps(eltype(Q)),
max_iter = length(Q),
dims = :,
reshape_tuple = size(Q),
) where {AT}
# allocate arrays (5)
r0 = reshape(similar(Q), reshape_tuple)
z0 = reshape(similar(Q), reshape_tuple)
r1 = reshape(similar(Q), reshape_tuple)
z1 = reshape(similar(Q), reshape_tuple)
p1 = reshape(similar(Q), reshape_tuple)
# allocate array of different shape (2)
alias_p0 = similar(Q)
alias_Lp = similar(Q)
# allocate create aliased arrays (2)
p0 = reshape(alias_p0, reshape_tuple)
Lp = reshape(alias_Lp, reshape_tuple)
container = [
rtol,
atol,
r0,
z0,
p0,
r1,
z1,
p1,
Lp,
alias_p0,
alias_Lp,
dims,
reshape_tuple,
max_iter,
]
# create struct instance by splatting the container into the default constructor
return ConjugateGradient{
typeof(z0),
typeof(Q),
eltype(Q),
typeof(dims),
typeof(reshape_tuple),
typeof(max_iter),
}(container...)
end
# Define the outer constructor for the ConjugateGradient struct
"""
ConjugateGradient(
Q::MPIStateArray;
rtol = eps(eltype(Q)),
atol = eps(eltype(Q)),
max_iter = length(Q),
dims = :,
reshape_tuple = size(Q),
)
# Description
Outer constructor for the ConjugateGradient struct with MPIStateArrays. THIS IS A HACK DUE TO RESHAPE FUNCTIONALITY ON MPISTATEARRAYS.
# Arguments
- `Q`:(array). The kind of object that linearoperator! acts on.
# Keyword Arguments
- `rtol`: (float). relative tolerance
- `atol`: (float). absolute tolerance
- `dims`: (tuple or : ). the dimensions to reduce over
- `reshape_tuple`: (tuple). the dimensions that the conjugate gradient solver operators over
# Comment
- The reshape tuple is necessary in case the linearoperator! is defined over vectors of a different size as compared to what plays nicely with the dimension reduction in the ConjugateGradient. It also allows the user to define preconditioners over arrays that are more convenienently shaped.
# Return
- ConjugateGradient struct
"""
function ConjugateGradient(
Q::MPIStateArray;
rtol = eps(eltype(Q)),
atol = eps(eltype(Q)),
max_iter = length(Q),
dims = :,
reshape_tuple = size(Q),
)
# create empty container for pushing struct objects
# allocate arrays (5)
r0 = reshape(similar(Q.data), reshape_tuple)
z0 = reshape(similar(Q.data), reshape_tuple)
r1 = reshape(similar(Q.data), reshape_tuple)
z1 = reshape(similar(Q.data), reshape_tuple)
p1 = reshape(similar(Q.data), reshape_tuple)
# allocate array of different shape (2)
alias_p0 = similar(Q)
alias_Lp = similar(Q)
# allocate create aliased arrays (2)
p0 = reshape(alias_p0.data, reshape_tuple)
Lp = reshape(alias_Lp.data, reshape_tuple)
container = [
rtol,
atol,
r0,
z0,
p0,
r1,
z1,
p1,
Lp,
alias_p0,
alias_Lp,
dims,
reshape_tuple,
max_iter,
]
# create struct instance by splatting the container into the default constructor
return ConjugateGradient{
typeof(z0),
typeof(Q),
eltype(Q),
typeof(dims),
typeof(reshape_tuple),
typeof(max_iter),
}(container...)
end
"""
initialize!(
linearoperator!,
Q,
Qrhs,
solver::ConjugateGradient,
args...,
)
# Description
- This function initializes the iterative solver. It is called as part of the AbstractIterativeSystemSolver routine. SEE CODEREF for documentation on AbstractIterativeSystemSolver
# Arguments
- `linearoperator!`: (function). This applies the predefined linear operator on an array. Applies a linear operator to object "y" and overwrites object "z". The function argument i s linearoperator!(z,y, args...) and it returns nothing.
- `Q`: (array). This is an object that linearoperator! outputs
- `Qrhs`: (array). This is an object that linearoperator! acts on
- `solver`: (struct). This is a scruct for dispatch, in this case for ColumnwisePreconditionedConjugateGradient
- `args...`: (arbitrary). This is optional arguments that can be passed into linearoperator! function.
# Keyword Arguments
- There are no keyword arguments
# Return
- `converged`: (bool). A boolean to say whether or not the iterative solver has converged.
- `threshold`: (float). The value of the residual for the first timestep
# Comment
- This function does nothing for conjugate gradient
"""
function initialize!(
linearoperator!,
Q,
Qrhs,
solver::ConjugateGradient,
args...,
)
return false, Inf
end
"""
doiteration!(
linearoperator!,
preconditioner,
Q,
Qrhs,
solver::ConjugateGradient,
threshold,
args...;
applyPC! = (x, y) -> x .= y,
)
# Description
- This function enacts the iterative solver. It is called as part of the AbstractIterativeSystemSolver routine. SEE CODEREF for documentation on AbstractIterativeSystemSolver
# Arguments
- `linearoperator!`: (function). This applies the predefined linear operator on an array. Applies a linear operator to object "y" and overwrites object "z". It is a function with arguments linearoperator!(z,y, args...), where "z" gets overwritten by "y" and "args..." are additional arguments passed to the linear operator. The linear operator is assumed to return nothing.
- `Q`: (array). This is an object that linearoperator! overwrites
- `Qrhs`: (array). This is an object that linearoperator! acts on. This is the rhs to the linear system
- `solver`: (struct). This is a scruct for dispatch, in this case for ConjugateGradient
- `threshold`: (float). Either an absolute or relative tolerance
- `applyPC!`: (function). Applies a preconditioner to objecy "y" and overwrites object "z". applyPC!(z,y)
- `args...`: (arbitrary). This is necessary for the linearoperator! function which has a signature linearoperator!(b, x, args....)
# Keyword Arguments
- There are no keyword arguments
# Return
- `converged`: (bool). A boolean to say whether or not the iterative solver has converged.
- `iteration`: (int). Iteration number for the iterative solver
- `threshold`: (float). The value of the residual for the first timestep
# Comment
- This function does conjugate gradient
"""
function doiteration!(
linearoperator!,
preconditioner,
Q,
Qrhs,
solver::ConjugateGradient,
threshold,
args...;
applyPC! = (x, y) -> x .= y,
)
# unroll names for convenience
rtol = solver.rtol
atol = solver.atol
residual_norm = typemax(eltype(Q))
dims = solver.dims
converged = false
max_iter = solver.max_iter
r0 = solver.r0
z0 = solver.z0
p0 = solver.p0
r1 = solver.r1
z1 = solver.z1
p1 = solver.p1
Lp = solver.Lp
alias_p0 = solver.alias_p0
alias_Lp = solver.alias_Lp
alias_Q = reshape(Q, solver.reshape_tuple)
# Smack residual by linear operator
linearoperator!(alias_Lp, Q, args...)
# make sure that arrays are of the appropriate size
alias_p0 .= Qrhs
r0 .= p0 - Lp
# apply the preconditioner
applyPC!(z0, r0)
# update p0
p0 .= z0
# TODO: FIX THIS
absolute_residual = maximum(sqrt.(sum(r0 .* r0, dims = dims)))
relative_residual =
absolute_residual / maximum(sqrt.(sum(Qrhs .* Qrhs, dims = :)))
# TODO: FIX THIS
if (absolute_residual <= atol) || (relative_residual <= rtol)
# wow! what a great guess
converged = true
return converged, 1, absolute_residual
end
for j in 1:max_iter
linearoperator!(alias_Lp, alias_p0, args...)
α = sum(r0 .* z0, dims = dims) ./ sum(p0 .* Lp, dims = dims)
# Update along preconditioned direction, (note that broadcast will indeed work as expected)
@. alias_Q += α * p0
@. r1 = r0 - α * Lp
# TODO: FIX THIS
absolute_residual = maximum(sqrt.(sum(r1 .* r1, dims = dims)))
relative_residual =
absolute_residual / maximum(sqrt.(sum(Qrhs .* Qrhs, dims = :)))
# TODO: FIX THIS
converged = false
if (absolute_residual <= atol) || (relative_residual <= rtol)
converged = true
return converged, j, absolute_residual
end
applyPC!(z1, r1)
β = sum(z1 .* r1, dims = dims) ./ sum(z0 .* r0, dims = dims)
# Update
@. p0 = z1 + β * p0
@. z0 = z1
@. r0 = r1
end
# TODO: FIX THIS
converged = true
return converged, max_iter, absolute_residual
end
"""
doiteration!(
linearoperator!,
preconditioner,
Q::MPIStateArray,
Qrhs::MPIStateArray,
solver::ConjugateGradient,
threshold,
args...;
applyPC! = (x, y) -> x .= y,
)
# Description
This function enacts the iterative solver. It is called as part of the AbstractIterativeSystemSolver routine. SEE CODEREF for documentation on AbstractIterativeSystemSolver. THIS IS A HACK TO WORK WITH MPISTATEARRAYS. THE ISSUE IS WITH RESHAPE.
# Arguments
- `linearoperator!`: (function). This applies the predefined linear operator on an array. Applies a linear operator to object "y" and overwrites object "z". It is a function with arguments linearoperator!(z,y, args...), where "z" gets overwritten by "y" and "args..." are additional arguments passed to the linear operator. The linear operator is assumed to return nothing.
- `Q`: (array). This is an object that linearoperator! overwrites
- `Qrhs`: (array). This is an object that linearoperator! acts on. This is the rhs to the linear system
- `solver`: (struct). This is a scruct for dispatch, in this case for ConjugateGradient
- `threshold`: (float). Either an absolute or relative tolerance
- `applyPC!`: (function). Applies a preconditioner to objecy "y" and overwrites object "z". applyPC!(z,y)
- `args...`: (arbitrary). This is necessary for the linearoperator! function which has a signature linearoperator!(b, x, args....)
# Keyword Arguments
- There are no keyword arguments
# Return
- `converged`: (bool). A boolean to say whether or not the iterative solver has converged.
- `iteration`: (int). Iteration number for the iterative solver
- `threshold`: (float). The value of the residual for the first timestep
# Comment
- This function does conjugate gradient
"""
function doiteration!(
linearoperator!,
preconditioner,
Q::MPIStateArray,
Qrhs::MPIStateArray,
solver::ConjugateGradient,
threshold,
args...;
applyPC! = (x, y) -> x .= y,
)
# unroll names for convenience
rtol = solver.rtol
atol = solver.atol
residual_norm = typemax(eltype(Q))
dims = solver.dims
converged = false
max_iter = solver.max_iter
r0 = solver.r0
z0 = solver.z0
p0 = solver.p0
r1 = solver.r1
z1 = solver.z1
p1 = solver.p1
Lp = solver.Lp
alias_p0 = solver.alias_p0
alias_Lp = solver.alias_Lp
alias_Q = reshape(Q.data, solver.reshape_tuple)
# Smack residual by linear operator
linearoperator!(alias_Lp, Q, args...)
# make sure that arrays are of the appropriate size
alias_p0 .= Qrhs.data
r0 .= p0 - Lp
# apply the preconditioner
applyPC!(z0, r0)
# update p0
p0 .= z0
# TODO: FIX THIS
absolute_residual = maximum(sqrt.(sum(r0 .* r0, dims = dims)))
relative_residual =
absolute_residual / maximum(sqrt.(sum(Qrhs .* Qrhs, dims = :)))
# TODO: FIX THIS
if (absolute_residual <= atol) || (relative_residual <= rtol)
# wow! what a great guess
converged = true
return converged, 1, absolute_residual
end
for j in 1:max_iter
linearoperator!(alias_Lp, alias_p0, args...)
α = sum(r0 .* z0, dims = dims) ./ sum(p0 .* Lp, dims = dims)
# Update along preconditioned direction, (note that broadcast will indeed work as expected)
@. alias_Q += α * p0
@. r1 = r0 - α * Lp
# TODO: FIX THIS
absolute_residual = maximum(sqrt.(sum(r1 .* r1, dims = dims)))
relative_residual =
absolute_residual / maximum(sqrt.(sum(Qrhs .* Qrhs, dims = :)))
# TODO: FIX THIS
converged = false
if (absolute_residual <= atol) || (relative_residual <= rtol)
converged = true
return converged, j, absolute_residual
end
applyPC!(z1, r1)
β = sum(z1 .* r1, dims = dims) ./ sum(z0 .* r0, dims = dims)
# Update
@. p0 = z1 + β * p0
@. z0 = z1
@. r0 = r1
end
# TODO: FIX THIS
converged = true
return converged, max_iter, absolute_residual
end