curand - generating discrete random variables from a histogram


I need to generate discrete random variables from a numerically specified distribution function (histogram) on the device. I can see two options:

  1. On the device: have an array which holds cumulative probabilities, draw a uniform variable from curand, do a first occurrence search for this uniform variable in the array (potentially slow as the search will likely create divergence problems)

  2. Utilise undocumented API in curand: Create a curandDiscreteDistribution_t histogram on the host (how?), from the device use the api curand_discrete to generate a random variable. Clean up with curandDestroyDistribution

Clearly 2) is the superior approach and Nvidia have undoubtedly put lots of thought into making curand_discrete efficient.

The relevant help documentation is below:

double lambda,
curandDiscreteDistribution_t *discrete_distribution)
The curandCreatePoissonDistribution() function is used to create a histogram for the
Poisson distribution with the given lambda.

__device__ unsigned int
curand_discrete (
curandState_t *state,
curandDiscreteDistribution_t discrete_distribution)
This function returns a single discrete distributed unsigned int based on a distribution for
the given discrete distribution histogram.

curandDiscreteDistribution_t discrete_distribution)

Is the source code for curandCreatePoissonDistribution available somewhere? This would help me understand how to create my own curandDiscreteDistribution_t *discrete_distribution for my own histogram.


hmm wont a histogram force a potentially infinite distribution into a finite one?
Numerical Recipes in C gives ways of converting uniform random distributions
to a variety of distributions. I am sure it includes Gaussian and so perhaps
also has Poisson.
I have a CUDA implementation of Park-Miller’s minimum PRNG


No, I have to explicitly implement a custom discrete probability mass function here, it cannot be approximated.

It looks like someone had a go at this before but did not manage to create their own histogram using the api.

Anyone from Nvidia on the forum who can shed some light on how to create a curandDiscreteDistribution_t ?

If you want to see something documented, I suggest filing a RFE.

Thanks, will do.