c++randomgpuopenaccpgi

Portable random number generation with OpenACC


Is there a portable way to generate random numbers with OpenACC? I know that it is possible to directly use cuRand but then I am restricted to Nvidia GPUs. Another option seems to be generating numbers on the host and then moving them to the device, but that does not seem like the best solution performance-wise.

Is there a better way?


Solution

  • Using a random number generator in parallel is inherently a tricky operation. RNGs carry a state variable which needs private to each parallel thread. Using the system's "rand" is problematic in that it uses a global state variable which gives undefined behavior (a race-condition) when used in a parallel context. More parallel RNGs such as "rand_r" and Boost's PRNG should be used instead.

    A second issue is that RNGs are not always portable. Different platforms may implement "rand" in ways. As you determined, there isn't a "rand" call available on NVIDIA devices and instead you need to use cuRAND calls.

    OpenACC is designed to help exploit parallelism in your code in a platform agnostic way. Hence, platform specific operations such a parallel RNG are difficult to define within the standard itself. Perhaps something can be done, especially for something as useful as a PRNG, and I would suggest you contact the OpenACC standards committee requesting this support (feedback_at_openacc_dot_org).

    The only truly portable way to do this now, is to write your own parallel RNG and include it as part of your code. Offhand, I don't have an OpenACC example written up and am a bit swamped so don't know if I'll have time, but will do my best to pull one together.

    I did write a CUDA C version of a Mersenne Twister algorithm as part of an article I wrote about 8 years ago that might be helpful (though it predates OpenACC). It uses the PGI Accelerator Model at the beginning of the article and CUDA Fortran in the middle so don't worry too much about the content, just the MT source.

    https://www.pgroup.com/blogs/posts/tune-gpu-monte-carlo.htm

    Source package: https://www.pgroup.com/lit/samples/pginsider/pgi_mc_example.tar.gz