// // 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 using namespace std; 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, lowerbound; MPI::Init (argc, argv); myrank = MPI::COMM_WORLD.Get_rank(); size = MPI::COMM_WORLD.Get_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) cout << a[i][j] << " "; cout << endl; } } // Create a submatrix datatype MPI::INT.Get_extent(lowerbound, sizeofint); // Create datatype for the sub-row subrow = MPI::INT.Create_vector(5, 1, 2); // Commit the datatype created subrow.Commit(); // Create datatype for the sub-matrix submatrix = subrow.Create_hvector(5, 1, 20*sizeofint); // Commit the datatype created submatrix.Commit(); // Send it to self and print it MPI::COMM_WORLD.Sendrecv(a, 1, submatrix, myrank, 0, c, 5 * 5, MPI::INT, myrank, 0); // Print the submatrix if (myrank == 0) { for (i = 0; i < 5; ++i) { for (j = 0; j < 5; ++j) cout << c[i][j] << " "; cout << endl; } } MPI::Finalize(); return 0; }