Matrix multiplication theory : Given two matrices, A and B, where matrix A conta
ID: 3672473 • Letter: M
Question
Matrix multiplication theory: Given two matrices, A and B, where matrix A contains m rows and k columns and matrix B contains k rows and n columns, the matrix product of A and B is matrix P, where P contains m rows and n columns. Entries in matrix P for row i and column j is the sum of the products of the elements for i throw in matrix A and jth column in matrix B.
Multi-threaded matrix multiplication: In this problem, you are required to write a multi-threaded program for matrix product. Calculate each cell of the product matrix P in a separate worker thread. This will involve creating m × n worker threads. The main (or, parent) thread will initialize the matrices A and B and allocate space to hold the matrix P. Ideally, these matrices should be declared as global data so that each worker thread has access to these matrices. The parent thread will create m × n worker threads, passing each worker the values of row i and column j that it uses in calculating the matrix product. Once all worker threads have completed, the main thread will output the product contained in matrix P. This requires the main thread to wait for all worker threads to finish before it can output the value of the matrix product.
Programming instruction: your program should read the data from a file. The file name is provided as command-line argument at execution time. The data file format is as follows: first line has 4 integer numbers, separated by single spaces, specifying dimension of two matrices. For example, 3 2 2 3 means matrix A has dimension 3 × 2 and matrix B has dimension 2 × 3. Your program should check, at the beginning, whether two matrices are eligible for multiplication (that is, the middle two numbers are equal or not) and indicate it with appropriate message. The program then reads the matrices (see the sample data file MatrixData.txt), completes calculation as specified above, and produces the output on console in a matrix format (standard row-column format). You can assume that the data file will have integer value matrices only. However, the given data file is just a sample. Instructor may test your program using another, but similar, data file where the matrices have larger dimensions.
Important requirement: For each worker thread, display the begin of the thread as “Thread i starts calculating..” and then display the value it produces before the thread ends with “Thread i returns value ”. Here i is the thread index. The final product matrix should be displayed on the terminal in proper matrix (rowcolumn) form.
HERE IS THE TEXT FILE..
Matrix Data.txt:
6 7 7 8
1 0 -1 0 1 -1 0
0 1 1 -1 0 0 1
-1 1 0 0 0 1 -1
0 1 1 0 1 0 -1
1 -1 0 1 0 -1 0
-1 0 0 -1 0 -1 1
2 0 1 3 0 2 3 1
1 0 3 2 1 2 3 0
0 1 2 3 2 3 0 1
3 2 1 0 3 2 1 0
2 1 0 3 0 2 1 3
0 1 2 3 0 1 2 3
1 2 3 0 3 2 1 0
OUTPUT SHOULD LOOK LIKE :
4 -1 -3 0 -2 0 2 0
-1 1 7 5 3 5 3 1
-2 -1 1 2 -2 -1 1 2
2 0 2 8 0 5 3 4
4 1 -3 -2 2 1 -1 -2
-4 -1 -1 -6 0 -3 -5 -4
Explanation / Answer
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
int p, q, r, s;
Integer[][] A, B, C;
Scanner scanner = new Scanner(new File("MatrixData.txt"));
p = scanner.nextInt();
q = scanner.nextInt();
r = scanner.nextInt();
s = scanner.nextInt();
if (q == r) {
System.out.println("Matrices are eligible for multiplication!");
} else {
System.out.println("Matrices are not eligible for multiplication!");
return;
}
A = new Integer[p][q];
for (int i = 0; i < p; ++i) {
for (int j = 0; j < q; ++j) {
A[i][j] = scanner.nextInt();
}
}
B = new Integer[r][s];
for (int i = 0; i < r; ++i) {
for (int j = 0; j < s; ++j) {
B[i][j] = scanner.nextInt();
}
}
C = new Integer[p][s];
for (int i = 0; i < p; ++i) {
Arrays.fill(C[i], 0);
}
for (int i = 0; i < p; ++i) {
for (int j = 0; j < s; ++j) {
Thread t = new Thread(new MatrixMultiplier(A, B, C, i, j, q));
t.setName(Integer.toString(i * p + j + 1));
t.start();
}
}
Thread.sleep(10000);
for (int i = 0; i < p; ++i) {
for (int j = 0; j < s; ++j) {
System.out.print(C[i][j] + " ");
}
System.out.println();
}
}
private static class MatrixMultiplier implements Runnable {
private Integer[][] A, B, C;
private int row, col, middle;
public MatrixMultiplier(Integer[][] A, Integer[][] B, Integer[][] C, int i, int j, int k) {
this.A = A;
this.B = B;
this.C = C;
this.row = i;
this.col = j;
this.middle = k;
}
@Override
public void run() {
System.out.println("Thread " + Thread.currentThread().getName() + " starts calculating..");
for (int k = 0; k < middle; ++k) {
C[row][col] += A[row][k] * B[k][col];
}
}
}
}