gpt4 book ai didi

c++ - OpenMP "master"编译指示不得包含在 "parallel for"编译指示中

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:22:29 24 4
gpt4 key购买 nike

为什么英特尔编译器不允许我指定 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;
}

编译器错误 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...
}

关于c++ - OpenMP "master"编译指示不得包含在 "parallel for"编译指示中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7661114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com