https://github.com/babak2/mpi-matrix-processing
https://github.com/babak2/mpi-matrix-processing
cpp mpi multiprocessing parrallel-computing
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/babak2/mpi-matrix-processing
- Owner: babak2
- Created: 2023-12-09T16:04:31.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-01-05T20:02:57.000Z (over 1 year ago)
- Last Synced: 2025-02-01T19:14:24.694Z (4 months ago)
- Topics: cpp, mpi, multiprocessing, parrallel-computing
- Language: C++
- Homepage:
- Size: 3.91 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MPI Matrix Processing
## Description
This program demonstrates the use of MPI (Message Passing Interface) to parallelize a computationally heavy task, specifically the computation of the sum of elements in a matrix. The program is designed to compare the execution time of the task with and without MPI, showcasing the potential speedup achieved through parallelization. Additionally, it calculates and prints the speedup factor and percentage improvement.
## Demo Features
- MPI Parallelization: Utilizes MPI to distribute the matrix elements among multiple processes, enabling parallel computation.
- The execution time for both non-MPI and MPI (for each number of processes used) versions is printed to the console for performance analysis.
Using these printouts, we can manually do the following:
- Execution Time Comparison: Measures and compares the execution time of the matrix sum computation with and without MPI.
- Speedup Factor Calculation: Computes the speedup factor achieved through parallelization.
- Percentage Improvement: Calculates and prints the percentage improvement in execution time when using MPI.
# Requirements
- MPI (Message Passing Interface) library
- C++ compiler with MPI support (for compiling MPI version)
- C++ compiler (for compiling non-MPI version)# Usage
## MPI Compilation
To compile the program with MPI support, use the following command:
```mpic++ -g mpi-matrix-processing.cpp -o mpi-matrix-processing```
Or, with macro definition:
```mpic++ -DUSE_MPI -g mpi-matrix-processing.cpp -o mpi-matrix-processing```
## MPI Execution
To execute the MPI version, use the following command:
`mpirun --oversubscribe -np ./mpi-matrix-processing`
For example:
```mpirun --oversubscribe -np 4 ./mpi-matrix-processing```
## Non-MPI Compilation
To compile the program without MPI support, use the following command:
```g++ -g mpi-matrix-processing.cpp -o matrix-processing-no-mpi```
## Non-MPI Execution
To execute the non-MPI version, use the following command:
```./matrix-processing-no-mpi```
# Output examples
First MPI run, using 4 parallel processes:
`mpirun --oversubscribe -np 4 ./mpi-matrix-processing`
Output:
Execution Time (MPI): 7.55e-07 seconds
Execution Time (MPI): 2.03e-07 seconds
Execution Time (MPI): 1.42e-07 seconds
Execution Time (MPI): 2.02e-07 seconds
Secondly, no-MP run:
`./matrix-processing-no-mpi`
Output:
Execution Time (Non-MPI): 8e-06 seconds
## Speed up calculation
Calculating speedup (performance) gaind using MPI parallel computation:
Speedup = Execution Time (Non-MPI) / Total Execution Time (MPI)
Total Execution Time (MPI) = Execution Time1 + Execution Time2 + Execution Time3 + Execution Time4
So:
Total Execution Time (MPI) = (7.55e−07 + 2.03e−07 + 1.42e−07 + 2.02e−07) seconds
Total Execution Time (MPI) ~ 1.4e−06 seconds
Speedup = 8e−06 / 1.4e−06 ~ 5.71
So, the overall speedup achieved by using MPI compared to the non-MPI version in this example was approximately 5.71 times.
# Matrix Size Configuration
The program generates a random matrix for demonstration purposes, and the initial matrix size is set to a smaller value (const int matrix_size = 10) for debugging convenience. If you wish to work with larger matrices, you can modify the matrix_size constant in the source code to adjust the size accordingly.
const int matrix_size = 10; // Adjust the matrix size for debugging
# Notes
- The MPI code section is marked with #ifdef USE_MPI. Ensure to define the macro USE_MPI during compilation to enable MPI features.
- The MPI-related code is enclosed within preprocessor directives, allowing easy switching between MPI and non-MPI versions during compilation.# License
The mpi-matrix-processing program is licensed under the [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).

# Author
Babak Mahdavi Ardestani