2017-06-07 5 views
1

これは私が達成しようとしているものです。MPIリングリーダーの選挙はセグメンテーションエラーを返します

Blue is the message. 
Yellow is when the specific node changes the leader known to it. 
Green is the final election of each node. 

enter image description here

コードは、私には正しいようだが、それは必ずしも関係なく、私が試したものをwhileループ内で立ち往生していないです。実行中の少数のノードではしばらくしてからセグメンテーション・フォールトを返します。すべてのランクについてランクの数の中でいくつかの最小数を決定するために

election_status=0; 
firstmsg[0]=world_rank;  // self rank 
firstmsg[1]=0;    // counter for hops 
chief=world_rank;   // each node declares himself as leader 
counter=0;     // message counter for each node 

// each node sends the first message to the next one 
MPI_Send(&firstmsg, 2, MPI_INT, (world_rank+1)%world_size, 1, MPI_COMM_WORLD); 
printf("Sent ID with counter to the right node [%d -> %d]\n",world_rank, (world_rank+1)%world_size); 

while (election_status==0){ 
    // EDIT: Split MPI_Recv for rank 0 and rest 
    if (world_rank==0){ 
     MPI_Recv(&incoming, 2, MPI_INT, world_size-1, 1, MPI_COMM_WORLD, &status); 
    } 
    else { 
     MPI_Recv(&incoming, 2, MPI_INT, (world_rank-1)%world_size, 1, MPI_COMM_WORLD, &status); 
    } 
    counter=counter+1; 
    if (incoming[0]<chief){ 
     chief=incoming[0]; 
    } 
    incoming[1]=incoming[1]+1; 

    // if message is my own and hopped same times as counter 
    if (incoming[0]==world_rank && incoming[1]==counter) { 
     printf("Node %d declares node %d a leader.\n", world_rank, chief); 
     election_status=1; 
    } 
    //sends the incremented message to the next node 
    MPI_Send(&incoming, 2, MPI_INT, (world_rank+1)%world_size, 1, MPI_COMM_WORLD); 
} 

MPI_Finalize(); 
+1

whileループから脱出できない場合は、2番目のif()に移動しないことを意味します。だからあなたはなぜそれを理解する必要があります。 – Gam

+0

それは私に不思議そうだ、カウンターと入って来る[1]が停止することなく増分されている。理論的には、各ノードで実行されるコマンドの続きが存在する必要があるのはなぜですか?ノードがメッセージを受け取った後、各ノードに対してcounter = counter + 1コマンドを実行しないでください。 – quelotic

+1

ちょっと、 'MPI_Recv'の'(world_rank-1) 'はあまり良くありません。 'rank = 0'でルートを考えてみましょう。 –

答えて

1

MPI_Allreduceを使用!

  • MPI_Sendがブロックしています。それは、一致する受信が送信されるまで永遠にブロックすることができます。あなたのプログラムは、MPI_Sendへの最初の呼び出しでデッドロックします。これを避けるには、特にMPI_Sendrecvを使用してください。
  • (world_rank-1)%world_sizeworld_rank == 0に対して-1を生成します。ランク番号として-1を使用することは無効です。それは同時にMPI_ANY_SOURCEであるかもしれません。
+0

私は2つのifsに受信を分割しました。美しく動作しています...ありがとう! – quelotic

+0

あなたのプログラムはまだ間違っています! 'MPI_Send'が時々出すノンブロッキングの振る舞いに頼らないでください。移植可能なプログラムには 'MPI_Sendrecv'を使います。 – Zulan

関連する問題