#include "mpi.h" #include #include #include #include #define size 16000 #define TAG 1 void send (int sendto) { int myid,numprocs; double *data = (double *) malloc (size * sizeof (double)); assert (NULL != data); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); printf ("myid = %d numprocs = %d\n", myid, numprocs); printf ("myid = %d: Sending to %d size = %d\n", myid, sendto, size); MPI_Send (data, size, MPI_DOUBLE, sendto, TAG, MPI_COMM_WORLD); } void receive (void) { MPI_Status status; int myid,numprocs; double *data = (double *) malloc (size * sizeof (double)); assert (NULL != data); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); printf ("myid = %d numprocs = %d\n", myid, numprocs); printf ("myid = %d: Receiving from 0 size = %d\n", myid, size); MPI_Recv (data, size, MPI_DOUBLE, 0, TAG, MPI_COMM_WORLD, &status); } int main(int argc, char *argv[]) { int ierr = 0; int myid, numprocs; int i = 0; ierr = MPI_Init(&argc,&argv); printf ("MPI_Init returned %d\n",ierr); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); if (myid==0) { for (i=1; i< numprocs; i++) { printf ("myid = %d i = %d\n", myid, i); send (i); } } else receive (); MPI_Finalize(); return 0; }