Reputation: 3484
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
Upvotes: 0
Views: 954
Reputation: 1929
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.
Upvotes: 0
Reputation: 393457
You can do what you suggest, but
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);
// ...
}
Upvotes: 2