gpt4 book ai didi

c++ - omp 并行 block 外的 omp pragma

转载 作者:可可西里 更新时间:2023-11-01 17:58:49 24 4
gpt4 key购买 nike

是否可以在 omp 并行 block 之外使用 omp pragma,如 critical、single、master 或 barrier?我有一个函数可以从 OMP 并行 block 调用,也可以不调用。如果是,我需要将部分代码包含在关键部分中。换句话说,这段代码可以吗?

void myfunc(){
#pragma omp critical
{ /* code */ }
}

// not inside an omp parallel region
myfunc();

#pragma omp parallel
{
// inside an omp parallel region
myfunc();
}

我在 OpenMP 文档中没有发现这一点。我想代码的行为应该与 1 线程执行完全一样——这就是它与 gcc 一起工作的方式。我想知道这种行为是否是可移植的,或者它是否是规范未定义的并且可以预期的东西。

最佳答案

根据 this文档:

The DO/for, SECTIONS, SINGLE, MASTER and BARRIER directives bind to the dynamically enclosing PARALLEL, if one exists. If no parallel region is currently being executed, the directives have no effect.

所以答案是那些 pragma 可以在并行区域之外使用。虽然我仍然没有发现它明确地写在文档中。

关于c++ - omp 并行 block 外的 omp pragma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12316776/

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