-
Notifications
You must be signed in to change notification settings - Fork 0
/
MersenneTwister.cu
335 lines (240 loc) · 9.6 KB
/
MersenneTwister.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
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
/*
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property and
* proprietary rights in and to this software and related documentation.
* Any use, reproduction, disclosure, or distribution of this software
* and related documentation without an express license agreement from
* NVIDIA Corporation is strictly prohibited.
*
* Please refer to the applicable NVIDIA end user license agreement (EULA)
* associated with this source code for terms and conditions that govern
* your use of this NVIDIA software.
*
*/
/*
* This sample implements Mersenne Twister random number generator
* and Cartesian Box-Muller transformation on the GPU.
* See supplied whitepaper for more explanations.
*/
#include "MersenneTwister.h"
///////////////////////////////////////////////////////////////////////////////
// Common host and device function
///////////////////////////////////////////////////////////////////////////////
//ceil(a / b)
extern "C" int iDivUp(int a, int b){
return ((a % b) != 0) ? (a / b + 1) : (a / b);
}
//floor(a / b)
extern "C" int iDivDown(int a, int b){
return a / b;
}
//Align a to nearest higher multiple of b
extern "C" int iAlignUp(int a, int b){
return ((a % b) != 0) ? (a - a % b + b) : a;
}
//Align a to nearest lower multiple of b
extern "C" int iAlignDown(int a, int b){
return a - a % b;
}
///////////////////////////////////////////////////////////////////////////////
// Data configuration
///////////////////////////////////////////////////////////////////////////////
static int PATH_N_GAUSS;
static int PATH_N_UNIF;
static int N_PER_RNG_GAUSS;
static int N_PER_RNG_UNIF;
static int RAND_N_GAUSS;
static int RAND_N_UNIF;
__device__ mt_struct_stripped ds_MT[MT_RNG_COUNT];
static mt_struct_stripped h_MT[MT_RNG_COUNT];
__device__ unsigned int d_mtstatus[MT_RNG_COUNT][MT_NN];
// fields for the MT random number generator
//__device__ float * dev_rndunif_field;
//__device__ float * dev_rndgauss_field;
//Load twister configurations
void loadMTGPU(const char *fname){
FILE *fd = fopen(fname, "rb");
if(!fd){
printf("initMTGPU(): failed to open %s\n", fname);
printf("FAILED\n");
exit(0);
}
if( !fread(h_MT, sizeof(h_MT), 1, fd) ){
printf("initMTGPU(): failed to load %s\n", fname);
printf("FAILED\n");
exit(0);
}
fclose(fd);
}
//Initialize/seed twister for current GPU context
void seedMTGPU(){
int i;
//Need to be thread-safe
mt_struct_stripped *MT = (mt_struct_stripped *)malloc(MT_RNG_COUNT * sizeof(mt_struct_stripped));
/* initialize poor rng: */
srand ( time(NULL) );
/* initialize MT rng seeds */
for(i = 0; i < MT_RNG_COUNT; i++){
MT[i] = h_MT[i];
MT[i].seed = (unsigned int) rand();
}
CUDA_SAFE_CALL( cudaMemcpyToSymbol(ds_MT, MT, sizeof(h_MT)) );
free(MT);
}
//Save twister for current GPU context
void saveMTGPU(const char *fname){
FILE *fd = fopen(fname, "w");
if(!fd){
printf("saveMTGPU(): failed to open %s\n", fname);
printf("FAILED\n");
exit(0);
}
fwrite(h_MT, sizeof(h_MT), 1, fd);
fclose(fd);
}
////////////////////////////////////////////////////////////////////////////////
// Write MT_RNG_COUNT vertical lanes of NPerRng random numbers to *d_Random.
// For coalesced global writes MT_RNG_COUNT should be a multiple of warp size.
// Initial states for each generator are the same, since the states are
// initialized from the global seed. In order to improve distribution properties
// on small NPerRng supply dedicated (local) seed to each twister.
// The local seeds, in their turn, can be extracted from global seed
// by means of any simple random number generator, like LCG.
////////////////////////////////////////////////////////////////////////////////
__global__ void RandomGPU(
float *d_Random,
int NPerRng, int initialized
){
const int tid = blockDim.x * blockIdx.x + threadIdx.x;
const int THREAD_N = blockDim.x * gridDim.x;
int iState, iState1, iStateM, iOut;
unsigned int mti, mti1, mtiM, x;
unsigned int mt[MT_NN];
for(int iRng = tid; iRng < MT_RNG_COUNT; iRng += THREAD_N){
//Load bit-vector Mersenne Twister parameters
mt_struct_stripped config = ds_MT[iRng];
if(!initialized){
// initialize seed and construct status mt must be initialized from host before
mt[0] = ds_MT[iRng].seed;
for(iState = 1; iState < MT_NN; iState++)
mt[iState] = (1812433253U * (mt[iState - 1] ^ (mt[iState - 1] >> 30)) + iState) & MT_WMASK;
}
else{
for(iState = 0; iState < MT_NN; iState++) mt[iState] = d_mtstatus[iRng][iState];
}
iState = 0;
mti1 = mt[0];
for(iOut = 0; iOut < NPerRng; iOut++){
//iState1 = (iState + 1) % MT_NN
//iStateM = (iState + MT_MM) % MT_NN
iState1 = iState + 1;
iStateM = iState + MT_MM;
if(iState1 >= MT_NN) iState1 -= MT_NN;
if(iStateM >= MT_NN) iStateM -= MT_NN;
mti = mti1;
mti1 = mt[iState1];
mtiM = mt[iStateM];
x = (mti & MT_UMASK) | (mti1 & MT_LMASK);
x = mtiM ^ (x >> 1) ^ ((x & 1) ? config.matrix_a : 0);
mt[iState] = x;
iState = iState1;
//Tempering transformation
x ^= (x >> MT_SHIFT0);
x ^= (x << MT_SHIFTB) & config.mask_b;
x ^= (x << MT_SHIFTC) & config.mask_c;
x ^= (x >> MT_SHIFT1);
//Convert to (0, 1] float and write to global memory
d_Random[iRng + iOut * MT_RNG_COUNT] = ((float)x + 1.0f) / 4294967296.0f;
}
// save status of mt
ds_MT[iRng].seed = mt[0];
for(iState = 0; iState < MT_NN; iState++) d_mtstatus[iRng][iState] = mt[iState];
}
}
////////////////////////////////////////////////////////////////////////////////
// Transform each of MT_RNG_COUNT lanes of NPerRng uniformly distributed
// random samples, produced by RandomGPU(), to normally distributed lanes
// using Cartesian form of Box-Muller transformation.
// NPerRng must be even.
////////////////////////////////////////////////////////////////////////////////
#define PIf 3.14159265358979f
__device__ inline void BoxMuller(float& u1, float& u2){
float r = sqrtf(-2.0f * logf(u1));
float phi = 2 * PIf * u2;
u1 = r * __cosf(phi);
u2 = r * __sinf(phi);
}
__global__ void BoxMullerGPU(float *d_Random, int NPerRng){
const int tid = blockDim.x * blockIdx.x + threadIdx.x;
const int THREAD_N = blockDim.x * gridDim.x;
for(int iRng = tid; iRng < MT_RNG_COUNT; iRng += THREAD_N)
for(int iOut = 0; iOut < NPerRng; iOut += 2)
BoxMuller(
d_Random[iRng + (iOut + 0) * MT_RNG_COUNT],
d_Random[iRng + (iOut + 1) * MT_RNG_COUNT]
);
}
extern "C" void init_MT(int n_gaussnumbers, int n_unifnumbers){
cudaError_t cudaerr;
//determine sizes for gauss numbers
printf("Initializing MT random number generator...\n");
PATH_N_GAUSS = n_gaussnumbers;
N_PER_RNG_GAUSS = iAlignUp(iDivUp(PATH_N_GAUSS, MT_RNG_COUNT), 2);
RAND_N_GAUSS = MT_RNG_COUNT * N_PER_RNG_GAUSS;
printf("No. of gauss random numbers: %d\n", RAND_N_GAUSS );
//determine sizes for unif. numbers
PATH_N_UNIF = n_unifnumbers;
N_PER_RNG_UNIF = iAlignUp(iDivUp(PATH_N_UNIF, MT_RNG_COUNT), 2);
RAND_N_UNIF = MT_RNG_COUNT * N_PER_RNG_UNIF;
printf("No. of unif. dist. random numbers: %d\n", RAND_N_UNIF );
// load and initialize twister configurations on device
// seed the twisters
const char *dat_path = "MersenneTwister.dat";
printf("Loading GPU twisters configurations from file %s...\n", dat_path);
loadMTGPU(dat_path);
seedMTGPU();
//allocate fields for random numbers
printf("Allocating device memory for random numbers...\n");
CUDA_SAFE_CALL(cudaMalloc((void **)&dev_rndgauss_field, RAND_N_GAUSS * sizeof(float)) );
CUDA_SAFE_CALL(cudaMalloc((void **)&dev_rndunif_field, RAND_N_UNIF * sizeof(float)));
// CREATE FIRST RANDOM NUMBERS
/* update the random field for gauss numbers -> BoxMuller afterwards*/
cudaThreadSynchronize();
RandomGPU<<<32, 128>>>(dev_rndgauss_field, N_PER_RNG_GAUSS,0);
BoxMullerGPU<<<32, 128>>>(dev_rndgauss_field, N_PER_RNG_GAUSS);
/* update the random field for unif. dist. numbers*/
cudaThreadSynchronize();
RandomGPU<<<32, 128>>>(dev_rndunif_field, N_PER_RNG_UNIF,0);
cudaThreadSynchronize();
cudaerr = cudaGetLastError();
if(cudaerr != cudaSuccess){
printf("%s\n", cudaGetErrorString(cudaerr));
}
}
extern "C" void update_MT(){
/* update the random field for gauss numbers -> BoxMuller afterwards*/
cudaThreadSynchronize();
RandomGPU<<<32, 128>>>(dev_rndgauss_field, N_PER_RNG_GAUSS,1);
BoxMullerGPU<<<32, 128>>>(dev_rndgauss_field, N_PER_RNG_GAUSS);
cudaThreadSynchronize();
/* update the random field for unif. dist. numbers*/
RandomGPU<<<32, 128>>>(dev_rndunif_field, N_PER_RNG_UNIF, 1);
cudaThreadSynchronize();
/*
float * blub = (float*) malloc(RAND_N_UNIF*sizeof(float));
printf("%d \n", RAND_N_UNIF);
CUDA_SAFE_CALL(cudaMemcpy(blub, dev_rndunif_field, (size_t)(RAND_N_UNIF*sizeof(float)), cudaMemcpyDeviceToHost));
for(int k=0; k<4; k++){
for(int j=VOLUME/2-10; j<VOLUME/2; j++){
printf("%f, ", blub[4*j+k]);
}
}
printf("\n\n");
free(blub);
*/
}
extern "C" void finalize_MT(){
cudaFree(dev_rndgauss_field);
cudaFree(dev_rndunif_field);
}