#include "mpi.h" #include #define ARRAYSIZE 60000 #define MASTER 0 /* taskid of first process */ int main(argc,argv) int argc; char *argv[]; { int numtasks, /* total number of MPI tasks in partition */ numworkers, /* number of worker tasks */ taskid, /* task identifier */ rc, /* return error code */ dest, /* destination task id to send message */ index, /* index into the array */ i, /* loop variable */ arraymsg = 1, /* setting a message type */ indexmsg = 2, /* setting a message type */ source, /* origin task id of message */ chunksize; /* for partitioning the array */ float data[ARRAYSIZE], /* the intial array */ result[ARRAYSIZE]; /* for holding results of array operations */ MPI_Status status; /*------------------------ initializations ------------------------------- Find out how many tasks are in this partition and what my task id is. Then define the number of worker tasks and the array partition size as chunksize. Note: For this example, the MP_PROCS environment variable should be set to an odd number...to insure even distribution of the array to numtasks-1 worker tasks. -------------------------------------------------------------------------*/ /*-----------------*/ /* Initialize MPI */ /*-----------------*/ rc = MPI_Init(&argc,&argv); /*--------------------------------------------*/ /* Determine size of the global communicator */ /*--------------------------------------------*/ rc|= MPI_Comm_size(MPI_COMM_WORLD,&numtasks); /*--------------------------------------------*/ /* Determine rank in the global communicator */ /*--------------------------------------------*/ rc|= MPI_Comm_rank(MPI_COMM_WORLD,&taskid); if (rc != 0) printf ("error initializing MPI and obtaining task ID information\n"); numworkers = numtasks-1; chunksize = (ARRAYSIZE / numworkers); /**************************** master task ********************************/ if (taskid == MASTER) { printf("MASTER: number of worker tasks will be= %d\n",numworkers); fflush(stdout); /*----------------------*/ /* Initialize the array */ /*----------------------*/ for(i=0; i MASTER) { source = MASTER; /*------------------------------------------------------------------*/ /* Receive index value from the master indicating start position in */ /* data array. */ /*------------------------------------------------------------------*/ MPI_Recv(&index, 1, MPI_INT, source, indexmsg, MPI_COMM_WORLD, &status); /*-------------------------------------------------*/ /* Receive chunksize bit of data starting at index */ /*-------------------------------------------------*/ MPI_Recv(&result[index], chunksize, MPI_FLOAT, source, arraymsg, MPI_COMM_WORLD, &status); /*-----------------------------------------------------------*/ /* Do a simple value assignment to each of my array elements */ /*-----------------------------------------------------------*/ for(i=index; i < index + chunksize; i++) result[i] = i + 1; dest = MASTER; /*------------------------------------------------------------------*/ /* Send index value so that master knows which portion of data each */ /* process was working on. */ /*------------------------------------------------------------------*/ MPI_Send(&index, 1, MPI_INT, dest, indexmsg, MPI_COMM_WORLD); /*-----------------------------------------------*/ /* Send chunksize bit of results back to master */ /*-----------------------------------------------*/ MPI_Send(&result[index], chunksize, MPI_FLOAT, MASTER, arraymsg, MPI_COMM_WORLD); } /*--------------*/ /* Finalize MPI */ /*--------------*/ MPI_Finalize(); }