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

标签 c++ com callback progress-bar openmp

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

相关文章:

c++ - 在 Windows XP 中使用 C/C++ 中的动态数组最简单的方法是什么?

c++ - 使用 COM 传递数组?

c# - 如何从 C# 中的 com 对象返回数组(double[])?

java - 可以修改第三方 ActiveX 控件中的枚举吗?

ios - 当两个 ViewController 打开时,如何在两个 ViewController 中接收相同的回调?

c++ - std::condition_variable 伪阻塞

c++ - 是否有函数/WinAPI 以不区分大小写的语言方式判断一个字符串是否以另一个字符串开头?

c++ 隐式复制构造函数是否复制数组成员变量?

在函数完成之前触发 Javascript 回调(无 jQuery)

android - RxJava 代替接口(interface)回调 (Android)