Re: [boost] problems with the Random library in Boost v1.47

Hi, Steven Thanks for your reply. I'm using MSVC 2010, too. I met such problems when I generated a million random numbers with the mersenne_twister generator and OpenMP. I'm not sure whether v1.47 is thread safe, though. Here is the code triggered the memory access violation: int num_point = 1000; int n_para = 1000; boost::numeric::ublas::matrix<double> ui(num_point, n_para); #pragma omp parallel for shared(ui, num_point, n_para) private(i, j) for (i=0; i<num_point; i++) { for (j=0; j<n_para; j++) { ui(i,j) = unirand_real(0.0,1.0); } } Thanks, Jacky On 07/13/2011 11:35 AM, Hongyu Miao wrote:
Sorry to bother you with this. I met a problem with the Random library in the latest release v1.47.
<snip>
However, when I adopted my code to v1.47, the following code failed due to
memory access violation after a number of function calls:
double unirand_real(double a, double b) { // Create a random number generator engine static boost::random::mt19937 rng_real(static_cast<unsigned int>(std::time(0)));
// select Uniform probability distribution boost::random::uniform_real_distribution<> dist(a, b);
// return random sample return dist(rng_real); }
I will appreciate your help with this problem as right now I think the Random library may have a bug.
I can't reproduce this problem with MSVC 2010. a) What compiler are you using? b) Can you provide a complete minimal test case that fails? In Christ, Steven Watanabe

AMDG On 07/15/2011 08:40 AM, Hongyu Miao wrote:
Hi, Steven
Thanks for your reply. I'm using MSVC 2010, too. I met such problems when I generated a million random numbers with the mersenne_twister generator and OpenMP. I'm not sure whether v1.47 is thread safe, though.
Using a generator concurrently in multiple threads has never been supported. Even if this code worked (or more likely, appeared to work) in previous versions, it's wrong.
Here is the code triggered the memory access violation:
int num_point = 1000; int n_para = 1000; boost::numeric::ublas::matrix<double> ui(num_point, n_para); #pragma omp parallel for shared(ui, num_point, n_para) private(i, j) for (i=0; i<num_point; i++) { for (j=0; j<n_para; j++) { ui(i,j) = unirand_real(0.0,1.0); } }
In Christ, Steven Watanabe

Hongyu Miao <jackymiao <at> gmail.com> writes:
Hi, Steven
Thanks for your reply. I'm using MSVC 2010, too. I met such problems when I generated a million random numbers with the mersenne_twister generator and OpenMP. I'm not sure whether v1.47 is thread safe, though. Here is the code triggered the memory access violation:
int num_point = 1000;
int n_para = 1000;
boost::numeric::ublas::matrix<double> ui(num_point, n_para);
#pragma omp parallel for shared(ui, num_point, n_para) private(i, j)
for (i=0; i<num_point; i++)
{
for (j=0; j<n_para; j++)
{
ui(i,j) = unirand_real(0.0,1.0);
}
}
Thanks,
Jacky
Yes, this is wrong since the generators are not threadsafe. If you really need to parallelize the generation of random numbers then you have a couple of options: -) use multiple generators- one for each thread. This has quite a bit of extra overhead and you have to be very careful in seeding them so that the random numbers are truly independent in the different streams. Check out this link: http://theo.phys.sci.hiroshima-u.ac.jp/~ishikawa/PRNG/mt_stream_en.html -) Instead of trying to paralleize your initialization loop, just use an openmp version of the random generator instead. You can find details here: http://www.pgroup.com/lit/articles/insider/v2n2a4.htm This is not in boost that I know, but perhaps it might be considered to add an openmp version- there is a nice speedup with very little work, and would be a simple solution for people who run into this kind of problem.
participants (3)
-
Hongyu Miao
-
Steven Watanabe
-
tcb