EXERCISE- 4
EXERCISE- 4
SARANBALAJI 22IZ034
EXERCISE- 4
AIM:-
To study and analyze the use of MPI for parallel computing.
MPI:-
The message passing interface (MPI) is a standardized means of exchanging messages between
multiple computers running a parallel program across distributed memory.
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Calculate how many rows each process will handle
int rows_per_proc = N / size;
int remainder = N % size;
// Determine starting and ending row for each process
int start_row = rank * rows_per_proc + (rank < remainder ? rank : remainder);
R.SARANBALAJI 22IZ034
if (rank == 0) {
// Copy local results into C
for (int i = 0; i < local_rows; i++)
for (int j = 0; j < N; j++)
C[i][j] = local_C[i][j];
// Receive results from other processes
int current_row = local_rows;
for (int src = 1; src < size; src++) {
int src_rows = rows_per_proc + (src < remainder ? 1 : 0);
MPI_Recv(&C[current_row][0], src_rows * N, MPI_INT, src, 1, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
current_row += src_rows;
}
// Print the final result matrix
printMatrix(C, "C (Result)");
} else {
// Send computed result back to root
MPI_Send(local_C, local_rows * N, MPI_INT, 0, 1, MPI_COMM_WORLD);
}
// Free allocated memory
free(local_A);
free(local_C);
MPI_Finalize();
return 0;
}
OUTPUT:-
R.SARANBALAJI 22IZ034
2) FOR PI CALCULATION
#include <mpi.h>
#include <stdio.h>
double f(double x) {
return 4.0 / (1.0 + x * x); // Function to integrate
}
int main(int argc, char *argv[]) {
int rank, size;
double pi = 0.0, local_sum = 0.0;
int n = 1000000; // Number of intervals
double h = 1.0 / n; // Step size
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Determine workload for each process
int start = rank * (n / size);
int end = (rank + 1) * (n / size);
// Compute local sum
for (int i = start; i < end; i++) {
double x = (i + 0.5) * h;
local_sum += f(x);
}
local_sum *= h; // Multiply by step size
// Print local sum from each process
printf("Process %d computed local sum: %.15f\n", rank, local_sum);
// Reduce local sums to compute the final Pi value
MPI_Reduce(&local_sum, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
// Print final result from rank 0
if (rank == 0) {
printf("Final Calculated Pi: %.15f\n", pi);
}
MPI_Finalize();
return 0;
}
OUTPUT:-
R.SARANBALAJI 22IZ034
2)FOR PI CALCULATION:
#include <mpi.h>
#include <stdio.h>
double f(double x) {
return 4.0 / (1.0 + x * x); // Function to integrate
}
int main(int argc, char *argv[]) {
int rank, size;
double pi = 0.0, local_sum = 0.0;
int n = 100000000; // Number of intervals
double h = 1.0 / n; // Step size
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Broadcast the number of intervals to all processes
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
// Each process computes its local sum using a strided approach
for (int i = rank; i < n; i += size) {
double x = (i + 0.5) * h;
local_sum += f(x);
}
local_sum *= h;
R.SARANBALAJI 22IZ034
OUTPUT: