This project has moved. For the latest updates, please go here.

Thread Safe Random Variate Generation

Mar 20, 2014 at 3:18 PM
Hi,

I have an algorithm that wants to generate lots of random gamma and poissons in parrallel, I think my understanding of the code is correct but wanted to ask:

If I download the Net4.0 binaries from this site, create a mersennetwister class and pass in the threadsafe boolean, than the RNG will be robustly made as a ThreadSafe Mersenne twister, and I can pass this around as needed. Does this seem correct? (that is no obvious problems lurking?).

Thanks for any help,
N
Coordinator
Mar 21, 2014 at 7:28 PM
Edited Mar 21, 2014 at 7:31 PM
Hi,

Yes, the RNGs will be thread-safe then (with v3 alpha there's also a SystemRandomSource that wraps System.Random in a thread-safe way but is faster than our MersenneTwister). However, if you can control the threads and on what thread the code is executing, you'd be much faster with a separate RNG instance per thread where each RNG is only used from within a single thread and thread-safety/locking can thus be disabled completely.

Unfortunately the equivalent of the significantly faster array-based routines (that fill a whole array with random numbers in one go) of the RNGs in recent v3 alphas is not yet available in the probability distributions, but this is currently being worked on.

Thanks,
Christoph

PS: I'd be interested in your exact use case (i.e. what would be the ideal API to retrieve these random numbers in your code base) to validate the API design.
Mar 22, 2014 at 4:04 PM
Hello,

Thanks for the response (and the awesome library). I am somewhat new to the ThreadLocal class and so was great to hear about this.

So I have a step in an MCMC routine that runs parallel across multiple threads (in theory they are completely independent). Right now I am using ThreadLocal<Mersenne> on each one and running that through. It seems to be reasonably fast in the sense that I don’t see any memory contentions at present. I think this might be ideal because the parameter for the poisson change on each call, so I can’t preallocate the poisson (but could the uniforms).

I think prefilling an array would be involve a lot more contention if all threads were using it relative to the threadlocal RNGs, but I may be mistaken.

Thanks again for the help!
Nigel


PS As a random factoid I recently found out that the .NET System.Random class has been coded incorrectly and so does not represent the actual algorithm they state in the documentation (see http://connect.microsoft.com/VisualStudio/feedback/details/634761/system-random-serious-bug, apparently they copied it wrong from numeric recipes). Not sure how big of an issue this is though.
Coordinator
Mar 25, 2014 at 12:26 AM
Thanks for the details, and also to the System.Random bug.

Indeed, the array-based routines would not help at all in this use case.

Thanks,
Christoph