I have a loop which should be nicely parallelized by insering one openmp pragma:
boost::normal_distribution<double> ddist(0, pow(retention, i - 1));
boost::variate_generator<gen &, BOOST_TYPEOF(ddist)> dgen(rng, ddist);
// Diamond
const std::uint_fast32_t dno = 1 << i - 1;
// #pragma omp parallel for
for (std::uint_fast32_t x = 0; x < dno; x++)
for (std::uint_fast32_t y = 0; y < dno; y++)
{
const std::uint_fast32_t diff = size/dno;
const std::uint_fast32_t x1 = x*diff, x2 = (x + 1)*diff;
const std::uint_fast32_t y1 = y*diff, y2 = (y + 1)*diff;
double avg =
(arr[x1][y1] + arr[x1][y2] + arr[x2][y1] + arr[x2][y2])/4;
arr[(x1 + x2)/2][(y1 + y2)/2] = avg + dgen();
}
(unless I make an error each execution does not depend on others at all. Sorry that not all of code is inserted).
However my question is - are boost RNG thread-safe? They seems to refer to gcc code for gcc so even if gcc code is thread-safe it may not be the case for other platforms.
Browsing through the Boost mailing list archives gives:
Boost.Random does not maintain global state that would need protection from multi-threading.
Boost.Random is thread-safe as long as you don't access any given object from two threads simultaneously. (Accessing two different objects is ok, as long as they don't share an engine). If you require that kind of safety, it's trivial to roll that on your own with an appropriate mutex wrapper.
If you are worried about thread safety don't use boost, use TRNG. Its a parallel random number generation library built to be run on the TINA cluster in Germany. It allows you to create multiple streams of random numbers. There is a tutorial on how to use TRNG with OpenMP here http://www.lindonslog.com/programming/parallel-random-number-generation-trng/ just like you are trying to do. You create an independent number of streams according to how many threads you are using, and then draw from them by using the rank of the thread. Its all in the above tutorial.
来源:https://stackoverflow.com/questions/2920497/thread-safety-of-boost-rng