-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconjugate_residuals.cc
219 lines (198 loc) · 7.41 KB
/
conjugate_residuals.cc
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
// Copyright (c) 2018 ERGO-Code. See license.txt for license.
#include "conjugate_residuals.h"
#include <algorithm>
#include <cmath>
#include "timer.h"
#include "utils.h"
namespace ipx {
ConjugateResiduals::ConjugateResiduals(const Control& control) :
control_(control) {}
void ConjugateResiduals::Solve(LinearOperator& C, const Vector& rhs,
double tol, const double* resscale, Int maxiter,
Vector& lhs) {
const Int m = rhs.size();
Vector residual(m); // rhs - C*lhs
Vector step(m); // update to lhs
Vector Cresidual(m); // C * residual
Vector Cstep(m); // C * step
double cdot = 0.0; // dot product from C.Apply
Timer timer;
errflag_ = 0;
iter_ = 0;
time_ = 0.0;
if (maxiter < 0)
maxiter = m+100;
// Initialize residual, step and Cstep.
if (Infnorm(lhs) == 0.0) {
residual = rhs; // saves a matrix-vector op
} else {
C.Apply(lhs, residual, nullptr);
residual = rhs-residual;
}
C.Apply(residual, Cresidual, &cdot);
step = residual;
Cstep = Cresidual;
while (true) {
// Termination check.
double resnorm = 0.0;
if (resscale)
for (Int i = 0; i < m; i++)
resnorm = std::max(resnorm, std::abs(resscale[i]*residual[i]));
else
resnorm = Infnorm(residual);
if (resnorm <= tol)
break;
if (iter_ == maxiter) {
control_.Debug(3)
<< " CR method not converged in " << maxiter << " iterations."
<< " residual = " << sci2(resnorm) << ','
<< " tolerance = " << sci2(tol) << '\n';
errflag_ = IPX_ERROR_cr_iter_limit;
break;
}
if (cdot <= 0.0) {
errflag_ = IPX_ERROR_cr_matrix_not_posdef;
break;
}
// Update lhs, residual and Cresidual.
const double denom = Dot(Cstep,Cstep);
const double alpha = cdot/denom;
if (!std::isfinite(alpha)) {
errflag_ = IPX_ERROR_cr_inf_or_nan;
break;
}
lhs += alpha*step;
residual -= alpha*Cstep;
double cdotnew;
C.Apply(residual, Cresidual, &cdotnew);
// Update step and Cstep.
const double beta = cdotnew/cdot;
step = residual + beta*step;
Cstep = Cresidual + beta*Cstep;
cdot = cdotnew;
iter_++;
if ((errflag_ = control_.InterruptCheck()) != 0)
break;
}
time_ = timer.Elapsed();
}
void ConjugateResiduals::Solve(LinearOperator& C, LinearOperator& P,
const Vector& rhs, double tol,
const double* resscale, Int maxiter, Vector& lhs){
const Int m = rhs.size();
Vector residual(m); // rhs - C*lhs
Vector sresidual(m); // preconditioned residual
Vector step(m); // update to lhs
Vector Csresidual(m); // C * sresidual
Vector Cstep(m); // C * step
double cdot = 0.0; // dot product from C.Apply
Timer timer;
// resnorm_precond_system = residual' * P * residual.
// This quantity is minimized by the preconditioned CR method over a Krylov
// subspace. Hence (in theory) must decrease strictly monotonically. If it
// does not, then the method stagnated due to round-off errors. This
// happened in a few cases with augmented diagonal preconditioning (i.e.
// diagonal preconditioning with dense columns treated as low-rank update)
// because operations with P were not sufficiently accurate.
double resnorm_precond_system = 0.0;
errflag_ = 0;
iter_ = 0;
time_ = 0.0;
if (maxiter < 0)
maxiter = m+100;
// Initialize residual, sresidual, step and Cstep.
if (Infnorm(lhs) == 0.0) {
residual = rhs; // saves a matrix-vector op
} else {
C.Apply(lhs, residual, nullptr);
residual = rhs-residual;
}
P.Apply(residual, sresidual, &resnorm_precond_system);
C.Apply(sresidual, Csresidual, &cdot);
step = sresidual;
Cstep = Csresidual;
while (true) {
// Termination check.
double resnorm = 0.0;
if (resscale)
for (Int i = 0; i < m; i++)
resnorm = std::max(resnorm, std::abs(resscale[i]*residual[i]));
else
resnorm = Infnorm(residual);
if (resnorm <= tol)
break;
if (iter_ == maxiter) {
control_.Debug(3)
<< " PCR method not converged in " << maxiter << " iterations."
<< " residual = " << sci2(resnorm) << ','
<< " tolerance = " << sci2(tol) << '\n';
errflag_ = IPX_ERROR_cr_iter_limit;
break;
}
if (cdot <= 0.0) {
control_.Debug(3)
<< " matrix in PCR method not posdef. cdot = " << sci2(cdot)
<< ", infnorm(sresidual) = " << sci2(Infnorm(sresidual))
<< ", infnorm(residual) = " << sci2(Infnorm(residual))
<< '\n';
errflag_ = IPX_ERROR_cr_matrix_not_posdef;
break;
}
// Update lhs, residual, sresidual and Csresidual.
double cdotnew;
{
// Uses Csresidual as storage for preconditioned Cstep.
Vector& precond_Cstep = Csresidual;
double pdot;
P.Apply(Cstep, precond_Cstep, &pdot);
if (pdot <= 0.0) {
errflag_ = IPX_ERROR_cr_precond_not_posdef;
break;
}
const double alpha = cdot/pdot;
if (!std::isfinite(alpha)) {
errflag_ = IPX_ERROR_cr_inf_or_nan;
break;
}
lhs += alpha*step;
residual -= alpha*Cstep;
sresidual -= alpha*precond_Cstep;
C.Apply(sresidual, Csresidual, &cdotnew);
// Now Csresidual is restored and alias goes out of scope.
}
// Update step and Cstep.
const double beta = cdotnew/cdot;
step = sresidual + beta*step;
Cstep = Csresidual + beta*Cstep;
cdot = cdotnew;
iter_++;
if (iter_%5 == 0) {
// We recompute the preconditioned residual from its definition
// all 5 iterations. Otherwise it happened that the preconditioned
// residual approached zero but the true residual stagnated. This
// was caused by the update to the preconditioned residual being not
// sufficiently accurate.
// As a second safeguard, we check that resnorm_precond_system
// decreased during the last 5 iterations.
double rsdot;
P.Apply(residual, sresidual, &rsdot);
if (rsdot >= resnorm_precond_system) {
control_.Debug(3)
<< " resnorm_precond_system old = "
<< sci2(resnorm_precond_system) << '\n'
<< " resnorm_precond_system new = "
<< sci2(rsdot) << '\n';
errflag_ = IPX_ERROR_cr_no_progress;
break;
}
resnorm_precond_system = rsdot;
}
if ((errflag_ = control_.InterruptCheck()) != 0)
break;
}
time_ = timer.Elapsed();
}
Int ConjugateResiduals::errflag() const { return errflag_; }
Int ConjugateResiduals::iter() const { return iter_; }
double ConjugateResiduals::time() const { return time_; }
} // namespace ipx