Commit bb71ea95 authored by julanar.ali's avatar julanar.ali

Initial commit

parents
# Makefile
# Source file
SRC = mpi_integration.c
# number of process
NP = 4
# Compiler
CC = mpicc
# Executable name
EXE = ./out
all: $(EXE)
$(EXE): $(SRC)
$(CC) -o $(EXE) $(SRC)
clean:
rm -f $(EXE)
run:
mpirun -np $(NP) $(EXE)
master
slave1 user=mpiuser
slave2 user=mpiuser
#include <stdio.h>
#include <mpi.h>
#define NUM_RECTANGLES 1000 // Number of rectangles for integration
double calculate_area(double x) {
return x * x;
}
int main(int argc, char *argv[]) {
int rank, size;
double start = 0.0;
double end = 1.0;
double total_area = 0.0;
double width = (end - start) / NUM_RECTANGLES;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
double start_time = MPI_Wtime();
// Each process computes its partial area
double partial_area = 0.0;
for (int i = rank; i < NUM_RECTANGLES; i += size) {
double x = (i + 0.5) * width;
partial_area += calculate_area(x);
}
// Sum up the partial areas across all processes
MPI_Reduce(&partial_area, &total_area, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
// Only the root process prints the result
if (rank == 0) {
double end_time = MPI_Wtime();
double execution_time = end_time - start_time;
total_area *= width;
printf("Total area under the curve: %lf\n", total_area);
printf("Execution time: %f\n", execution_time);
}
MPI_Finalize();
return 0;
}
File added
SRC = integral_OpenMp.c
#Compiler
CC = gcc
CFLAGS = -Wall -g -fopenmp -lgomp
#Executable name
EXE = ./out
all: $(EXE)
$(EXE): $(SRC)
$(CC) -fopenmp -o $(EXE) $(SRC)
clean:
rm -f $(EXE)
run:
$(EXE)
#include <stdio.h>
#include <omp.h>
#define NUM_THREADS 4
#define NUM_RECTANGLES 1000 // Number of rectangles for integration
double calculate_area(double x) {
return x * x;
}
int main() {
double start = 0.0;
double end = 1.0;
double total_area = 0.0;
double width =(end - start) / NUM_RECTANGLES;
double start_time = omp_get_wtime();
#pragma omp parallel num_threads(NUM_THREADS)
{
int tid = omp_get_thread_num();
double partial_area = 0.0;
#pragma omp for
for (int i = 0; i < NUM_RECTANGLES; i++) {
double x = (i + 0.5) * width;
partial_area += calculate_area(x);
}
printf("Partial area computed by thread %d is: %f \n",tid,partial_area);
#pragma omp critical
total_area += partial_area;
#pragma omp barrier
}
double end_time = omp_get_wtime();
total_area *= width;
double execution_time = end_time - start_time;
printf("Total area under the curve: %lf\n", total_area);
printf("Execution time: %lf seconds\n", execution_time);
return 0;
}
File added
https://colab.research.google.com/drive/1Ckfw28vwreLkH811y1E4FOdXbO5FSTqt?usp=sharing
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment