DAA Mini Project-1
DAA Mini Project-1
DAA Mini Project-1
1
KJ COLLEGE OF ENGINEERING AND
MANAGEMENT RESEARCH
DEPARTMENT OF COMPUTER ENGINEERING
CERTIFICATE
This is certify that the project entitled
submitted by
is a record of bonafide work carried out by them, in the partial fulfilment of the
requirement for the award of Degree of Bachelor of Engineering (Computer
Engineering) at KJ COLLEGE OF ENGINEERING AND MANAGEMENT
RESEARCH, Pune under the University of Pune. This work is done during year
2023-2024, under our guidance.
Date: / /
2
ACKNOWLEDGEMENT
I take this opportunity to express my sincere gratitude and respect to KJ COLLEGE OF
ENGINEERING AND MANGEMENT RESEARCH, Pune for providing me a platform to
pursue my studies and carry out my final year project.
I would like to thank Dr. Nikita Kulkarni, Professor and Head, Department of Computer
Engineering, KJ COLLEGE OF ENGINEERING AND MANGEMENT RESEARCH, Pune
who has been a constant support and encouragement throughout the course of this project. I
consider it a privilege and honor to express my sincere gratitude to my guide. Prof Ashwini
G Kamble, Associative professor, Department of Computer Engineering, for the valuable
guidance throughout the tenure of this review.
I also extend my thanks to all the faculty of Computer Engineering who directly or indirectly
encouraged me.
3
CONTENTS
1. Abstract 5
2. Introduction 6
3. Problem Statement 7
5. Objectives 8
6. Theory 9
7. Result 11
8. Conclusion 14
9. References 15
Abstract
4
Introduction
Problem Statement
Write a program to implement Matrix multiplication & multithreaded matrix
multiplication with either one thread per row or one thread per cell. Analyze and
compare their performance.
5
Motivation
6
.
Objectives
Theory
THREAD
METHODOLOGY
ALGORITHM:
7
• Input: Two matrices, A (dimensions MxN) and B (dimensions NxP).
• Output: A resulting matrix C (dimensions MxP).
• Check if the number of columns in matrix A is equal to the number of rows in matrix B.
If not, matrix multiplication is not possible.
• Initialize an empty result matrix C with dimensions MxP.
• Iterate through each row 'i' of matrix A:
• For each row 'i' of matrix A, iterate through each column 'j' of matrix B:
• For each element 'k' in the row 'i' of matrix A, multiply it by the element in the column 'j'
of matrix B.
• Sum the products for all elements 'k' to compute the value of C[i][j].
• Assign this computed value to the corresponding position in matrix C.
• Continue this process until all elements in the resulting matrix C have been calculated.
• The resulting matrix C contains the product of matrices A and B, where C[i][j]
represents the dot product of row 'i' of A and column 'j' of B.
•
• The algorithm is complete, and the resulting matrix C can be used for further
computations or analysis.
8
C(i, j) = Σ(A(i, k) * B(k, j)) for k = 1 to n
This operation has a time complexity of O(mnp), making it computationally expensive for large
matrices. To optimize its performance, we can leverage multithreading.
import numpy as np
import threading
import time
9
def multiply_row(row): global matA, matB, matC
for j in range(4): for k in range(4): matC[row][j] +=
matA[row][k] * matB[k][j]
num_threads = 4
threads = []
start_time_single =
time.time() for i in range(4):
multiply_row(i)
end_time_single = time.time()
start_time_multi =
time.time()
end_time_multi = time.time()
print("Matrix A:") print(matA)
print("Matrix B:") print(matB)
print("Matrix C (Result of A x
B):") print(matC)
10
RESULT
.
Reference serial code performance:
11
Native threads-based implementation:
12
• Work within the line limits
Benchmarking:
significantly.
One Thread per Cell: Similar to the one-thread-per-row strategy, this section provides an in-
depth examination of the one-thread-per-cell approach. Its advantages, potential disadvantages,
and associated performance metrics are explored. The fine-grained parallelism of this approach
may lead to performance improvements, but potential bottlenecks and synchronization
challenges are also considered.
Conclusion
In conclusion, the choice between matrix multiplication and multithreaded matrix multiplication
with one thread per row or one thread per cell depends on the specific requirements of the task.
Traditional matrix multiplication is suitable for small to moderately sized matrices and is
generally easier to implement. However, for large matrices, multithreaded matrix multiplication
can significantly improve performance by utilizing parallel processing. When using one thread
per row, the performance gain is more evident for wide matrices, while one thread per cell is
effective for square or tall matrices. The choice ultimately hinges on the matrix size, hardware
capabilities, and the trade-off between complexity and performance, making it essential to
evaluate and select the most suitable approach based on the specific application.
13
References
• https://en.wikipedia.org/wiki/Matrix_multiplication
• https://www.sciencedirect.com/science/article/abs/pii/S0957417414004473
• https://matrix.reshish.com/multiplication.php
• https://www.geeksforgeeks.org/matrix-multiplication/
14