[英]Error in sending/receiving by multi-threads in hybrid openMP/MPI code
[英]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)
brow
和bnum
是共享變量。 通過將名稱bnum
和brow
添加到private子句,您將為每個線程使用這樣的名稱創建新的自動變量,並且默認情況下它們是未定義的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.