OpenMP“主” pragma不能被“parallel for”所包围。 pragma [英] the OpenMP "master" pragma must not be enclosed by the "parallel for" pragma

查看:605
本文介绍了OpenMP“主” pragma不能被“parallel for”所包围。 pragma的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么intel编译器不允许我指定一个openmp 并行为块的一些动作应该只由主线程执行?

Why won't the intel compiler let me specify that some actions in an openmp parallel for block should be executed by the master thread only?

如果没有这种功能,我该怎么办呢?

And how can I do what I'm trying to achieve without this kind of functionality?

'm尝试做的是通过并行的回调来更新进度条:

What I'm trying to do is update a progress bar through a callback in a 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 只有一个线程一次使用回调)我得到以下运行时异常:

I want only the master thread to call the callback, because if I don't enforce that (say by using omp critical instead to ensure only one thread uses the callback at once) I get the following runtime exception:

The application called an interface that was marshalled for a different thread.

...因此希望在主线程中保留所有回调。

...hence the desire to keep all callbacks in the master 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

Compiler Error C3034 OpenMP 'master' directive cannot be directly nested within 'parallel for' directive 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...
}

这篇关于OpenMP“主” pragma不能被“parallel for”所包围。 pragma的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