Question: Attached at the bottom is the file Matrix.java, which contains a sequential Matrix multiplication method, seqMult() . Using the methods for concurrency in compute-intensive applications
Attached at the bottom is the file Matrix.java, which contains a sequential Matrix multiplication method, seqMult().
Using the methods for concurrency in compute-intensive applications provided by the book in section 2.2 (ConcurrentPrimeFinder), complete the concMult() multiplication method to do the matrix multiplication. To determining the number of parts to use for concurrency, use a separate part for each element of the destination matrix. (Which means each part will calculate one cell of the result matrix by multiplying a row of the first matrix by a column of the second).
Verify that your concurrent method produces the correct result by demonstrating that the results calling concMult() and seqMult() are the same for two or more test cases. (The results may be printed, and you could also use the Arrays.deepEquals() Java function to compare the results.)
import java.util.concurrent.ExecutionException; public class Matrix { public static void printMatrix(Double[][] M) { int n = M.length; int m = M[0].length; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { System.out.print("\t"+M[i][j]); } System.out.println(); } } public static Double[][] seqMult(Double[][] A, Double[][] B) { int aRows = A.length; int aColumns = A[0].length; int bRows = B.length; int bColumns = B[0].length; if (aColumns != bRows) { throw new IllegalArgumentException("A:Columns: " + aColumns + " did not match B:Rows " + bRows + "."); } Double[][] C = new Double[aRows][bColumns]; for (int i = 0; i < aRows; i++) { for (int j = 0; j < bColumns; j++) { C[i][j] = 0.00000; } } for (int i = 0; i < aRows; i++) { // aRow for (int j = 0; j < bColumns; j++) { // bColumn for (int k = 0; k < aColumns; k++) { // aColumn C[i][j] += A[i][k] * B[k][j]; } } } return C; } public static Double[][] concMult(Double[][] A, Double[][] B) throws ExecutionException, InterruptedException { int aRows = A.length; int aColumns = A[0].length; int bRows = B.length; int bColumns = B[0].length; if (aColumns != bRows) { throw new IllegalArgumentException("A:Columns: " + aColumns + " did not match B:Rows " + bRows + "."); } Double[][] C = new Double[aRows][bColumns]; for (int i = 0; i < aRows; i++) { for (int j = 0; j < bColumns; j++) { C[i][j] = 0.00000; } } // Fill in Concurrent Matrix Multiplication here // // Hint: You may need a Callable of type Void (in other words creating the // anonymous class that contains your work may go something like: // new Callable() ). // Hint2: A function that returns Void in Java requires a return null; return C; } public static void main(final String[] args) throws ExecutionException, InterruptedException { Double[][] A = { { 1.00, 2.00, 3.00 }, { 4.00, 5.00, 6.00 } , { 7.00, 8.00, 9.00 } }; Double[][] B = { { 2.00, 4.00, 6.00 }, { 8.00, 10.00, 12.00 }, { 14.00, 16.00, 18.00 } }; Double[][] C = seqMult(A, B); printMatrix(C); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
