2013-01-23 53 views
1

我有一個嵌套循環,並從循環內我稱爲MPI發送,我想它 發送到接收器一個特定的值,然後在接收器接收數據,並再次發送MPI消息 到另一組CPU ......我使用了類似這樣的東西,但它看起來像接收中存在問題......並且我不能看到我出錯的地方......「機器進入無限循環某處.. 。MPI調用和嵌套循環接收

我試圖使它像這樣的工作: 主CPU >>發送到其它CPU >>發送到從CPU

. 
. 
. 

int currentCombinationsCount; 
int mp; 

if (rank == 0) 
{ 


    for (int pr = 0; pr < combinationsSegmentSize; pr++) 
    { 
     int CblockBegin = CombinationsSegementsBegin[pr]; 
     int CblockEnd = CombinationsSegementsEnd [pr]; 
     currentCombinationsCount = numOfCombinationsEachLoop[pr]; 
     prossessNum = 1; //specify which processor we are sending to 

     // now substitute and send to the main Processors 
     for (mp = CblockBegin; mp <= CblockEnd; mp++) 
     { 

      MPI_Send(&mp , 1, MPI_INT , prossessNum, TAG, MPI_COMM_WORLD); 

      prossessNum ++; 
     } 

    }//this loop goes through all the specified blocks for the combinations 
} // end of rank 0 
else if (rank > currentCombinationsCount) 
{ 
     // here I want to put other receives that will take values from the else below 
} 
else 
{ 
    MPI_Recv(&mp , 1, MPI_INT , 0, TAG, MPI_COMM_WORLD, &stat); 
    // the code stuck here in infinite loop 
} 

回答

0

您只在if(rank==0)分支內初始化了currentCombinationsCount,所以其他所有procs都會看到未初始化的變量。這將result in undefined behaviour和結果取決於您的編譯器。您的程序可能會崩潰,或者該值可能設置爲0或未確定的值。

如果你夠幸運,the value may be set to 0在這種情況下,您的分支簡化爲:因此

if (rank == 0) { /* rank == 0 will enter this } 
else if (rank > 0) { /* all other procs enter this } 
else { /* never entered! Recvs are never called to match the sends */ } 

你最終將是不匹配任何接收。由於MPI_Send is potentially blocking,發送進程可能無限期地停止。使用特殊功能阻止發送,它當然可以看作「...機器進入無限循環...」

如果給currentCombinationsCount一個任意值(而不是0),那麼rank!=0 procs將進入任意分支(所有分支都進入最後的else)。然後,您會收到第二組未收到的收件,導致與上述相同的問題。

+0

謝謝Shawn回答,但我沒有在任何情況下使用currentCombinationsCount ...我想確保mp在每次迭代中達到所需的值,因此我在循環內評論了currentCombinationsCount,並且程序仍在某處......你認爲這可能是一個合乎邏輯的問題嗎? – SOSO

+0

您在分支條件下使用它,所以是的,它似乎是您的程序邏輯的問題。 –