-
Notifications
You must be signed in to change notification settings - Fork 5
/
random.cu
55 lines (47 loc) · 1.75 KB
/
random.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
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <curand_kernel.h>
extern "C"
{
__global__ void setup_kernel(curandState *state, int seed, int n, int verbose)
{
// Usual block/thread indexing...
int myblock = blockIdx.x + blockIdx.y * gridDim.x;
int blocksize = blockDim.x * blockDim.y * blockDim.z;
int subthread = threadIdx.z*(blockDim.x * blockDim.y) + threadIdx.y*blockDim.x + threadIdx.x;
int idx = myblock * blocksize + subthread;
if (verbose){
printf("Setting up RNG in thread %d (n=%d)...\n",idx,n);
}
curand_init(seed, idx, 0, &state[idx]);
return;
}
__global__ void rnorm_basic_kernel(curandState *state, double *vals, int n, double mu, double sigma)
{
// Usual block/thread indexing...
int myblock = blockIdx.x + blockIdx.y * gridDim.x;
int blocksize = blockDim.x * blockDim.y * blockDim.z;
int subthread = threadIdx.z*(blockDim.x * blockDim.y) + threadIdx.y*blockDim.x + threadIdx.x;
int idx = myblock * blocksize + subthread;
if (idx < n) {
vals[idx] = mu + sigma * curand_normal_double(&state[idx]);
}
return;
}
__global__ void rnorm_kernel(curandState *state, double *vals, int n, double mu, double sigma, int numSamples)
{
// Usual block/thread indexing...
int myblock = blockIdx.x + blockIdx.y * gridDim.x;
int blocksize = blockDim.x * blockDim.y * blockDim.z;
int subthread = threadIdx.z*(blockDim.x * blockDim.y) + threadIdx.y*blockDim.x + threadIdx.x;
int idx = myblock * blocksize + subthread;
int k;
int startIdx = idx*numSamples;
for(k = 0; k < numSamples; k++) {
if(startIdx + k < n)
vals[startIdx + k] = mu + sigma * curand_normal_double(&state[idx]);
}
return;
}
} // END extern