DAA Mini Project
DAA Mini Project
ON
“Multithreading Matrix Multiplication”
Of
BE(Computer Engineering)
Academic Year:2023-2024
Submitted by
Roll No: B212058
Name: Ajinkya Somawanshi
Guided By
Asst. Prof. Nitisha Rajgure
CERTIFICATE
This is to certify that the mini project entitles
“Multithreading Matrix Multiplication”
Submitted by
Ajinkya Somawanshi
is bonafide students of this institute and the work has been carried out by me under the
supervision of Prof. Nitisha Rajgure and it is approved for the partial fulfillment of the
requirement of Savitribai Phule Pune University, for the award of the degree of Bachelor of
Engineering (Computer Engineering).
Place: Pune
Date:
Our great obligation would remain towards our Head of Department Prof. Aparna
Mote, whose contribution in stimulating suggestions and encouragement helped us for
writing report. She provided with an opportunity to undertake the Mini Project at Zeal
College of Engineering & Research, Narhe, Pune. We appreciate the guidance given by
other staff members of Computer Engineering Department forimproving our presentation
skills thanks to their comment and advice.
We sincerely thanks to our respected Principal proved to be a constant motivation for
the knowledgeacquisition and moral support during our course curriculum.
Abstract 1
1. Introduction 2
2.2 Aim
2.3 Objective
3. Implementation 4
3.1 Algorithm
3.2 Flowchart
4. Analysis 9
5. Conclusion 10
1
1. INTRODUCTION
Matrix Multiplication
Matrix multiplication, also known as matrix product and the multiplication of two matrices,
produces a single matrix. It is a type of binary operation.
If A and B are the two matrices, then the product of the two matrices A and B are denoted by:
X = AB
Hence, the product of two matrices is the dot product of the two matrices.
2.2 Objective
1. Develop a program that implements matrix multiplication using a sequential
approach.
2. Analyse and compare the performance of the sequential and multithreaded
approaches in terms of execution time and resource utilization.
3
ZCOER, Department of Computer Engineering 2023-24
3. IMPLEMENTATION
3.1 Algorithm
1. Start with two input matrices, A and B, of dimensions (m x n) and (n x p)
respectively.
2. Initialize an output matrix, C, of dimensions (m x p) to store the result.
3. Perform a nested loop over the rows of A and the columns of B:
• Iterate i from 0 to m-1 (rows of A).
• Iterate j from 0 to p-1 (columns of B).
• Iterate k from 0 to n-1 (columns of A or rows of B).
• Update C[i][j] by adding the product of A[i][k] and B[k][j].
4. Return the resulting matrix C.
4
ZCOER, Department of Computer Engineering 2023-24
3.2 Flowchart
5
ZCOER, Department of Computer Engineering 2023-24
3.3 Source Code
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
// Function to multiply two matrices using multithreading with one thread per row
public static int[][] multiplyMatricesWithThreadPerRow(int[][] matA, int[][] matB)
{
int m = matA.length;
int p = matB[0].length;
int[][] result = new int[m][p];
executor.shutdown();
try {
6
ZCOER, Department of Computer Engineering 2023-24
executor.awaitTermination(Long.MAX_VALUE,
TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
// Function to multiply two matrices using multithreading with one thread per cell
public static int[][] multiplyMatricesWithThreadPerCell(int[][] matA, int[][] matB)
{
int m = matA.length;
int p = matB[0].length;
int[][] result = new int[m][p];
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE,
TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
7
ZCOER, Department of Computer Engineering 2023-24
// Print results
System.out.println("Sequential Result:");
printMatrix(resultSequential);
System.out.println("Result with Thread per Row:");
printMatrix(resultThreadPerRow);
System.out.println("Result with Thread per Cell:");
printMatrix(resultThreadPerCell);
}
OUTPUT :
8
ZCOER, Department of Computer Engineering 2023-24
4. ANALYSIS
The time complexity analysis for matrix multiplication can be broken down as follows:
1. Sequential Matrix Multiplication:
In the sequential approach, the time complexity of matrix multiplication for two
matrices of dimensions A(m x n) and B(n x p) is O(mnp).
9
ZCOER, Department of Computer Engineering 2023-24
5. CONCLUSION
Hence, we have successfully implemented and analysed matrix multiplication. And
it demonstrates that while the sequential matrix multiplication algorithm is effective
for small to medium-sized matrices, the integration of multithreading significantly
enhances the computational efficiency for larger matrices.
10
ZCOER, Department of Computer Engineering 2023-24