Generating random numbers from various distributions in CUDA
I am playing around with doing MCMC on the GPU, and need implementations for various samplers, written for CUDA.
Most of the posts I've seen on StackOverflow relate to uniform, binomial, and normal sampling. Are there any libraries that allow me the simplicity and variety of the dpqr functions in R (See this page)?
I would like to be able to sample from Gamma, Normal, Binomial, and the inverse distributions used in Bayesian problems (inverse chi square, inverse gamma), and would prefer not to have to write my own using inverse probability transforms and acceptance-rejection sampling.
For the Gamma distribution, this is what I use at the moment. It is the GSL function modified to work with CuRAND.
__device__ double ran_gamma (curandState localState, const double a, const double b){
/* assume a > 0 */
if (a < 1){
double u = curand_uniform_double(&localState);
return ran_gamma (localState, 1.0 + a, b) * pow (u, 1.0 / a);
}
{
double x, v, u;
double d = a - 1.0 / 3.0;
double c = (1.0 / 3.0) / sqrt (d);
while (1){
do{
x = curand_normal_double(&localState);
v = 1.0 + c * x;
} while (v <= 0);
v = v * v * v;
u = curand_uniform_double(&localState);
if (u < 1 - 0.0331 * x * x * x * x)
break;
if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
break;
}
return b * d * v;
}
}
链接地址: http://www.djcxy.com/p/24818.html
上一篇: 随机矢量a与R中均匀分布的元素
下一篇: 从CUDA中的各种分布生成随机数