2016-08-10 2 views
1

MPIアプリケーションコード内でOpenMPマルチスレッド領域を1つのプロセスで起動したいとします。例えば:私のサンプルコードで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; 
} 

、私は行列Bに行列Aからデータをコピーするために2つのスレッドを起動すると、私のマシンは、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つのスレッドが私のコードで実行させることができますか?ありがとうございました!

+0

[MCVEの例](http://stackoverflow.com/help/mcve)を提供してください。たとえば、MatrixXdはどこにも定義されていません。また、期待する出力はどれですか?それはあなたが得るものとどのように違いますか?そして、2つのスレッドが実行されていないことをどのように知っていますか? – Harald

+0

@Harald、MatrixXdはインクルードファイルからのもので、マトリックスクラスです。私は –

+0

申し訳ありませんアレクサンダー質問を編集しましたが、そのヘッダーはどこから来たのですか?例えば私のシステムにはありません。 – Harald

答えて

1

変更

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

を追加するのに十分な評判を持っていない共有変数です。 プライベート句にbnumbrowという名前を追加すると、スレッドごとにそのような名前の新しい自動変数が作成されます。デフォルトでは未定義です。

1

parallelという単語には、コンパイラがキャッチしないタイプインがあります。

#pragma omp prallel

PS:私は

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

browbnumにコメント

+0

ありがとうございますが、それを 'parallel'に変更した後、私はすべてゼロを得ましたか?私のコードで何が間違っていますか? –

関連する問題