MPI_Send和Receive - Wrapper

时间:2011-06-11 01:38:06

标签: mpi parallel-processing

我正在尝试编写这个非常基本的MPI代码,但我一直在挂断。 任务是为MPI_Send和Receive例程编写一个包装器,以便隐藏指针用法。

以下是我开发的内容:

#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 个答案:

答案 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);

其中任何一个都应该有效。