-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgpu_operations.h
580 lines (482 loc) · 19.7 KB
/
gpu_operations.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
Copyright © 2015-2017 Thomas Unterthiner
Additional Contributions by Thomas Adler, Balázs Bencze
Licensed under GPL, version 2 or a later (see LICENSE.txt)
*/
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <curand.h>
#include <curand_kernel.h>
#include <cusolverDn.h>
#include <cstring>
#include <ctype.h>
#include <map>
#include <cusparse_v2.h>
#include <typeinfo> /* for typeid */
#ifndef COMPILE_FOR_R
#include <stdio.h>
#include <assert.h>
#else
#include "use_R_impl.h"
#endif
#ifdef MEM_DEBUG
extern size_t allocated_memory;
#endif
// This code to print a stack trace will only work on Linux
#ifndef NDEBUG
#include <execinfo.h>
static void print_stacktrace() {
const int MAX_SIZE = 15;
void *array[MAX_SIZE];
int size = backtrace(array, MAX_SIZE);
char **strings = 0;
strings = backtrace_symbols (array, size);
printf("Obtained %d stack frames:\n", size);
for (int i = 0; i < size; ++i)
printf ("%s\n", strings[i]);
free (strings);
}
#else
static void print_stacktrace() {
}
#endif
using std::fprintf;
inline cublasFillMode_t uplo_to_cublas(const char* uplo) {
return tolower(uplo[0]) == 'l' ? CUBLAS_FILL_MODE_LOWER : CUBLAS_FILL_MODE_UPPER;
}
inline cusparseOperation_t op_to_cusparse(const char* op) {
return tolower(op[0]) == 't' ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE;
}
static const char* cusparseErrorString(cusparseStatus_t error) {
switch (error) {
case CUSPARSE_STATUS_SUCCESS: return "CUSPARSE_STATUS_SUCCESS";
case CUSPARSE_STATUS_NOT_INITIALIZED: return "CUSPARSE_STATUS_NOT_INITIALIZED";
case CUSPARSE_STATUS_ALLOC_FAILED: return "CUSPARSE_STATUS_ALLOC_FAILED";
case CUSPARSE_STATUS_INVALID_VALUE: return "CUSPARSE_STATUS_INVALID_VALUE";
case CUSPARSE_STATUS_ARCH_MISMATCH: return "CUSPARSE_STATUS_ARCH_MISMATCH";
case CUSPARSE_STATUS_MAPPING_ERROR: return "CUSPARSE_STATUS_MAPPING_ERROR";
case CUSPARSE_STATUS_EXECUTION_FAILED: return "CUSPARSE_STATUS_EXECUTION_FAILED";
case CUSPARSE_STATUS_INTERNAL_ERROR: return "CUSPARSE_STATUS_INTERNAL_ERROR";
case CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED: return "CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED";
case CUSPARSE_STATUS_ZERO_PIVOT: return "CUSPARSE_STATUS_ZERO_PIVOT";
default: return "<unknown>";
}
}
static const char* cublasErrorString(cublasStatus_t error) {
switch (error) {
case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
#if CUDA_VERSION >= 6000
case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
#endif
default: return "<unknown>";
}
}
#ifndef DNDEBUG
#define CUDA_CALL(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort = true) {
if (code != cudaSuccess) {
fprintf(stderr, "CUDA Error: %s %s:%d\n", cudaGetErrorString(code), file, line);
print_stacktrace();
if (abort)
exit(code);
}
}
#define CUBLAS_CALL(ans) { cublasAssert((ans), __FILE__, __LINE__); }
inline void cublasAssert(cublasStatus_t code, const char *file, int line) {
//printf("%d (%s:%d)\n", code, file, line);
if (code != CUBLAS_STATUS_SUCCESS) {
fprintf(stderr, "CUBLAS Error: %s %s:%d\n", cublasErrorString(code), file, line);
print_stacktrace();
exit(code);
}
}
#define CUSPARSE_CALL(ans) { cusparseAssert((ans), __FILE__, __LINE__); }
inline void cusparseAssert(cusparseStatus_t code, const char *file, int line) {
// printf("%d (%s:%d)\n", code, file, line);
if (code != CUSPARSE_STATUS_SUCCESS) {
fprintf(stderr, "CUSPARSE Error: %s %s:%d\n", cusparseErrorString(code), file, line);
print_stacktrace();
exit(code);
}
}
static const char* cusolverErrorString(cusolverStatus_t error) {
switch (error) {
case CUSOLVER_STATUS_SUCCESS: return "CUSOLVER_STATUS_SUCCESS";
case CUSOLVER_STATUS_NOT_INITIALIZED: return "CUSOLVER_STATUS_NOT_INITIALIZED";
case CUSOLVER_STATUS_ALLOC_FAILED: return "CUSOLVER_STATUS_ALLOC_FAILED";
case CUSOLVER_STATUS_INVALID_VALUE: return "CUSOLVER_STATUS_INVALID_VALUE";
case CUSOLVER_STATUS_ARCH_MISMATCH: return "CUSOLVER_STATUS_ARCH_MISMATCH";
case CUSOLVER_STATUS_EXECUTION_FAILED: return "CUSOLVER_STATUS_EXECUTION_FAILED";
case CUSOLVER_STATUS_INTERNAL_ERROR: return "CUSOLVER_STATUS_INTERNAL_ERROR";
case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED: return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED";
default: return "<unknown>";
}
}
#define CUSOLVER_CALL(ans) { cusolverAssert((ans), __FILE__, __LINE__); }
inline void cusolverAssert(cusolverStatus_t code, const char *file, int line) {
//printf("%d (%s:%d)\n", code, file, line);
if (code != CUSOLVER_STATUS_SUCCESS) {
fprintf(stderr, "CUBLAS Error: %s %s:%d\n", cusolverErrorString(code), file, line);
print_stacktrace();
exit(code);
}
}
#else
#define CUBLAS_CALL(ans) (ans)
#define CUDA_CALL(ans) (ans)
#define CUSOLVER_CALL(ans) (ans)
#define CUSPARSE_CALL(ans) (ans)
#endif
#define MAX_STREAMS 16
class GPU_Operations {
cublasHandle_t handle;
curandState* rng_state;
cusolverDnHandle_t cudense_handle;
cusparseHandle_t cusparse_handle;
std::map<size_t, void*> buffer_map; // keeps track of buffers allocated for potrf
int* devinfo; // cuSOLVER error reporting
cudaStream_t streams[MAX_STREAMS];
cusparseMatDescr_t descr;
public:
float* ones;
struct SparseMatrix {
float *values;
int *columns;
int *rowPointers;
int n; // number of rows
int nnz; // number of nonzero elements
};
const SparseMatrix INVALID = {
(float*)-1, (int*)-1, (int*)-1, 0, 0
};
static SparseMatrix create_sparse_matrix(const float* Xvals, const int* Xcols, const int *Xrowptr, int n, int m);
static void free_sparse_matrix(const SparseMatrix& x);
GPU_Operations(int n, int m, int k, unsigned long seed, int gpu_id);
~GPU_Operations();
float* to_device(const float* src, size_t size) const;
int* to_device(const int* src, size_t size) const;
SparseMatrix* to_device(const SparseMatrix* src, size_t size) const;
float* to_host(float* src, float* dst, size_t size) const {
CUDA_CALL(cudaMemcpy(dst, src, size, cudaMemcpyDeviceToHost));
free(src);
return dst;
}
int* to_host(int* src, int* dst, size_t size) const {
CUDA_CALL(cudaMemcpy(dst, src, size, cudaMemcpyDeviceToHost));
free(src);
return dst;
}
float* copy_to_host(const float* src, float* dst, size_t size) const {
CUDA_CALL(cudaMemcpy(dst, src, size, cudaMemcpyDeviceToHost));
return dst;
}
int* copy_to_host(const int* src, int* dst, size_t size) const {
CUDA_CALL(cudaMemcpy(dst, src, size, cudaMemcpyDeviceToHost));
return dst;
}
void set_stream(unsigned iterator) const {
unsigned stream_id = iterator % MAX_STREAMS;
CUBLAS_CALL(cublasSetStream_v2(handle, streams[stream_id]));
}
void synchronize_stream(unsigned iterator) const {
unsigned stream_id = iterator % MAX_STREAMS;
CUDA_CALL(cudaStreamSynchronize(streams[stream_id]));
}
void synchronize_all_streams() const {
for (unsigned i = 0; i < MAX_STREAMS; ++i) {
synchronize_stream(i);
}
}
void default_stream() const {
CUBLAS_CALL(cublasSetStream_v2(handle, NULL));
}
void gemm(const char *transa, const char *transb, const int m, const int n, const int k, const float alpha,
const float *a, const int lda, const float *b, const int ldb, const float beta, float *c,
const int ldc) const {
cublasOperation_t ta = tolower(transa[0]) == 'n' ? CUBLAS_OP_N : CUBLAS_OP_T;
cublasOperation_t tb = tolower(transb[0]) == 'n' ? CUBLAS_OP_N : CUBLAS_OP_T;
CUBLAS_CALL(cublasSgemm(handle, ta, tb, m, n, k, &alpha, a, lda, b, ldb, &beta, c, ldc));
}
void gemm(const char *transa, const char *transb, const int m,
const int n, const int k, const float alpha,
const SparseMatrix* a, const int lda, const float *b,
const int ldb, const float beta, float *c,
const int ldc);
void gemm(const char *transa, const char *transb, const int m,
const int n, const int k, const float alpha, const float *a,
const int lda, const SparseMatrix* b, const int ldb,
const float beta, float *c, const int ldc);
void dgmm(const char* mode, const int m, const int n, const float* A,
int lda, const float* x, int incx, float* C,
int ldc) const {
cublasSideMode_t lr = mode[0] == 'l' ? CUBLAS_SIDE_LEFT : CUBLAS_SIDE_RIGHT;
CUBLAS_CALL(cublasSdgmm(handle, lr, m, n, A, lda, x, incx, C, ldc));
}
void symm(const char *side, const char *uplo, const int m, const int n,
const float alpha, const float *a, const int lda, const float *b,
const int ldb, const float beta, float *c, const int ldc) const {
cublasSideMode_t s = tolower(side[0]) == 'l' ? CUBLAS_SIDE_LEFT : CUBLAS_SIDE_RIGHT;
cublasFillMode_t ul = uplo_to_cublas(uplo);
CUBLAS_CALL(cublasSsymm(handle, s, ul, m, n, &alpha,a, lda, b, ldb, &beta, c, ldc));
}
void dot (int n, const float *x, int incx, const float *y, int incy, float *result) {
CUBLAS_CALL(cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_DEVICE)); // we pass result in device space
CUBLAS_CALL(cublasSdot(handle, n, x, incx, y, incy, result));
CUBLAS_CALL(cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST));
}
void axpy(const int n, const float alpha, const float* x, const int incx, float *y, const int incy) const {
CUBLAS_CALL(cublasSaxpy(handle, n, &alpha, x, incx, y, incy));
}
int potrf(const char *uplo, int n, float* a, int lda) {
cublasFillMode_t ul = uplo_to_cublas(uplo);
int bufsize = 0;
int info = 0;
CUSOLVER_CALL(cusolverDnSpotrf_bufferSize(cudense_handle, ul, n, a, lda, &bufsize));
float* buffer = (float*) get_buffer(bufsize * sizeof(float));
CUSOLVER_CALL(cusolverDnSpotrf(cudense_handle, ul, n, a, lda, buffer, bufsize, devinfo));
CUDA_CALL(cudaMemcpy(&info, devinfo, sizeof(int), cudaMemcpyDeviceToHost));
return info;
}
void* get_buffer(size_t bufsize) {
// See if we already have a buffer of correct size, otherwise allocate
void* buffer = 0;
auto it = buffer_map.find(bufsize);
if (it != buffer_map.end()) {
buffer = it->second;
} else {
buffer = malloc(bufsize);
buffer_map[bufsize] = buffer;
}
return buffer;
}
int potrs(const char *uplo, int n, int nrhs, float * a, int lda, float *b, int ldb) const {
int info;
cublasFillMode_t ul = uplo_to_cublas(uplo);
CUSOLVER_CALL(cusolverDnSpotrs(cudense_handle, ul, n, nrhs, a, lda, b, ldb, devinfo));
CUDA_CALL(cudaMemcpy(&info, devinfo, sizeof(info), cudaMemcpyDeviceToHost));
return info;
}
int posv(const char *uplo, int n, int nrhs, float * a, int lda, float *b, int ldb) {
int info = potrf(uplo, n, a, lda);
if (info == 0)
info = potrs(uplo, n, nrhs, a, lda, b, ldb);
return info;
}
void* memset(void* dest, int ch, size_t count) const {
CUDA_CALL(cudaMemset(dest, ch, count));
return dest;
}
float* memcpy(void* dest, const void *src, size_t count) const {
CUDA_CALL(cudaMemcpy(dest, src, count, cudaMemcpyDeviceToDevice));
return 0;
}
void free(void* ptr) const {
if (ptr != 0 && ptr != &INVALID) {
CUDA_CALL(cudaFree(ptr));
}
}
void free_devicememory(void* ptr) const {
if (ptr != 0) {
CUDA_CALL(cudaFree(ptr));
}
}
void free_devicememory(SparseMatrix* matrix) {
if (matrix != 0 && matrix != &INVALID) {
free(matrix->columns);
free(matrix->values);
free(matrix->rowPointers);
std::free(matrix);
}
}
float* malloc(size_t size) const {
#ifdef MEM_DEBUG
allocated_memory += size;
#endif
float* retval = 0;
cudaError_t err = cudaMalloc(&retval, size);
CUDA_CALL(err);
if (err != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed\n");
retval = 0;
}
return retval;
}
int* malloci(size_t size) const {
#ifdef MEM_DEBUG
allocated_memory += size;
#endif
int* retval = 0;
cudaError_t err = cudaMalloc(&retval, size);
CUDA_CALL(err);
if (err != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed\n");
retval = 0;
}
return retval;
}
void fill_eye(float* X, unsigned n) const;
void fill(float* X, const unsigned size, const float value) const;
void maximum(float* x, const float value, const unsigned size) const;
void leaky_relu(float* x, const float value, const unsigned size) const;
void tanh(float* x, const unsigned size) const;
void sigmoid(float* x, const unsigned size) const;
void soft_threshold(float* x, const float alpha, const int size) const;
void invsqrt(float* s, const unsigned n) const;
void invert(float* X, const unsigned size) const;
void calculate_column_variance(const float* X, const unsigned nrows, const unsigned ncols, float* variances, float eps) const;
void scale_columns(float* X, const unsigned nrows, const unsigned ncols, float* s) const;
void scale_rows(float* X, const unsigned nrows, const unsigned ncols, float* s) const;
void dropout(float* X, const unsigned size, const float dropout_rate) const;
void add_saltpepper_noise(float* X, const unsigned size, const float noise_rate) const;
void add_gauss_noise(float* X, const unsigned size, const float noise_rate) const;
void calculate_column_variance(const SparseMatrix* X, const unsigned nrows, const unsigned ncols, float* variances, float eps);
void scale_columns(SparseMatrix* X, const unsigned nrows, const unsigned ncols, float* s) const;
void scale_rows(SparseMatrix* X, const unsigned nrows, const unsigned ncols, float* s) const;
void dropout(SparseMatrix* X, const unsigned size, const float dropout_rate) const;
void add_saltpepper_noise(SparseMatrix* X, const unsigned size, const float noise_rate) const;
void add_gauss_noise(SparseMatrix* X, const unsigned size, const float noise_rate) const;
template<typename T>
T init_invalid(void) {
return (typeid(T) == typeid(SparseMatrix*) ? (T) &INVALID : (T) 0);
}
template<typename T>
T malloc_matrix(int rows, int cols) {
return malloc_matrix(rows, cols, init_invalid<T>());
}
SparseMatrix* malloc_matrix(int rows, int cols, SparseMatrix* dummy) {
SparseMatrix* matrix = (SparseMatrix*) std::malloc(sizeof(SparseMatrix));
matrix->rowPointers = malloci((rows + 1) * sizeof(int));
return matrix;
}
float* malloc_matrix(int rows, int cols, float *dummy) {
return (float*) malloc(rows * cols * sizeof(float));
}
void free_malloc_matrix(float* matrix) {
free(matrix);
}
void free_malloc_matrix(SparseMatrix* matrix) {
if (matrix != &INVALID) {
free(matrix->rowPointers);
std::free(matrix);
}
}
float *memcpy_matrix(float *dest, float *src, int nrows_to_copy, int src_ncol, int first_row = 0) const {
return memcpy(dest, &src[first_row * src_ncol], nrows_to_copy * src_ncol * sizeof(float));
}
SparseMatrix* memcpy_matrix(SparseMatrix* dest, SparseMatrix* src, int nrows_to_copy, int src_ncol, int first_row = 0) const {
int fromIndex = 0;
int toIndex = 0;
CUDA_CALL(cudaMemcpy(&fromIndex, &src->rowPointers[first_row], sizeof(int), cudaMemcpyDeviceToHost));
CUDA_CALL(cudaMemcpy(&toIndex , &src->rowPointers[first_row + nrows_to_copy], sizeof(int), cudaMemcpyDeviceToHost));
dest->nnz = (toIndex - fromIndex);
dest->n = nrows_to_copy;
dest->values = malloc(dest->nnz * sizeof(float));
dest->columns = malloci(dest->nnz * sizeof(int));
// rowPointers already exists, see malloc_matrix, free_malloc_matrix and free_memcpy_matrix
//dest->rowPointers = malloci((nrows_to_copy + 1) * sizeof(int));
memcpy(dest->values, &src->values[fromIndex], dest->nnz * sizeof(float));
memcpy(dest->columns, &src->columns[fromIndex], dest->nnz * sizeof(int));
memcpy(dest->rowPointers, &src->rowPointers[first_row], (nrows_to_copy + 1) * sizeof(int));
subtract_first_element(dest->rowPointers, nrows_to_copy + 1);
return dest;
}
void free_memcpy_matrix(float* matrix) {
// do nothing
}
void free_memcpy_matrix(SparseMatrix* matrix) {
free(matrix->values);
free(matrix->columns);
}
void subtract_first_element(int* a, unsigned len) const;
void free_batch(void *ptr) {
}
void free_batch(SparseMatrix* a) {
// see get batch
if (handle_valid(a)) {
std::free(a);
}
}
bool handle_valid(SparseMatrix* a) {
return a != &INVALID;
}
float* get_batch(const float* X, int ncol, int batch_num, int batch_size) {
/* return pointer */
return (float*) &X[batch_num * batch_size * ncol];
}
SparseMatrix* get_batch(SparseMatrix* X, int ncol, int batch_num, int batch_size) {
int fromIndex = 0;
int toIndex = 0;
CUDA_CALL(cudaMemcpy(&fromIndex, &X->rowPointers[batch_num * batch_size], sizeof(int), cudaMemcpyDeviceToHost));
CUDA_CALL(cudaMemcpy(&toIndex, &X->rowPointers[(batch_num + 1) * batch_size], sizeof(int), cudaMemcpyDeviceToHost));
SparseMatrix* dest = (SparseMatrix*) std::malloc(sizeof(SparseMatrix));
dest->rowPointers = X->rowPointers + batch_num * batch_size;
dest->n = batch_size;
dest->nnz = toIndex - fromIndex;
dest->columns = X->columns + fromIndex;
dest->values = X->values + fromIndex;
return dest;
}
SparseMatrix* transpose(const SparseMatrix* x, int ncol) {
SparseMatrix* t = (SparseMatrix*) std::malloc(sizeof(SparseMatrix));
t->values = //(float*) get_buffer(x->nnz * sizeof(float));
malloc(x->nnz * sizeof(float));
t->columns = //(int*) get_buffer(x->nnz * sizeof(int));
malloci(x->nnz * sizeof(int));
t->rowPointers = //(int*) get_buffer((ncol + 1) * sizeof(int));
malloci((ncol + 1) * sizeof(int));
t->nnz = x->nnz;
t->n = ncol;
CUSPARSE_CALL(cusparseScsr2csc(cusparse_handle, x->n, ncol, x->nnz, x->values, x->rowPointers, x->columns, t->values,
t->columns, t->rowPointers, CUSPARSE_ACTION_NUMERIC, CUSPARSE_INDEX_BASE_ZERO));
return t;
}
// Useful for debugging
void printm(const char* name, const SparseMatrix *a, int n, int m) const {
printf("%s\n", name);
printMatrixSPM(a, n, m, 0);
}
void printm(const char* name, const float* a, int n, int m) const {
printf("%s\n", name);
printMatrixRM(a, n, m, 0);
}
void printMatrixCM(const float* a, int n, int m, const char* fmt) const;
void printMatrixRM(const float* a, int n, int m, const char* fmt) const;
void printMatrixSP(const SparseMatrix* a, const char* fmt) const;
void printMatrixRM(const SparseMatrix* a, int n, int m, const char* fmt) const {
printMatrixSPM(a, n, m, fmt);
}
void printMatrixSPM(const SparseMatrix* a, int n, int m, const char* fmt) const;
void prints(const float* f, unsigned l) const {
float* src = (float*) std::malloc(l * sizeof(float));
copy_to_host(f, src, l * sizeof(float));
for (unsigned i = 0; i < l; ++i) {
printf("%f ", src[i]);
}
printf("\n");
std::free(src);
}
void printsu(const int* f, unsigned l) const {
int* src = (int*) std::malloc(l * sizeof(int));
copy_to_host(f, src, l * sizeof(int));
for (unsigned i = 0; i < l; ++i) {
printf("%d ", src[i]);
}
printf("\n");
std::free(src);
}
#ifdef MEM_DEBUG
void print_memory_usage() {
printf("Memory usage: %zu bytes\n", allocated_memory);
}
void reset_memory_usage_counter() {
allocated_memory = 0;
}
#endif
};