OpenMP "master"杂注不得被"parallel for"杂注包围
the OpenMP "master" pragma must not be enclosed by the "parallel for" pragma
为什么英特尔编译器不让我指定openmp parallel for
块中的一些动作应该只由主线程执行?
如果没有这种功能,我怎么能做我想做的事情呢?
我想做的是通过并行回调更新进度条:
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;
}
编译错误C3034OpenMP '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...
}
你得到错误的原因是,当代码到达#pragma omp master
行时,主线程大多数时候都不存在。例如,让我们从Artyom:
#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;
}
如果代码可以编译,则可能发生以下情况:
假设线程0启动(主线程)。它到达的pragma实际上是在说"Master,执行下面的代码段"。作为主机,它可以运行这个函数。但是,当线程1、2或3等到达这段代码时会发生什么?
主指令是告诉当前/监听团队,主线程必须执行f()
。但这个团队是一个单一的线程,没有主子在场。过了这个点,程序就不知道该怎么做了。
我认为这就是为什么master不允许出现在for循环中。
用if (omp_get_thread_num() == 0)
代替master directive
可以工作,因为现在程序说,"如果你是大师,做这个。否则忽略"。
相关文章:
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- 如何在C++中从两个单独的for循环中添加两个数组
- 在Linux for Windows上编译C++代码时出错
- 调用专用模板时出错"no matching function for call to [...]"
- 为什么我的for循环不能正确获取argv
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- OpenMP #pragma omp for v/s #pragma omp parallel for 之间的区别?
- 函数中的"pragma omp parallel for"在另一个并行循环中调用函数时无效
- C++ + OpenMP for Parallel Computing:如何在Visual Studio中进行设置
- Parallel for with omp
- OpenMP:'parallel for loop'瓶颈
- 简单将 OpenMp Parallel for 转换为 C# Parallel for
- Parallel of std::reference_wrapper for std::shared_ptrs
- OpenMP 'parallel for'预先初始化
- omp parallel for:如何让线程写入私有数组,并在所有线程完成处理后合并所有数组
- OpenMP "master"杂注不得被"parallel for"杂注包围
- C++ Break omp parallel for
- OpenMP "parallel for"在特定程序中很奇怪
- 用OpenMP(Parallel for)并行化时分割错误