-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTPZCudaCalls.cu
290 lines (251 loc) · 10 KB
/
TPZCudaCalls.cu
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
#include "TPZCudaCalls.h"
#include "pzreal.h"
#include "pzvec.h"
// #include "MatMulKernels.h"
#include "KernelsComputeSigma.h"
#include "KernelsMatMul.h"
#include "KernelsMatrixAssemble.h"
#define NT 64
TPZCudaCalls::TPZCudaCalls() {
cusparse_h = false;
cublas_h = false;
}
TPZCudaCalls::~TPZCudaCalls() {
if(cublas_h == true) {
cublasDestroy(handle_cublas);
}
if(cusparse_h == true) {
cusparseDestroy(handle_cusparse);
}
}
TPZCudaCalls &TPZCudaCalls::operator=(const TPZCudaCalls ©) {
if(© == this){
return *this;
}
handle_cusparse = copy.handle_cusparse;
cusparse_h = copy.cusparse_h;
handle_cublas = copy.handle_cublas;
cublas_h = copy.cublas_h;
return *this;
}
void TPZCudaCalls::Multiply(bool trans, int *m, int *n, int *k, REAL *A, int *strideA,
REAL *B, int *strideB, REAL *C, int *strideC, REAL alpha, int nmatrices) {
int numBlocks = (nmatrices + NT - 1) / NT;
MatrixMultiplicationKernel<<<numBlocks,NT>>> (trans, m, n, k, A, strideA, B, strideB, C, strideC, alpha, nmatrices);
cudaDeviceSynchronize();
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess) {
std::string error_string = cudaGetErrorString(error);
std::string error_message = "failed to perform MatrixMultiplicationKernel: " + error_string;
throw std::runtime_error(error_message);
}
}
void TPZCudaCalls::GatherOperation(int n, REAL *x, REAL *y, int *id) {
if(cusparse_h == false) {
cusparse_h = true;
cusparseStatus_t result = cusparseCreate(&handle_cusparse);
if (result != CUSPARSE_STATUS_SUCCESS) {
throw std::runtime_error("failed to initialize cuSparse");
}
}
cusparseStatus_t result = cusparseDgthr(handle_cusparse, n, x, y, id, CUSPARSE_INDEX_BASE_ZERO);
if (result != CUSPARSE_STATUS_SUCCESS) {
throw std::runtime_error("failed to perform cusparseDgthr");
}
}
void TPZCudaCalls::ScatterOperation(int n, REAL *x, REAL *y, int *id) {
if(cusparse_h == false) {
cusparse_h = true;
cusparseStatus_t result = cusparseCreate(&handle_cusparse);
if (result != CUSPARSE_STATUS_SUCCESS) {
throw std::runtime_error("failed to initialize cuSparse");
}
}
cusparseStatus_t result = cusparseDsctr(handle_cusparse, n, x, id, y, CUSPARSE_INDEX_BASE_ZERO);
if (result != CUSPARSE_STATUS_SUCCESS) {
throw std::runtime_error("failed to perform cusparseDsctr");
}
}
void TPZCudaCalls::DaxpyOperation(int n, double alpha, double *x, double *y) {
if(cublas_h == false) {
cublas_h = true;
cublasStatus_t result = cublasCreate(&handle_cublas);
if (result != CUBLAS_STATUS_SUCCESS) {
throw std::runtime_error("failed to initialize cuBLAS");
}
}
cublasStatus_t result = cublasDaxpy(handle_cublas, n, &alpha, x, 1., y, 1.);
if (result != CUBLAS_STATUS_SUCCESS) {
throw std::runtime_error("failed to perform cublasDaxpy");
}
}
void TPZCudaCalls::SpMV(int opt, int sym, int m, int k, int nnz, REAL alpha, REAL *csrVal, int *csrRowPtr, int *csrColInd, REAL *B, REAL *C) {
if(cusparse_h == false) {
cusparse_h = true;
cusparseStatus_t result = cusparseCreate(&handle_cusparse);
if (result != CUSPARSE_STATUS_SUCCESS) {
throw std::runtime_error("failed to initialize cuSparse");
}
}
cusparseMatDescr_t descr;
cusparseCreateMatDescr(&descr);
cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO);
if(sym == 0) {
cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_GENERAL);
}
else {
cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_SYMMETRIC);
}
cusparseOperation_t op;
if(opt == 0) {
op = CUSPARSE_OPERATION_NON_TRANSPOSE;
} else {
op = CUSPARSE_OPERATION_TRANSPOSE;
}
REAL beta = 0.;
cusparseStatus_t result = cusparseDcsrmv(handle_cusparse, op, m, k, nnz, &alpha, descr, csrVal, csrRowPtr, csrColInd, B, &beta, C);
if (result != CUSPARSE_STATUS_SUCCESS) {
throw std::runtime_error("failed to perform cusparseDcsrmv");
}
}
void TPZCudaCalls::SpMSpM(int opt, int sym, int m, int n, int k, int nnzA, REAL *csrValA, int *csrRowPtrA, int *csrColIndA,
int nnzB, REAL *csrValB, int *csrRowPtrB, int *csrColIndB,
int nnzC, REAL *csrValC, int *csrRowPtrC) {
if(cusparse_h == false) {
cusparse_h = true;
cusparseStatus_t result = cusparseCreate(&handle_cusparse);
if (result != CUSPARSE_STATUS_SUCCESS) {
throw std::runtime_error("failed to initialize cuSparse");
}
}
cusparseMatDescr_t descr;
cusparseCreateMatDescr(&descr);
cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO);
if(sym == 0) {
cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_GENERAL);
}
else {
cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_SYMMETRIC);
}
cusparseOperation_t op;
if(opt == 0) {
op = CUSPARSE_OPERATION_NON_TRANSPOSE;
} else {
op = CUSPARSE_OPERATION_TRANSPOSE;
}
int *csrColIndC;
cudaMalloc((void**)&csrColIndC, sizeof(int)*nnzC);
cusparseStatus_t result = cusparseDcsrgemm(handle_cusparse, op, CUSPARSE_OPERATION_NON_TRANSPOSE, m, n, k,
descr, nnzA, csrValA, csrRowPtrA, csrColIndA,
descr, nnzB, csrValB, csrRowPtrB, csrColIndB,
descr, csrValC, csrRowPtrC, csrColIndC);
if (result != CUSPARSE_STATUS_SUCCESS) {
throw std::runtime_error("failed to perform cusparseDcsrgemm");
}
}
void TPZCudaCalls::ComputeSigma(bool update_mem, int npts, REAL *glob_delta_strain, REAL *glob_sigma, REAL lambda, REAL mu, REAL mc_phi, REAL mc_psi, REAL mc_cohesion, REAL *dPlasticStrain,
REAL *dMType, REAL *dAlpha, REAL *dSigma, REAL *dStrain, REAL *weight) {
int numBlocks = (npts + NT - 1) / NT;
ComputeSigmaKernel<<<numBlocks,NT>>> (update_mem, npts, glob_delta_strain, glob_sigma, lambda, mu, mc_phi, mc_psi, mc_cohesion, dPlasticStrain, dMType, dAlpha, dSigma, dStrain, weight);
cudaDeviceSynchronize();
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess) {
std::string error_string = cudaGetErrorString(error);
std::string error_message = "failed to perform ComputeSigmaKernel: " + error_string;
throw std::runtime_error(error_message);
}
}
void TPZCudaCalls::ComputeSigmaDep(bool update_mem, int npts, REAL *glob_delta_strain, REAL *glob_sigma, REAL *glob_dep, REAL lambda, REAL mu, REAL mc_phi, REAL mc_psi, REAL mc_cohesion, REAL *dPlasticStrain,
REAL *dMType, REAL *dAlpha, REAL *dSigma, REAL *dStrain, REAL *weight) {
int numBlocks = (npts + NT - 1) / NT;
ComputeSigmaDepKernel<<<numBlocks,NT>>> (update_mem, npts, glob_delta_strain, glob_sigma, glob_dep, lambda, mu, mc_phi, mc_psi, mc_cohesion, dPlasticStrain, dMType, dAlpha, dSigma, dStrain, weight);
cudaDeviceSynchronize();
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess) {
std::string error_string = cudaGetErrorString(error);
std::string error_message = "failed to perform ComputeSigmaDepKernel: " + error_string;
throw std::runtime_error(error_message);
}
}
void TPZCudaCalls::MatrixAssemble(REAL *Kc, REAL *dep, int nel, int *el_color_index,
REAL *storage, int *rowsizes, int *colsizes, int *rowfirstindex, int *colfirstindex, int *matrixposition, int *matrixstride) {
int numBlocks = (nel + NT_sm - 1) / NT_sm;
MatrixAssembleKernel<<<numBlocks,NT_sm>>>(nel, Kc, dep, el_color_index, storage, rowsizes, colsizes, rowfirstindex, colfirstindex, matrixposition, matrixstride);
cudaDeviceSynchronize();
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess) {
std::string error_string = cudaGetErrorString(error);
std::string error_message = "failed to perform MatrixAssembleKernel: " + error_string;
throw std::runtime_error(error_message);
}
}
void TPZCudaCalls::DeToDevice(REAL lambda, REAL mu) {
REAL De_host[] = {lambda + 2.0*mu, 0, lambda, 0, mu, 0, lambda, 0, lambda + 2.0*mu};
cudaMemcpyToSymbol(De, &De_host, 9 * sizeof(REAL));
}
void TPZCudaCalls::SolveCG(int n, int nnzA, REAL *csrValA, int *csrRowPtrA, int *csrColIndA, REAL *r, REAL *x) {
if(cusparse_h == false) {
cusparse_h = true;
cusparseStatus_t result = cusparseCreate(&handle_cusparse);
if (result != CUSPARSE_STATUS_SUCCESS) {
throw std::runtime_error("failed to initialize cuSparse");
}
}
if(cublas_h == false) {
cublas_h = true;
cublasStatus_t result = cublasCreate(&handle_cublas);
if (result != CUBLAS_STATUS_SUCCESS) {
throw std::runtime_error("failed to initialize cuBLAS");
}
}
cusparseMatDescr_t descr;
cusparseCreateMatDescr(&descr);
cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_SYMMETRIC);
cusparseSetMatFillMode(descr, CUSPARSE_FILL_MODE_UPPER);
cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO);
REAL alpha = 1.0;
REAL alpham1 = -1.0;
REAL beta = 0.0;
REAL r0 = 0.;
REAL b;
REAL r1;
REAL dot;
REAL a;
REAL na;
REAL *d_Ax;
REAL *d_p;
cudaMalloc((void **)&d_Ax, n*sizeof(REAL));
cudaMalloc((void **)&d_p, n*sizeof(REAL));
cusparseDcsrmv(handle_cusparse,CUSPARSE_OPERATION_NON_TRANSPOSE, n, n, nnzA, &alpha, descr, csrValA, csrRowPtrA, csrColIndA, x, &beta, d_Ax);
cublasDaxpy(handle_cublas, n, &alpham1, d_Ax, 1, r, 1);
cublasDdot(handle_cublas, n, r, 1, r, 1, &r1);
const REAL tol = 1.e-5;
const int max_iter = 10000;
int k;
k = 1;
while (r1 > tol*tol && k <= max_iter)
{
if (k > 1)
{
b = r1 / r0;
cublasDscal(handle_cublas, n, &b, d_p, 1);
cublasDaxpy(handle_cublas, n, &alpha, r, 1, d_p, 1);
}
else
{
cublasDcopy(handle_cublas, n, r, 1, d_p, 1);
}
cusparseDcsrmv(handle_cusparse, CUSPARSE_OPERATION_NON_TRANSPOSE, n, n, nnzA, &alpha, descr, csrValA, csrRowPtrA, csrColIndA, d_p, &beta, d_Ax);
cublasDdot(handle_cublas, n, d_p, 1, d_Ax, 1, &dot);
a = r1 / dot;
cublasDaxpy(handle_cublas, n, &a, d_p, 1, x, 1);
na = -a;
cublasDaxpy(handle_cublas, n, &na, d_Ax, 1, r, 1);
r0 = r1;
cublasDdot(handle_cublas, n, r, 1, r, 1, &r1);
cudaThreadSynchronize();
k++;
}
cudaFree(d_p);
cudaFree(d_Ax);
}