#include #include "mpi.h" #define NELTS 50000 /* number of elements in the vector */ #define MASTER 0 /* id of the first process */ #define FROM_MASTER 1 /* a message type */ #define FROM_WORKER 2 /* a message type */ MPI_Status status; main(int argc, char **argv) { double v[NELTS], /* the vector to be summed */ sum, /* the overall vector sum */ ptSum; /* a partial sum received from a worker */ int numProcs, /* number of processes in virtual machine */ numWorkers, /* number of worker processes */ rank, /* our rank */ source, /* rank of message source */ dest, /* rank of message destination */ mtype, /* message type */ aveSz, /* average partition size */ extra, /* number of left over elements */ offset, /* offset of partition in v */ i, /* simple counter */ count; /* the actual number of elements in a partition */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &numProcs); numWorkers = numProcs-1; /******* master process ***********/ if (rank == MASTER) { // Initialize the vector for (i=0; i MASTER) { // receive out partition from the master mtype = FROM_MASTER; source = MASTER; MPI_Recv(&count,1,MPI_INT,source,mtype,MPI_COMM_WORLD,&status); MPI_Recv(&v,count,MPI_DOUBLE,source,mtype,MPI_COMM_WORLD,&status); // compute our partial sum sum=0; for (i=0;i