// Argo Beowulf Cluster: MPI Commands and Examples // http://www.uic.edu/depts/accc/hardware/argo/mpi_routines.html // See MPI_Gather for more information // gather.c #include "mpi.h" #include #define elements 4 int main( int argc, char *argv[]) { int numprocs, myrank, loop; int y[elements] = {2, 4,6,8}; int *products; // this is used to collect the answers from each process int sum = 0; int i; //loop counter MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); products = (int *)malloc(sizeof(int)*numprocs); // Allocate space for the // array of answers. if (myrank == 0) printf("\nExample program to demonstrate MPI_Gather\n\n"); MPI_Bcast(y, elements, MPI_INT, 0, MPI_COMM_WORLD); if (myrank == 0) { for(loop=0; loop < elements; loop++) { sum = sum + y[loop]; } printf("In process %d, product=%d\n", myrank, sum); } else { printf("In process %d:\n"); printf("Array y:\n"); for(loop=0; loop < elements; loop++) { printf("\t%d", y[loop]); } printf("\n"); for(loop=0; loop < elements; loop++) { sum = sum + (myrank+1)*y[loop]; } printf("In process %d, dot product=%d\n", myrank, sum); } MPI_Gather(&sum, 1, MPI_INT, products, 1, MPI_INT, 0, MPI_COMM_WORLD); if (myrank==0) { printf("Process 0, products = {"); for(i=0; i