vSMC
vSMC: Scalable Monte Carlo
|
The Random123 library is ideal for generating random numbers on OpenCL device. It is easy to use, for example, say inside a kernel, to generate four standard normal random variates,
As seen above, for every call to threefry4x32
we can generate four standard normal random variates. It is fine if we just happen to need four random numbers. But this is rarely the case. Then we need some tricky code to keep track of when to call threefry4x32
again etc., or call it every time we need a random number and waste a lot of time and generated numbers.
vSMC's RNG module provide some facilities to partially solve this problem.
The counter-based RNGs are wrapped in the above header. Eight are defined,
The default engines use threefry4x32
etc. To use the Philox engine, define macros CBRNG4x32
and CBRNG4x32KEYINIT
etc., before including vSMC headers. For example,
Each RNG engine is used as the following,
The function threefry4x32_rand
will be responsible for increasing the counters. The keys and counters can also be set manually. For example,
One may keep the states of counters between OpenCL kernel calls as in the following example,
Since OpenCL does not support static local variables, this is the most convenient way to ensure that the RNG used in each kernel call does not overlap their counters.
For each distribution, a set of types and functions are defined. Each of them use either 32- or 64-bits RNG and generate float
or double
precision results. For example,
generate float
precision uniform random variates on (0, 1]
using 32-bits integers. Another example,
is the type used to construct objects that can be used to generate float
precision standard Normal random variates using threefry4x32
engines.
Not all OpenCL devices have double
precision support. Therefore by default, only 32-bits and float
versions of these types and functions are defined. To enable 64-bits and double
versions, define the macro VSMC_HAS_OPENCL_DOUBLE
with a non-zero value.
In a single program, usually only float
or double
precision is used. Macros are defined according to the value of VSMC_HAS_OPENCL_DOUBLE
. For example,
is the type used to construct objects that can be used to generate standard Normal random variates using threefry4x32
engines. The generated results is double
if VSMC_HAS_OPENCL_DOUBLE
is defined and non-zero or float
otherwise.
In the documentation of each distribution, the following notations are used,
<N>
: 2 or 4, the rounds of counter-based RNG algorithm<W>
: 32 or 64, the bits of integer RNG<FT>
: float or double<F>
: 24 if <FT>
is float, 53 if <FT>
is double