forked from bioinf-jku/librfn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_operations.cu
More file actions
414 lines (335 loc) · 13.4 KB
/
Copy pathgpu_operations.cu
File metadata and controls
414 lines (335 loc) · 13.4 KB
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
/*
Copyright © 2015 Thomas Unterthiner
Licensed under GPL, version 2 or a later (see LICENSE.txt)
*/
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <curand_kernel.h>
#include <stdexcept>
#include "gpu_operations.h"
static const int RNG_THREADS = 128;
static const int RNG_BLOCKS = 128;
/*
cublasHandle_t GPU_Operations::handle;
float* GPU_Operations::ones = 0;
curandState* GPU_Operations::rng_state = 0;
cudaStream_t* GPU_Operations::streams = 0;
*/
// taken from PyCUDA
void get_grid_sizes(int problemsize, int* blocks, int* threads) {
int min_threads = 32;
int max_threads = 256;
int max_blocks = 384;
if (problemsize < min_threads) {
*blocks = 1;
*threads = min_threads;
} else if (problemsize < max_blocks * min_threads) {
*blocks = (problemsize + min_threads - 1) / min_threads;
*threads = min_threads;
} else if (problemsize < max_blocks * max_threads) {
*blocks = max_blocks;
int grp = (problemsize + min_threads - 1) / min_threads;
*threads = ((grp + max_blocks - 1) / max_blocks) * min_threads;
} else {
*blocks = max_blocks;
*threads = max_threads;
}
}
__global__ void setup_rng(curandState* rng_state, unsigned long seed)
{
const int tid = blockIdx.x*blockDim.x+threadIdx.x;
curand_init(seed, tid, 0, &rng_state[tid]);
}
__global__ void dropout_eltw(float* x, const unsigned size,
const float dropout_rate,
curandState* rng_state) {
const unsigned tid = blockIdx.x*blockDim.x+threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
curandState localState = rng_state[tid];
for (unsigned i = tid; i < size; i += num_threads)
x[i] = (curand_uniform(&localState) < dropout_rate) ? 0.0 : x[i];
rng_state[tid] = localState;
}
__global__ void saltpepper_noise_eltw(float* x, const unsigned size,
const float noise_rate,
curandState* rng_state) {
const unsigned tid = blockIdx.x*blockDim.x+threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
curandState localState = rng_state[tid];
for (unsigned i = tid; i < size; i += num_threads)
if (curand_uniform(&localState) < noise_rate) {
x[i] = (curand_uniform(&localState) < 0.5f) ? 0.0f : 1.0f;
}
rng_state[tid] = localState;
}
__global__ void gauss_noise_eltw(float* x, const unsigned size,
const float noise_rate,
curandState* rng_state) {
const unsigned tid = blockIdx.x*blockDim.x+threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
curandState localState = rng_state[tid];
for (unsigned i = tid; i < size; i += num_threads)
x[i] += curand_normal(&localState) * noise_rate ;
rng_state[tid] = localState;
}
__global__ void leaky_relu_eltw(float* x, const float value, const unsigned size) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
for (unsigned i = tid; i < size; i += num_threads) {
x[i] = (x[i] < 0.0f) ? x[i] * value : x[i];
}
}
__global__ void maximum_eltw(float* x, const float value, const unsigned size) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
for (unsigned i = tid; i < size; i += num_threads) {
x[i] = fmaxf(x[i], value);
}
}
__global__ void sigmoid_eltw(float* x, const unsigned size) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
for (unsigned i = tid; i < size; i += num_threads) {
x[i] = 1 / (1 + __expf(-x[i]));
}
}
__global__ void tanh_eltw(float* x, const unsigned size) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
for (unsigned i = tid; i < size; i += num_threads) {
x[i] = tanhf(x[i]);
}
}
__global__ void softthreshold_eltw(float* x, float alpha, const unsigned size) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
for (unsigned i = tid; i < size; i += num_threads) {
const float f = x[i];
x[i] = f > 0 ? fmaxf(0., f - alpha) : fminf(0., f + alpha);
}
}
__global__ void fill_eltw(float* x, const unsigned size, const float value) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
for (unsigned i = tid; i < size; i += num_threads) {
x[i] = value;
}
}
__global__ void invert_eltw(float* x, const unsigned size) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = gridDim.x*blockDim.x;
for (unsigned i = tid; i < size; i += num_threads) {
x[i] = 1.0f / x[i];
}
}
__global__ void col_variance_kernel(const float* X, float* var,
const unsigned nrows,
const unsigned ncols) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = blockDim.x * gridDim.x;
for (unsigned i = tid; i < ncols; i += num_threads) {
var[i] = 0.0;
for (unsigned j = 0; j < nrows; ++j) {
var[i] += X[j*ncols + i];
}
float m = var[i] / nrows;
var[i] = 0.0;
for (unsigned j = 0; j < nrows; ++j) {
float tmp = X[j*ncols + i] - m;
var[i] += tmp*tmp;
}
var[i] /= nrows;
}
}
__global__ void invsqrt_eltw(float* x, const unsigned k) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = blockDim.x * gridDim.x;
for (unsigned i = tid; i < k; i += num_threads) {
x[i] = (x[i] > 1e-7) ? rsqrtf(x[i]) : 1.0;
}
}
__global__ void scale_columns_kernel(float* X, float* a, const unsigned nrows, const unsigned ncols) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = blockDim.x * gridDim.x;
for (unsigned i = tid; i < ncols * nrows; i += num_threads) {
X[i] *= a[i % ncols];
}
}
__global__ void scale_rows_kernel(float* X, float* a, const unsigned nrows, const unsigned ncols) {
const unsigned tid = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned num_threads = blockDim.x * gridDim.x;
for (unsigned i = tid; i < ncols * nrows; i += num_threads) {
X[i] *= a[i / ncols];
}
}
GPU_Operations::GPU_Operations(const int n, const int m, const int k,
unsigned long seed, int gpu_id) {
// if no GPU was specified, try to pick the best one automatically
if (gpu_id < 0) {
gpu_id = 0;
int num_devices, device;
cudaGetDeviceCount(&num_devices);
if (num_devices > 1) {
size_t max_freememory = 0;
for (device = 0; device < num_devices; device++) {
size_t free, total;
cudaSetDevice(device);
cudaMemGetInfo(&free, &total);
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, device);
//printf("Found device %d (%s) with %d MiB of free memory\n",
// device, prop.name, free / (1024l*1024l));
if (free > max_freememory) {
max_freememory = free;
gpu_id = device;
}
cudaDeviceReset();
}
}
}
assert(gpu_id >= 0);
cudaSetDevice(gpu_id);
// the following call does not work if the current process has already
// called into librfn previously. Then, this call will return
// cudaErrorSetOnActiveProcess. Resetting the device won't work either,
// because then the subsequent cublasCreate call will just fail with
// CUBLAS_STATUS_NOT_INITIALIZED. I don't know why any of this is happening
//CUDA_CALL(cudaSetDeviceFlags(cudaDeviceScheduleYield));
cublasStatus_t status = cublasCreate(&handle);
if (status != CUBLAS_STATUS_SUCCESS) {
const char* errmsg = cublasErrorString(status);
fprintf(stderr, "CUBLAS initialization error: %s\n", errmsg);
cudaDeviceReset();
throw std::runtime_error(errmsg);
}
CUSOLVER_CALL(cusolverDnCreate(&cudense_handle));
CUDA_CALL(cudaMalloc(&rng_state, RNG_BLOCKS*RNG_THREADS*sizeof(curandState)));
setup_rng<<<RNG_BLOCKS, RNG_THREADS>>>(rng_state, seed);
int ones_size = n > k ? n : k;
ones = malloc(ones_size*sizeof(float));
fill(ones, ones_size, 1.0f);
CUDA_CALL(cudaMalloc(&devinfo, sizeof(int)));
}
GPU_Operations::~GPU_Operations() {
free(devinfo);
free(ones);
for (auto i : buffer_map) {
free(i.second);
}
CUSOLVER_CALL(cusolverDnDestroy(cudense_handle));
CUBLAS_CALL(cublasDestroy(handle));
}
float* GPU_Operations::to_device(const float* src, size_t size) const {
float* dst = 0;
CUDA_CALL(cudaMalloc(&dst, size));
CUDA_CALL(cudaMemcpy(dst, src, size, cudaMemcpyHostToDevice));
return dst;
}
void GPU_Operations::fill(float* X, const unsigned size, const float value) const {
int threads, blocks;
get_grid_sizes(size, &threads, &blocks);
fill_eltw<<<blocks, threads>>>(X, size, value);
assert(!cudaGetLastError());
}
void GPU_Operations::dropout(float* X, const unsigned size,
const float dropout_rate) const {
dropout_eltw<<<RNG_BLOCKS, RNG_THREADS>>>(X, size, dropout_rate, rng_state);
assert(!cudaGetLastError());
}
void GPU_Operations::add_gauss_noise(float* X, const unsigned size,
const float noise_rate) const {
gauss_noise_eltw<<<RNG_BLOCKS, RNG_THREADS>>>(X, size, noise_rate, rng_state);
assert(!cudaGetLastError());
}
void GPU_Operations::add_saltpepper_noise(float* X, const unsigned size,
const float noise_rate) const {
saltpepper_noise_eltw<<<RNG_BLOCKS, RNG_THREADS>>>(X, size, noise_rate, rng_state);
assert(!cudaGetLastError());
}
void GPU_Operations::invert(float* X, const unsigned size) const {
int threads, blocks;
get_grid_sizes(size, &threads, &blocks);
invert_eltw<<<blocks, threads>>>(X, size);
assert(!cudaGetLastError());
}
void GPU_Operations::maximum(float* x, const float value, const unsigned size) const {
int threads, blocks;
get_grid_sizes(size, &threads, &blocks);
maximum_eltw<<<blocks, threads>>>(x, value, size);
assert(!cudaGetLastError());
}
void GPU_Operations::leaky_relu(float* x, const float value, const unsigned size) const {
int threads, blocks;
get_grid_sizes(size, &threads, &blocks);
leaky_relu_eltw<<<blocks, threads>>>(x, value, size);
assert(!cudaGetLastError());
}
void GPU_Operations::sigmoid(float* x, const unsigned size) const {
int threads, blocks;
get_grid_sizes(size, &threads, &blocks);
sigmoid_eltw<<<blocks, threads>>>(x, size);
assert(!cudaGetLastError());
}
void GPU_Operations::tanh(float* x, const unsigned size) const {
int threads, blocks;
get_grid_sizes(size, &threads, &blocks);
tanh_eltw<<<blocks, threads>>>(x, size);
assert(!cudaGetLastError());
}
void GPU_Operations::soft_threshold(float* x, const float alpha, const unsigned size) const {
int threads, blocks;
get_grid_sizes(size, &threads, &blocks);
softthreshold_eltw<<<blocks, threads>>>(x, alpha, size);
assert(!cudaGetLastError());
}
void GPU_Operations::fill_eye(float* X, unsigned n) const {
memset(X, 0, n*n*sizeof(float));
axpy(n, 1.0f, ones, 0, X, n+1);
}
void GPU_Operations::calculate_column_variance(const float* X, const unsigned nrows,
const unsigned ncols, float* variance) {
int threads, blocks;
get_grid_sizes(ncols, &threads, &blocks);
col_variance_kernel<<<threads, blocks>>>(X, variance, nrows, ncols);
}
void GPU_Operations::invsqrt(float* s, const unsigned n) const {
int t, b;
get_grid_sizes(n, &t, &b);
invsqrt_eltw<<<t, b>>>(s, n);
}
void GPU_Operations::scale_columns(float* X, const unsigned nrows, const unsigned ncols, float* s) const {
int threads, blocks;
get_grid_sizes(ncols*nrows, &threads, &blocks);
scale_columns_kernel<<<threads, blocks>>>(X, s, nrows, ncols);
}
void GPU_Operations::scale_rows(float* X, const unsigned nrows, const unsigned ncols, float* s) const {
int threads, blocks;
get_grid_sizes(ncols*nrows, &threads, &blocks);
scale_rows_kernel<<<threads, blocks>>>(X, s, nrows, ncols);
}
void GPU_Operations::printMatrixRM(const float* a, int n, int m, const char* fmt) {
const char* format = fmt == 0 ? "%1.3f " : fmt;
size_t size = n*m*sizeof(float);
float* tmp = (float*) std::malloc(size);
CUDA_CALL(cudaMemcpy(tmp, a, size, cudaMemcpyDeviceToHost));
for (int i = 0; i < n; ++i) {
for (int j =0 ; j < m; ++j)
printf(format, tmp[i*m + j]);
printf("\n");
}
printf("\n");
std::free(tmp);
}
void GPU_Operations::printMatrixCM(const float* a, int n, int m, const char* fmt) {
const char* format = fmt == 0 ? "%1.3f " : fmt;
size_t size = n*m*sizeof(float);
float* tmp = (float*) std::malloc(size);
CUDA_CALL(cudaMemcpy(tmp, a, size, cudaMemcpyDeviceToHost));
for (int i = 0; i < n; ++i) {
for (int j =0 ; j < m; ++j)
printf(format, tmp[i + j*n]);
printf("\n");
}
printf("\n");
std::free(tmp);
}