2

I was trying to code this pretty basic piece of MPI code but I keep getting hangup. The task is to write a wrapper for MPI_Send and Receive routines so that the pointer usage can be hidden.

The following is what I developed:

#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. I am keeping track of the replies I get now. Thanks.

Sanjay

1 Answer 1

1

To specify a message source or recipient, you have to specify a rank and a communicator; the pair uniquely specify the process. The rank alone is like a street number without a street name.

You're passing in a communicator, but it has undefined value; your code

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

passes in a communicator, but you haven't assigned it a value anywhere. Depending on how the value in that variable and how your MPI implementation handles it, you could get a deadlock -- or, with openmpi, a helpful error message.

What you probably want is this:

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

or, equivalently, drop the mpicomm variable entirely:

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

either of those should work.

Sign up to request clarification or add additional context in comments.

4 Comments

Jonathan Thank You so much. I am trying it out now. Will keep you posted. Also am I right in saying that(wrt your nice analogy) to communicate with a processor in the cluster I specify the rank of that processor and also have to specify that it can be talked to using the communicator handle that's common for all the nodes in the MPI_COMM_WORLD ?
That's right. When you start an MPI program, all processors are members of MPI_COMM_WORLD, and you communciate with them by specifying (rank, MPI_COMM_WORLD). You can also create your own communicators that have only subsets of all of the processes, or that contain all the processes but have them relabeled in some way for convenience. In that case, the same process may have different ranks in different communicators; that's why you always have to specify the communicator.
Thanks so much. Can you pl also suggest a good book/resource for understanding such details. ( Though I have Michael Allen and Wilkinson)
I personally like Using MPI ( mcs.anl.gov/research/projects/mpi/usingmpi ). There are several online tutorials, as well - at NERSC, nersc.gov/nusers/help/tutorials/mpi/intro , LLNL, computing.llnl.gov/tutorials/mpi , and at the cyberinfrastructure tutorial pages (free registration required): citutor.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.