@@ -57,15 +57,14 @@ public static void parallelMatrixMultiply(Matrix a, Matrix b, Matrix c,
57
57
final int size = mpi .MPI_Comm_size (mpi .MPI_COMM_WORLD );
58
58
59
59
final int nrows = c .getNRows ();
60
- final int ncols = c .getNCols ();
61
60
final int rowChunk = (nrows + size - 1 ) / size ;
62
61
final int startRow = myrank * rowChunk ;
63
- final int endRow = Math .min ((myrank + 1 ) * rowChunk , nrows );
62
+ int endRow = Math .min ((myrank + 1 ) * rowChunk , nrows );
64
63
65
64
mpi .MPI_Bcast (a .getValues (), 0 , a .getNRows () * a .getNCols (), 0 , mpi .MPI_COMM_WORLD );
66
65
mpi .MPI_Bcast (b .getValues (), 0 , b .getNRows () * b .getNCols (), 0 , mpi .MPI_COMM_WORLD );
67
66
68
- for (int i = 0 ; i < c . getNRows () ; i ++) {
67
+ for (int i = 0 ; i < endRow ; i ++) {
69
68
for (int j = 0 ; j < c .getNCols (); j ++) {
70
69
c .set (i , j , 0.0 );
71
70
@@ -76,19 +75,19 @@ public static void parallelMatrixMultiply(Matrix a, Matrix b, Matrix c,
76
75
}
77
76
78
77
if (myrank == 0 ) {
79
- final MPI .MPI_Request [] requests = new MPI .MPI_Request [size - 1 ];
78
+ MPI .MPI_Request [] requests = new MPI .MPI_Request [size - 1 ];
80
79
for (int i = 1 ; i < size ; i ++) {
81
80
final int rankStartRow = i * rowChunk ;
82
81
final int randEndRow = Math .min ((i + 1 ) * rowChunk , nrows );
83
82
84
- final int rowOffset = rankStartRow * ncols ;
85
- final int nElements = (randEndRow - rankStartRow ) * ncols ;
83
+ final int rowOffset = rankStartRow * c . getNCols () ;
84
+ final int nElements = (randEndRow - rankStartRow ) * c . getNCols () ;
86
85
87
86
requests [i - 1 ] = mpi .MPI_Irecv (c .getValues (), rowOffset , nElements , i , i , mpi .MPI_COMM_WORLD );
88
87
}
89
88
mpi .MPI_Waitall (requests );
90
89
} else {
91
- mpi .MPI_Send (c .getValues (), startRow * ncols , (endRow - startRow ) * ncols , 0 , myrank , mpi .MPI_COMM_WORLD );
90
+ mpi .MPI_Send (c .getValues (), startRow * c . getNCols () , (endRow - startRow ) * c . getNCols () , 0 , myrank , mpi .MPI_COMM_WORLD );
92
91
}
93
92
}
94
93
}
0 commit comments