## Transcribed Text

Project 2 Java Multithreading for Matrix
Multiplication
These are the instructions for your second Operating Systems programming project. Recall that
all code must be written individually and independently. Recall that violating this rule will
result in zero credit for the project and probably a failing grade in the course.
Write a multithreaded program in Java or C++ that performs matrix multiplication. Specifically,
use two matrices, A and B, where A is a matrix with M rows and K columns and matrix B
contains K rows and N columns (the column number of A must be equal to the row number ofB).
The matrix product of A and B is C, where C contains M rows and N columns. The entry in
matrix C for row i column j (C;j) is the sum of the products of elements for row i in matrix A and
column j in matrix B. That is
k
C.. L(A. xB .) 1,1 n=l
1,n n ,J
For example, if A were a 3-by-2 matrix and B were a 2-by-3 matrix, element C3,1 would be the
sum of A
3 , 1 x B1 , 1 and A3 , 2 x B2 . 1 .
For this project, you need to calculate each element CJ in a single worker thread. This will
involve creating totally M*N threads in parallel. The main or parent thread will initialize the
matrices A and B and allocate sufficient memory for matrix C, which will hold the product of
matrices A and B. These matrices will be declared as global data so that each worker thread has
access to A, B, and C.
#define M 3
#define K 2
#define N 3
Then you use a random generator to uniformly generate the element values of matrices A and B
in range of [O, 100]. Then use A and B to calculate tbe product matrix C.
Once ali worker threads have completed, the main thread will output the product contained in
matrices A, B, and C. This requires the main thread to wait for ali worker threads to finish before
it can output the value ofthe matrix product. You could use pthreadjoin() or join() for Pthread
(C++) or Java, respectively.
#define NUM_ THREADS 10
r an array of threads to be joined upon */
pthread_t workers[NUM_ THREADS];
for (int i=O; i< NUM_ THREADS; i++)

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.

//Program Name: Project2.java

//Programmer: <Your name here>, <ID>

//Assignment Number: Project #2

//Purpose: Perform matrix multiplication using Java Thread.

/*

This class takes 5 parameters to construct.

It fills entry (row, column) in matrix C.

*/

class Worker extends Thread {

private int row, column;

private int[][] A;

private int[][] B;

private int[][] C;

public Worker(int[][] A, int[][] B, int[][] C, int row, int column) {

this.A = A;

this.B = B;

this.C = C;

this.row = row;

this.column = column;

}

public void run() {

int K = B.length;

//Fill in entry (row, col) at matrix C

C[this.row][this.column] = 0;

for (int i = 0; i < K; i++)

C[this.row][this.column] += A[this.row][i] * B[i][this.column];

System.out.println("Thread for calculating entry (" + this.row + ", " + this.column + ") is done");

}

}...