#include #include const int MAXITEMS = 10; int main(int argc, char **argv) { int size; int rank; int value=34567; int root=0; int array[MAXITEMS]; int i; int anytag =0; int sum, totalsum; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank == 0) { srand(time(0)); for (i=0; i < MAXITEMS; i++) { array[i] = rand() % 100; } printf("Here's the entire array from process %d: (starts on address %u)\n", rank, array); for (i=0; i < MAXITEMS; i++) { printf("%d ", array[i]); } printf("\n"); //Send the array to each process other then process 0 (root process) for (i=1; i < size; i++) { MPI_Send(array, MAXITEMS, MPI_INT, i, anytag, MPI_COMM_WORLD); } } else { MPI_Recv(array, MAXITEMS, MPI_INT, 0, anytag, MPI_COMM_WORLD, &status); printf("Hello from process %d out of %d Received:\n", rank, size); printf("(starts on address %u)\n", array); for (i=0; i < MAXITEMS; i++) { printf("%d ", array[i]); } printf("\n"); } sum = 0; for (i=0; i < MAXITEMS; i++) { sum = sum + array[i]; } MPI_Reduce(&sum, &totalsum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); printf("Process: %d, Sum = %d\n", rank, sum); if (rank == 0) printf("*** Reporting from ROOT: Totalsum = %d ***\n", totalsum); MPI_Finalize(); return 0; } /* mpirun N sendArray2 Here's the entire array from process 0: (starts on address 3221223488) 44 60 28 40 71 59 2 96 5 60 Hello from process 2 out of 6 Received: (starts on address 3221223744) 44 60 28 40 71 59 2 96 5 60 Hello from process 1 out of 6 Received: (starts on address 3221223744) 44 60 28 40 71 59 2 96 5 60 Process: 1, Sum = 465 Hello from process 3 out of 6 Received: (starts on address 3221223744) 44 60 28 40 71 59 2 96 5 60 Process: 3, Sum = 465 Process: 2, Sum = 465 Hello from process 5 out of 6 Received: (starts on address 3221223744) 44 60 28 40 71 59 2 96 5 60 Process: 5, Sum = 465 Process: 0, Sum = 465 *** Reporting from ROOT: Totalsum = 2790 *** Hello from process 4 out of 6 Received: (starts on address 3221223744) 44 60 28 40 71 59 2 96 5 60 Process: 4, Sum = 465 */