samedi 21 novembre 2015

runif function in Cuda

I am trying to implement a Metropolis-Hastings algorithm in Cuda. For this algorithm, I need to be able to generate many uniform random numbers with varying range. Therefore, I would like to have a function called runif(min, max) that returns a uniformly distributed number in this range. This function has to be called multiple times inside another function that actually implements the algorithm.

Based on this post, I tried to put the code shown there into a function (see below). If I understood this correctly, the same state leads to the same sequences of numbers. So, if the state doesn't change, I always get the same output. One alternative would be to generate a new state inside the runif function so that each time the function is called, it is called with another state. As I've heard though, this is not advisable since the function gets slow.

So, what would be the best implementation of such a function? Should I generate a new state inside the function or generate a new one outside each time I call the function? Or is there yet another approach?

__device__ float runif(float a, float b, curandState state)
{
  float myrandf = curand_uniform_double(&state);
  myrandf *=  (b - a + 0.999999);
  myrandf += a;
  return myrandf;
}




Aucun commentaire:

Enregistrer un commentaire