I'm getting a "bus error" from an OpenMP parallel section of code. I recreated a simple version of my problem below. The code essentially makes many calls to the function uniform_distribution
, which draws an integer between 0 and 20000 using Boost's uniform_int_distribution.
This post warns of two threads accessing the same object. I'm guessing that's eng
in my case. (Unfortunately I don't know how to write "an appropriate mutex wrapper", as that post suggests).
A possible dirty solution I thought of was to create a local eng
inside the #pragma for
loop and to pass that as an argument to uniform_distribution
. I don't like this idea because in my real code, I'm calling many functions, and passing a local eng
would be cumbersome. Also, my concern is that different threads will generate the same random number sequence if I declare eng
inside uniform_distribution
. So I have two requirements: How do I parallelize in a way that
- Each thread is generating probabilistically independent draws from other threads?
- No race conditions occur on the RNG?
Thanks; any help is warmly appreciated.
#include <omp.h>
#include <boost/random/uniform_int_distribution.hpp>
boost::random::mt19937 eng;
int uniform_distribution(int rangeLow, int rangeHigh) {
boost::random::uniform_int_distribution<int> unirv(rangeLow, rangeHigh);
return unirv(eng);
}
int main()
{
# pragma omp parallel for private(eng)
for (int bb=0; bb<10000; bb++)
for (int i=0; i<20000; i++)
int a = uniform_distribution(0,20000);
return 0;
}