Skip to content

Commit 0ddba2a

Browse files
committed
complete
1 parent 5a62d67 commit 0ddba2a

File tree

1 file changed

+6
-7
lines changed
  • Distributed Programming/miniproject_3/src/main/java/edu/coursera/distributed

1 file changed

+6
-7
lines changed

Distributed Programming/miniproject_3/src/main/java/edu/coursera/distributed/MatrixMult.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ public static void parallelMatrixMultiply(Matrix a, Matrix b, Matrix c,
5757
final int size = mpi.MPI_Comm_size(mpi.MPI_COMM_WORLD);
5858

5959
final int nrows = c.getNRows();
60-
final int ncols = c.getNCols();
6160
final int rowChunk = (nrows + size - 1) / size;
6261
final int startRow = myrank * rowChunk;
63-
final int endRow = Math.min((myrank + 1) * rowChunk, nrows);
62+
int endRow = Math.min((myrank + 1) * rowChunk, nrows);
6463

6564
mpi.MPI_Bcast(a.getValues(), 0, a.getNRows() * a.getNCols(), 0, mpi.MPI_COMM_WORLD);
6665
mpi.MPI_Bcast(b.getValues(), 0, b.getNRows() * b.getNCols(), 0, mpi.MPI_COMM_WORLD);
6766

68-
for (int i = 0; i < c.getNRows(); i++) {
67+
for (int i = 0; i < endRow; i++) {
6968
for (int j = 0; j < c.getNCols(); j++) {
7069
c.set(i, j, 0.0);
7170

@@ -76,19 +75,19 @@ public static void parallelMatrixMultiply(Matrix a, Matrix b, Matrix c,
7675
}
7776

7877
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];
8079
for (int i = 1; i < size; i++) {
8180
final int rankStartRow = i * rowChunk;
8281
final int randEndRow = Math.min((i + 1) * rowChunk, nrows);
8382

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();
8685

8786
requests[i - 1] = mpi.MPI_Irecv(c.getValues(), rowOffset, nElements, i, i, mpi.MPI_COMM_WORLD);
8887
}
8988
mpi.MPI_Waitall(requests);
9089
} 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);
9291
}
9392
}
9493
}

0 commit comments

Comments
 (0)