diff --git a/cs677/hw4/src/transpose.cc b/cs677/hw4/src/transpose.cc index 361033d..a81bbee 100644 --- a/cs677/hw4/src/transpose.cc +++ b/cs677/hw4/src/transpose.cc @@ -1,8 +1,6 @@ #include #include -#include /* gethostname() */ -#include /* rand() */ #include using namespace std; @@ -56,11 +54,13 @@ int main(int argc, char * argv[]) } #endif - /* Initialize the matrix */ + /* Initialize the matrices */ int matrix[n][n]; - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) + for (int i = 0; i < n; i++) /* zero the matrices for prettier */ + for (int j = 0; j < n; j++) /* printing */ matrix[i][j] = 0; + + /* Determine which rows I am responsible for */ int my_first_row; int my_num_rows; taskAllocate(n, p, my_rank, &my_first_row, &my_num_rows); @@ -85,25 +85,21 @@ int main(int argc, char * argv[]) total += count; } - /* Transpose the matrix with p gather operations */ - int recvbuf[p]; - for (int i = 0; i < p; i++) + /* Transpose the matrix with n gather operations */ + for (int i = 0, toproc = 0, proccount = counts[0]; i < n; i++) { - int col_i_vals_for_proc_p[my_num_rows]; + int my_col_i_vals[my_num_rows]; for (int row_offset = 0; row_offset < my_num_rows; row_offset++) + my_col_i_vals[row_offset] = matrix[my_first_row + row_offset][i]; + MPI_Gatherv(&my_col_i_vals[0], my_num_rows, MPI_INT, + &matrix[i][0], &counts[0], &displs[0], + MPI_INT, toproc, MPI_COMM_WORLD); + proccount--; + if (proccount <= 0) { - col_i_vals_for_proc_p[row_offset] = - matrix[my_first_row + row_offset][i]; + toproc++; + proccount = ccounts[toproc]; } - MPI_Gatherv(&col_i_vals_for_proc_p[0], my_num_rows, MPI_INT, - &recvbuf[0], &counts[0], &displs[0], - MPI_INT, i, MPI_COMM_WORLD); - } - - /* Put my received entries into my columns of the matrix */ - for (int i = 0; i < p; i++) - { - matrix[i][my_rank] = recvbuf[i]; } /* Print out the final matrix */ @@ -114,7 +110,7 @@ int main(int argc, char * argv[]) { for (int j = 0; j < n; j++) { - cout << matrix[i][j] << " "; + cout << recvmatrix[i][j] << " "; } cout << endl; }