-
Notifications
You must be signed in to change notification settings - Fork 0
/
solverschnellundfalsch.c
239 lines (200 loc) · 6.49 KB
/
solverschnellundfalsch.c
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
/*****************************************************
* CG Solver (HPC Software Lab)
*
* Parallel Programming Models for Applications in the
* Area of High-Performance Computation
*====================================================
* IT Center (ITC)
* RWTH Aachen University, Germany
* Author: Tim Cramer (cramer@itc.rwth-aachen.de)
* Fabian Schneider (f.schneider@itc.rwth-aachen.de)
* Date: 2010 - 2015
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifdef _OPENACC
# include <openacc.h>
#endif
#ifdef CUDA
# include <cuda.h>
#endif
#include "solver.h"
#include "output.h"
/* ab <- a' * b */
void vectorDot(const floatType* a, const floatType* b, const int n, floatType* ab){
int i;
floatType temp = 0;
#pragma acc parallel loop reduction(+:temp) present(a[0:n], b[0:n]) num_gangs(64) vector_length(192)
for(i=0; i<n; i++){
temp += a[i]*b[i];
}
*ab = temp;
}
/* y <- ax + y */
void axpy(const floatType a, const floatType* x, const int n, floatType* y){
int i;
#pragma acc parallel present(x[0:n], y[0:n]) num_gangs(64) vector_length(192)
#pragma acc loop independent gang, vector
for(i=0; i<n; i++){
y[i]=a*x[i]+y[i];
}
}
/* y <- x + ay */
void xpay(const floatType* x, const floatType a, const int n, floatType* y){
int i;
#pragma acc parallel present(x[0:n], y[0:n]) num_gangs(64) vector_length(192)
#pragma acc loop independent gang, vector
for(i=0; i<n; i++){
y[i]=x[i]+a*y[i];
}
}
/* y <- A*x
* Remember that A is stored in the ELLPACK-R format (data, indices, length, n, nnz, maxNNZ). */
inline void matvec(const int n, const int nnz, const int maxNNZ, const floatType* data, const int* indices, const int* length, const floatType* x, floatType* y){
int i;
#pragma acc parallel present(data[0:n*maxNNZ], indices[0:n*maxNNZ], length[0:n], x[0:n], y[0:n])
#pragma acc loop independent gang, vector private(i)
for (i = 0; i < n; i++) {
floatType temp = 0;
int j;
int len = length[i];
#pragma acc loop seq private(j)
for (j = 0; j < len; j++) {
int k = j * n + i;
temp += data[k] * x[indices[k]];
}
y[i] = temp;
}
}
/* nrm <- ||x||_2 */
void nrm2(const floatType* x, const int n, floatType* nrm){
int i;
floatType temp;
temp = 0;
#pragma acc parallel loop reduction(+:temp) present(x[0:n]) num_gangs(64) vector_length(192)
for(i = 0; i<n; i++){
temp+=(x[i]*x[i]);
}
*nrm=sqrt(temp);
}
/***************************************
* Conjugate Gradient *
* This function will do the CG *
* algorithm without preconditioning. *
* For optimiziation you must not *
* change the algorithm. *
***************************************
r(0) = b - Ax(0)
p(0) = r(0)
rho(0) = <r(0),r(0)>
***************************************
for k=0,1,2,...,n-1
q(k) = A * p(k)
dot_pq = <p(k),q(k)>
alpha = rho(k) / dot_pq
x(k+1) = x(k) + alpha*p(k)
r(k+1) = r(k) - alpha*q(k)
check convergence ||r(k+1)||_2 < eps
rho(k+1) = <r(k+1), r(k+1)>
beta = rho(k+1) / rho(k)
p(k+1) = r(k+1) + beta*p(k)
***************************************/
void cg(const int n, const int nnz, const int maxNNZ, const floatType* data, const int* indices, const int* length, const floatType* b, floatType* x, struct SolverConfig* sc){
floatType* r, *p, *q;
floatType alpha, beta, rho, rho_old, dot_pq, bnrm2;
int iter;
double timeMatvec_s;
double timeMatvec=0;
int i;
floatType temp;
/* allocate memory */
r = (floatType*)malloc(n * sizeof(floatType));
p = (floatType*)malloc(n * sizeof(floatType));
q = (floatType*)malloc(n * sizeof(floatType));
#pragma acc data copyin(data[0:n*maxNNZ], indices[0:n*maxNNZ], length[0:n], n, nnz, maxNNZ, b[0:n]) copy(x[0:n]) create(alpha, beta, r[0:n], p[0:n], q[0:n], i, temp) //eigentlich auch copy(x[0:n]) aber error: not found on device???
{
DBGMAT("Start matrix A = ", n, nnz, maxNNZ, data, indices, length)
DBGVEC("b = ", b, n);
DBGVEC("x = ", x, n);
/* r(0) = b - Ax(0) */
timeMatvec_s = getWTime();
matvec(n, nnz, maxNNZ, data, indices, length, x, r);
//hier inline ausprobieren
/*int i, j, k;
#pragma acc parallel loop present(data, indices, length, x)
for (i = 0; i < n; i++) {
r[i] = 0;
for (j = 0; j < length[i]; j++) {
k = j * n + i;
r[i] += data[k] * x[indices[k]];
}
}*/
timeMatvec += getWTime() - timeMatvec_s;
xpay(b, -1.0, n, r);
DBGVEC("r = b - Ax = ", r, n);
/* Calculate initial residuum */
nrm2(r, n, &bnrm2);
bnrm2 = 1.0 /bnrm2;
/* p(0) = r(0) */
memcpy(p, r, n*sizeof(floatType));
DBGVEC("p = r = ", p, n);
/* rho(0) = <r(0),r(0)> */
vectorDot(r, r, n, &rho);
printf("rho_0=%e\n", rho);
for(iter = 0; iter < sc->maxIter; iter++){
DBGMSG("=============== Iteration %d ======================\n", iter);
/* q(k) = A * p(k) */
timeMatvec_s = getWTime();
matvec(n, nnz, maxNNZ, data, indices, length, p, q);
timeMatvec += getWTime() - timeMatvec_s;
DBGVEC("q = A * p= ", q, n);
/* dot_pq = <p(k),q(k)> */
vectorDot(p, q, n, &dot_pq);
DBGSCA("dot_pq = <p, q> = ", dot_pq);
/* alpha = rho(k) / dot_pq */
alpha = rho / dot_pq;
DBGSCA("alpha = rho / dot_pq = ", alpha);
/* x(k+1) = x(k) + alpha*p(k) */
axpy(alpha, p, n, x);
#pragma acc update host(x[0:n])
DBGVEC("x = x + alpha * p= ", x, n);
/* r(k+1) = r(k) - alpha*q(k) */
axpy(-alpha, q, n, r);
DBGVEC("r = r - alpha * q= ", r, n);
rho_old = rho;
DBGSCA("rho_old = rho = ", rho_old);
/* rho(k+1) = <r(k+1), r(k+1)> */
vectorDot(r, r, n, &rho);
DBGSCA("rho = <r, r> = ", rho);
/* Normalize the residual with initial one */
sc->residual= sqrt(rho) * bnrm2;
/* Check convergence ||r(k+1)||_2 < eps
* If the residual is smaller than the CG
* tolerance specified in the CG_TOLERANCE
* environment variable our solution vector
* is good enough and we can stop the
* algorithm. */
printf("res_%d=%e\n", iter+1, sc->residual);
if(sc->residual <= sc->tolerance)
break;
/* beta = rho(k+1) / rho(k) */
beta = rho / rho_old;
DBGSCA("beta = rho / rho_old= ", beta);
/* p(k+1) = r(k+1) + beta*p(k) */
xpay(r, beta, n, p);
DBGVEC("p = r + beta * p> = ", p, n);
}
/* Store the number of iterations and the
* time for the sparse matrix vector
* product which is the most expensive
* function in the whole CG algorithm. */
sc->iter = iter;
sc->timeMatvec = timeMatvec;
/* Clean up */
free(r);
free(p);
free(q);
}//ende data region
}