From 43a5b30712cae6de73517a394040f8950a082e50 Mon Sep 17 00:00:00 2001 From: josh Date: Sun, 23 Nov 2008 03:23:55 +0000 Subject: [PATCH] first attempt completed, needs rework to receive multiple columns git-svn-id: svn://anubis/gvsu@245 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs677/hw4/src/transpose.cc | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cs677/hw4/src/transpose.cc b/cs677/hw4/src/transpose.cc index c694f97..f991287 100644 --- a/cs677/hw4/src/transpose.cc +++ b/cs677/hw4/src/transpose.cc @@ -44,6 +44,7 @@ int main(int argc, char * argv[]) MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &p); +#if 0 for (int i = 0; i < argc; i++) { if (!strncmp(argv[i], "-s", 2)) @@ -53,8 +54,13 @@ int main(int argc, char * argv[]) : argv[++i]); } } +#endif + /* Initialize the matrix */ int matrix[n][n]; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + matrix[i][j] = 0; int my_first_row; int my_num_rows; taskAllocate(n, p, my_rank, &my_first_row, &my_num_rows); @@ -66,6 +72,41 @@ int main(int argc, char * argv[]) } } + /* Populate the displacements array */ + int displs[p]; + int counts[p]; + for (int i = 0, total = 0; i < p; i++) + { + int first; + int count; + taskAllocate(n, p, i, &first, &count); + displs[i] = total; + count[i] = count; + total += count; + } + + /* Transpose the matrix with p gather operations */ + int recvbuf[p]; + for (int i = 0; i < p; i++) + { + int col_i_vals_for_proc_p[my_num_rows]; + for (int row_offset = 0; row_offset < my_num_rows; row_offset++) + { + col_i_vals_for_proc_p[row_offset] = + matrix[my_first_row + row_offset][i]; + } + MPI_Gatherv(&col_i_vals_for_proc_p[0], my_num_rows, MPI_INT, + &recvbuf[0], &counts[0], &displs[0], + MPI_INT, MPI_COMM_WORLD); + } + + /* Put my received entries into my columns of the matrix */ + for (int i = 0; i < p; i++) + { + matrix[i][p] = recvbuf[i]; + } + + /* Print out the final matrix */ if (my_rank == 0) { cout << "Final matrix:" << endl;