0% found this document useful (0 votes)
27 views20 pages

DAAchinmay 1

Here are the key steps of the matrix multiplication algorithm: 1. Generate random matrices A and B of specified dimensions 2. Implement single-threaded matrix multiplication of A and B and record execution time 3. Enhance program with multithreading support 4. Implement two multithreaded strategies: - One thread per row - One thread per cell 5. Record execution times for multithreaded strategies 6. Analyze and compare performance of single-threaded vs multithreaded approaches in terms of execution time and speedup 7. Evaluate scalability by varying matrix sizes and number of threads 8. Document code, results and conclusions in a report The algorithm focuses on developing an efficient matrix multiplication implementation
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)
27 views20 pages

DAAchinmay 1

Here are the key steps of the matrix multiplication algorithm: 1. Generate random matrices A and B of specified dimensions 2. Implement single-threaded matrix multiplication of A and B and record execution time 3. Enhance program with multithreading support 4. Implement two multithreaded strategies: - One thread per row - One thread per cell 5. Record execution times for multithreaded strategies 6. Analyze and compare performance of single-threaded vs multithreaded approaches in terms of execution time and speedup 7. Evaluate scalability by varying matrix sizes and number of threads 8. Document code, results and conclusions in a report The algorithm focuses on developing an efficient matrix multiplication implementation
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/ 20

A Mini Project Report on

“Write a program to implement matrix multiplication. Also implement


multithreaded matrix multiplication with either one thread per row or one
thread per cell. Analyze and compare their performance”
Submitted

By

Chinmay Sanjay Chaudhari (10)


Shruti Nandkishor Waghadkar (44)
Vaishnavi Narendra Gosavi (15)
Abhishek Laxman Mahale (20)
Satyam Bharat Nighute (25)

in partial fulfilment of the requirements for


the award of the degree of

Bachelor in
COMPUTER ENGINEERING
For Academic Year 2023-2024

DEPARTMENT OF COMPUTER ENGINEERING

MET’s Institute of Engineering Bhujbal Knowledge City


Adgaon, Nashik-422003
Certificate
This is to Certify that

“Chinmay Sanjay Chaudhari ”

(Roll No: 10)

has completed the necessary Mini Project work and prepared the report on

“Write a program to implement matrix multiplication. Also implement


multithreaded matrix multiplication with either one thread per row or one thread
per cell. Analyze and compare their performance”

in satisfactory manner as a fulfillment of the requirement of the awardof


degree of Bachelor of Computer Engineering in the Academic year
2023-2024
in Satisfactorily manner as a fulfilment of the requirement of the award of
degree of the Bachelor in Computer Engineering in the Academic Year
2023 – 2024

DEPARTMENT OF COMPUTER ENGINEERING


MET’s Institute of Engineering Bhujbal Knowledge City
Adgaon, Nashik – 422003.

Project Guide
Prof.K.V.Metre
Acknowledgement

I take this opportunity to thank all those who have contributed in successful
completionof this Project work. I would like to express my sincere thanks to my Project
Guided by Prof. K. V . Metre, who has encouraged me to work on this project and
guided me wheneverrequired.

We also would like to express our gratitude to our H.O.D. Dr. M. U. Kharat for
givingus opportunities to undertake this project work at Met Institute of engineering,
Nashik.

We are extremely grateful to our Principal Dr. V. P. Wani for his constant
inspiration and keen interest to make the project and presentation absolutely flawless.

At the last but not the least we would like to thank our Teaching staff member,
Workshop staff member, Friends and family member for their timely co-operation and
help.

By

Chinmay Sanjay Chaudhari (10)


Shruti Nandkishor Waghadkar (44)
Vaishnavi Narendra Gosavi (15)
Abhishek Laxman Mahale (20)
Satyam Bharat Nighute (25)
Course Objective:

 Analyze performance of an algorithm.


 Learn how to implement algorithms that follow algorithm design strategies
namely divide and conquer, greedy, dynamic programming, backtracking,
branch and bound.
 To analyze performance of different algorithmic strategies in terms of time
and space.
 To develop time and space efficient algorithms.
 To Understand Multithreaded and Distributed Algorithms

Course Outcome:

On completion of the course, learners should be able to


CO1: Formulate the problem
CO2: Analyze the asymptotic performance of algorithms
CO3: Decide and apply algorithmic strategies to solve given problem.
CO4: Analyze performance of an algorithm.
CO5: Implement an algorithm that follows one of the following algorithm design
strategies: divide and conquer, greedy, dynamic programming, backtracking, branch and
bound.
Abstract

Matrix multiplication is a fundamental operation in linear algebra and finds


application in various domains, including computer graphics, scientific computing, and
machine learning. This program aims to implement matrix multiplication and compare
the performance of two different approaches: single-threaded multiplication and
multithreaded multiplication with one thread per row or one thread per cell.

The program begins by generating random matrices of specified dimensions. It


then measures the time taken to multiply these matrices using a conventional single-
threaded approach. Subsequently, it employs multithreading to parallelize the matrix
multiplication process. The two multithreaded strategies involve dedicating one thread
per row and one thread per cell, respectively. The execution time of each approach is
recorded.

