Changeset 5721 in /cluster/svnroot


Ignore:
Timestamp:
Jun 5, 2016 5:34:01 PM (4 years ago)
Author:
skylar
Message:

comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • bccd-ng/bw-institute/2016/day06/matmul/mpi.c

    r5719 r5721  
    1414    struct matrix m1,m2,local_dst_m,dst_m;
    1515
     16    // Initiailize MPI
    1617    MPI_Init(&argc,&argv);
     18    // num_ranks will be used later to represent the number of processes
    1719    MPI_Comm_size(MPI_COMM_WORLD,&num_ranks);
     20    // The position of the current process within num_ranks
    1821    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    1922
     
    2225#endif
    2326
     27    // Set to 0 to catch input and runtime errors
    2428    m1.rows = m1.cols = m2.rows = m2.cols = 0;
    2529
     
    5357    }
    5458
     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
    5562    if(m1.cols != m2.rows) {
    5663        fprintf(stderr,"Invalid matrix dimensions!\n");
     
    107114        }
    108115
     116        // The displacement for rank 0 will always be 0
    109117        displs[0] = 0;
    110118
     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         */
    111124        for(i=0;i<num_ranks-1;i++) {
    112125            row_counts[i] = dst_m.rows / num_ranks;
     
    115128#endif
    116129        }
     130        // The last rank will process the base number of rows, plus the remaining
     131        // rows
    117132        row_counts[num_ranks-1] = dst_m.rows / num_ranks
    118133            + (dst_m.rows % num_ranks);
     
    121136#endif
    122137
     138        // recv_counts will hold the number of cells each rank will process
    123139        for(i=0;i<num_ranks;i++) {
    124140            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
    127145        for(i=1;i<num_ranks;i++) {
    128146            displs[i] = displs[i-1] + recv_counts[i-1];
     
    143161    // Calculate row offset in product matrix to start and stop calculation
    144162    row_stride              = row_counts[rank];
     163    // The first rank starts at row 0
    145164    start_row               = 0;
     165    // Each subsequent rank will start at the row equal to the sum of all
     166    // the previous ranks' row counts
    146167    for(i=0;i<rank;i++) {
    147168        start_row += row_counts[i];
    148169    }
     170    // Each rank will stop after processing the rows between start_row and
     171    // row_stride
    149172    stop_row                = start_row+row_stride;
    150173
    151     // Only need to allocate enough for the local computation
     174    // Only need to allocate a matrix big enough for the local computation
    152175    local_dst_m.cols        = m2.cols;
    153176    local_dst_m.rows        = row_stride;
     
    177200    fprintf(stderr,"Rank %d gathering from %u cells\n",rank,local_cells);
    178201#endif
     202
     203    // Gather the local destination matrices into the first rank's
     204    // full-size destination matrix
    179205    MPI_Gatherv(
    180206            local_dst_m.matrix,
Note: See TracChangeset for help on using the changeset viewer.