如果堆栈中的功能较低,请忽略对并行计算的请求

发布于 2025-02-06 14:46:47 字数 583 浏览 1 评论 0原文

当openMP中,当堆栈中较低的函数点燃多处理时,openMP设施会忽略功能主体中的多处理请求时,是否可以使用OpenMP?

这是OpenMP总是有效的方式吗?如果没有,我可以这样做吗?如何?

void do1()
{
    #pragma omp parallel for
    for (unsigned int i = 0; i < 10; ++i);
}
void do2()
{
    #pragma omp parallel for
    for (unsigned int i = 0; i < 10; ++i) do1();
}
void do3()
{
    #pragma omp parallel for
    for (unsigned int i = 0; i < 10; ++i) do2();
}
int main()
{
    do1(); // runs in parallel
    do2(); // do2() runs in parallel, do1() I want not
    do3(); // do3() runs in parallel, do1() and do2() I want not
}

Is it possible with OpenMP, when a function, lower in stack, ignites multiprocessing, then OpenMP facilites ignore multiprocessing requests from functions' bodies, higher in stack?

Is this the way OpenMP always works? If not, can I do this? How?

void do1()
{
    #pragma omp parallel for
    for (unsigned int i = 0; i < 10; ++i);
}
void do2()
{
    #pragma omp parallel for
    for (unsigned int i = 0; i < 10; ++i) do1();
}
void do3()
{
    #pragma omp parallel for
    for (unsigned int i = 0; i < 10; ++i) do2();
}
int main()
{
    do1(); // runs in parallel
    do2(); // do2() runs in parallel, do1() I want not
    do3(); // do3() runs in parallel, do1() and do2() I want not
}

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

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

发布评论

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

评论(2

愛放△進行李 2025-02-13 14:46:47

如果禁用了嵌套的并行性,则OpenMP的工作原理完全如您所描述的。在当前的OpenMP实现上,默认情况下它是禁用的,但并未由标准指定,因此,为了安全起见,值得通过设置 op_max_active_levels环境变量通过使用

OpenMP works exactly as you described, if nested parallelism is disabled. On current OpenMP implementations it is disabled by default, but it is not specified by the standard, so to be on the safe side it is worth disabling it by setting OMP_MAX_ACTIVE_LEVELS environmental variable to 1 or by using omp_set_max_active_levels(1); function in your code.

瞎闹 2025-02-13 14:46:47

这种情况称为嵌套,通常效率低下(尽管这取决于运行时)。您可以调整OpenMP运行时参数以减轻性能问题(请参阅@laci的答案),但老实说,这种模式通常会引起比解决的问题更多的问题。现代解决方案仅是使用任务,更具体地说是taskloop指令。另外,您可以使用如果(...)子句调整并行部分行为(例如,关于给定条件的并行性)。任务的调度取决于运行时。例如,IOMP(Clang/ICC)基于(随机)工作偷窃算法,而GCC使用有界的集中式队列。任务的开销可能比平行部分之一(尤其是静态调度)更大。

请注意,堆栈框架可以由编译器优化。实际上,do1do2do3可以被镶嵌,以便在运行时没有这些函数的剩余跟踪。

This situation is called nesting and it is generally inefficient (this is dependent of the runtime though). You can tweak OpenMP runtime parameter to mitigate performance issue (see the answer of @Laci), but honestly, this pattern often causes more issues than it solves. The modern solution is simply to use tasks and more specifically taskloop directives in your case. Alternatively, you can use the if(...) clause to tune the parallel section behaviour (eg. disable parallelism regarding a given condition). The scheduling of the task is dependent of the runtime. For example, IOMP (Clang/ICC) is based on a (randomized) work-stealing algorithm while GCC use a bounded centralized queue. The overhead of tasks can be bigger than the one of parallel sections (especially with a static scheduling).

Note that the stack frame can be optimized by compilers. In fact, do1, do2 and do3 can be inlined so that at runtime there is no trace left of theses functions.

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