Changeset 5727 in /cluster/svnroot
 Timestamp:
 Jun 5, 2016 10:34:04 PM (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

bccdng/bwinstitute/2016/day06/matmul/mpi.c
r5724 r5727 18 18 unsigned int 19 19 i, // Loop index 20 start_row, // Local row to start at 21 stop_row, // Local row to end at 22 row_stride, // Number of rows between start and stop row 20 23 local_cells, // Number of cells between start and stop row 21 24 dst_cells; // Total number of cells in destination matrix 22 bool 23 print = false; // Controls whether to print matrices to STDERR 25 bool print = false; // Controls whether to print matrices to STDERR 24 26 struct matrix 25 m1, // Full copy of input matrix 1 26 m2, // Full copy of input matrix 2 27 local_m1, // Local (smaller) copy of input matrix 1 28 local_m2, // Local (smaller) copy of input matrix 2 27 m1, // Local copy of matrix 1 28 m2, // Local copy of matrix 2 29 29 local_dst_m, // Local copy of destination matrix 30 30 dst_m; // Final copy of destination matrix … … 81 81 } 82 82 83 m1.matrix = safe_malloc_int( 84 m1.rows*m1.cols, 85 "Allocating first matrix" 86 ); 87 m2.matrix = safe_malloc_int( 88 m2.rows*m2.cols, 89 "Allocating second matrix" 90 ); 91 83 92 row_counts = safe_malloc_int( 84 93 num_ranks, … … 99 108 // Each thread will get a separate random seed 100 109 unsigned int *random_seeds = init_random_seeds(); 101 102 m1.matrix = safe_malloc_int(103 m1.rows*m1.cols,104 "Allocating first matrix"105 );106 m2.matrix = safe_malloc_int(107 m2.rows*m2.cols,108 "Allocating second matrix"109 );110 111 110 init_matrix(&m1,random_seeds); 112 111 init_matrix(&m2,random_seeds); … … 171 170 MPI_Bcast(&m2.rows,1,MPI_INT,FIRST_RANK,MPI_COMM_WORLD); 172 171 MPI_Bcast(&m2.cols,1,MPI_INT,FIRST_RANK,MPI_COMM_WORLD); 172 MPI_Bcast(m1.matrix,m1.rows*m1.cols,MPI_INT,FIRST_RANK,MPI_COMM_WORLD); 173 MPI_Bcast(m2.matrix,m2.rows*m2.cols,MPI_INT,FIRST_RANK,MPI_COMM_WORLD); 173 174 MPI_Bcast(row_counts,num_ranks,MPI_UNSIGNED,FIRST_RANK,MPI_COMM_WORLD); 174 175 MPI_Bcast(recv_counts,num_ranks,MPI_UNSIGNED,FIRST_RANK,MPI_COMM_WORLD); 175 176 176 // Initialize local source matrices dimensions 177 local_m1.rows = row_counts[rank]; 178 local_m2.rows = row_counts[rank]; 179 local_m1.cols = m1.cols; 180 local_m2.cols = m2.cols; 181 182 // Allocate memory for local source matrices 183 local_m1.matrix = safe_malloc_int( 184 local_m1.rows*local_m1.cols, 185 "Allocating first local matrix" 186 ); 187 local_m2.matrix = safe_malloc_int( 188 local_m2.rows*local_m2.cols, 189 "Allocating second local matrix" 190 ); 191 192 // Only need to allocate a destination matrix big enough 193 // for the local computation 177 178 179 // Calculate row offset in product matrix to start and stop calculation 180 row_stride = row_counts[rank]; 181 // The first rank starts at row 0 182 start_row = 0; 183 // Each subsequent rank will start at the row equal to the sum of all 184 // the previous ranks' row counts 185 for(i=0;i<rank;i++) { 186 start_row += row_counts[i]; 187 } 188 // Each rank will stop after processing the rows between start_row and 189 // row_stride 190 stop_row = start_row+row_stride; 191 192 // Only need to allocate a matrix big enough for the local computation 194 193 local_dst_m.cols = m2.cols; 195 local_dst_m.rows = row_ counts[rank],194 local_dst_m.rows = row_stride; 196 195 local_cells = recv_counts[i]; 197 196 local_dst_m.matrix = safe_malloc_int( … … 200 199 ); 201 200 202 MPI_Scatterv( 203 &m1.matrix, // Matrix to scatter 204 recv_counts, // Number of cells each rank receives 205 displs, // Displacement within the matrix at which to begin scatter 206 // for each rank 207 MPI_INT, 208 &local_m1.matrix, 209 recv_counts[rank], 210 MPI_INT, 211 FIRST_RANK, 212 MPI_COMM_WORLD); 213 214 MPI_Scatterv( 215 &m2.matrix, // Matrix to scatter 216 recv_counts, // Number of cells each rank receives 217 displs, // Displacement within the matrix at which to begin scatter 218 // for each rank 219 MPI_INT, 220 &local_m2.matrix, 221 recv_counts[rank], 222 MPI_INT, 223 FIRST_RANK, 224 MPI_COMM_WORLD); 225 226 matmul(&m1,&m2,&local_dst_m,0,m1.rows); 201 #ifdef DEBUG 202 fprintf(stderr,"Rank %d  Row stride %u, start_row %u, stop_row %u\n", 203 rank,row_stride,start_row,stop_row); 204 #endif 205 matmul(&m1,&m2,&local_dst_m,start_row,stop_row); 227 206 228 207 #ifdef DEBUG
Note: See TracChangeset
for help on using the changeset viewer.