#include #include int main(int argc, char *argv[]) { /* this program demonstrates a very simple MPI program illustrating communication between two nodes. Node 0 sends a message consisting of 3 integers to node 1. Upon receipt, node 1 replies with another message consisting of 3 integers. */ /* get definition of MPI constants */ #include "mpi.h" /* define variables ierror = error indicator nproc = number of processors participating idproc = each processor's id number (0 <= idproc < nproc) len = length of message (in words) tag = message tag, used to distinguish between messages */ int ierror, nproc, idproc, len=3, tag=1; /* status = status array returned by MPI_Recv */ MPI_Status status; /* sendmsg = message being sent */ /* recvmsg = message being received */ /* replymsg = reply message being sent */ int recvmsg[3]; int sendmsg[3] = {1802399587,1798073198,1868786465}; int replymsg[3] = {2003332903,1931506792,1701995839}; printf("Knock program initializing...\n"); /* initialize the MPI execution environment */ ierror = MPI_Init(&argc, &argv); /* stop if MPI could not be initialized */ if (ierror) exit(1); /* determine nproc, number of participating processors */ ierror = MPI_Comm_size(MPI_COMM_WORLD,&nproc); /* determine idproc, the processor's id */ ierror = MPI_Comm_rank(MPI_COMM_WORLD,&idproc); /* use only even number of processors */ nproc = 2*(nproc/2); if (idproc < nproc) { /* even processor sends and prints message, then receives and prints reply */ if (idproc%2==0) { ierror = MPI_Send(&sendmsg,len,MPI_INT,idproc+1,tag, MPI_COMM_WORLD); printf("proc %d sent: %.12s\n",idproc,sendmsg); ierror = MPI_Recv(&recvmsg,len,MPI_INT,idproc+1,tag+1, MPI_COMM_WORLD,&status); printf("proc %d received: %.12s\n",idproc,recvmsg); } /* odd processor receives and prints message, then sends reply and prints it */ else { ierror = MPI_Recv(&recvmsg,len,MPI_INT,idproc-1,tag, MPI_COMM_WORLD,&status); printf("proc %d received: %.12s\n",idproc,recvmsg); ierror = MPI_Send(&replymsg,len,MPI_INT,idproc-1,tag+1, MPI_COMM_WORLD); printf("proc %d sent: %.12s\n",idproc,replymsg); } } /* terminate MPI execution environment */ ierror = MPI_Finalize(); if (idproc < nproc) { printf("hit carriage return to continue\n"); getchar(); } return 0; }