2011-06-11 33 views
2

我試圖編寫這個非常基本的MPI代碼,但我一直掛斷。 該任務是爲MPI_Send和Receive例程編寫一個包裝器,以便可以隱藏指針的使用情況。MPI_Send和接收 - 包裝

以下是我開發的:

#include "mpi.h" 
#include<iostream> 
#include<cstdlib> 

#define _MAXSIZE_ 10 

using namespace std; 

/** Goal: Avoid pointers in MPI_Send and MPI_Recieve */ 

/* Wrapper for regular MPI_Send. */ 
void Send(int data, int destination, MPI_Comm mpicomm) { 
    MPI_Send(&data, 1, MPI_INT, destination, 0, mpicomm); 
    cout << "Data sent successfully" << data << endl; 
} 

/* Wrapper for regular MPI_Recieve */ 
int Recieve(MPI_Status stat, MPI_Comm mpicomm, int source_id = 0) { 
    int data; 
    MPI_Recv(&data, 1, MPI_INT, source_id, 0, mpicomm, &stat); 
    cout << "Data Recieved: " << data << endl; 
    return data; 
} 

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

    int myid, numprocs; 
    int arr[10]; 
    MPI_Init(&argc, &argv); 
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs); 
    MPI_Comm_rank(MPI_COMM_WORLD, &myid); 
    MPI_Status status; 
    MPI_Comm mpicomm; 

    /** Trying to send an array of 10 integers without pointer usage */ 
    int data = 3; 
    int destination = rand() % numprocs; // choose a destination to send other than the master itself 
    cout << "Destination: " << destination << "\n" << endl; 
    if(myid == 0) { 
     if(destination != 0) { 
      Send(data, destination, mpicomm); 
     } 
    } 
    else if(myid == destination) { 
      int data = Recieve(status,mpicomm, 0); 
      cout << "Data Received Successfully" << data << endl; 
    } 

    MPI_Finalize(); 
    return 0; 
    } 

附:我正在跟蹤我現在得到的答覆。謝謝。

桑傑

回答

1

要指定一個消息源或接收者,你必須指定一個等級和溝通能力;這一對唯一地指定了過程。排名就像一個沒有街道名稱的街道號碼。

你正在傳遞一個通信器,但它有未定義的值;你的代碼

MPI_Comm mpicomm; 
// ... 
Send(data, destination, mpicomm); 

通過傳播者,但你沒有在任何地方給它賦值。根據該變量的值以及MPI實現如何處理它,您可能會遇到死鎖 - 或者,openmpi會提供一條有用的錯誤消息。

你可能想要的是這樣的:

MPI_Comm mpicomm = MPI_COMM_WORLD; 
//.. 
Send(data, destination, mpicomm); 
int data = Recieve(status, mpicomm, 0); 

,或等效,完全放下了mpicomm變量:

Send(data, destination, MPI_COMM_WORLD); 
//... 
int data = Recieve(status, MPI_COMM_WORLD, 0); 

無論是那些應該工作。

+0

Jonathan非常感謝你。我正在嘗試它。會及時向大家發佈。我也正確地說(與你好的類比)與集羣中的處理器進行通信,我指定了該處理器的等級,並且還必須指定可以使用對所有節點通用的通信器句柄MPI_COMM_WORLD? – svk 2011-06-12 22:31:27

+1

沒錯。當你啓動一個MPI程序時,所有處理器都是MPI_COMM_WORLD的成員,並且通過指定(rank,MPI_COMM_WORLD)與它們通信。您也可以創建自己的傳播者,這些傳播者只包含所有流程的子集,或者包含所有流程但爲了方便而以某種方式重新標記它們。在這種情況下,同一過程在不同的傳播者中可能有不同的等級;這就是爲什麼你總是需要指定傳播者。 – 2011-06-13 13:14:40

+0

非常感謝。你還可以建議一本好書/資源來理解這些細節。 (雖然我有邁克爾艾倫和威爾金森) – svk 2011-06-14 02:49:23