我有一个可以并行化的C ++程序。我正在使用Visual Studio 2010,32位编译。
简而言之,该计划的结构如下
#define num_iterations 64 //some number
struct result
{
//some stuff
}
result best_result=initial_bad_result;
for(i=0; i<many_times; i++)
{
result *results[num_iterations];
for(j=0; j<num_iterations; j++)
{
some_computations(results+j);
}
// update best_result;
}
由于每个 some_computations()
是独立的(一些全局变量读取,但没有修改全局变量)我并行内部 for
-循环。
我的第一次尝试是 提高::螺纹,
thread_group group;
for(j=0; j<num_iterations; j++)
{
group.create_thread(boost::bind(&some_computation, this, result+j));
}
group.join_all();
结果很好,但我决定尝试更多。
我试过了 OpenMP的 图书馆
#pragma omp parallel for
for(j=0; j<num_iterations; j++)
{
some_computations(results+j);
}
结果比...差 boost::thread
的。
然后我尝试了 PPL 库和使用 parallel_for()
:
Concurrency::parallel_for(0,num_iterations, [=](int j) {
some_computations(results+j);
})
结果是最糟糕的。
我发现这种行为非常令人惊讶。由于OpenMP和ppl是为并行化而设计的,我希望得到更好的结果 boost::thread
。我错了吗?
为什么是 boost::thread
给我更好的结果?