#include "mpi.h" #include #define ARRAYSIZE 360000 #define MASTER 0 /* taskid of first process */ int main(int argc,char **argv) { int ntasks, /* total number of MPI tasks in partitiion */ nworkers, /* number of worker tasks */ taskid, /* task identifier */ rc, /* return error code */ dest, /* destination task id to send message */ index; /* index into the array */ long int i, j, chunksize; /* loop variable */ int arraymsg = 1, /* setting a message type */ indexmsg = 2, /* setting a message type */ source; /* origin task id of message */ float data[ARRAYSIZE], /* the intial array */ result[ARRAYSIZE]; /* for holding results of array operations */ MPI_Status status; rc = MPI_Init(&argc,&argv); rc|= MPI_Comm_size(MPI_COMM_WORLD,&ntasks); rc|= MPI_Comm_rank(MPI_COMM_WORLD,&taskid); if (rc != MPI_SUCCESS) printf ("Error initializing MPI and obtaining task ID information\n"); else printf ("MPI task ID = %d\n", taskid); printf("%d tasks, I am task %d\n", ntasks, taskid); nworkers = ntasks-1; chunksize = (ARRAYSIZE / nworkers); /**************************** master task ************************************/ if (taskid == MASTER) { printf("MASTER: number of worker tasks will be= %d\n",nworkers); for(i=0; i MASTER) { // Receive my portion of array from the master task source = MASTER; //MPI_Recv(buffer,count,type,source,tag,comm,status) MPI_Recv(&index, 1, MPI_INT, source, indexmsg, MPI_COMM_WORLD, &status); 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++) { for (j=1; j<10000; j++); result[i] = i + 1; } // Send my results back to the master task dest = MASTER; MPI_Send(&index, 1, MPI_INT, dest, indexmsg, MPI_COMM_WORLD); MPI_Send(&result[index], chunksize, MPI_FLOAT, MASTER, arraymsg, MPI_COMM_WORLD); } MPI_Finalize(); } //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$