/
cuda_helper.h
215 lines (181 loc) · 6.3 KB
/
cuda_helper.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
#ifndef CUDA_HELPER_H
#define CUDA_HELPER_H
#ifdef USE_GPU
#include <curand.h>
#include <cuda_runtime.h>
#include <cusparse_v2.h>
#include <cublas_v2.h>
__device__ inline int get_sp_row_idx(int i, int* row_ptr, int n_rows)
{
int l = 0, r = n_rows - 1, row;
while (l <= r)
{
row = (l + r) / 2;
if (row_ptr[row] <= i)
{
if (row_ptr[row + 1] > i)
break;
else
l = row + 1;
} else r = row - 1;
}
return row;
}
__device__ inline float cuda_rsqrt(const float& src)
{
return rsqrtf(src);
}
__device__ inline float cuda_rsqrt(const double& src)
{
return rsqrt(src);
}
__device__ inline float cuda_pow(const float& x, const float& y)
{
return powf(x, y);
}
__device__ inline double cuda_pow(const double& x, const double& y)
{
return pow(x, y);
}
__device__ inline float cuda_fabs(const float& src)
{
return fabsf(src);
}
__device__ inline double cuda_fabs(const double& src)
{
return fabs(src);
}
__device__ inline float cuda_exp(const float& src)
{
return expf(src);
}
__device__ inline double cuda_exp(const double& src)
{
return exp(src);
}
__device__ inline float cuda_log(const float& src)
{
return logf(src);
}
__device__ inline double cuda_log(const double& src)
{
return log(src);
}
inline float Cuda_Dot(cublasHandle_t& handle, int n, const float *x, const float* y)
{
float result;
cublasSdot(handle, n, x, 1, y, 1, &result);
return result;
}
inline double Cuda_Dot(cublasHandle_t& handle, int n, const double *x, const double* y)
{
double result;
cublasDdot(handle, n, x, 1, y, 1, &result);
return result;
}
inline float Cuda_Norm2(cublasHandle_t& handle, int n, const float *x)
{
float result;
cublasSnrm2(handle, n, x, 1, &result);
return result;
}
inline double Cuda_Norm2(cublasHandle_t& handle, int n, const double *x)
{
double result;
cublasDnrm2(handle, n, x, 1, &result);
return result;
}
inline void Cuda_Amax(cublasHandle_t& handle, int n, const float *x, int* result)
{
cublasIsamax(handle, n, x, 1, result);
}
inline void Cuda_Amax(cublasHandle_t& handle, int n, const double *x, int* result)
{
cublasIdamax(handle, n, x, 1, result);
}
inline float Cuda_Asum(cublasHandle_t& handle, int n, const float *x)
{
float result;
cublasSasum(handle, n, x, 1, &result);
return result;
}
inline double Cuda_Asum(cublasHandle_t& handle, int n, const double *x)
{
double result;
cublasDasum(handle, n, x, 1, &result);
return result;
}
inline void Cuda_Ger(cublasHandle_t& handle, int m, int n, const float* alpha, const float* x, const float* y, float* A)
{
cublasSger(handle, m, n, alpha, x, 1, y, 1, A, m);
}
inline void Cuda_Ger(cublasHandle_t& handle, int m, int n, const double* alpha, const double* x, const double* y, double* A)
{
cublasDger(handle, m, n, alpha, x, 1, y, 1, A, m);
}
inline void Cuda_GeMV(cublasHandle_t& handle, cublasOperation_t trans,
int m, int n,
const float* alpha, const float* A, int lda,
const float *x, int incx,
const float *beta, float* y, int incy)
{
cublasSgemv(handle, trans, m, n, alpha, A, lda, x, incx, beta, y, incy);
}
inline void Cuda_GeMV(cublasHandle_t& handle, cublasOperation_t trans,
int m, int n,
const double* alpha, const double* A, int lda,
const double *x, int incx,
const double *beta, double* y, int incy)
{
cublasDgemv(handle, trans, m, n, alpha, A, lda, x, incx, beta, y, incy);
}
inline void Cuda_GeaM(cublasHandle_t& handle, cublasOperation_t transa, cublasOperation_t transb, int m, int n,
const float* alpha, const float* A, int lda, const float* beta, const float* B, int ldb,
float* C, int ldc)
{
cublasSgeam(handle, transa, transb, m, n, alpha, A, lda, beta, B, ldb, C, ldc);
}
inline void Cuda_GeaM(cublasHandle_t& handle, cublasOperation_t transa, cublasOperation_t transb, int m, int n,
const double* alpha, const double* A, int lda, const double* beta, const double* B, int ldb,
double* C, int ldc)
{
cublasDgeam(handle, transa, transb, m, n, alpha, A, lda, beta, B, ldb, C, ldc);
}
inline void Cuda_CSRMM(cusparseHandle_t& handle, cusparseOperation_t transA, int m, int n, int k, int nnz,
const float *alpha, const float *csrValA, const int *csrRowPtrA, const int *csrColIndA,
const float *B, int ldb, const float *beta, float *C, int ldc)
{
cusparseMatDescr_t descrA;
cusparseCreateMatDescr(&descrA);
cusparseSetMatType(descrA, CUSPARSE_MATRIX_TYPE_GENERAL);
cusparseSetMatIndexBase(descrA, CUSPARSE_INDEX_BASE_ZERO);
cusparseScsrmm(handle, transA, m, n, k, nnz, alpha, descrA, csrValA, csrRowPtrA, csrColIndA, B, ldb, beta, C, ldc);
}
inline void Cuda_CSRMM(cusparseHandle_t& handle, cusparseOperation_t transA, int m, int n, int k, int nnz,
const double *alpha, const double *csrValA, const int *csrRowPtrA, const int *csrColIndA,
const double *B, int ldb, const double *beta, double *C, int ldc)
{
cusparseMatDescr_t descrA;
cusparseCreateMatDescr(&descrA);
cusparseSetMatType(descrA, CUSPARSE_MATRIX_TYPE_GENERAL);
cusparseSetMatIndexBase(descrA, CUSPARSE_INDEX_BASE_ZERO);
cusparseDcsrmm(handle, transA, m, n, k, nnz, alpha, descrA, csrValA, csrRowPtrA, csrColIndA, B, ldb, beta, C, ldc);
}
inline void Cuda_Axpy(cublasHandle_t& handle, int n, const float *alpha, const float *x, float *y)
{
cublasSaxpy(handle, n, alpha, x, 1, y, 1);
}
inline void Cuda_Axpy(cublasHandle_t& handle, int n, const double *alpha, const double *x, double *y)
{
cublasDaxpy(handle, n, alpha, x, 1, y, 1);
}
inline void Cuda_SetRandNormal(curandGenerator_t& generator, float* outputPtr, size_t n, float mean, float stddev)
{
curandGenerateNormal(generator, outputPtr, n, mean, stddev);
}
inline void Cuda_SetRandNormal(curandGenerator_t& generator, double* outputPtr, size_t n, double mean, double stddev)
{
curandGenerateNormalDouble(generator, outputPtr, n, mean, stddev);
}
#endif
#endif