-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc.cpp
161 lines (148 loc) · 4.06 KB
/
gc.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
#include <string>
#include <iostream>
#include <string>
#include <cstdlib>
#include "csrmatrix.h"
#include "matrix.h"
#include "vector.h"
#include "chrono.h"
#include "gc.h"
#include "omp.h"
using namespace std;
///////////////////////////////////////////
//SCALAR PRODUCTS
//With Preconditionner, Vector Object
double scalC(const Vector& v1,
const Vector& v2,
const Vector& C){
return scalProduct(vectProduct(C,v1), v2);
}
//With Preconditionner, Matrix Object
double scalC(const Vector& v1,
const Vector& v2,
const Matrix& C){
return scalProduct(C*v1, v2);
}
//Without Preconditionner
double scal(const Vector& v1,
const Vector& v2){
return scalProduct(v1,v2);
}
///////////////////////////////////////////
//CONJUGATE GRADIENT
//Classical, no preconditionning
Vector GC(const CSRMatrix& A,
const Vector& b,
const int nP){
//Parameters
int nI = 10000;
double eps = 1e-5;
int nR = A.GetNumberOfRows();
Vector X(nR, 0.00000000000);
//Initialization
Vector g = A.parallelProduct(X, nP) - b;
Vector h = -g;
Vector Ah(nR), g_new(nR);
double rau=0, gamma=0, r=0;
//Iterations
for (int i = 0 ; i < nI ; i++){
Ah = A.parallelProduct(h, nP);
rau = -scal(g, h)/scal(h, Ah);
X += h * rau;
g_new = g + Ah * rau;
gamma = scal(g_new, g_new)/scal(g, g);
g = g_new;
h = -g + h * gamma;
r = scal(g_new, g_new);
if( r < eps ){return X;}
}
return X;
}
//Preconditionning with a diagonal matrix, Vector object
Vector PGC(const CSRMatrix& A,
const Vector& b,
const Vector& C,
const int nP){
//Parameters
int nI = 2000;
double eps = 1e-5;
int nR = A.GetNumberOfRows();
Vector X(nR, 0);
//Initialization
double t = omp_get_wtime();
int inc = 0;
Vector G = A.parallelProduct(X, nP) - b;
t = chrono(t, inc, "G initialization");
Vector CG = vectProduct(C, G);
Vector H = - CG;
t = chrono(t, inc, "H initialization");
Vector AH(nR), G_new(nR);
double rau=0, gamma=0, r=0;
//Iterations
for (int i = 0 ; i < nI ; i++){
if(i==0){t = chrono(t, inc, "Iterations start");}
AH = A.parallelProduct(H, nP);
if(i==0){t = chrono(t, inc, "A * H product");}
rau = -scal(G, H)/scal(H, AH);
if(i==0){t = chrono(t, inc, "Rau computation");}
X += H * rau;
if(i==0){t = chrono(t, inc, "X update");}
G_new = G + AH * rau;
if(i==0){t = chrono(t, inc, "G_new computation");}
gamma = scalC(G_new, G_new, C) / scalC(G, G, C);
if(i==0){t = chrono(t, inc, "Gamma computation");}
G = G_new;
CG = vectProduct(C,G);
if(i==0){t = chrono(t, inc, "CG update");}
H = -CG + H * gamma;
if(i==0){t = chrono(t, inc, "H update");}
r = scalC(G, G, C);
if(i==0){t = chrono(t, inc, "R computation");}
if( r < eps ){return X;}
}
return X;
}
//Preconditionning with a matrix, Matrix object
Vector PGC(const CSRMatrix& A,
const Vector& b,
const Matrix& C,
const int nP){
//Parameters
int nI = 2000;
double eps = 1e-5;
int nR = A.GetNumberOfRows();
Vector X(nR, 0);
//Initialization
double t = omp_get_wtime();
int inc = 0;
Vector G = A.parallelProduct(X, nP) - b;
t = chrono(t, inc, "G initialization");
Vector CG = C * G;
Vector H = - CG;
t = chrono(t, inc, "H initialization");
Vector AH(nR), G_new(nR);
double rau=0, gamma=0, r=0;
//Iterations
for (int i = 0 ; i < nI ; i++){
if(i==0){t = chrono(t, inc, "Iterations start");}
AH = A.parallelProduct(H, nP);
if(i==0){t = chrono(t, inc, "A * H product");}
rau = -scal(G, H)/scal(H, AH);
if(i==0){t = chrono(t, inc, "Rau computation");}
X += H * rau;
if(i==0){t = chrono(t, inc, "X update");}
G_new = G + AH * rau;
if(i==0){t = chrono(t, inc, "G_new computation");}
gamma = scalC(G_new, G_new, C) / scalC(G, G, C);
if(i==0){t = chrono(t, inc, "Gamma computation");}
G = G_new;
CG = C * G;
if(i==0){t = chrono(t, inc, "CG update");}
H = -CG + H * gamma;
if(i==0){t = chrono(t, inc, "H update");}
r = scalC(G, G, C);
if(i==0){t = chrono(t, inc, "R computation");}
if( r < eps ){return X;}
}
return X;
}