#include #include const int MAXROWS = 5; const int MAXCOLS = 5; int main(int argc, char **argv) { int size; int rank; int value=34567; int root=0; int matrix[MAXROWS][MAXCOLS]; int i, j; int anytag =0; int sum, totalsum, rowToSend; 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 < MAXROWS; i++) { for (j = 0; j < MAXCOLS; j++) { matrix[i][j] = rand() % 100; } } printf("Here's the entire array from process %d: (starts on address %u)\n", rank, matrix); for (i=0; i < MAXROWS; i++) { for (j = 0; j < MAXCOLS; j++) { printf("%5d ", matrix[i][j]); } printf("\n"); } printf("\n"); //Send the array to each process other then process 0 (root process) for (i=1; i < size; i++) { rowToSend = i % MAXROWS; MPI_Send(&rowToSend, 1, MPI_INT, i, anytag, MPI_COMM_WORLD); MPI_Send(matrix[rowToSend], MAXCOLS, MPI_INT, i, anytag, MPI_COMM_WORLD); } } else { MPI_Recv(&rowToSend, MAXCOLS, MPI_INT, 0, anytag, MPI_COMM_WORLD, &status); MPI_Recv(matrix[rowToSend], MAXCOLS, 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", matrix); printf("Here's row %d from process %d:\n", rowToSend, rank); for (j = 0; j < MAXCOLS; j++) { printf("%5d ", matrix[rowToSend][j]); } printf("\n"); } MPI_Finalize(); return 0; } /* mpirun N sendMatrix2 Here's the entire array from process 0: (starts on address 3221223408) 34 7 80 32 14 35 66 61 60 37 69 89 53 83 2 14 81 46 86 35 3 7 24 86 72 Hello from process 1 out of 6 Received: (starts on address 3221223664) Here's row 1 from process 1: 35 66 61 60 37 Hello from process 2 out of 6 Received: (starts on address 3221223664) Here's row 2 from process 2: 69 89 53 83 2 Hello from process 3 out of 6 Received: (starts on address 3221223664) Here's row 3 from process 3: 14 81 46 86 35 Hello from process 4 out of 6 Received: (starts on address 3221223664) Here's row 4 from process 4: 3 7 24 86 72 Hello from process 5 out of 6 Received: (starts on address 3221223664) Here's row 0 from process 5: 34 7 80 32 14 */