I use TBB to multithread a part of my application.
The following code was logically responsible for a non-deterministic behavior:
std::mt19937 engine;
std::uniform_real_distribution<double> distribution(-1., 1.);
double x[N];
tbb::parallel_for(0, N, [&](int i)
{
// ... complicated stuff done in this loop
x[i] = distribution(engine);
});
I changed the code to use one PRNG per thread, using TBB TLS, and seed the PRNG using the loop index.
It seems to work but looks weird to me. Is it a common practice?
tbb::enumerable_thread_specific<std::mt19937> engine;
std::uniform_real_distribution<double> distribution(-1., 1.);
double x[N];
tbb::parallel_for(0, N, [&](int i)
{
// ... complicated stuff done in this loop
engine.local().seed(i);
x[i] = distribution(engine.local());
});
Aucun commentaire:
Enregistrer un commentaire