Changeset 5721 in /cluster/svnroot
 Timestamp:
 Jun 5, 2016 5:34:01 PM (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

bccdng/bwinstitute/2016/day06/matmul/mpi.c
r5719 r5721 14 14 struct matrix m1,m2,local_dst_m,dst_m; 15 15 16 // Initiailize MPI 16 17 MPI_Init(&argc,&argv); 18 // num_ranks will be used later to represent the number of processes 17 19 MPI_Comm_size(MPI_COMM_WORLD,&num_ranks); 20 // The position of the current process within num_ranks 18 21 MPI_Comm_rank(MPI_COMM_WORLD,&rank); 19 22 … … 22 25 #endif 23 26 27 // Set to 0 to catch input and runtime errors 24 28 m1.rows = m1.cols = m2.rows = m2.cols = 0; 25 29 … … 53 57 } 54 58 59 // The number of columns in the first matrix must match the number of 60 // rows in the second matrix, due to the matrix multiplication 61 // algorithm 55 62 if(m1.cols != m2.rows) { 56 63 fprintf(stderr,"Invalid matrix dimensions!\n"); … … 107 114 } 108 115 116 // The displacement for rank 0 will always be 0 109 117 displs[0] = 0; 110 118 119 /* 120 * Each cell of row_counts will hold the number of rows the current 121 * rank will process. Each rank except the last one will process the 122 * same number of rows. 123 */ 111 124 for(i=0;i<num_ranks1;i++) { 112 125 row_counts[i] = dst_m.rows / num_ranks; … … 115 128 #endif 116 129 } 130 // The last rank will process the base number of rows, plus the remaining 131 // rows 117 132 row_counts[num_ranks1] = dst_m.rows / num_ranks 118 133 + (dst_m.rows % num_ranks); … … 121 136 #endif 122 137 138 // recv_counts will hold the number of cells each rank will process 123 139 for(i=0;i<num_ranks;i++) { 124 140 recv_counts[i] = row_counts[i] * dst_m.cols; 125 displs[i] = 0; 126 } 141 } 142 // displs will hold the cell displacement for each rank, which 143 // will be equial to the previous rank's displacement plus the number 144 // of cells the previous rank will process 127 145 for(i=1;i<num_ranks;i++) { 128 146 displs[i] = displs[i1] + recv_counts[i1]; … … 143 161 // Calculate row offset in product matrix to start and stop calculation 144 162 row_stride = row_counts[rank]; 163 // The first rank starts at row 0 145 164 start_row = 0; 165 // Each subsequent rank will start at the row equal to the sum of all 166 // the previous ranks' row counts 146 167 for(i=0;i<rank;i++) { 147 168 start_row += row_counts[i]; 148 169 } 170 // Each rank will stop after processing the rows between start_row and 171 // row_stride 149 172 stop_row = start_row+row_stride; 150 173 151 // Only need to allocate enough for the local computation174 // Only need to allocate a matrix big enough for the local computation 152 175 local_dst_m.cols = m2.cols; 153 176 local_dst_m.rows = row_stride; … … 177 200 fprintf(stderr,"Rank %d gathering from %u cells\n",rank,local_cells); 178 201 #endif 202 203 // Gather the local destination matrices into the first rank's 204 // fullsize destination matrix 179 205 MPI_Gatherv( 180 206 local_dst_m.matrix,
Note: See TracChangeset
for help on using the changeset viewer.