-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
381 lines (298 loc) · 12.7 KB
/
main.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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iostream>
#include <algorithm>
#include <math.h>
#include "read-vectors.hpp"
#include "rocsparse-ilu0.hpp"
#include "rocsolver-ilu.hpp"
#include <umfpack.h>
#include <dune/common/timer.hh>
#include <hip/hip_runtime_api.h>
#include <hip/hip_runtime.h>
#include <hip/hip_version.h>
#define VAR_NAME(var) (#var)
__global__ void test(double a, double b, double res){
if (threadIdx.x == 0)
printf("a: %f\n", a);
printf("b: %f\n", b);
res = a - b;
}
double errorInfinityNorm(std::vector<double> vecSol, std::vector<double> vec, bool printError = false, bool printErrorVector = false){
std::vector<double> error(size(vecSol), -1.0);
if (size(vecSol) == size(vec)){
for (int i=0; i<size(vec); i++){
error[i]= fabs(vec[i]-vecSol[i]);
}
double norm_error = *std::max_element(error.begin(),error.end());
if (printErrorVector) {
std::cout << "Error vector: [ ";
for (const auto& e : error) {
std::cout << e << " ";
}
std::cout << "]" << std::endl;
}
if (printError) std::cout << "Inifinity norm of error: " << norm_error << std::endl;
return norm_error;
} else { return -1.0;}
}
void errorInfinityNormDebug(std::vector<double> vecSol, std::vector<double> vec, double epsilon, bool printError = false, bool printErrorVector = false){
std::vector<double> error(size(vecSol), -1.0);
if (size(vecSol) == size(vec)){
for (int i=0; i<size(vec); i++){
error[i]= fabs(vec[i]-vecSol[i]);
if (error[i] > epsilon){
std::cout << "Element " << i << " has an error of " << error[i] << std::endl;
}
}
double norm_error = *std::max_element(error.begin(),error.end());
if (printErrorVector) {
std::cout << "Error vector: [ ";
for (const auto& e : error) {
std::cout << e << " ";
}
std::cout << "]" << std::endl;
}
if (printError) std::cout << "Inifinity norm of error: " << norm_error << std::endl;
}
}
double sumVector(std::vector<double> vec){
double sum = 0.0;
for (const auto& val : vec) sum += val;
return sum;
}
template <typename T>
void printVector(const std::vector<T>& vec, const std::string& name){
std::cout << name <<": " << size(vec) << std::endl;
std::cout << "[ ";
for (const auto& val : vec) std::cout << val << " ";
std::cout << "]";
std::cout << std::endl;
std::cout << std::endl;
}
template <typename T>
void printVectorWrapper(const std::vector<T>& vec, const std::string& name) {
printVector(vec, name);
}
#define PRINT_VECTOR(vec) printVectorWrapper(vec, VAR_NAME(vec))
int main(int argc, char ** argv)
{
std::vector<double> UMFPackTimes(3, 0.0);
std::vector<double> RocSPARSETimes(3, 0.0);
std::vector<double> RocSOLVERTimes(3, 0.0);
const static int dim = 3;
const static int dim_wells = 4;
std::string reservoir;
if (argc > 1){
reservoir = argv[1];
std::cout << "Reservoir: " << reservoir << std::endl;
} else {
std::cout << "No reservoir provided. Please enter one option as command-line argument (norne, msw)." << std::endl;
return 1;
}
std::cout << std::endl;
/*
WellMatrices wellMatrices;
wellMatrices.read_matrices();
// std::cout << wellMatrices.duneB[0][0] << std::endl;
// Matrix type for matrix D
using DiagMatrixBlockWellType = Dune::FieldMatrix<double,dim_wells,dim_wells>;
using DiagMatWell = Dune::BCRSMatrix<DiagMatrixBlockWellType>;
// Matrix type for matrices D and C^t
using OffDiagMatrixBlockWellType = Dune::FieldMatrix<double,dim_wells,dim>;
using OffDiagMatWell = Dune::BCRSMatrix<OffDiagMatrixBlockWellType>;
DiagMatWell duneD;
OffDiagMatWell duneB;
OffDiagMatWell duneC;
const char* fileName_D = "matrix-D.mm";
const char* fileName_B = "matrix-B.mm";
const char* fileName_C = "matrix-C.mm";
std::ifstream fileIn_D(fileName_D);
std::ifstream fileIn_B(fileName_B);
std::ifstream fileIn_C(fileName_C);
Dune::readMatrixMarket(duneD, fileIn_D);
Dune::readMatrixMarket(duneB, fileIn_B);
Dune::readMatrixMarket(duneC, fileIn_C);
std::cout << "########## Dune Matrices ##########" << std::endl;
std::cout << wellMatrices.duneD.nonzeroes() << std::endl;
std::cout << wellMatrices.duneD.N() << std::endl;
std::cout << wellMatrices.duneD.M() << std::endl;
//std::cout << wellMatrices.duneB.nonzeroes() << std::endl;
//std::cout << wellMatrices.duneB.N() << std::endl;
//std::cout << wellMatrices.duneB.M() << std::endl;
//std::cout << wellMatrices.duneC.nonzeroes() << std::endl;
//std::cout << wellMatrices.duneC.N() << std::endl;
//std::cout << wellMatrices.duneC.M() << std::endl;
*/
//std::cout << "########## std::vector ##########" << std::endl;
std::vector<double> Dvals, Bvals, Cvals;
std::vector<int> Dcols, Drows;
std::vector<unsigned int> Bcols, Ccols, Brows, Crows;
loadSparseMatrixVectors(Dvals, Dcols, Drows, "data/"+reservoir+"/matrix-D.bin");
loadSparseMatrixVectors(Bvals, Bcols, Brows, "data/"+reservoir+"/matrix-B.bin");
loadSparseMatrixVectors(Cvals, Ccols, Crows, "data/"+reservoir+"/matrix-C.bin");
std::vector<double> vecRes = loadResVector("data/"+reservoir+"/vector-Res.bin");
std::vector<double> vecSol = loadResVector("data/"+reservoir+"/vector-Sol.bin");
//std::cout << size(vecRes) << std::endl;
//PRINT_VECTOR(Bvals);
//PRINT_VECTOR(Bcols);
//PRINT_VECTOR(Brows);
unsigned int Mb = size(Brows)-1 ;
std::cout << "Number of blockrows: " << Mb << std::endl;
unsigned int length = dim_wells*Mb;
std::vector<double> z1(length, 0.0); // z1 = B * x
std::vector<double> z2(length, 0.0);
Dune::Timer cpuMatrix;
cpuMatrix.start();
// B*x multiplication
for (unsigned int row = 0; row < Mb; ++row) {
// for every block in the row
for (unsigned int blockID = Brows[row]; blockID < Brows[row + 1]; ++blockID) {
unsigned int colIdx = Bcols[blockID];
for (unsigned int j = 0; j < dim_wells; ++j) {
double temp = 0.0;
for (unsigned int k = 0; k < dim; ++k) {
double B_elem = Bvals[blockID * dim * dim_wells + j * dim + k];
double x_elem = vecRes[colIdx * dim + k];
temp += Bvals[blockID * dim * dim_wells + j * dim + k] * vecRes[colIdx * dim + k];
//printf("B_elem: %.15f(%u), x_elem: %.15f(%u), local_out: %.15f\n", B_elem, blockID * dim * dim_wells + j * dim + k, x_elem, colIdx * dim + k, temp);
}
z1[row * dim_wells + j] += temp;
//printf("y_elem: %.15f(%u)\n", z1[row * dim_wells + j], row * dim_wells + j);
}
}
}
cpuMatrix.stop();
double cpuTime = cpuMatrix.lastElapsed();
unsigned int M = dim_wells*Mb;
void *UMFPACK_Symbolic, *UMFPACK_Numeric;
std::cout << "########## UMFPack Solver ########## " << std::endl;
Dune::Timer UMFPackTimer;
UMFPackTimer.start();
umfpack_di_symbolic(M, M, Dcols.data(), Drows.data(), Dvals.data(), &UMFPACK_Symbolic, nullptr, nullptr);
umfpack_di_numeric(Dcols.data(), Drows.data(), Dvals.data(), UMFPACK_Symbolic, &UMFPACK_Numeric, nullptr, nullptr);
UMFPackTimer.stop();
UMFPackTimes[0] = UMFPackTimer.lastElapsed();
UMFPackTimer.start();
umfpack_di_solve(UMFPACK_A, Dcols.data(), Drows.data(), Dvals.data(), z2.data(), z1.data(), UMFPACK_Numeric, nullptr, nullptr);
UMFPackTimer.stop();
UMFPackTimes[2] = UMFPackTimer.lastElapsed();
//PRINT_VECTOR(z2);
PRINT_VECTOR(UMFPackTimes);
double UMFPackTotalTime = sumVector(UMFPackTimes);
std::cout << "Total time: "<< UMFPackTotalTime+cpuTime << "s" << std::endl;
double errorCPU = errorInfinityNorm(vecSol, z2, true);
std::cout << std::endl;
umfpack_di_free_symbolic(&UMFPACK_Symbolic);
umfpack_di_free_numeric(&UMFPACK_Numeric);
/*
std::cout << "########## RocSPARSE Solver ########## " << std::endl;
Dune::Timer RocSPARSETimer;
*/
unsigned int nnzs = size(Dvals);
unsigned int sizeDvals = size(Dvals);
unsigned int sizeDcols = size(Dcols);
unsigned int sizeDrows = size(Drows);
/*
std::vector<double> Dvals_(sizeDvals);
std::vector<int> Dcols_(sizeDvals);
std::vector<int> Drows_(sizeDcols);
RocSPARSETimer.start();
squareCSCtoCSR(Dvals, Drows, Dcols, Dvals_, Drows_, Dcols_);
RocSPARSETimer.stop();
RocSPARSETimes[0] += RocSPARSETimer.lastElapsed();
unsigned int sizeDvals_ = size(Dvals_);
unsigned int sizeDrows_ = size(Drows_);
unsigned int sizeDcols_ = size(Dcols_);
M = size(Drows_)-1 ;
RocsparseMSWContribution rocsparseMswc;
RocSPARSETimer.start();
rocsparseMswc.initialize(M, nnzs, sizeDvals_, sizeDrows_, sizeDcols_);
RocSPARSETimer.stop();
RocSPARSETimes[0] += RocSPARSETimer.lastElapsed();
RocSPARSETimer.start();
rocsparseMswc.copyHostToDevice(Dvals_, Drows_, Dcols_, z1);
RocSPARSETimer.stop();
RocSPARSETimes[1] += RocSPARSETimer.lastElapsed();
RocSPARSETimer.start();
std::vector<double> z2_rocsparse = rocsparseMswc.solveSytem();
RocSPARSETimer.stop();
RocSPARSETimes[2] += RocSPARSETimer.lastElapsed();
//PRINT_VECTOR(z2_rocsparse);
PRINT_VECTOR(RocSPARSETimes);
double RocSparseTotalTime = sumVector(RocSPARSETimes);
std::cout << "Total time: "<< RocSparseTotalTime+cpuTime << "s" << std::endl;
double errorGPURocSPARSE = errorInfinityNorm(vecSol, z2_rocsparse, true);
std::cout << std::endl;
*/
std::cout << "########## RocSOLVER Solver ########## " << std::endl;
Dune::Timer RocSOLVERTimer;
std::vector<double> vecy = loadResVector("data/"+reservoir+"/vector-y.bin");
//std::vector<double> vecY = loadResVector("data/"+reservoir+"/vector-Y.bin");
int lda = sizeDcols-1;
int sizeBvals = size(Bvals);
int sizeBrows = size(Brows);
int sizeBcols = size(Bcols);
int resSize = size(vecRes);
int CzSize = size(vecy);
RocSOLVERTimer.start();
double* Dmatrix = squareCSCtoMatrix(Dvals, Drows, Dcols);
RocSOLVERTimer.stop();
RocSOLVERTimes[0] += RocSOLVERTimer.lastElapsed();
RocsolverMSWContribution rocsolverMswc;
RocSOLVERTimer.start();
rocsolverMswc.initialize(lda, lda, sizeBvals, sizeBcols, sizeBrows, resSize, CzSize);
RocSOLVERTimer.stop();
RocSOLVERTimes[1] += RocSOLVERTimer.lastElapsed();
RocSOLVERTimer.start();
rocsolverMswc.copyHostToDevice(Dmatrix, Cvals, Bvals, Brows, Bcols, vecRes, vecy);
RocSOLVERTimer.stop();
RocSOLVERTimes[1] += RocSOLVERTimer.lastElapsed();
RocSOLVERTimer.start();
std::vector<double> z2_rocsolver = rocsolverMswc.apply();
RocSOLVERTimer.stop();
RocSOLVERTimes[2] += RocSOLVERTimer.lastElapsed();
PRINT_VECTOR(RocSOLVERTimes);
double RocSolverTotalTime = (RocSOLVERTimes[0]+RocSOLVERTimes[2]);
std::cout << "Total time: "<< RocSolverTotalTime << "s" << std::endl;
double errorGPURocSOLVER = errorInfinityNorm(vecSol, z2_rocsolver, true);
std::cout << std::endl;
std::vector<double> vecY_hip = rocsolverMswc.vectorCtz();
int row = 0;
cpuMatrix.start();
// y -= (C^T * z2)
// y -= (C^T * (D^-1 * (B * x)))
for (unsigned int blockrow = 0; blockrow < Mb; ++blockrow) {
// for every block in the row
for (unsigned int blockID = Brows[blockrow]; blockID < Brows[blockrow + 1]; ++blockID) {
unsigned int colIdx = Bcols[blockID];
for (unsigned int j = 0; j < dim; ++j) {
double temp = 0.0;
row = colIdx * dim + j;
//printf("Row: %u,\n", row);
for (unsigned int k = 0; k < dim_wells; ++k) {
double C_elem = Cvals[blockID * dim * dim_wells + j + k * dim];
double z_elem = z2[blockrow * dim_wells + k];
//temp += C_elem * z_elem;
vecy[row] -= C_elem * z_elem;
//printf("C_elem: %.15f(%u), z_elem: %.15f(%u), local_out: %.15f, y(row): %.15f\n", C_elem, blockID * dim * dim_wells + j + k * dim, z_elem, blockrow * dim_wells + k, C_elem * z_elem, vecy[row]);
}
//printf("block: %i, col: %i\n", blockID, colIdx);
//printf("y_elem: %i\n", colIdx * dim + j);
//vecy[row] -= temp;
//printf("y(row): %.12f\n", vecy[row]);
}
}
}
std::cout << std::endl;
cpuMatrix.stop();
cpuTime += cpuMatrix.lastElapsed();
std::cout << "Well contribution" << std::endl;
errorInfinityNormDebug(vecY_hip, vecy, 1e-10, true);
std::cout << std::endl;
std::cout << "########## Speed Factors ########## " << std::endl;
//std::cout << "RocSPARSE: " << (RocSparseTotalTime+cpuTime)/(UMFPackTotalTime+cpuTime) << std::endl;
std::cout << "RocSOLVER: " << (UMFPackTotalTime+cpuTime)/RocSolverTotalTime << std::endl;
std::cout << std::endl;
}