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