为什么英特尔编译器不允许我指定 openmp parallel for
block 中的某些操作应该仅由主线程执行?
如果没有这种功能,我该如何实现我想要实现的目标?
我想做的是通过并行回调来更新进度条:
long num_items_computed = 0;
#pragma omp parallel for schedule (guided)
for (...a range of items...)
{
//update item count
#pragma omp atomic
num_items_computed++;
//update progress bar with number of items computed
//master thread only due to com marshalling
#pragma omp master
set_progressor_callback(num_items_computed);
//actual computation goes here
...blah...
}
我只希望主线程调用回调,因为如果我不强制执行(比如使用 omp critical
来确保一次只有一个线程使用回调),我会得到以下运行时异常:
The application called an interface that was marshalled for a different thread.
...因此希望将所有回调保留在主线程中。
提前致谢。
最佳答案
#include <omp.h>
void f(){}
int main()
{
#pragma omp parallel for schedule (guided)
for (int i = 0; i < 100; ++i)
{
#pragma omp master
f();
}
return 0;
}
编译器错误 C3034 OpenMP“master”指令不能直接嵌套在“parallel for”指令中 Visual Studio 2010 OpenMP 2.0
可能是这样:
long num_items_computed = 0;
#pragma omp parallel for schedule (guided)
for (...a range of items...)
{
//update item count
#pragma omp atomic
num_items_computed++;
//update progress bar with number of items computed
//master thread only due to com marshalling
//#pragma omp master it is error
//#pragma omp critical it is right
if (omp_get_thread_num() == 0) // may be good
set_progressor_callback(num_items_computed);
//actual computation goes here
...blah...
}
关于c++ - OpenMP "master"编译指示不得包含在 "parallel for"编译指示中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7661114/