parallel uniform distribution pseudorandom namber using OpenMP
i use in my program boost uniform distribution between 0 and 1:
#include <boost/random/uniform_01.hpp>
#include <boost/random.hpp>
static boost::mt19937 rng;
static boost::uniform_01<boost::mt19937&> zeroone(rng);
zeroone()
function is called inside a for-loop which i would like to parallel, using OpenMP.
for ( int index = 0 ; index < 4096 ; index++ ) {
if ( node[ index ] == false ) {
if ( zeroone() < 0.03 )
node[ index ] = true;
}
}
The question if it is possible 开发者_运维知识库using OpenMP to parallel the for-loop
and not damaging the uniform distribution pseudorandom number generator?
e.g. Is it possible to define for the first core a seed, for the second core the corresponding seed that the first core pseudorandom number generator will reach after 6 times?
Regards
You can do what you suggest, but
- it would be insane...ly inefficient (because the purpose of a RNG is to generate hard-to-predict pseudo random sequences, the only way to .... predict the nth successor would be to generate the intervening ones as well.)
- most likely unnecessary:
You can give each thread it's own private RNG (seed each one independently). The uniform distribution property will still hold unless your dataset is really small (in which case, the uniformness would be swamped by the sample noise anyway, regardless of threads)
My scheme would look something like this:
typedef boost::.... rng_t;
static rng_t g_rng; // notice how you never seed this, is this on purpose?
#per thread
{
rng_t rng(g_rng()); // need synchronized access to g_rng here
boost::uniform_01<boost::mt19937&> zeroone(rng);
// ...
}
The documentation for my C++ random number library, RandomLib, contains an illustration of using parallel number streams in OpenMP; see http://randomlib.sourceforge.net/html/parallel.html. You might be able to adapt the ideas presented there to your application.
精彩评论