This repository contains two MPI-based parallel C programs for High-Performance Computing (HPC) coursework:
-
Problem 1: Counting Primes
Count the number of prime numbers in a user-specified range [x, y] using two variants:- Method (a):
MPI_Bcast+MPI_Reduce - Method (b):
MPI_Send+MPI_Recv
- Method (a):
-
Problem 2: Matrix Summation
Compute the element-wise sum of two NรM matrices A and B in parallel, usingMPI_ScatterandMPI_Gather.
.
โโโ problem1/
โ โโโ primes_bcast_reduce.c # Solution using MPI_Bcast & MPI_Reduce
โ โโโ primes_bcast_reduce # Executable
โ โโโ primes_send_recv.c # Solution using MPI_Send & MPI_Recv
โ โโโ primes_send_recv # Executable
โ
โโโ problem2/
โ โโโ matrix_sum_scatter_gather.c # Solution using MPI_Scatter & MPI_Gather
โ โโโ matrix_sum_scatter_gather # Executable
โ
โโโ README.md # (you are here)
- MPI implementation (e.g., Open MPI or MPICH)
- C compiler (
mpicc) - Unix-like shell (Linux/macOS)
Count the prime numbers within a user-defined range using:
- Method A:
MPI_BcastandMPI_Reduceonly. - Method B:
MPI_SendandMPI_Recvonly.
# Compile and run the Bcast/Reduce version
cd 'Problem 1'
mpicc primes_bcast_reduce.c -o primes_bcast_reduce -lm
mpirun -np 4 ./primes_bcast_reduceSample Output:
Enter first Number: 1
Enter second Number: 16
The number of prime numbers between 1 and 16 is: 6
# Compile and run the Send/Recv version
mpicc primes_send_recv.c -o primes_send_recv -lm
mpirun -np 4 ./primes_send_recvSample Output:
Enter first Number: 1
Enter second Number: 16
Process 1 found 3 prime numbers between 1 and 5
Process 2 found 1 prime numbers between 6 and 10
Process 3 found 2 prime numbers between 11 and 16
Total prime numbers between 1 and 16 are: 6
Sum two user-defined matrices using MPI_Scatter and MPI_Gather.
cd ../'Problem 2'
mpicc matrix_sum_scatter_gather.c -o matrix_sum_scatter_gather
mpirun -np 4 ./matrix_sum_scatter_gatherSample Output:
Enter the size of the matrices (n m): 2 2
Enter the elements of matrix A (4 values):
1 2 3 4
Enter the elements of matrix B (4 values):
1 2 3 4
The sum matrix C is:
2 4
6 8
- All programs are written in C using MPI.
- Compilation uses
-lmfor linking the math library in Problem 1. - Programs are tested using 4 MPI processes.
- Both problems assume the work divides evenly across processes.
- In Problem 2,
n*mmust be divisible by the number of processes. Otherwise, the program will abort with an error. - Ensure your MPI environment variables (e.g.,
PATHandLD_LIBRARY_PATH) are properly set for your MPI installation.