gpt4 book ai didi

c++ - 内部嵌套循环的实用语法未在嵌套循环中正确忽略

转载 作者:行者123 更新时间:2023-12-02 10:28:24 25 4
gpt4 key购买 nike

我正在尝试实现以下代码,以查看如何在嵌套循环中管理OpenMP线程,在该嵌套循环中,每个内部/外部循环分别在函数及其调用者中实现。
每个循环都通过以下语句实现#pragma omp parallel for,我假设将忽略内部循环的pragma
为了看到这一点,我在每个循环中打印了线程号。
然后,我可以看到以下内容,其中内部循环中的线程ID始终为零,与调用方所对应的线程号不同。为什么会这样?

Calling 0 from 0
Calling 2 from 1
Calling 6 from 4
Calling 8 from 6
Calling 4 from 2
Calling 7 from 5
Calling 5 from 3
Calling 0 from 0 // Expecting 3
Calling 1 from 0
Calling 2 from 0
Calling 3 from 0
Calling 0 from 0
Calling 1 from 0
Calling 2 from 0
Calling 3 from 0
Calling 0 from 0
Calling 0 from 0
Calling 0 from 0
Calling 1 from 0
Calling 2 from 0
Calling 3 from 0
Calling 9 from 7
Calling 1 from 0 // Expecting 7
Calling 2 from 0
Calling 3 from 0
Calling 0 from 0
Calling 3 from 1
Calling 0 from 0 // Expecting 1
Calling 1 from 0
Calling 2 from 0
Calling 1 from 0
Calling 2 from 0
Calling 3 from 0
Calling 3 from 0
Calling 1 from 0
Calling 2 from 0
Calling 3 from 0
Calling 0 from 0
Calling 1 from 0
Calling 2 from 0
Calling 3 from 0
Calling 0 from 0
Calling 1 from 0
Calling 2 from 0
Calling 3 from 0
Calling 1 from 0
Calling 0 from 0
Calling 1 from 0
Calling 2 from 0
Calling 3 from 0
#include <vector>                                                                                                                                                                                                                                                          
#include <omp.h>
#include <iostream>
#include <cstdio>
#include <limits>
#include <cstdint>
#include <cinttypes>

using namespace std;

const size_t kM = 4;

struct Mat
{
int elem[kM];

Mat(const Mat& copy)
{
for (size_t i = 0; i<kM; ++i)
this->elem[i] = copy.elem[i];
}
Mat()
{
for (size_t i = 0; i<kM; ++i)
elem[i] = 0;
}

void do_mat(Mat& m)
{
#pragma omp parallel for
for (int i = 0; i<kM; ++i)
{
printf(" \tCalling %d from %d\n", i, omp_get_thread_num());
elem[i] += m.elem[i];
}
}
};

int main ()
{
const int kN = 10;
vector<Mat> matrices(kN);

Mat m;
#pragma omp parallel for
for (int i = 0; i < kN; i++)
{
int tid = omp_get_thread_num();
printf("Calling %d from %d\n", i, tid);
matrices[i].do_mat(m);
}

return 0;
}

最佳答案

我不确定我了解您的期望是什么,但是您得到的结果是完全可以预期的。
默认情况下,OpenMP嵌套并行性是禁用的,这意味着任何嵌套parallel区域将创建与1个线程组成的线程组一样多,而外部线程遇到这些线程。
在您的情况下,最外面的parallel区域创建了一个由8个线程组成的团队。它们中的每一个都将到达最里面的parallel区域,并创建第二级的1线程团队。每个二级线程在其自己的团队中的排名为0,因此您具有打印的0。
使用通过g++ 9.3.0编译的完全相同的代码,通过设置2个环境变量OMP_NUM_THREADSOMP_NESTED,我得到以下信息:

OMP_NUM_THREADS="2,3" OMP_NESTED=true ./a.out 
Calling 0 from 0
Calling 5 from 1
Calling 0 from 0
Calling 1 from 0
Calling 2 from 1
Calling 0 from 0
Calling 1 from 0
Calling 3 from 2
Calling 3 from 2
Calling 2 from 1
Calling 6 from 1
Calling 1 from 0
Calling 0 from 0
Calling 1 from 0
Calling 3 from 2
Calling 2 from 1
Calling 2 from 0
Calling 0 from 0
Calling 1 from 0
Calling 2 from 1
Calling 3 from 2
Calling 0 from 0
Calling 1 from 0
Calling 3 from 2
Calling 2 from 1
Calling 3 from 0
Calling 7 from 1
Calling 0 from 0
Calling 3 from 2
Calling 2 from 1
Calling 3 from 2
Calling 0 from 0
Calling 1 from 0
Calling 1 from 0
Calling 2 from 1
Calling 4 from 0
Calling 8 from 1
Calling 0 from 0
Calling 3 from 2
Calling 2 from 1
Calling 2 from 1
Calling 0 from 0
Calling 1 from 0
Calling 3 from 2
Calling 1 from 0
Calling 9 from 1
Calling 2 from 1
Calling 0 from 0
Calling 1 from 0
Calling 3 from 2
也许这与您期望看到的更好?

关于c++ - 内部嵌套循环的实用语法未在嵌套循环中正确忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63312894/

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