gpt4 book ai didi

c++ - 如何在 MPI + openmp 中启动多线程?

转载 作者:行者123 更新时间:2023-11-30 03:39:27 24 4
gpt4 key购买 nike

我想在我的 MPI 应用程序代码中的一个进程中启动一个 OpenMP 多线程区域。例如:

#include <iostream>
#include <omp.h>
#include <mpi.h>
#include <Eigen/Dense>
using std::cin;
using std::cout;
using std::endl;

using namespace Eigen;

int main(int argc, char ** argv)
{
int rank, num_process;
MatrixXd A = MatrixXd::Ones(8, 4);
MatrixXd B = MatrixXd::Zero(8, 4);
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_process);
MPI_Status status;
if (rank == 0)
{
int i, j, bnum = 2, brow = 4, thid;
#pragma omp parallel shared(A, B) private(i, j, brow, bnum, thid) num_threads(2)
for (i = 0; i < brow; i ++)
{
for (j = 0; j < 4; j ++)
{
thid = omp_get_thread_num();
//cout << "thid " << thid << endl;
B(thid * brow+i,j) = A(thid*brow+i, j);
}
}
cout << "IN rank 0" << endl;
cout << B << endl;
cout << "IN rank 0" << endl;
MPI_Send(B.data(), 32, MPI_DOUBLE, 1, 1, MPI_COMM_WORLD);
}
else
{
MPI_Recv(B.data(), 32, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &status);
cout << "IN rank 1" << endl;
cout << B << endl;
cout << "IN rank 1" << endl;
}
MPI_Finalize();
return 0;
}

在我的示例代码中,我想启动 2 个线程来将数据从矩阵 A 复制到矩阵 B,而我的机器有 4 个内核。但是运行程序时,矩阵B只得到了一半的数据。

$ mpirun -n 2 ./shareMem
IN rank 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
IN rank 0
IN rank 1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
IN rank 1

$ mpirun -n 4 ./shareMem # it just hang on and doesn't exit
IN rank 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
IN rank 0
IN rank 1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
IN rank 1

我期望的输出是

$ mpirun -n 2 ./shareMem # it just hang on and doesn't exit
IN rank 0
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
IN rank 0
IN rank 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
IN rank 1

如何修复它并让 2 个线程在我的代码中运行?谢谢!

最佳答案

改变

#pragma omp parallel shared(A, B) private(i, j, brow, bnum, thid) num_threads(2)

#pragma omp parallel shared(A, B) private(i, j, thid) num_threads(2)

brow, bnum 是共享变量。通过将名称 bnumbrow 添加到 private 子句,您正在为每个线程创建具有此类名称的新自动变量,默认情况下它们是未定义的。

关于c++ - 如何在 MPI + openmp 中启动多线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38875144/

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