I am trying to parallelize a for-loop with OpenMP. Usually this should be fairly straightforward. However I need to perform thread specific initializations prior to executing the for-loop.
Specifically I have the following problem: I have a random number generator which is not thread-safe so I need create an instance of the RNG for every thread. But I want to make sure that not every thread will produce the same random numbers.
So I tried the following:
#pragma omp parallel
{
int rndseed = 42;
#ifdef _OPENMP
rndseed += omp_get_thread_num();
#endif
// initialize randon number generator
#pragma omp for
for (int sampleid = 0; sampleid < numsamples; ++sampleid)
{
// do stuff
}
}
If I use this construct I get the following error message at runtime:
Fatal User Error 1002: '#pragma omp for' improperly nested in a work-sharing construct
So is there a way to do thread-specific initializations?
Thanks
/dev/urandom
, which will give you good seeds and different values each thread? – Lee Daniel Crocker