The performance comparison includes measuring execution times and analyzing


the speedup achieved through multithreading. This study aims to evaluate how
multithreading can potentially enhance matrix multiplication efficiency, particularly for
large matrices. The results provide insights into the advantages and trade-offs of
employing parallel computing techniques in matrix operations.
Contents

Sr. No TITLE Page no

1. Introduction 1

2. Objectives 3

3. System Requirement 4

4. Algorithm 5

5. System Overview 6

6. Code & Output 7

7. Conclusion 14

8. References 15
Introduction

Matrix multiplication is a fundamental operation in various scientific and


engineering applications, ranging from physics simulations to data processing in
machine learning. As matrices continue to grow in size and complexity, optimizing this
operation becomes increasingly crucial for overall computational efficiency. In this
program, we delve into them world of matrix multiplication, implementing both
conventional and multithreaded approaches, and conducting a performance analysis to
evaluate the benefits of parallel computing.

The primary objective of this program is to develop a matrix multiplication


algorithm and explore the advantages of utilizing multithreading for this
computationally intensive task. We will specifically investigate two multithreaded
strategies: one thread per row and one thread per cell.

To begin, we will generate two random matrices of user-defined dimensions.


These matrices will serve as the input for the matrix multiplication operations. We will
first implement a single-threaded matrix multiplication algorithm, a traditional
approach that is easy to understand and provides a baseline for performance
comparison.

Subsequently, we will introduce multithreading to the matrix multiplication


process. The two multithreaded strategies, one thread per row and one thread per cell,
will be explored to assess their respective efficiency. By dividing the workload across
multiple threads, we aim to accelerate the multiplication process and, potentially,
achieve a significant reduction in time.

1
Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the figure
given :

We can add, subtract, multiply and divide 2 matrices. To do so, we are taking
input from the user for row number, column number, first matrix elements and second
matrix elements. Then we are performing multiplication on the matrices entered by the
user. In matrix multiplication first matrix one row element is multiplied by second
matrix all column elements.

2
Objectives

1. To Develop a Java program for matrix multiplication.

2. To Measure and record execution times for all methods.

3. To Analyze performance, focusing on execution time and multithreading

speedup.

3
Software/Hardware Requirements

Hardware Requirement:
 Processor: Intel (i5, i7, i9)
 HDD: Min 32 GB; Recommended 64 GB or more
 RAM: Min 1 GB; Recommended 4 GB or above

Software Requirement:

 Operating System: Windows/ubuntu


 Language: C++/JAVA
 IDE: Code Blocks/VS Code

4
Algorithm

Step 1: Start

Step 2: Matrix Multiplication


- Generate random matrices A and B.
- Implement single-threaded matrix multiplication and record its
execution time.

Step 3: Multithreaded Matrix Multiplication


- Enhance the program with multithreading.
- Implement row-level and cell-level multithreading.
- Record execution times for each multithreaded approach.

Step 3: Performance Analysis


- Collect execution time data for all methods.
- Calculate and compare speedup values.

Step 4: Scalability Evaluation


- Assess scalability by varying matrix sizes and thread counts.
- Determine how performance scales with changing input and resources.

Step 5: Multithreading Benefits


- Uncover advantages and trade-offs of multithreading.
- Identify scenarios where multithreading is most beneficial.

Step 6: Usability and Instructions


- Ensure user-friendly execution and result interpretation.
- Provide clear instructions for users.

Step 7: Documentation and Reporting


- Thoroughly document code and findings.
- Prepare a summary report or presentation.

Step 8: Practical Application


- Offer practical insights for applying multithreading in real-world
scenarios.

Step 9: Stop.

5
System overview

This program implements matrix multiplication and explores multithreaded


strategies for enhanced performance analysis.

 Matrix Multiplication: Generate matrices, implement single-threaded and


multithreaded methods.
 Performance Analysis: Measure execution times, calculate speedup values.
 Scalability Assessment: Evaluate performance across varying data and
resources.
 Multithreading Insights: Discover benefits and trade-offs of multithreading.
 User-Friendly Interface: Ensure easy usage with clear instructions.
 Documentation and Reporting: Thoroughly document code and prepare a
summary report.
 Practical Application: Offer guidance for real-world scientific and data-driven
tasks.

6
CODE and OUTPUT

import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MatrixMultiplication {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of rows for matrix A: ");


int rowsA = scanner.nextInt();
System.out.print("Enter the number of columns for matrix A: ");
int colsA = scanner.nextInt();
System.out.print("Enter the number of rows for matrix B: ");
int rowsB = scanner.nextInt();
System.out.print("Enter the number of columns for matrix B: ");
int colsB = scanner.nextInt();

if (colsA != rowsB) {
System.out.println("Matrix multiplication is not possible with these dimensions.");
return;
}

int[][] matrixA = new int[rowsA][colsA];


int[][] matrixB = new int[rowsB][colsB];

System.out.println("Enter elements for matrix A:");


for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
matrixA[i][j] = scanner.nextInt();
}
}

