How to generate pseudo random in cuda

2019-06-04 21:03发布

问题:

I am attempting to build a particle system utilizing CUDA to do the heavy lifting. I want to randomize some of the particles' initial values like velocity and life span. The random numbers don't have to be super random since it's just for visual effect. I found this post that addresses the same subject:

Random Number Generation in CUDA

That suggests a linear congruential is the way to go. It seems like it should be simple to implement, but I am having trouble getting anything useful of my implementation. Can anyone provide some code that will run in the device?

I am using CUDA with VC++ on Windows 7 64bit.

回答1:

CUDA pseudo random number generators are

  1. included in the NVidia SDK eg C/src/MersenneTwister/ and C/src/quasirandomGenerator

  2. available as separate papers and source:

    2.a Langdon's paper and Langdon's source code

    2.b Mersenne Twister on GPU



回答2:

Depending on your requirements there are a number of open source options. There are also several commercial options such as NAG who have implemented l'Ecuyer's MRG32k3a. Be wary of using an LCG if you need to ensure that your streams are not correlated - you can use leapfrog/splitting but you'll need a very long period!

If you want to go open source then you should definitely consider using thrust for it's simplicity. There are also some RNGs in the NVIDIA SDK, including the Mersenne Twister PRNG sample (MT607, MT19937 is on the forums) and the Sobol and Niederreiter QRNGs.

Finally, CUDPP also has a random number generator.