【发布时间】:2013-03-19 15:53:57
【问题描述】:
我从 OpenMP 并行代码部分收到“总线错误”。我在下面重新创建了我的问题的简单版本。该代码本质上对函数 uniform_distribution 进行了多次调用,该函数使用 Boost 的 uniform_int_distribution 绘制 0 到 20000 之间的整数。
post 警告两个线程访问同一个对象。我猜在我的情况下是eng。 (不幸的是,我不知道如何编写“适当的互斥体包装器”,正如那篇帖子所暗示的那样)。
我想到的一个可能的肮脏解决方案是在#pragma for 循环内创建一个本地eng 并将其作为参数传递给uniform_distribution。我不喜欢这个想法,因为在我的真实代码中,我调用了许多函数,并且传递本地 eng 会很麻烦。另外,我担心的是,如果我在uniform_distribution 中声明eng,不同的线程会生成相同 随机数序列。所以我有两个要求:如何以某种方式并行化
- 每个线程都在从其他线程生成概率独立的绘图?
- RNG 上没有出现竞争条件?
谢谢;非常感谢任何帮助。
#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;
}
【问题讨论】: