-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcg.cpp
493 lines (409 loc) · 13.5 KB
/
pcg.cpp
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
491
492
493
#include "partition.hpp"
using namespace linalgcpp;
// This file takes an edge list for a connected graph and creates a graph Laplacian.
// Then a row and column is deleted to make the matrix positive definite.
// A random vector x0 is used to set a right-hand side vector b.
// We solve Ax = b using preconditoned conjugate gradient.
// The output includes the last three residual norms, the number of iterations
// and the distance between the exact solution and approximate one.
// Regular conjugate gradient method.
// Returns number of iterations.
int CG(const SparseMatrix<double>& A,
Vector<double>& x,
const Vector<double>& x0,
const Vector<double>& b,
double tol = 1e-9,
bool verbose = true);
// Preconditioned conjugate gradient method.
// Returns number of iterations.
int PCG(const SparseMatrix<double>& A,
Vector<double>& x,
const Vector<double>& x0,
const Vector<double>& b,
Vector<double>(SparseMatrix<double>::*precond)
(Vector<double>) const,
double tol = 1e-9,
bool verbose = true);
// Conjugate gradient method with two-level preconditioner.
// Returns number of iterations.
int TL(const SparseMatrix<double>& A,
Vector<double>& x,
const Vector<double>& x0,
const Vector<double>& b,
Vector<double>(*precond)
(const SparseMatrix<double>&,
const Vector<double>&,
const SparseMatrix<int>&,
const SparseMatrix<double>&),
double tol = 1e-9,
bool verbose = true);
// Two-level preconditioner used in function TL.
Vector<double> TwoLevel(const SparseMatrix<double>& A,
const Vector<double>& b,
const SparseMatrix<int>& P,
const SparseMatrix<double>& Ac);
// Conjugate gradient method with multilevel preconditioner.
// Returns number of iterations.
int ML(const SparseMatrix<double>& A0,
Vector<double>& x,
const Vector<double>& x0,
const Vector<double>& b,
Vector<double>(*precond)
(const std::vector<SparseMatrix<int>>&,
const std::vector<SparseMatrix<double>>&,
const Vector<double>&,
const int,
int k),
int ncoarse,
int max_level,
bool verbose = true,
double tol = 1e-9);
// Multilevel preconditioner used in function ML.
Vector<double> Multilevel(const std::vector<SparseMatrix<int>>& P,
const std::vector<SparseMatrix<double>>& A,
const Vector<double>& b,
const int L, // Number of levels
int k); // Current level index
// Get sequence of interpolation matrices P_k
// and course graph laplacians A_k.
int GetSequence(std::vector<SparseMatrix<int>>& P,
std::vector<SparseMatrix<double>>& A,
int ncoarse,
double q,
int k = 0);
int main()
{
// Create graph Laplacian from edge list.
// Must have zeroth vertex.
std::string filename;
std::cout << "Please type the input filename: ";
getline(std::cin, filename);
SparseMatrix<double> A(ReadGraphList(filename));
// Make the matrix positive definite.
A.EliminateRowCol(A.Rows() - 1);
A.EliminateZeros();
// Let n be length of a vector.
const int n = A.Cols();
// Compare size of matrix with number of iterations
std::cout << "The matrix is " << n << "x" << n
<< ".\n" <<std::endl;
Vector<double> x0(n); // Exact solution x0.
Randomize(x0, -10.0, 10.0); // Fill x0 with random values.
Vector<double> b(A.Mult(x0)); // Definition of b.
Vector<double> x(n); // Iterate x.
int num_iter;
std::cout << "CG: ";
CG(A, x, x0, b);
// Each preconditioner is a method of the SparseMatrix
// class. The syntax leaves something to be desired.
std::cout << "PCG Jacobi: ";
PCG(A, x, x0, b, &SparseMatrix<double>::Jacobi);
std::cout << "PCG l1-smoother: ";
PCG(A, x, x0, b, &SparseMatrix<double>::L1);
std::cout << "PCG Gauss-Seidel: ";
PCG(A, x, x0, b, &SparseMatrix<double>::GaussSeidel);
std::cout << "TL: ";
TL(A, x, x0, b, TwoLevel);
std::cout << "ML: ";
ML(A, x, x0, b, Multilevel, cbrt(n), 10);
return 0;
}
// This function solves Ax = b.
int CG(const SparseMatrix<double>& A,
Vector<double>& x,
const Vector<double>& x0,
const Vector<double>& b,
double tol,
bool verbose)
{
x = 0; // Set initial interate to zero.
// Because x = 0, the first residual r = b - A(x) = b.
Vector<double> r(b);
Vector<double> p(r); // Initial search direction.
Vector<double> g; // See usage below.
int num_iter = 0;
double c0 = r.Mult(r); // r dot r
Vector<double> c(A.Cols() + 2); // Squares of residual norm.
c[0] = c0;
double alpha, beta, c1, t;
// Beginning of CG algorithm.
for (int i = 0; i < A.Rows() + 1; ++i)
{
A.Mult(p, g); // g := Ap.
t = p.Mult(g);
alpha = c[i] / t;
x.Add(alpha, p);
r.Sub(alpha, g);
c1 = c[i];
c[i + 1] = r.Mult(r);
++num_iter;
if (c[i + 1] < tol * tol * c0)
break;
beta = c[i + 1] / c1;
p *= beta;
p += r;
}
if (verbose)
{
// If num_iter > A.Cols(), then the algorithm
// did not converge in the expected (theoretical)
// number of iterations.
printf("num_iter = %d\n", num_iter);
// Print the last three residual norms
// that are computed.
for (int i = num_iter - 2; i < num_iter + 1; ++i)
printf("|r| = %.3e\n", sqrt(c[i]));
// Let us see how close the approximation is
// in the euclidean norm.
r = x - x0;
double error(L2Norm(r));
std::cout << "Compare the approx soln with the exact: ";
printf("|x - x0| = %.3e\n", error);
std::cout << std::endl;
}
return num_iter;
}
// This function solves Ax = b using one of the three
// preconditioners shown in the main.
int PCG(const SparseMatrix<double>& A,
Vector<double>& x,
const Vector<double>& x0,
const Vector<double>& b,
Vector<double>(SparseMatrix<double>::*precond)
(Vector<double>) const,
double tol,
bool verbose)
{
x = 0; // Set initial interate to zero.
// Because x = 0, the first residual r = b - A(x) = b.
Vector<double> r(b);
Vector<double> y = (A.*precond)(r); // Preconditioned residual.
Vector<double> p(y); // Initial search direction.
Vector<double> g; // See usage below.
int num_iter = 0;
double c0 = r.Mult(y); // r dot y
Vector<double> c(A.Cols() + 2); // Squares of residual norm.
c[0] = c0;
double alpha, beta, c1, t;
// Beginning of PCG algorithm.
for (int i = 0; i < A.Rows() + 1; ++i)
{
A.Mult(p, g); // g := Ap.
t = p.Mult(g);
alpha = c[i] / t;
x.Add(alpha, p);
r.Sub(alpha, g);
// Copy two vectors by value. May be inefficient.
y = (A.*precond)(r);
c1 = c[i];
c[i + 1] = r.Mult(y);
++num_iter;
if (c[i + 1] < tol * tol * c0)
break;
beta = c[i + 1] / c1;
p *= beta;
p += y;
}
if (verbose)
{
// If num_iter > A.Cols(), then the algorithm
// did not converge in the expected (theoretical)
// number of iterations.
printf("num_iter = %d\n", num_iter);
// Print the last three residual norms
// that are computed.
for (int i = num_iter - 2; i < num_iter + 1; ++i)
printf("|r| = %.3e\n", sqrt(c[i]));
// Let us see how close the approximation is
// in the euclidean norm.
r = x - x0;
double error(L2Norm(r));
std::cout << "Compare the approx soln with the exact: ";
printf("|x - x0| = %.3e\n", error);
std::cout << std::endl;
}
return num_iter;
}
int TL(const SparseMatrix<double>& A,
Vector<double>& x,
const Vector<double>& x0,
const Vector<double>& b,
Vector<double>(*precond)
(const SparseMatrix<double>&,
const Vector<double>&,
const SparseMatrix<int>&,
const SparseMatrix<double>&),
double tol,
bool verbose)
{
// Determine interpolation matrix P and coarse graph Laplacian Ac.
int nparts = std::max(2.0, cbrt(A.Cols()));
SparseMatrix<int> P(Unweighted(Partition(A, nparts)));
SparseMatrix<double> Ac = P.Transpose().Mult(A.Mult(P));
Ac.EliminateZeros();
x = 0; // Set initial iterate to zero.
// Because x = 0, the first residual r = b - A(x) = b.
Vector<double> r(b);
Vector<double> y = (*precond)(A, r, P, Ac); // Preconditioned residual.
Vector<double> p(y); // Initial search direction.
Vector<double> g; // See usage below.
int num_iter = 0;
double c0 = r.Mult(y); // r dot y
Vector<double> c(A.Cols() + 2); // Squares of residual norm.
c[0] = c0;
double alpha, beta, c1, t;
// Beginning of PCG algorithm.
for (int i = 0; i < A.Rows() + 1; ++i)
{
A.Mult(p, g); // g := Ap.
t = p.Mult(g);
alpha = c[i] / t;
x.Add(alpha, p);
r.Sub(alpha, g);
// Copy two vectors by value. May be inefficient.
y = (*precond)(A, r, P, Ac);
c1 = c[i];
c[i + 1] = r.Mult(y);
++num_iter;
if (c[i + 1] < tol * tol * c0)
break;
beta = c[i + 1] / c1;
p *= beta;
p += y;
}
if (verbose)
{
// If num_iter > A.Cols(), then the algorithm
// did not converge in the expected (theoretical)
// number of iterations.
printf("nparts = %d, num_iter = %d\n", nparts, num_iter);
// Print the last three residual norms
// that are computed.
for (int i = num_iter - 2; i < num_iter + 1; ++i)
printf("|r| = %.3e\n", sqrt(c[i]));
// Let us see how close the approximation is
// in the euclidean norm.
r = x - x0;
double error(L2Norm(r));
std::cout << "Compare the approx soln with the exact: ";
printf("|x - x0| = %.3e\n", error);
std::cout << std::endl;
}
return num_iter;
}
Vector<double> TwoLevel(const SparseMatrix<double>& A,
const Vector<double>& b,
const SparseMatrix<int>& P,
const SparseMatrix<double>& Ac)
{
Vector<double> x(A.GaussSeidel(b));
Vector<double> rc(P.MultAT(b - A.Mult(x))); // x is x_(1/3)
Vector<double> xc(Ac.Cols());
CG(Ac, xc, b, rc, 1e-12, false); // Pass b for placeholder.
x.Add(P.Mult(xc));
x.Add(A.GaussSeidel(b - A.Mult(x))); // x is x_(2/3)
return x;
}
int ML(const SparseMatrix<double>& A0,
Vector<double>& x,
const Vector<double>& x0,
const Vector<double>& c,
Vector<double>(*precond)
(const std::vector<SparseMatrix<int>>&,
const std::vector<SparseMatrix<double>>&,
const Vector<double>&,
const int,
int k),
int ncoarse,
int max_level,
bool verbose,
double tol)
{
std::vector<SparseMatrix<double>> A;
A.push_back(A0);
const int n = A0.Cols();
double q = fmin(0.6, pow(1.0 * ncoarse / n, 1.0 / max_level));
std::vector<SparseMatrix<int>> P;
int L = GetSequence(P, A, ncoarse, q);
printf("q = %.2f, L = %d, ", q, L);
x = 0; // Set initial iterate to zero.
// Because x = 0, the first residual r = c - A(x) = c.
Vector<double> r(c);
Vector<double> y = (*precond)(P, A, r, L, 0); // Preconditioned residual.
Vector<double> p(y); // Initial search direction.
Vector<double> g; // See usage below.
int num_iter = 0;
double d0 = r.Mult(y); // r dot y
Vector<double> d(A0.Cols() + 2); // Squares of residual norm.
d[0] = d0;
double alpha, beta, d1, t;
// Beginning of PCG algorithm.
for (int i = 0; i < A0.Rows() + 1; ++i)
{
A0.Mult(p, g); // g := A0p.
t = p.Mult(g);
alpha = d[i] / t;
x.Add(alpha, p);
r.Sub(alpha, g);
// Copy two vectors by value. May be inefficient.
y = (*precond)(P, A, r, L, 0);
d1 = d[i];
d[i + 1] = r.Mult(y);
++num_iter;
if (d[i + 1] < tol * tol * d0)
break;
beta = d[i + 1] / d1;
p *= beta;
p += y;
}
if (verbose)
{
// If num_iter > A.Cols(), then the algorithm
// did not converge in the expected (theoretical)
// number of iterations.
printf("num_iter = %d\n", num_iter);
// Print the last three residual norms
// that are computed.
for (int i = num_iter - 2; i < num_iter + 1; ++i)
printf("|r| = %.3e\n", sqrt(d[i]));
// Let us see how close the approximation is
// in the euclidean norm.
r = x - x0;
double error(L2Norm(r));
std::cout << "Compare the approx soln with the exact: ";
printf("|x - x0| = %.3e\n", error);
std::cout << std::endl;
}
return num_iter;
}
int GetSequence(std::vector<SparseMatrix<int>>& P,
std::vector<SparseMatrix<double>>& A,
int ncoarse,
double q,
int k)
{
int nparts = std::max(2.0, A[k].Cols() * q); // METIS does not like nparts = 1.
P.push_back(Unweighted(Partition(A[k], nparts)));
A.push_back(P[k].Transpose().Mult(A[k].Mult(P[k])));
A[k + 1].EliminateZeros();
if (A[k].Cols() > ncoarse)
k = GetSequence(P, A, ncoarse, q, k + 1);
return k;
}
Vector<double> Multilevel(const std::vector<SparseMatrix<int>>& P,
const std::vector<SparseMatrix<double>>& A,
const Vector<double>& b,
const int L,
int k) // Current level
{
Vector<double> x(A[k].ForwardGauss(b));
Vector<double> r(P[k].MultAT(b - A[k].Mult(x))); // r_{k + 1}
Vector<double> y(A[k + 1].Cols()); // x_{k + 1}
if (L == k + 1)
CG(A[L], y, b, r, 1e-12, false); // Pass b for placeholder.
else
y = Multilevel(P, A, r, L, k + 1);
x.Add(P[k].Mult(y));
x.Add(A[k].BackwardGauss(b - A[k].Mult(x)));
return x;
}