作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的 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
是共享变量。通过将名称 bnum
和 brow
添加到 private 子句,您正在为每个线程创建具有此类名称的新自动变量,默认情况下它们是未定义的。
关于c++ - 如何在 MPI + openmp 中启动多线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38875144/
我是一名优秀的程序员,十分优秀!