/* * Open Systems Laboratory * http://www.lam-mpi.org/tutorials/ * Indiana University * * MPI Tutorial * Lab - Datatypes * * Mail questions regarding tutorial material to lam at lam dash mpi dot org */ /* * Create a datatype called submatrix that consists of elements in * alternate rows and alternate columns of the given original matrix. * * Use MPI_SENDRECV to send the submatrix to itself and print the * results. To test this program you can run the program on just * one processor. * * For example, if the given matrix is: * * 1 2 3 4 5 6 * 7 8 9 10 11 12 * 13 14 15 16 17 18 * 19 20 21 22 23 23 * 24 25 26 27 28 29 * * The submatrix created should look like: * * 1 3 5 * 13 15 17 * 24 26 28 * */ #include #include int main(int argc, char *argv[]) { int myrank, size; int i, j, mym = 10, myn = 10; int a[10][10], b[5][5], c[5][5]; MPI_Datatype subrow, submatrix; MPI_Aint sizeofint; MPI_Init (&argc, &argv); MPI_Comm_rank (MPI_COMM_WORLD, &myrank); MPI_Comm_size (MPI_COMM_WORLD, &size); /* Initialize the local array */ for (i = 0; i < mym; ++i) for (j = 0; j < myn; ++j) a[i][j] = i * myn + j + 1; /* Print the local matrix */ if (myrank == 0) { for (i = 0; i < mym; ++i) { for (j = 0; j < myn; ++j) printf("%d ", a[i][j]); printf("\n"); } } /* Create a submatrix datatype */ MPI_Type_extent(MPI_INT, &sizeofint); /* Create datatype for the sub-row */ MPI_Type_vector(5, 1, 2, MPI_INT, &subrow); /* Commit the datatype created */ MPI_Type_commit(&subrow); /* Create datatype for the sub-matrix */ MPI_Type_hvector(5, 1, 20*sizeofint, subrow, &submatrix); /* Commit the datatype created */ MPI_Type_commit(&submatrix); /* Send it to self and print it */ MPI_Sendrecv(a, 1, submatrix, myrank, 0, c, 5 * 5, MPI_INT, myrank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); /* Print the submatrix */ if (myrank == 0) { for (i = 0; i < 5; ++i) { for (j = 0; j < 5; ++j) printf("%d ", c[i][j]); printf("\n"); } } MPI_Finalize(); return 0; }