2011-06-11 3 views
2

아주 기본적인 MPI 코드를 작성하려고했지만 계속 전화를 끊습니다. 작업은 포인터 사용을 숨길 수 있도록 MPI_Send 및 Receive 루틴에 대한 래퍼를 작성하는 것입니다.MPI_Send and Receive - Wrapper

#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; 
    } 

P.S.을 :

다음

내가 개발 한 것입니다 나는 내가 지금받는 답장을 추적하고있다. 감사.

산 제이

답변

1

메시지 소스 또는받는 사람을 지정하려면, 당신은 계급과 의사 소통을 지정해야합니다; 쌍은 프로세스를 고유하게 지정합니다. 계급은 거리 이름이없는 거리 번호와 같습니다.

전달자를 전달 중입니다.하지만 값은 정의되지 않았습니다. 귀하의 코드

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

은 커뮤니케이터를 통과하지만 아무 값도 지정하지 않았습니다. 변수의 값과 MPI 구현이 어떻게 처리하는지에 따라 교착 상태가 발생하거나 openmpi에서 유용한 오류 메시지가 표시 될 수 있습니다. 그 중 하나가 작동합니다

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

: 동등하게, 완전히 mpicomm 변수를 드롭,

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

나 :

은 당신이 아마 원하는 것은 이것이다.

+0

조나단 고맙습니다. 나는 지금 그것을 시험해보고있다. 계속 게시 할 것입니다. 또한 나는 (그 좋은 비유로) 클러스터의 프로세서와 통신하기 위해 그 프로세서의 순위를 지정하고 또한 모든 노드에 공통적 인 커뮤니케이터 핸들을 사용하여 말할 수 있어야한다고 말하고 있습니다. MPI_COMM_WORLD? – svk

+1

맞습니다. MPI 프로그램을 시작할 때, 모든 프로세서는 MPI_COMM_WORLD의 멤버이며, (rank, MPI_COMM_WORLD)를 지정하여 그들과 통신합니다. 또한 모든 프로세스의 하위 세트 만 포함하거나 모든 프로세스를 포함하지만 편의를 위해 어떤 식 으로든 레이블을 다시 지정하는 고유 한 커뮤니케이터를 만들 수 있습니다. 이 경우 동일한 프로세스가 다른 의사 소통 자에서 다른 순위를 가질 수 있습니다. 그래서 당신은 항상 의사 소통자를 지정해야합니다. –

+0

고마워요. 당신은 또한 그러한 세부 사항을 이해할 수있는 좋은 책/자료를 제안 할 수 있습니까? (비록 내가 Michael Allen과 Wilkinson을 가지고있다.) – svk