-
Notifications
You must be signed in to change notification settings - Fork 12
/
curand_buffer.h
177 lines (161 loc) · 5.08 KB
/
curand_buffer.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
#ifndef _CURAND_BUFFER_H
#define _CURAND_BUFFER_H
#include <stdio.h>
#include <curand.h>
#include <cuda.h>
enum ProbDistr
{
RAND, // uniform distribution over [0,1)
RANDN // standard normal distribution with mean 0 and std 1
};
template <class randomNumber_t> // random number type
// only float and double are supported as template types
class CurandBuffer
/* This class generates a fixed sized buffer of random numbers on a cuda device,
* copies them to the host and whenever the operater[] is called from the host
* it returns the next random number. After all random numbers returned once,
* a new set of numbers is generated.
*/
{
private:
unsigned int buffer_size;
unsigned int current_idx;
bool memory_allocated;
randomNumber_t* host_data;
randomNumber_t* dev_data;
curandGenerator_t* generator;
ProbDistr distribution;
void generate_numbers()
{
if (current_idx != buffer_size && memory_allocated)
{
printf("WARNING: CurandBuffer::generate_numbers() called before "
"buffer was empty (current_idx = %u, buffer_size = %u)",
current_idx, buffer_size);
}
// TODO: should we allocate the memory in the constructor (even if we end up not using it)?
if (!memory_allocated)
{
// allocate host memory
host_data = new randomNumber_t[buffer_size];
if (!host_data)
{
printf("ERROR allocating host_data for CurandBuffer (size %ld)\n", sizeof(randomNumber_t)*buffer_size);
exit(EXIT_FAILURE);
}
// allocate device memory
cudaError_t status = cudaMalloc((void **)&dev_data, buffer_size*sizeof(randomNumber_t));
if (status != cudaSuccess)
{
printf("ERROR allocating memory on device (size = %ld) in %s(%d):\n\t%s\n",
buffer_size*sizeof(randomNumber_t), __FILE__, __LINE__,
cudaGetErrorString(status));
exit(EXIT_FAILURE);
}
memory_allocated = true;
}
// generate random numbers on device
if (distribution == RAND)
{
curandStatus_t status = generateUniform(*generator, dev_data, buffer_size);
if (status != CURAND_STATUS_SUCCESS)
{
printf("ERROR generating random numbers in %s(%d):\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
}
else // distribution == RANDN
{
curandStatus_t status = generateNormal(*generator, dev_data, buffer_size, 0, 1);
if (status != CURAND_STATUS_SUCCESS)
{
printf("ERROR generating normal distributed random numbers in %s(%d):\n",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
}
// copy random numbers to host
cudaError_t status = cudaMemcpy(host_data, dev_data, buffer_size*sizeof(randomNumber_t), cudaMemcpyDeviceToHost);
if (status != cudaSuccess)
{
printf("ERROR copying device to host memory (size = %ld) in %s(%d):\n\t%s\n",
buffer_size*sizeof(randomNumber_t), __FILE__, __LINE__,
cudaGetErrorString(status));
exit(EXIT_FAILURE);
}
// reset buffer index
current_idx = 0;
}
curandStatus_t generateUniform(curandGenerator_t generator, randomNumber_t *outputPtr, size_t num)
{
printf("ERROR curand can only generate random numbers as 'float' or 'double' types.\n");
exit(EXIT_FAILURE);
}
curandStatus_t generateNormal(curandGenerator_t generator, randomNumber_t *outputPtr,
size_t n, randomNumber_t mean, randomNumber_t stddev)
{
printf("ERROR curand can only generate random numbers as 'float' or 'double' types.\n");
exit(EXIT_FAILURE);
}
public:
CurandBuffer(curandGenerator_t* gen, ProbDistr distr)
{
generator = gen;
distribution = distr;
buffer_size = 10000;
current_idx = 0;
memory_allocated = false;
}
~CurandBuffer()
{
if (memory_allocated)
{
delete[] host_data;
cudaError_t status = cudaFree(dev_data);
if (status != cudaSuccess)
{
printf("ERROR freeing device memory in %s(%d):%s\n",
__FILE__, __LINE__, cudaGetErrorString(status));
exit(EXIT_FAILURE);
}
}
}
// don't return reference to prohibit assignment
randomNumber_t operator[](const int dummy)
{
// we ignore dummy and just return the next number in the buffer
if (current_idx == buffer_size || !memory_allocated)
generate_numbers();
randomNumber_t number = host_data[current_idx];
current_idx += 1;
return number;
}
}; // class CurandBuffer
// define generator functions depending on curand float type
// normal (RANDN)
template <> inline
curandStatus_t CurandBuffer<float>::generateNormal(curandGenerator_t generator,
float *outputPtr, size_t n, float mean, float stddev)
{
return curandGenerateNormal(generator, outputPtr, n, mean, stddev);
}
template <> inline
curandStatus_t CurandBuffer<double>::generateNormal(curandGenerator_t generator,
double *outputPtr, size_t n, double mean, double stddev)
{
return curandGenerateNormalDouble(generator, outputPtr, n, mean, stddev);
}
// uniform (RAND)
template <> inline
curandStatus_t CurandBuffer<float>::generateUniform(curandGenerator_t generator,
float *outputPtr, size_t num)
{
return curandGenerateUniform(generator, outputPtr, num);
}
template <> inline
curandStatus_t CurandBuffer<double>::generateUniform(curandGenerator_t generator,
double *outputPtr, size_t num)
{
return curandGenerateUniformDouble(generator, outputPtr, num);
}
#endif