[Rcpp-devel] Parallel random numbers using Rcpp and OpenMP
Matt D.
matdzb at gmail.com
Mon Apr 28 10:39:28 CEST 2014
On 4/28/2014 01:30, Matteo Fasiolo wrote:
> [...]
>
> As I understand, doing things such as:
>
> NumericMatrix out(n, d);
> #pragma omp for schedule(static)
> for(int kk = 0; kk < d; kk++) out( _, kk) = rnorm(n);
>
> is not going to work, because rnorm() is not thread safe
> (in fact this code crashed R). On the other hand R level parallelism
> using clusterApply, mclapply etc appears to be too slow to be of any
> use for this purpose.
>
> Is anybody aware of any package providing a parallel C++ rng which my
> package might link to?
>
Your first choice can be to go with the C++ Standard Library -- header
<random> -- (importantly) using one object per thread:
http://stackoverflow.com/questions/8813592/c11-thread-safety-of-random-number-generators
See:
http://en.cppreference.com/w/cpp/numeric/random
http://isocpp.org/blog/2013/03/n3551-random-number-generation
Since you're using OpenMP, you can get the distinct thread ID by using
the `omp_get_thread_num` function:
http://stackoverflow.com/questions/15918758/how-to-make-each-thread-use-its-own-rng-in-c11
Note that if you're doing large scale parallel statistics (say, Monte
Carlo) you may also want a PRNG with statistical properties which don't
deteriorate (e.g., no spurious correlation, let alone overlapping) for
very large samples; preferably, also one that doesn't maintain any kind
of global state (so-called "stateless") is going to be easier to use,
too (nothing to synchronize/lock across threads).
A relatively recent work that's particularly relevant is Random123:
https://www.deshawresearch.com/resources_random123.html
http://www.thesalmons.org/john/random123/
MIT-licensed C++ version is available here:
http://www.sitmo.com/article/parallel-random-number-generator-in-c/
With a simple example:
http://www.sitmo.com/article/multi-threaded-random-number-generation-in-c11/
// The author is currently working on getting it into Boost.Random;
discussion:
http://www.wilmott.com/messageview.cfm?catid=44&threadid=95882&STARTPAGE=2#710955
HTH,
Best,
Matt
More information about the Rcpp-devel
mailing list