2014-03-26 67 views
0

我在fortran中使用MPI來計算我的數據。我通過打印數據證實,每個進程的計算正在按期望的頻率進行,但是,主機無法整理數據。MPI master無法接收

這裏是我試圖使其工作代碼: 編輯:創建一個標籤,它是用於發送和recv

integer :: tag 
    tag = 123 
    if(pid.ne.0) then 
    print *,'pid: ',pid,'sending' 
    DO j = start_index+1, end_index  
    CALL MPI_SEND(datapacket(j),1, MPI_REAL,0, tag, MPI_COMM_WORLD) 
    !print *,'sending' 
    END DO 
    print *,'send complete' 

    else 

    DO slave_id = 1, npe-1 
    rec_start_index = slave_id*population_size+1 
    rec_end_index = (slave_id + 1) * population_size; 

    IF (slave_id == npe-1) THEN 
     rec_end_index = total-1;   
    ENDIF 
    print *,'received 1',rec_start_index,rec_end_index 
    CALL MPI_RECV(datapacket(j),1,MPI_REAL,slave_id,tag,MPI_COMM_WORLD, & 
&  status) 
    !print *,'received 2',rec_start_index,rec_end_index 
    END DO 

它從來沒有打印received或之後任何事情MPI_RECV電話,但不斷,我可以看到發送的情況很好,但除了依賴打印語句之外,沒有辦法驗證它。

real, dimension (:), allocatable :: datapacket 

有沒有辦法,我做錯了什麼事: 變量databpacket如下初始化?

編輯:對於測試設置所有的進程正在本地主機上運行。

回答

0

您對所有的發送使用不同的消息標記,但是在接收時,您只使用j,這在根進程中永遠不會改變。還要注意你的實現看起來像一個MPI_Gather,我建議你使用它,而不是自己實現它。

編輯:對不起,您更新後我現在認識到,您實際上是從每個等級> 0(start_index + 1到end_index)發送多個消息,如果您需要的話,您確實需要標記區分個人消息。但是,您還需要在您的主設備上進行多次接收。 也許最好說出你實際想要達到的效果。

你想是這樣的:

integer :: tag 
tag = 123 
if(pid.ne.0) then 

    print *,'pid: ',pid,'sending' 
    CALL MPI_SEND(datapacket(start_index+1:end_index),end_index-start_index, MPI_REAL,0, tag, MPI_COMM_WORLD) 
    !print *,'sending' 
    print *,'send complete' 

else 

    DO slave_id = 1, npe-1 
    rec_start_index = slave_id*population_size+1 
    rec_end_index = (slave_id + 1) * population_size; 

    IF (slave_id == npe-1) THEN 
     rec_end_index = total-1;   
    ENDIF 
    print *,'received 1',rec_start_index,rec_end_index 
    CALL MPI_RECV(datapacket(rec_start_index:rec_end_index),rec_end_index-rec_start_index+1,MPI_REAL,slave_id,tag,MPI_COMM_WORLD, & 
     &  status) 
    !print *,'received 2',rec_start_index,rec_end_index 
    END DO 

end if 
+0

謝謝,我會記MPI_gather,稍後再試吧。在curent上下文中發送和接收一個常量標籤並沒有幫助。我編輯了我目前的實施。 – Trancey

+0

我更新了我的答案,並試圖想出一些東西,我想你正在努力實現。不過,我強烈建議你查閱MPI_Gather/MPI_Gatherv。 – haraldkl