OpenMP“主控” pragma 不得包含在“parallel for”中。杂注

发布于 2024-12-08 13:19:15 字数 825 浏览 0 评论 0原文

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

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

提前致谢。

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?

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...
}

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.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2024-12-15 13:19:15
#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...
}
#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;
}

Compiler Error C3034
OpenMP 'master' directive cannot be directly nested within 'parallel for' directive
Visual Studio 2010 OpenMP 2.0

May be so:

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...
}
欲拥i 2024-12-15 13:19:15

出现错误的原因是,当代码到达 #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 启动(主线程)。它达到了实际上说“大师,执行以下代码段”的编译指示。主机可以运行该功能。
但是,当线程 1 或 2 或 3 等到达该代码段时会发生什么?

主指令告诉出席/聆听团队主线程必须执行f()。但团队是单线程,没有master在场。程序不知道在那之后要做什么。

我认为这就是为什么 master 不允许进入 for 循环的原因。

if (omp_get_thread_num() == 0) 替换 master 指令 是有效的,因为现在程序会说:“如果您是 master,请执行此操作。否则忽略”。

The reason why you get the error is because the master thread isn't there most of the times when the code reaches the #pragma omp master line.
For example, let's take the code from 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;
}

If the code would compile, the following could happen:

Let's say thread 0 starts (the master thread). It reaches the pragma that practically says "Master, do the following piece of code". It being the master can run the function.
However, what happens when thread 1 or 2 or 3, etc, reaches that piece of code?

The master directive is telling the present/listening team that the master thread has to execute f(). But the team is a single thread and there is no master present. The program wouldn't know what to do past that point.

And that's why, I think, the master isn't allowed to be inside the for-loop.

Substituting the master directive with if (omp_get_thread_num() == 0) works because now the program says, "If you are master, do this. Otherwise ignore".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文