forked from miking-lang/miking-dppl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin_flip_mean.cu
44 lines (36 loc) · 1.01 KB
/
coin_flip_mean.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
/*
* File coin_flip_mean.cu defines a simple example that is useful for demonstrating RootPPL.
*/
#include <stdio.h>
#include "inference/smc/smc.cuh"
/*
* Initialize array for storing BBLOCK function pointers and define progState to be used in BBLOCKS.
* First argument is the program state, second argument is
* the number of BBLOCKS in the model.
*/
INIT_MODEL(int)
/*
* Define BBLOCK (fragment of model)
*/
BBLOCK(coinFlip, {
PSTATE = SAMPLE(bernoulli, 0.6);
NEXT = NULL;
})
/*
* Define callback function that calculates and prints mean of coin flips before particles are deleted.
*/
CALLBACK(sampleMean, {
double sum = 0;
for(int i = 0; i < N; i++)
sum += PSTATES[i];
double mean = sum / N;
printf("Sample mean: %f\n", mean);
})
/*
* The main function, any code can be added here. In order to run SMC, blocks must be added
* and SMC be called with the callback function as argument (or NULL if no callback is used).
*/
MAIN({
FIRST_BBLOCK(coinFlip);
SMC(sampleMean);
})