Changeset 5719 in /cluster/svnroot
 Timestamp:
 Jun 5, 2016 5:02:29 PM (4 years ago)
 Location:
 bccdng/bwinstitute/2016/day06/matmul
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

bccdng/bwinstitute/2016/day06/matmul/Makefile
r5718 r5719 3 3 CC = gcc 4 4 MPICC = cc 5 CFLAGS = 5 ifeq ($(NON_BW),1) 6 MPICC = mpicc 7 endif 8 CFLAGS = 9 ifeq ($(NON_BW),1) 10 CFLAGS = Wall 11 endif 6 12 MPI_FLAGS = D_MPI 7 13 ifeq ($(DEBUG),1) 
bccdng/bwinstitute/2016/day06/matmul/mpi.c
r5717 r5719 9 9 10 10 int main(int argc,char **argv) { 11 int rank,num_ranks,c ;12 unsigned int start_row,stop_row,stride,local_cells,dst_cells;11 int rank,num_ranks,c,*row_counts,*recv_counts,*displs; 12 unsigned int i,start_row,stop_row,row_stride,local_cells,dst_cells; 13 13 bool print = false; 14 14 struct matrix m1,m2,local_dst_m,dst_m; … … 58 58 } 59 59 60 61 60 m1.matrix = safe_malloc_int( 62 61 m1.rows*m1.cols, … … 66 65 m2.rows*m2.cols, 67 66 "Allocating second matrix" 67 ); 68 69 row_counts = safe_malloc_int( 70 num_ranks, 71 "Allocating row_counts array" 72 ); 73 74 recv_counts = safe_malloc_int( 75 num_ranks, 76 "Allocating recv_counts array" 77 ); 78 79 displs = safe_malloc_int( 80 num_ranks, 81 "Allocating displs array" 68 82 ); 69 83 … … 85 99 86 100 if(print) { 87 puts("Matrix 1\n");101 fprintf(stderr,"Matrix 1\n"); 88 102 print_matrix(&m1); 89 puts("");90 puts("Matrix 2\n");103 fprintf(stderr,"\n"); 104 fprintf(stderr,"Matrix 2\n"); 91 105 print_matrix(&m2); 92 puts(""); 106 fprintf(stderr,"\n"); 107 } 108 109 displs[0] = 0; 110 111 for(i=0;i<num_ranks1;i++) { 112 row_counts[i] = dst_m.rows / num_ranks; 113 #ifdef DEBUG 114 fprintf(stderr,"Rank %d  row_counts[%d] is %d\n",rank,i,row_counts[i]); 115 #endif 116 } 117 row_counts[num_ranks1] = dst_m.rows / num_ranks 118 + (dst_m.rows % num_ranks); 119 #ifdef DEBUG 120 fprintf(stderr,"Rank %d  row_counts[%d] is %d\n",rank,(num_ranks1),row_counts[num_ranks1]); 121 #endif 122 123 for(i=0;i<num_ranks;i++) { 124 recv_counts[i] = row_counts[i] * dst_m.cols; 125 displs[i] = 0; 126 } 127 for(i=1;i<num_ranks;i++) { 128 displs[i] = displs[i1] + recv_counts[i1]; 93 129 } 94 130 } … … 102 138 MPI_Bcast(m1.matrix,m1.rows*m1.cols,MPI_INT,FIRST_RANK,MPI_COMM_WORLD); 103 139 MPI_Bcast(m2.matrix,m2.rows*m2.cols,MPI_INT,FIRST_RANK,MPI_COMM_WORLD); 140 MPI_Bcast(row_counts,num_ranks,MPI_UNSIGNED,FIRST_RANK,MPI_COMM_WORLD); 141 MPI_Bcast(recv_counts,num_ranks,MPI_UNSIGNED,FIRST_RANK,MPI_COMM_WORLD); 104 142 105 143 // Calculate row offset in product matrix to start and stop calculation 106 stride = m1.rows/num_ranks; 107 start_row = rank*stride; 108 109 // Assign an even number of rows to each rank, except for the last rank 110 // which gets the remainder (from its start_row to the end of the matrix) 111 if(rank<(num_ranks1)) { 112 stop_row = stride*(rank+1); 144 row_stride = row_counts[rank]; 145 start_row = 0; 146 for(i=0;i<rank;i++) { 147 start_row += row_counts[i]; 113 148 } 114 else { 115 stop_row = m1.rows; 116 } 149 stop_row = start_row+row_stride; 117 150 118 151 // Only need to allocate enough for the local computation 119 152 local_dst_m.cols = m2.cols; 120 local_dst_m.rows = stop_rowstart_row;121 local_cells = local_dst_m.cols*local_dst_m.rows;153 local_dst_m.rows = row_stride; 154 local_cells = recv_counts[i]; 122 155 local_dst_m.matrix = safe_malloc_int( 123 156 local_cells, … … 126 159 127 160 #ifdef DEBUG 128 fprintf(stderr,"Rank %d  Stride %u, start_row %u, stop_row %u\n",129 rank, stride,start_row,stop_row);161 fprintf(stderr,"Rank %d  Row stride %u, start_row %u, stop_row %u\n", 162 rank,row_stride,start_row,stop_row); 130 163 #endif 131 164 matmul(&m1,&m2,&local_dst_m,start_row,stop_row); … … 134 167 fprintf(stderr,"Rank %d local destination matrix:\n",rank); 135 168 print_matrix(&local_dst_m); 136 fprintf(stderr,"Rank %d local size: %u\n",rank,(local_dst_m.cols*local_dst_m.rows));137 169 if(rank == FIRST_RANK) { 138 170 fprintf(stderr,"Rank %d full size: %u\n",rank,(dst_m.cols*dst_m.rows)); 139 171 fprintf(stderr,"Rank %d gathering to %u cells\n",rank,dst_cells); 172 fprintf(stderr,"Displacents:\n"); 173 for(i=0;i<num_ranks;i++) { 174 fprintf(stderr,"%d\n",displs[i]); 175 } 140 176 } 141 177 fprintf(stderr,"Rank %d gathering from %u cells\n",rank,local_cells); 142 178 #endif 143 MPI_Gather (144 &local_dst_m.matrix,179 MPI_Gatherv( 180 local_dst_m.matrix, 145 181 local_cells, 146 182 MPI_INT, 147 &dst_m.matrix, 148 local_cells, 183 dst_m.matrix, 184 recv_counts, 185 displs, 149 186 MPI_INT, 150 187 FIRST_RANK, … … 152 189 ); 153 190 191 if(rank == FIRST_RANK && print) { 192 fprintf(stderr,"Destination matrix:\n"); 193 print_matrix(&dst_m); 194 } 195 154 196 MPI_Finalize(); 155 197
Note: See TracChangeset
for help on using the changeset viewer.