System.out.println("Enter elements for matrix B:");


for (int i = 0; i < rowsB; i++) {
7
for (int j = 0; j < colsB; j++) {
matrixB[i][j] = scanner.nextInt();
}
}

int[][] result = new int[rowsA][colsB];

// Perform matrix multiplication without threads


matrixMultiply(matrixA, matrixB, result);
System.out.println("Matrix multiplication without threads:");
printMatrix(result);

// Perform matrix multiplication with one thread per row


matrixMultiplyMultithreadedRow(matrixA, matrixB, result);
System.out.println("Matrix multiplication with one thread per row:");
printMatrix(result);

// Perform matrix multiplication with one thread per cell


matrixMultiplyMultithreadedCell(matrixA, matrixB, result);
System.out.println("Matrix multiplication with one thread per cell:");
printMatrix(result);

// Measure performance of matrix multiplication without threads


long startTime = System.nanoTime();
matrixMultiply(matrixA, matrixB, result);
long endTime = System.nanoTime();
long singleThreadTime = TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
System.out.println("Matrix multiplication without threads took " + singleThreadTime + "
ms");

// Reset the result matrix


for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
}
}

// Measure performance of matrix multiplication with one thread per row

8
startTime = System.nanoTime();
matrixMultiplyMultithreadedRow(matrixA, matrixB, result);
endTime = System.nanoTime();
long rowThreadTime = TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
System.out.println("Matrix multiplication with one thread per row took " +
rowThreadTime + " ms");

// Reset the result matrix


for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
}
}

// Measure performance of matrix multiplication with one thread per cell


startTime = System.nanoTime();
matrixMultiplyMultithreadedCell(matrixA, matrixB, result);
endTime = System.nanoTime();
long cellThreadTime = TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
System.out.println("Matrix multiplication with one thread per cell took " +
cellThreadTime + " ms");

// Compare the execution times


System.out.println("Performance comparison:");
System.out.println("Single Thread: " + singleThreadTime + " ms");
System.out.println("Thread per Row: " + rowThreadTime + " ms");
System.out.println("Thread per Cell: " + cellThreadTime + " ms");
}

public static void matrixMultiply(int[][] A, int[][] B, int[][] result) {


int rowsA = A.length;
int colsA = A[0].length;
int colsB = B[0].length;

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


for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
result[i][j] += A[i][k] * B[k][j];

9
}
}
}
}

public static void matrixMultiplyMultithreadedRow(int[][] A, int[][] B, int[][] result) {


int rowsA = A.length;
int colsB = B[0].length;

ExecutorService executor = Executors.newFixedThreadPool(rowsA);

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


final int row = i;
executor.submit(() -> {
for (int j = 0; j < colsB; j++) {
for (int k = 0; k < A[0].length; k++) {
result[row][j] += A[row][k] * B[k][j];
}
}
});
}

executor.shutdown();
while (!executor.isTerminated()) {
// Wait for all threads to finish
}
}

public static void matrixMultiplyMultithreadedCell(int[][] A, int[][] B, int[][] result) {


int rowsA = A.length;
int colsB = B[0].length;

ExecutorService executor = Executors.newFixedThreadPool(rowsA * colsB);

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


for (int j = 0; j < colsB; j++) {
final int row = i;
final int col = j;
10
executor.submit(() -> {
for (int k = 0; k < A[0].length; k++) {
result[row][col] += A[row][k] * B[k][col];
}
});
}
}

executor.shutdown();
while (!executor.isTerminated()) {
// Wait for all threads to finish
}
}

public static void printMatrix(int[][] matrix) {


for (int[] row : matrix) {
for (int cell : row) {
System.out.print(cell + " ");
}
System.out.println();
}
}
}

OUTPUT:

Enter the number of rows for matrix A: 2

Enter the number of columns for matrix A: 2

Enter the number of rows for matrix B: 2

Enter the number of columns for matrix B: 2

Enter elements for matrix A:

12

34

11
Enter elements for matrix B:

56

78

Matrix multiplication without threads:

19 22

43 50

Matrix multiplication with one thread per row:

38 44

86 100

Matrix multiplication with one thread per cell:

57 66

129 150

Matrix multiplication without threads took 0 ms

Matrix multiplication with one thread per row took 12 ms

Matrix multiplication with one thread per cell took 12 ms

Performance comparison:

Single Thread: 0 ms

Thread per Row: 12 ms

Thread per Cell: 12 ms

12
Conclusion

Hence, we successfully implemented simple matrix multiplication and


multithreaded matrix multiplication with either one thread per row or one thread per
cell.
.

13
References

• https://www.geeksforgeeks.org/multiplication-of-matrix-using-threads/
• https://www.javaprogramto.com/2020/01/java-matrix-multiplication-
threads.html
• https://pocketstudyblog.wordpress.com/2017/09/12/implement-matrix-
multiplication-using-multithreading-application-should-make-use-of-pthread-
create-pthread-join-pthread-exit/

14

You might also like