2012-10-13 61 views
0

我一直在MPI項目中的奴隸都發送數據回到主。由於某種原因,如果我連續發送2個連續發送,主人將只收到數據。這很奇怪,我認爲這導致了一些其他奇怪的問題。任何想法會導致這種情況?我認爲第一次發送是發送某種垃圾數據或其他東西。儘管發送的是完全相同的代碼行。必須發送MPI消息兩次,由於某種原因

EDIT:代碼下面...

if (numProcs > 0) 
    MPI_Barrier(MPI_COMM_WORLD) ; //only wait if there are other processes to wait for 

if (rank != 0) 
{ 
    MPI_Send(handArray, 10, MPI_DOUBLE, 0, TAG_HAND, MPI_COMM_WORLD); 
    MPI_Send(handArray, 10, MPI_DOUBLE, 0, TAG_HAND, MPI_COMM_WORLD); 
} 
//8. After the main loop the master process receives and sums together the hand counts array 
// from each slave process. 
else 
{ 
    int activeProcs = numProcs - 1; 
    getHandsFromSlaves(activeProcs, handArray); 

則主進到打印某些數據...

這裏是getHands FromSlaves方法。請注意我也嘗試過使用阻塞調用以及相同的問題。

void getHandsFromSlaves(int& activeCount, double handTotals[10]){ 

static MPI_Request request; 
static int msgBuff, recvFlag; 
static double handBuff[10]; 
MPI_Status status; 

while (activeCount > 0) 
{ 
    if(request) 
    { 
     // Already listening for a message 

     // Test to see if message has been received 
     MPI_Test(&request, &recvFlag, &status); 
     //cout << "TAG: " << status.MPI_TAG << " SOURCE: "<< status.MPI_SOURCE << " ERROR: " << status.MPI_ERROR << endl; 
     if(recvFlag) 
     { 
      // Message received 
      if(status.MPI_TAG == TAG_HAND) 
      { 
       cout << "Hand Received!" << endl; 

       for(int m = 0; m < 10; ++m) 
       { 
        handTotals[m] += handBuff[m]; 
       } 

       activeCount--; 
      } 
      else 
      { 
       //error report... what happened? 
       cout << "TAG: " << status.MPI_TAG << " SOURCE: "<< status.MPI_SOURCE << " ERROR: " << status.MPI_ERROR << endl; 
      } 

      // Reset the request handle 
      request = 0; 
     } 
    } 

    if(!request && activeCount > 0) 
     // Start listening again 
     MPI_Irecv(&handBuff, 10, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &request); 
} 
} 
+0

可以顯示一些代碼? – tune2fs

+0

添加代碼。需要幫助請叫我。 – xxf8xx

+0

任何人都可以幫助嗎?任何幫助是極大的讚賞! – xxf8xx

回答

1

好吧,你可能想,因爲你的request變量上輸入您的getHandsFromSlaves()例行未定義處理一個太多的消息。自輸入以來,request幾乎可以肯定是非零的,即使您還沒有發佈Irecv,您也立即嘗試使用MPI_Test作爲消息。

事實上,在這裏發佈的代碼摘錄有很多非常奇怪的事情。爲什麼是局部變量static?爲什麼要在MPI_Test()上實現自己的等待,而不是使用MPI_Wait()?如果你在接收之間沒有做任何有用的事,你爲什麼要使用非阻塞接收?事實上,如果你只是總結所有的陣列,爲什麼你要做個別的點對點接收而不是做一個MPI_Reduce()

下更短的代碼seeems做你嘗試上面做什麼:

#include <stdio.h> 
#include <mpi.h> 


int main (int argc, char **argv) { 

    int rank, numProcs; 

    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &numProcs); 
    double handArray[10]; 
    double handTotal[10]; 

    for (int i=0; i<10; i++) 
     handArray[i] = rank + i; 

    if (rank == 0) // Since apparently rank 0 doesn't do anything 
    { 
     for (int i=0; i<10; i++) 
      handArray[i] = 0; 
    } 

    MPI_Reduce(handArray, handTotal, 10, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); 

    if (rank == 0) { 
     printf("Hand Totals= \n"); 
     for (int i=0; i<10; i++) 
      printf(" %lf ", handTotal[i]); 
     printf("\n"); 
    } 

    MPI_Finalize(); 
} 
+0

所有包括進程0的進程都添加到他們自己的個人手陣列中。最後,進程0會統計所有總數並打印數據。我只是沒有發佈所有進程都在工作時發生的代碼。 handArray的每個元素都存儲了它找到的每種類型的撲克手的數量。感謝您的幫助,我會檢查出MPI_Reduce,看看我能否正常工作。 – xxf8xx