#include #include #define ARRAYSIZE 300 #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 */ pares, 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 */ int data[ARRAYSIZE], /* the intial array */ result[ARRAYSIZE]; /* for holding results of array operations */ MPI_Status status; srand ( time(NULL) ); 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 ("\nMPI task ID = %d\n", taskid); fflush(stdout); //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); fflush(stdout); 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_INT, source, arraymsg, MPI_COMM_WORLD, &status); printf("Escravo %d: recebeu tarefa\n", taskid); fflush(stdout); // Do a simple value assignment to each of my array elements for(i=index; i< index + chunksize; i++) { if((result[i] % 2) == 0) result[i] = 1; else result[i] = 0; } // 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_INT, MASTER, arraymsg, MPI_COMM_WORLD); printf("Escravo %d: finalizou tarefa\n", taskid); fflush(stdout); } MPI_Finalize(); }