gpt4 book ai didi

c - 嵌套并行度 : Why only the main thread runs and executes the parallel for loop four times?

转载 作者:行者123 更新时间:2023-12-03 12:46:14 26 4
gpt4 key购买 nike

我的代码:

#include <cstdio>
#include "omp.h"

int main() {
omp_set_num_threads(4);
#pragma omp parallel
{
#pragma omp parallel for
for (int i = 0; i < 6; i++)
{
printf("i = %d, I am Thread %d\n", i, omp_get_thread_num());
}
}
return 0;
}
我得到的输出:
i = 0, I am Thread 0
i = 1, I am Thread 0
i = 2, I am Thread 0
i = 0, I am Thread 0
i = 1, I am Thread 0
i = 0, I am Thread 0
i = 1, I am Thread 0
i = 2, I am Thread 0
i = 2, I am Thread 0
i = 3, I am Thread 0
i = 0, I am Thread 0
i = 1, I am Thread 0
i = 3, I am Thread 0
i = 4, I am Thread 0
i = 5, I am Thread 0
i = 2, I am Thread 0
i = 3, I am Thread 0
i = 4, I am Thread 0
i = 5, I am Thread 0
i = 3, I am Thread 0
i = 4, I am Thread 0
i = 5, I am Thread 0
i = 4, I am Thread 0
i = 5, I am Thread 0
添加“并行”是问题的原因,但我不知道如何解释。
我的问题是:为什么只有主线程并运行四次for循环?

最佳答案

默认情况下,nested parallelism已禁用 .尽管如此,您可以明确 启用 nested parallelism ,通过:

   omp_set_nested(1);
或通过设置 OMP_NESTED环境变量为真。
也来自 OpenMP standard我们知道:

When a thread encounters a parallel construct, a team of threads iscreated to execute the parallel region.The thread that encountered the parallel construct becomesthe master thread of the new team, with a thread number of zero forthe duration of the new parallel region. All threads in the new team,including the master thread, execute the region. Once the team iscreated, the number of threads in the team remains constant for theduration of that parallel region.


来自 source您可以阅读以下内容。

OpenMP parallel regions can be nested inside each other. If nestedparallelism is disabled, then the new team created by a threadencountering a parallel construct inside a parallel region consistsonly of the encountering thread. If nested parallelism is enabled,then the new team may consist of more than one thread.


这解释了为什么添加第二个 parallel region 的原因。每个团队只有一个线程执行封闭代码(即 for 循环)。换句话说,从第一个 parallel region , 4创建线程,当遇到第二个 parallel region 时,每个线程都会被创建。将创建一个新团队并成为该团队的主人(即,将在新创建的团队中拥有 ID=0)。 但是,由于您没有明确启用嵌套并行性,因此每个团队仅由一个线程 组成。 .因此, 4各有一个线程的团队将执行 for 循环。因此,您将得到以下语句:
printf("i = %d, I am Thread %d\n", i, omp_get_thread_num());
正在打印 6 x 4 = 24 times (即,循环迭代总数乘以 4 团队中的线程总数)。下图提供了该流程的可视化:
enter image description here
如果您添加 printf第一个和第二个之间的语句 parallel region , 如下:
int main() {

omp_set_num_threads(4);
#pragma omp parallel
{
printf("Before nested parallel region: I am Thread{%d}\n", omp_get_thread_num());
#pragma omp parallel for // Adding "parallel" is the cause of the problem, but I don't know how to explain it.
for (int i = 0; i < 6; i++)
{
printf("i = %d, I am Thread %d\n", i, omp_get_thread_num());
}
}
return 0;
}
您将得到类似于以下输出的内容(请记住,输出第一个 4 行的顺序是不确定的)。
Before nested parallel region:  I am Thread{1}
Before nested parallel region: I am Thread{0}
Before nested parallel region: I am Thread{2}
Before nested parallel region: I am Thread{3}
i = 0, I am Thread 0
i = 0, I am Thread 0
i = 0, I am Thread 0
(...)
i = 5, I am Thread 0
意思是在第一个 parallel region内(但仍然在第二个并行区域之外)有一个由 4 个线程组成的团队—— IDs来自 03 ——并行执行。因此,这些线程中的每一个都将执行 printf陈述:
printf("I am Thread outside the nested region {%d}\n", omp_get_thread_num());
并为 omp_get_thread_num() 显示不同的值方法调用。
如前所述,嵌套并行被禁用。因此,当每个线程遇到第二个 parallel region 时,每个人都会创建一个新团队并成为主人(即,将在新创建的团队中拥有 ID=0)。 —— 也是唯一的成员 —— 该团队的成员。因此,为什么声明
 printf("i = %d, I am Thread %d\n", i, omp_get_thread_num());
在循环内,总是输出 (..) I am Thread 0 , 由于方法 omp_get_thread_num()在这种情况下将始终返回 0 .然而,即使方法 omp_get_thread_num()正在返回 0 ,这并不意味着代码是连续执行的(由带有 ID=0 的线程执行),而是每个 4 的每个主节点的每个主节点。团队正在返回他们的 ID=0 .
如果您启用了嵌套并行性,您将拥有如下图所示的流程:
enter image description here
线程的执行 13为简单起见省略了,但它与线程 0 相同。
所以,从第一个 parallel region , 团队与 4线程被创建。以后遇到下 parallel region来自前一个团队的每个线程,将创建一个新团队 4每个线程,所以目前我们总共有 16跨线程 4团队。最后,每个团队将执行整个 for 循环。但是,因为您有一个 #pragma omp parallel for构造函数,for 循环的迭代将在每个团队内的线程之间分配。
请记住,在上图中,我假设某个 static循环之间迭代的循环分布,我并不是暗示循环迭代将始终在 OpenMP 标准的所有实现中像这样划分。

关于c - 嵌套并行度 : Why only the main thread runs and executes the parallel for loop four times?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65119234/

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