-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcuMatSparse.h
429 lines (334 loc) · 13.4 KB
/
cuMatSparse.h
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
/*
* cuMatSparse.h
*
* Created on: 2016/02/24
* Author: takeshi.fujita
*/
#ifndef CUMATSPARSE_H_
#define CUMATSPARSE_H_
#include<iostream>
#include<cuda_runtime_api.h>
#include<cublas_v2.h>
#include<cusparse_v2.h>
#include<thrust/device_vector.h>
#include "cuMat.h"
class cuMatSparse {
public:
int rows = 0;
int cols = 0;
cusparseHandle_t cuHandle;
cusparseMatDescr_t descr;
float *csrVal = NULL;
int *csrRowPtr = NULL;
int *csrColInd = NULL;
float *csrValDevice = NULL;
int *csrRowPtrDevice = NULL;
int *csrColIndDevice = NULL;
int numVals = 0;
cuMat rt, bt;
cuMatSparse(){
cusparseCreate(&cuHandle);
cusparseCreateMatDescr(&descr);
cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_GENERAL);
cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO);
}
cuMatSparse(int rows, int cols, int numberOfVals){
cout << "cuMatSparse(int rows, int numberOfVals)" << endl;
cusparseCreate(&cuHandle);
cusparseCreateMatDescr(&descr);
cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_GENERAL);
cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO);
new_matrix(rows, cols, numberOfVals);
}
cuMatSparse(vector<float> &ids, int col_nums) : cuMatSparse(){
embed(ids, col_nums);
}
~cuMatSparse(){
cusparseDestroyMatDescr(descr);
cusparseDestroy(cuHandle);
free(csrVal);
free(csrRowPtr);
free(csrColInd);
cudaFree(csrValDevice);
cudaFree(csrRowPtrDevice);
cudaFree(csrColIndDevice);
}
void new_matrix(int rows, int cols, int numberOfVals){
this->rows = rows;
this->cols = cols;
this->numVals = numberOfVals;
cudaError_t error = cudaMalloc((void**) &csrValDevice, numberOfVals * sizeof(*csrValDevice));
error = cudaMalloc((void**) &csrRowPtrDevice, (rows+1) * sizeof(*csrRowPtrDevice));
error = cudaMalloc((void**) &csrColIndDevice, numberOfVals * sizeof(*csrColIndDevice));
cudaMemset(csrValDevice, 0x00, numberOfVals * sizeof(*csrValDevice));
cudaMemset(csrRowPtrDevice, 0x00, (rows+1) * sizeof(*csrRowPtrDevice));
cudaMemset(csrColIndDevice, 0x00, numberOfVals * sizeof(*csrColIndDevice));
}
cuMatSparse &operator=(const cuMatSparse &a) {
new_matrix(a.rows, a.cols, a.numVals);
cudaError_t error = cudaMemcpy(csrValDevice, a.csrValDevice, a.numVals * sizeof(*csrValDevice), cudaMemcpyDeviceToDevice);
error = cudaMemcpy(csrRowPtrDevice, a.csrRowPtrDevice, (a.rows+1) * sizeof(*csrRowPtrDevice), cudaMemcpyDeviceToDevice);
error = cudaMemcpy(csrColIndDevice, a.csrColIndDevice, a.numVals * sizeof(*csrColIndDevice), cudaMemcpyDeviceToDevice);
//cout << "a.rows:" << a.rows << " a.cols:" << a.cols << endl;
//cout << "this->rows:" << this->rows << " this->cols:" << this->cols << endl;
return *this;
}
void zeros(){
cudaMemset(csrValDevice, 0x00, numVals * sizeof(*csrValDevice));
cudaMemset(csrRowPtrDevice, 0x00, (rows+1) * sizeof(*csrRowPtrDevice));
cudaMemset(csrColIndDevice, 0x00, numVals * sizeof(*csrColIndDevice));
}
void memSetHost(float *v, int *r, int *c) {
cudaError_t error = cudaMemcpy(csrValDevice, v, numVals * sizeof(*csrValDevice), cudaMemcpyHostToDevice);
if (error != cudaSuccess) printf("memSetHost cudaMemcpy error: csrValDevice\n");
error = cudaMemcpy(csrRowPtrDevice, r, (rows+1) * sizeof(*csrRowPtrDevice), cudaMemcpyHostToDevice);
if (error != cudaSuccess) printf("memSetHost cudaMemcpy error: csrRowPtrDevice\n");
error = cudaMemcpy(csrColIndDevice, c, numVals * sizeof(*csrColIndDevice), cudaMemcpyHostToDevice);
if (error != cudaSuccess) printf("memSetHost cudaMemcpy error: csrColIndDevice\n");
}
//column majar format
void embed(vector<float> &ids, int col_nums){
rows = ids.size();
cols = col_nums;
int num_vals = rows;
numVals = num_vals;
csrVal = (float *)malloc(num_vals * sizeof(*csrVal));
csrRowPtr = (int *)malloc((rows+1) * sizeof(*csrRowPtr));
csrColInd = (int *)malloc(num_vals * sizeof(*csrColInd));
cudaError_t error = cudaMalloc((void**) &csrValDevice, num_vals * sizeof(*csrValDevice));
error = cudaMalloc((void**) &csrRowPtrDevice, (rows+1) * sizeof(*csrRowPtrDevice));
error = cudaMalloc((void**) &csrColIndDevice, num_vals * sizeof(*csrColIndDevice));
memset(csrRowPtr, 0x00, (rows+1) * sizeof(*csrRowPtr));
csrRowPtr[0] = 0;
for(int i=0; i<rows; i++){
csrVal[i] = 1.; //value is 1
csrColInd[i] = ids[i];
csrRowPtr[i+1] = csrRowPtr[i] + 1; //only a element per row
}
/*
cout << "csrVal:" << endl;
for(int i=0; i<num_vals; i++){
cout << csrVal[i] << " ";
}
cout << endl;
cout << "csrRowPtr:" << endl;
for(int i=0; i<row_nums+1; i++){
cout << csrRowPtr[i] << " ";
}
cout << endl;
cout << "csrColInd:" << endl;
for(int i=0; i<num_vals; i++){
cout << csrColInd[i] << " ";
}
cout << endl;
*/
memSetHost(csrVal, csrRowPtr, csrColInd);
}
/*
//row majar format
void embed(vector<float> &ids, int row_nums){
rows = row_nums;
cols = ids.size();
int num_vals = cols;
numVals = num_vals;
csrVal = (float *)malloc(num_vals * sizeof(*csrVal));
csrRowPtr = (int *)malloc((row_nums+1) * sizeof(*csrRowPtr));
csrColInd = (int *)malloc(num_vals * sizeof(*csrColInd));
cudaError_t error = cudaMalloc((void**) &csrValDevice, num_vals * sizeof(*csrValDevice));
error = cudaMalloc((void**) &csrRowPtrDevice, (row_nums+1) * sizeof(*csrRowPtrDevice));
error = cudaMalloc((void**) &csrColIndDevice, num_vals * sizeof(*csrColIndDevice));
memset(csrRowPtr, 0x00, (row_nums+1) * sizeof(*csrRowPtr));
int row_ptr_cnt = 0;
csrRowPtr[0] = row_ptr_cnt;
for(int i=0; i<num_vals; i++){
csrVal[i] = 1.;
for(int j=0; j<row_nums; j++){
if (ids[i] == j){
row_ptr_cnt++;
csrColInd[i] = i;
csrRowPtr[j+1] += row_ptr_cnt;
}
}
}
for(int j=1; j<row_nums; j++){
if (csrRowPtr[j] == 0.){
csrRowPtr[j] = csrRowPtr[j-1];
}
}
cout << "csrVal:" << endl;
for(int i=0; i<num_vals; i++){
cout << csrVal[i] << " ";
}
cout << endl;
cout << "csrRowPtr:" << endl;
for(int i=0; i<row_nums+1; i++){
cout << csrRowPtr[i] << " ";
}
cout << endl;
cout << "csrColInd:" << endl;
for(int i=0; i<num_vals; i++){
cout << csrColInd[i] << " ";
}
cout << endl;
memSetHost(csrVal, csrRowPtr, csrColInd);
}
*/
/*
friend ostream &operator<<(ostream &output, cuMat &a) {
for(int i=0; i<numVals; i++){
output << csrVal[i];
output << endl;
}
}*/
void s_s_dot(cuMatSparse &b, cuMatSparse &c){
cusparseStatus_t status =
cusparseScsrgemm(cuHandle,
CUSPARSE_OPERATION_NON_TRANSPOSE,
CUSPARSE_OPERATION_NON_TRANSPOSE,
rows,
b.cols,
cols,
descr,
numVals,
csrValDevice,
csrRowPtrDevice,
csrColIndDevice,
b.descr,
b.numVals,
b.csrValDevice,
b.csrRowPtrDevice,
b.csrColIndDevice,
c.descr,
c.csrValDevice,
c.csrRowPtrDevice,
c.csrColIndDevice );
if (status != CUSPARSE_STATUS_SUCCESS) {
cout << "ERROR cuMatSparse::s_s_dot cusparseXcsrgeamNnz" << endl;
}
cudaThreadSynchronize();
}
void s_d_dot(cuMat &b, cuMat &c){
float alpha = 1.;
float beta = 0.;
cusparseStatus_t status = cusparseScsrmm(cuHandle,
CUSPARSE_OPERATION_NON_TRANSPOSE,
rows,
b.cols,
cols,
numVals,
&alpha,
descr,
csrValDevice,
csrRowPtrDevice,
csrColIndDevice,
b.mDevice,
b.rows,
&beta,
c.mDevice,
c.rows);
if (status != CUSPARSE_STATUS_SUCCESS) {
cout << "ERROR cuMatSparse::s_d_dot cusparseScsrmm" << endl;
cout << "a rows:" << rows << " cols:" << cols << endl;
cout << "b rows:" << b.rows << " cols:" << b.cols << endl;
cout << "c rows:" << c.rows << " cols:" << c.cols << endl;
switch(status) {
case CUSPARSE_STATUS_NOT_INITIALIZED:
cout << "CUSPARSE_STATUS_NOT_INITIALIZED" << endl;
break;
case CUSPARSE_STATUS_ALLOC_FAILED:
cout << "CUSPARSE_STATUS_ALLOC_FAILED" << endl;
break;
case CUSPARSE_STATUS_INVALID_VALUE:
cout << "CUSPARSE_STATUS_INVALID_VALUE" << endl;
break;
case CUSPARSE_STATUS_ARCH_MISMATCH:
cout << "CUSPARSE_STATUS_ARCH_MISMATCH" << endl;
break;
case CUSPARSE_STATUS_EXECUTION_FAILED:
cout << "CUSPARSE_STATUS_EXECUTION_FAILED" << endl;
break;
case CUSPARSE_STATUS_INTERNAL_ERROR:
cout << "CUSPARSE_STATUS_INTERNAL_ERROR" << endl;
break;
case CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED:
cout << "CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED" << endl;
break;
}
}
cudaThreadSynchronize();
}
void d_s_dot(cuMat &b, cuMat &r){
cuMatSparse t = this->transpose(); //waste time here
if (rt.rows == 0){
rt = r.transpose();
}
if (bt.rows == 0){
bt = b.transpose();
}
b.transpose(bt);
t.s_d_dot(bt, rt);
rt.transpose(r);
}
void transpose(cuMatSparse &r){
cusparseStatus_t status = cusparseScsr2csc(cuHandle, rows, cols, numVals,
csrValDevice, csrRowPtrDevice,
csrColIndDevice, r.csrValDevice,
r.csrColIndDevice, r.csrRowPtrDevice,
CUSPARSE_ACTION_NUMERIC,
CUSPARSE_INDEX_BASE_ZERO);
if (status != CUSPARSE_STATUS_SUCCESS) {
cout << "transpose error" << endl;
}
cudaThreadSynchronize();
}
cuMatSparse transpose(){
//std::chrono::system_clock::time_point start, end;
//start = std::chrono::system_clock::now();
cuMatSparse r(cols, rows, numVals);
transpose(r);
return r;
}
cuMat toDense(){
cuMat r(rows, cols);
cusparseStatus_t status = cusparseScsr2dense(cuHandle,
r.rows,
r.cols,
descr,
csrValDevice,
csrRowPtrDevice,
csrColIndDevice,
r.mDevice,
rows);
if (status != CUSPARSE_STATUS_SUCCESS) {
cout << "toDense error" << endl;
}
cudaThreadSynchronize();
return r;
}
cuMatSparse toSparse(cuMat &a, int numVals){
cuMatSparse r(a.rows, a.cols, a.rows);
int *nnzPerRowColumn;
cudaMalloc((void **)&nnzPerRowColumn, sizeof(int) * r.rows);
int nnzTotalDevHostPtr = numVals;
cusparseStatus_t status = cusparseSnnz(r.cuHandle, CUSPARSE_DIRECTION_ROW, r.rows,
r.cols, r.descr,
a.mDevice,
r.rows, nnzPerRowColumn, &nnzTotalDevHostPtr);
if (status != CUSPARSE_STATUS_SUCCESS) {
cout << "toSparse cusparseSnnz error" << endl;
}
cudaThreadSynchronize();
status = cusparseSdense2csr(r.cuHandle, r.rows, r.cols,
r.descr,
a.mDevice,
r.rows, nnzPerRowColumn,
r.csrValDevice,
r.csrRowPtrDevice, r.csrColIndDevice);
if (status != CUSPARSE_STATUS_SUCCESS) {
cout << "toSparse cusparseSdense2csr error" << endl;
}
cudaThreadSynchronize();
return r;
}
};
#endif /* CUMATSPARSE_H_ */