https://github.com/ranimeshehata/matrix-multiplication
C program that performs matrix multiplication using three approaches as an application on multi-threading.
https://github.com/ranimeshehata/matrix-multiplication
cpp linux multithreading operating-system threads ubuntu
Last synced: 7 months ago
JSON representation
C program that performs matrix multiplication using three approaches as an application on multi-threading.
- Host: GitHub
- URL: https://github.com/ranimeshehata/matrix-multiplication
- Owner: ranimeshehata
- Created: 2024-03-15T04:35:36.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-15T04:52:38.000Z (almost 2 years ago)
- Last Synced: 2025-02-09T00:53:05.416Z (12 months ago)
- Topics: cpp, linux, multithreading, operating-system, threads, ubuntu
- Language: C
- Homepage:
- Size: 858 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Matrix Multiplication (Multi-Threading)

## 1. Objectives
* Gettin familiar with thread programming using the [Pthread library](https://hpc-tutorials.llnl.gov/posix/).
* To better understand processes and threads.
## 2. Overview
A multi-threaded [matrix multiplication](https://www.mathsisfun.com/algebra/matrix-multiplying.html) implementation program.
The input to the program is two matrixes A(x*y) and B(y*z) that are read from corresponding text files. The output is a matrix C(x*z) that is written to an output text file.
A parallelized version of matrix multiplication can be done using one of these three methods:
1. A thread computes the output C matrix i.e. without multi-threading. (A thread per matrix).

2. A thread computes each row in the output C matrix. (A thread per row).

3. A thread computes each element in the output C matrix. (A thread per element).

## 3. Comparing the three implementations according to the following:
Number of threads created and Execution time taken:
Creating and handling threads requires extra overhead and lots of computation, so, it's not always the ideal solution to go for multi-threading, and there's always a tradeoff.

The program does the following:
* If the user does not enter the file name, the default is a.txt and b.txt, for input matrixes A and B, and c for output matrices (of all three methods) C. The following example should clarify inputs/outputs files.
Arguments
Example:
./exe a b c
Input files:
a.txt
b.txt
Output files:
c_per_matrix.txt
c_per_row.txt
c_per_element.txt
No Arguments:
Example:
./exe
Input files:
a.txt
b.txt
Output files:
c_per_matrix.txt
c_per_row.txt
c_per_element.txt
Custom Arguments:
Example:
./exe x y z
Input files:
x.txt
y.txt
Output files:
z_per_matrix.txt
z_per_row.txt
z_per_element.txt
* Read the number of rows and columns of the input matrices. They are written in the first line of the file as ”row=x col=y”. The following is an example of the format on an input file.
row=3 col=5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
* Read the input matrices from their corresponding files. Each row is on a separate line, columns are separated by spaces.
* Use threads to calculate the matrix that results from multiplying the input two matrixes.
* Output the resulting matrices in three files (each file represents one method). The following is an example of the format of the three output files.
c_per_matrix.txt
Method: A thread per matrix
row=2 col=2
1 2
3 4
c_per_row.txt
Method: A thread per row
row=2 col=2
1 2
3 4
c_per_element.txt
Method: A thread per element
row=2 col=2
1 2
3 4
The output matrices are all be the same for all three methods.
* **The number of threads created and the time taken for all the three methods (three different outputs) is printed on the console.**
* Assuming matrix size is no larger than 20 x 20.