0% found this document useful (0 votes)
73 views

DAA Mini Project

This document is a mini project report on implementing multithreaded matrix multiplication. It aims to develop matrix multiplication using both sequential and multithreaded techniques, with one thread per row and one thread per cell. It analyzes and compares the performance of these approaches in terms of execution time and resource utilization. The document includes an introduction, aim and objectives, implementation details with algorithms and source code, and plans for analysis and conclusion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

DAA Mini Project

This document is a mini project report on implementing multithreaded matrix multiplication. It aims to develop matrix multiplication using both sequential and multithreaded techniques, with one thread per row and one thread per cell. It analyzes and compares the performance of these approaches in terms of execution time and resource utilization. The document includes an introduction, aim and objectives, implementation details with algorithms and source code, and plans for analysis and conclusion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

A Mini Project Report

ON
“Multithreading Matrix Multiplication”
Of
BE(Computer Engineering)
Academic Year:2023-2024

Zeal Education Society’s


Zeal College of Engineering & Research Narhe, Pune-411041
Department of Computer Engineering

Submitted by
Roll No: B212058
Name: Ajinkya Somawanshi

Guided By
Asst. Prof. Nitisha Rajgure

ZCOER, Department of Computer Engineering 2023-24


SAVITRIBAI PHULE PUNE UNIVERSITY 2023-2024

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).

Asst. Prof. Nitisha Rajgure Prof. Aparna V. Mote


Guide Head
Department of Computer Engineering Department of Computer Engineering

Place: Pune
Date:

ZCOER, Department of Computer Engineering 2023-24


Acknowledgement
We would like to express our deepest appreciation to all those who provided us the
possibility to completethis report. A special gratitude we will give to our mini project guide,
Prof. Nitisha Rajgure who invested her full effort in guiding us in achieving the goal.

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.

Name : Ajinkya Somawanshi


Class: BE B
Roll No: B212058

ZCOER, Department of Computer Engineering 2023-24


INDEX

Sr. No Page No.

Abstract 1

1. Introduction 2

2. Aim & Objective 3

2.2 Aim

2.3 Objective

3. Implementation 4

3.1 Algorithm

3.2 Flowchart

3.3 Source Code

4. Analysis 9

5. Conclusion 10

ZCOER, Department of Computer Engineering 2023-24


i. List of figures

Sr. No Figure Page No.


1. Flowchart of Matrix Multiplication 5
2. Output of Implemented Code 8

ZCOER, Department of Computer Engineering 2023-24


ABSTRACT
Multiplication of matrices is a key operation in many scientific and engineering
applications. The goal of this project is to develop matrix multiplication using both a
sequential and a multithreaded technique. The programme begins by demonstrating the
traditional approach of matrix multiplication. Following that, it provides two parallel
techniques, one using one thread per row and the other using one thread per cell. In
terms of execution time and resource utilisation, the project analyses and evaluates the
performance of these approaches. This comparative analysis will shed light on the
advantages and disadvantages of multithreading in matrix multiplication, as well as
provide insights into the effectiveness of various parallelization algorithms for this
computational activity. The findings of this study will help researchers better grasp the
practical ramifications of using multithreading in computationally intensive
applications.

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.

ZCOER, Department of Computer Engineering 2023-24 2


2. AIM & OBJECTIVE
2.1 Aim
Write a program to implement matrix multiplication. Also implement multithreaded
matrix multiplication with either one thread per row or one thread per cell. Analyse
and compare their performance.

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.

For multithreading with one thread per row:


1. Create a thread pool with a fixed number of threads equal to the number of rows in
matrix A.
2. For each row in A, assign a thread to perform the multiplication of that row with
matrix B.
3. Update the corresponding row in the result matrix.
4. Await the termination of all threads.

For multithreading with one thread per cell:


1. Create a thread pool with a fixed number of threads equal to the total number of
cells in the result matrix.
2. Assign a thread for each cell in the result matrix.
3. Calculate the value of each cell by performing the required multiplications from the
corresponding row of A and column of B.
4. Await the termination of all threads.

4
ZCOER, Department of Computer Engineering 2023-24
3.2 Flowchart

Fig. 1 Flowchart of Matrix Multiplication

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;

public class MatrixMultiplication {

// Function to multiply two matrices


public static int[][] multiplyMatrices(int[][] matA, int[][] matB) {
int m = matA.length;
int n = matA[0].length;
int p = matB[0].length;

int[][] result = new int[m][p];

for (int i = 0; i < m; i++) {


for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
result[i][j] += matA[i][k] * matB[k][j];
}
}
}
return result;
}

// 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];

ExecutorService executor = Executors.newFixedThreadPool(m);


for (int i = 0; i < m; i++) {
int finalI = i;
executor.execute(() -> {
for (int j = 0; j < p; j++) {
for (int k = 0; k < matA[0].length; k++) {
result[finalI][j] += matA[finalI][k] * matB[k][j];
}
}
});
}

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];

ExecutorService executor = Executors.newFixedThreadPool(m * p);

for (int i = 0; i < m; i++) {


for (int j = 0; j < p; j++) {
int finalI = i;
int finalJ = j;
executor.execute(() -> {
for (int k = 0; k < matA[0].length; k++) {
result[finalI][finalJ] += matA[finalI][k] * matB[k][finalJ];
}
});
}
}

executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE,
TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}

// Main method for demonstration


public static void main(String[] args) {
int[][] matA = {{7, 27}, {3, 4}};
int[][] matB = {{15, 6}, {7, 8}};
int[][] resultSequential = multiplyMatrices(matA, matB);
int[][] resultThreadPerRow = multiplyMatricesWithThreadPerRow(matA,matB);
int[][] resultThreadPerCell = multiplyMatricesWithThreadPerCell(matA, matB);

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);
}

// Function to print a matrix


public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}

OUTPUT :

Fig. 2 Output of the Implemented code

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).

2. Multithreaded Matrix Multiplication with One Thread per Row:


When using one thread per row, the time complexity remains the same as the sequential
approach, which is O(mnp).

3. Multithreaded Matrix Multiplication with One Thread per Cell:


Using one thread per cell does not alter the time complexity of the matrix multiplication,
which remains 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

You might also like