0% found this document useful (0 votes)
13 views6 pages

DAA Mini Project

Uploaded by

Kpranit
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)
13 views6 pages

DAA Mini Project

Uploaded by

Kpranit
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/ 6

DESIGN AND ANALYSIS OF ALGORITHMS

MINI PROJECT
Title: 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.

Abstract:

This document presents a program that demonstrates matrix multiplication using both single-
threaded and multithreaded approaches. The performance of both methods is analyzed and
compared based on execution time. The multithreaded approach leverages the capabilities of
modern processors by distributing the workload across multiple threads, thus potentially
reducing computation time. The study highlights the efficiency of multithreading in matrix
operations, providing insights into performance optimization techniques.

Introduction:

Matrix multiplication is a fundamental operation in various fields, including computer science,


physics, and engineering. Traditional matrix multiplication has a time complexity of O(n³),
which can be computationally expensive for large matrices. This paper explores how
multithreading can enhance performance by allowing simultaneous computation, thereby
utilizing multiple processor cores effectively. The implementation focuses on multiplying two
matrices of fixed size, demonstrating both single-threaded and multithreaded methods.

Objectives:

1. To implement matrix multiplication using a single-threaded approach.


2. To implement matrix multiplication using a multithreaded approach (one thread per row).
3. To analyze and compare the performance of both methods based on execution time.
4. To provide insights into the efficiency of multithreading in computational tasks.

System Requirements:

• A system with a minimum of dual-core processor.


• C++ compiler supporting C++11 or later (e.g., g++, MinGW).
• Basic libraries: <iostream>, <pthread.h>, <cstdlib>, <chrono>.
Methodology:

1. Matrix Generation: Random matrices are generated for multiplication.


2. Single-threaded Multiplication: A function is created to perform matrix multiplication
using nested loops.
3. Multithreaded Multiplication:
o Multiple threads are created, with each thread responsible for computing one row
of the resultant matrix.
o The pthread_create function is used to create threads, and pthread_join is
used to ensure all threads complete before proceeding.
4. Performance Measurement: The execution time of both methods is measured using
chrono for accurate timing.
5. Output: The resultant matrices from both methods are displayed, along with their
execution times.

Conclusion:

The program successfully demonstrates matrix multiplication using both single-threaded and
multithreaded approaches. The results indicate that while the single-threaded approach is
straightforward, the multithreaded approach can lead to performance improvements, particularly
in larger matrices. However, for smaller matrices, the overhead of thread creation may negate
these benefits. This exploration highlights the importance of choosing the appropriate method
based on matrix size and system architecture, emphasizing the value of multithreading in
enhancing computational efficiency.
# DAA MINI PROJECT (Multithreaded Matrix Multiplication: A Comparative
Performance Analysis)

#include <iostream>
#include <pthread.h>
#include <cstdlib>
#include <chrono>

using namespace std;

#define MAX 4 // Maximum size of the matrix


#define MAX_THREAD 4 // Maximum number of threads

int matA[MAX][MAX];
int matB[MAX][MAX];
int matC[MAX][MAX];
int step_i = 0;

// Function for single-threaded matrix multiplication


void multiplySingleThreaded()
{
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
matC[i][j] = 0; // Initialize the element
for (int k = 0; k < MAX; k++)
{
matC[i][j] += matA[i][k] * matB[k][j];
}
}
}
}

// Function for multithreading (one thread per row)


void* multi(void* arg)
{
int i = step_i++; // i denotes row number of resultant matC
for (int j = 0; j < MAX; j++)
{
for (int k = 0; k < MAX; k++)
{
matC[i][j] += matA[i][k] * matB[k][j];
}
}
}

void printMatrix(int matrix[MAX][MAX])


{
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

int main()
{
// Generating random values in matA and matB
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
matA[i][j] = rand() % 10;
matB[i][j] = rand() % 10;
}
}

cout << "\nMatrix A:" << endl;


printMatrix(matA);

cout << "\nMatrix B:" << endl;


printMatrix(matB);

// Measure time for single-threaded multiplication


auto start = chrono::high_resolution_clock::now();
multiplySingleThreaded();
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> singleThreadedTime = end - start;

cout << "\nResult of single-threaded multiplication (Matrix C):" <<


endl;
printMatrix(matC);
cout << "Single-threaded time: " << singleThreadedTime.count() << "
seconds" << endl;

// Reset matC
fill(&matC[0][0], &matC[0][0] + sizeof(matC) / sizeof(matC[0][0]),0);
// Declaring four threads for multithreading
pthread_t threads[MAX_THREAD];

// Measure time for multithreaded multiplication


start = chrono::high_resolution_clock::now();
for (int i = 0; i < MAX_THREAD; i++)
{
pthread_create(&threads[i], NULL, multi, NULL);
}
for (int i = 0; i < MAX_THREAD; i++)
{
pthread_join(threads[i], NULL);
}
end = chrono::high_resolution_clock::now();
chrono::duration<double> multiThreadedTime = end - start;

cout << "\nResult of multi-threaded multiplication (Matrix C):" <<


endl;
printMatrix(matC);
cout << "Multithreaded time: " << multiThreadedTime.count() << "
seconds" << endl;

return 0;
}

You might also like