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

How to launch multi-threads in MPI + openmp?

我想在我的 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 prallel

PS: 我没有足够的声誉来添加评论

改变

#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)

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