-
Notifications
You must be signed in to change notification settings - Fork 3
/
gibbs_metropolis.cu
executable file
·432 lines (324 loc) · 11.7 KB
/
gibbs_metropolis.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
Zebulun Arendsee
March 26, 2013
This program implements a MCMC algorithm for the following hierarchical
model:
y_k ~ Poisson(n_k * theta_k)
theta_k ~ Gamma(a, b)
a ~ Unif(0, a0)
b ~ Unif(0, b0)
I let a0 and b0 be arbitrarily large.
Arguments:
1) input file name
With two space delimited columns holding integer values for
y and float values for n.
2) number of trials (1000 by default)
Output: A comma delimited file containing a column for a, b, and each
theta. All output is written to standard out.
Example:
$ head -3 mydata.txt
4 0.91643
23 3.23709
7 0.40103
$ a.out mydata.txt 2500 > output.csv
This code borrows from the nVidia developer zone documentation,
specifically http://docs.nvidia.com/cuda/curand/index.html#topic_1_2_1
*/
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <math.h>
#include <curand_kernel.h>
#define CUDA_CALL(x) do { if((x) != cudaSuccess) { \
printf("Error at %s:%d\n",__FILE__,__LINE__); \
return EXIT_FAILURE;}} while(0)
#define CURAND_CALL(x) do { if((x) != CURAND_STATUS_SUCCESS) { \
printf("Error at %s:%d\n",__FILE__,__LINE__); \
return EXIT_FAILURE;}} while(0)
#define PI 3.14159265359f
#define THREADS_PER_BLOCK 64
#define THREADS_PER_BLOCK_ADD 64
__global__ void setup_kernel(curandState *state, unsigned int seed);
__global__ void sample_theta(curandState *state, float *theta,
int *y, float *n, float a, float b, int N);
__global__ void sum_blocks(float *theta, float *flat_sums,
float *log_sums, int N);
__device__ float rgamma(curandState *state, int id, float a, float b);
__host__ float rnorm();
__host__ float rgamma(float a, float b);
__host__ float sample_a(float a, float b, int N, float log_sum);
__host__ float sample_a(float a, float b, int N, float log_sum);
__host__ float sample_b(float a, int N, float flat_sum);
__host__ void printHeader(int N);
int main(int argc, char *argv[])
{
curandState *devStates;
float *dev_theta;
float *dev_fpsum, *dev_lpsum;
int *dev_y;
float *dev_n;
int trials = 1000; // Default number of trials
if(argc > 2)
trials = atoi(argv[2]);
/*------ Loading Data ------------------------------------------*/
FILE *fp;
if(argc > 1){
fp = fopen(argv[1], "r");
} else {
printf("Please provide input filename\n");
return 1;
}
if(fp == NULL){
printf("Cannot read file \n");
return 1;
}
int N = 0;
char line[128];
while( fgets (line, sizeof line, fp) != NULL ) {
N++;
}
rewind(fp);
int y[N];
float n[N];
for(int i = 0; i < N; i++){
fscanf(fp, "%d %f", &y[i], &n[i]);
}
fclose(fp);
/*------ Memory Allocations ------------------------------------*/
CUDA_CALL(cudaMalloc((void **)&dev_y, N * sizeof(int)));
CUDA_CALL(cudaMemcpy(dev_y, y, N * sizeof(int),
cudaMemcpyHostToDevice));
CUDA_CALL(cudaMalloc((void **)&dev_n, N * sizeof(float)));
CUDA_CALL(cudaMemcpy(dev_n, n, N * sizeof(float),
cudaMemcpyHostToDevice));
// Allocate memory for the partial flat and log sums
int nSumBlocks = (N + (THREADS_PER_BLOCK_ADD - 1)) / THREADS_PER_BLOCK_ADD;
CUDA_CALL(cudaMalloc((void **)&dev_fpsum, nSumBlocks * sizeof(float)));
CUDA_CALL(cudaMalloc((void **)&dev_lpsum, nSumBlocks * sizeof(float)));
float host_fpsum[N];
float host_lpsum[N];
/* Allocate space for theta on device and host */
CUDA_CALL(cudaMalloc((void **)&dev_theta, N * sizeof(float)));
float host_theta[N];
/* Allocate space for random states on device */
CUDA_CALL(cudaMalloc((void **)&devStates, N * sizeof(curandState)));
/*------ Setup RNG ---------------------------------------------*/
int nBlocks = (N + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK;
// Setup the rng machines (one for each thread)
setup_kernel<<<nBlocks, THREADS_PER_BLOCK>>>(devStates, time(NULL));
/*------ MCMC Algorithm ----------------------------------------*/
// I commented out the printing functions. Writing the output to
// the harddrive requires much more time than generating the actual
// samples. The data are transfered back to the CPU's memory, but
// currently only the most last result is stored (to avoid memory
// issues).
// printHeader(N);
float a = 20; // Set starting value
float b = 1; // Set starting value
for(int i = 0; i < trials; i++){
sample_theta<<<nBlocks, THREADS_PER_BLOCK>>>(devStates, dev_theta,
dev_y, dev_n, a, b, N);
// Copy device memory to host
CUDA_CALL(cudaMemcpy(host_theta, dev_theta, N * sizeof(float),
cudaMemcpyDeviceToHost));
/*
// Printing the output is by far the most time consuming step
printf("%f, %f", a, b);
for(int j = 0; j < N; j++){
printf(", %f", host_theta[j]);
}
printf("\n");
*/
// Sum the flat and log values of theta
sum_blocks<<<nSumBlocks, THREADS_PER_BLOCK_ADD>>>(dev_theta, dev_fpsum,
dev_lpsum, N);
CUDA_CALL(cudaMemcpy(host_fpsum, dev_fpsum, nSumBlocks * sizeof(float),
cudaMemcpyDeviceToHost));
CUDA_CALL(cudaMemcpy(host_lpsum, dev_lpsum, nSumBlocks * sizeof(float),
cudaMemcpyDeviceToHost));
// The GPU summed blocks of theta values, now the CPU sums the blocks
float flat_sum = 0;
float log_sum = 0;
for(int j = 0; j < nSumBlocks; j++){
flat_sum += host_fpsum[j];
log_sum += host_lpsum[j];
}
// Sample one random value from a's distribution
a = sample_a(a, b, N, log_sum);
// And then from b's distribution given the new a
b = sample_b(a, N, flat_sum);
}
/*------ Free Memory -------------------------------------------*/
CUDA_CALL(cudaFree(devStates));
CUDA_CALL(cudaFree(dev_theta));
CUDA_CALL(cudaFree(dev_fpsum));
CUDA_CALL(cudaFree(dev_lpsum));
CUDA_CALL(cudaFree(dev_y));
CUDA_CALL(cudaFree(dev_n));
return EXIT_SUCCESS;
}
/*
Initializes GPU random number generators
*/
__global__ void setup_kernel(curandState *state, unsigned int seed)
{
int id = threadIdx.x + blockIdx.x * blockDim.x;
/* Each thread gets same seed, a different sequence
number, no offset */
curand_init(seed, id, 0, &state[id]);
}
/*
Sample each theta from the appropriate gamma distribution
*/
__global__ void sample_theta(curandState *state,
float *theta, int *y, float *n,
float a, float b, int N)
{
int id = threadIdx.x + blockIdx.x * blockDim.x;
if(id < N){
float hyperA = a + y[id];
float hyperB = b + n[id];
theta[id] = rgamma(state, id, hyperA, hyperB);
}
}
/*
Sampling of a and b require the sum and product of all theta
values. This function performs parallel summations of
flat values and logs of theta for many blocks of length
THREADS_PER_BLOCK_ADD. The CPU will then sum the block
sums (see main method);
*/
__global__ void sum_blocks(float *theta, float *flat_sums,
float *log_sums, int N)
{
int id = threadIdx.x + blockIdx.x * blockDim.x;
__shared__ float flats[THREADS_PER_BLOCK_ADD];
__shared__ float logs[THREADS_PER_BLOCK_ADD];
flats[threadIdx.x] = (id < N) ? theta[id] : 0;
logs[threadIdx.x] = (id < N) ? log( theta[id] ) : 0;
__syncthreads();
int i = blockDim.x / 2;
while(i != 0){
if(threadIdx.x < i){
flats[threadIdx.x] += flats[threadIdx.x + i];
logs[threadIdx.x] += logs[threadIdx.x + i];
}
i /= 2;
}
if(threadIdx.x == 0){
flat_sums[blockIdx.x] = flats[0];
log_sums[blockIdx.x] = logs[0];
}
}
/*
Generate a single Gamma distributed random variable by the Marsoglia
algorithm (George Marsaglia, Wai Wan Tsang; 2001).
I chose this algorithm because it has a very high acceptance rate (>96%),
so this while loop will usually only need to run a few times. Many other
algorithms, while perhaps faster on a CPU, have acceptance rates on the
order of 50% (very bad in a massively parallel context).
*/
__device__ float rgamma(curandState *state, int id, float a, float b)
{
float d = a - 1.0 / 3;
float Y, U, v;
while(true){
// Generate a standard normal random variable
Y = curand_normal(&state[id]);
v = pow((1 + Y / sqrt(9 * d)), 3);
// Necessary to avoid taking the log of a negative number later
if(v <= 0)
continue;
// Generate a standard uniform random variable
U = curand_uniform(&state[id]);
// Accept proposed Gamma random variable under following condition,
// otherise repeat the loop
if(log(U) < 0.5 * pow(Y,2) + d * (1 - v + log(v)) ){
return d * v / b;
}
}
}
/*
Box-Muller Transformation: Generate one standard normal variable.
This algorithm can be more efficiently used by producing two
random normal variables. However, for the CPU, much faster
algorithms are possible (e.g. the Ziggurat Algorithm);
This is actually the algorithm chosen by nVidia to calculate
normal random variables on the GPU.
*/
__host__ float rnorm()
{
float U1 = rand() / float(RAND_MAX);
float U2 = rand() / float(RAND_MAX);
float V1 = sqrt(-2 * log(U1)) * cos(2 * PI * U2);
// float V2 = sqrt(-2 * log(U2)) * cos(2 * PI * U1);
return V1;
}
/*
See device rgamma function. This is probably not the
fastest way to generate random gamma variables on a CPU.
*/
__host__ float rgamma(float a, float b)
{
float d = a - 1.0 / 3;
float Y, U, v;
while(true){
/* Generate a standard normal random variable */
Y = rnorm();
v = pow((1 + Y / sqrt(9 * d)), 3);
/* If v is negative continue, this is necessary to avoid taking the log
of a negative number in the next step */
if(v <= 0)
continue;
/* Generate a standard uniform random variable */
U = rand() / float(RAND_MAX);
/* Accept proposed Gamma random variable under following condition,
otherise repeat the loop */
if(log(U) < 0.5 * pow(Y,2) + d * (1 - v + log(v)) ){
return d * v / b;
}
}
}
/*
Metropolis algorithm for producing random a values.
The proposal distribution in normal with a variance that
is adjusted at each step.
*/
__host__ float sample_a(float a, float b, int N, float log_sum)
{
static float sigma = 2;
float proposal = rnorm() * sigma + a;
if(proposal <= 0) return a;
float log_acceptance_ratio = (proposal - a) * log_sum +
N * (proposal - a) * log(b) -
N * (lgamma(proposal) - lgamma(a));
float U = rand() / float(RAND_MAX);
if(log(U) < log_acceptance_ratio){
sigma *= 1.1;
return proposal;
} else {
sigma /= 1.1;
return a;
}
}
/*
Returns a random b sample (simply a draw from the appropriate
gamma distribution)
*/
__host__ float sample_b(float a, int N, float flat_sum)
{
float hyperA = N * a + 1;
float hyperB = flat_sum;
return rgamma(hyperA, hyperB);
}
/*
prints: "alpha, beta, theta1, theta2, ... "
*/
__host__ void printHeader(int N)
{
printf("alpha, beta");
for(int i = 0; i < N; i++){
printf(", theta%d", i + 1);
}
printf("\n");
}