Commit 092e15e4 authored by abdullh.alsoleman's avatar abdullh.alsoleman

The last

parent 346b24b8
# Makefile for MPI program
# Source file
SRC = area.c
# Number of processes
NP = 8
# Compiler
CC = mpicc
# Executable name
EXE = ./out
all: $(EXE)
$(EXE): $(SRC)
$(CC) -o $(EXE) $(SRC)
clean:
rm -f $(EXE)
run:
mpirun -np $(NP) -f mpi_hosts $(EXE)
#include <stdio.h>
#include <mpi.h>
// Function to calculate the partial area under the curve X^2 for a given range
double calculate_partial_area(double start, double end, int num_steps, int rank, int size) {
double width = (end - start) / num_steps;
double partial_area = 0.0;
// Loop over the subintervals assigned to the current process
for (int i = rank; i < num_steps; i += size) {
double x_start = start + i * width;
double x_end = x_start + width;
double height_start = x_start * x_start;
double height_end = x_end * x_end;
// Use trapezoidal rule for integration
partial_area += 0.5 * (height_start + height_end) * width;
}
double total_area;
// Use MPI_Reduce to sum up the partial areas from all processes
MPI_Reduce(&partial_area, &total_area, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
return total_area;
}
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int num_steps = 1000000; // Adjust based on the required precision
// Measure execution time
double start_time = MPI_Wtime();
// Calculate the total area using MPI parallelization with trapezoidal rule
double total_area = calculate_partial_area(0.0, 1.0, num_steps, rank, size);
// Stop measuring time
double end_time = MPI_Wtime();
double elapsed_time = end_time - start_time;
if (rank == 0) {
// Print the results at the root process
printf("Total Area (MPI): %f\n", total_area);
printf("Time (MPI): %f seconds\n", elapsed_time);
}
MPI_Finalize();
return 0;
}
master
slave1 user=mpiuser
slave2 user=mpiuser
\ No newline at end of file
# Makefile
# Source file
SRC = area.c
# Compiler
CC = gcc
# 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>
// Function to calculate the partial area under the curve X^2 for a given range
double calculate_partial_area(double start, double end, int num_steps) {
double width = (end - start) / num_steps;
double partial_area = 0.0;
// Parallel loop with reduction to calculate partial areas for each thread
#pragma omp parallel for reduction(+:partial_area)
for (int i = 0; i < num_steps; i++) {
double x = start + i * width;
double height = x * x;
partial_area += height * width;
}
return partial_area;
}
int main() {
int num_steps = 1000000; // Adjust based on the required precision
// Measure execution time
double start_time = omp_get_wtime();
// Calculate the total area using OpenMP parallelization and reduction
double total_area = calculate_partial_area(0.0, 1.0, num_steps);
// Stop measuring time
double end_time = omp_get_wtime();
double elapsed_time = end_time - start_time;
// Print the results
printf("Total Area (OpenMP): %f\n", total_area);
printf("Time (OpenMP): %f seconds\n", elapsed_time);
return 0;
}
# Makefile for MPI + OpenMP program
# Source file
SRC = area.c
# Number of processes
NP = 4
# Compiler
CC = mpicc
# Executable name
EXE = ./out
all: $(EXE)
$(EXE): $(SRC)
$(CC) -o $(EXE) -fopenmp $(SRC)
clean:
rm -f $(EXE)
mpihost:
mpirun --hostfile mpihost -np $(NP) hostname > mpihost
run: mpihost
mpirun -np $(NP) -hostfile mpihost $(EXE)
#include <stdio.h>
#include <mpi.h>
#include <omp.h>
// Function to calculate the partial area under the curve X^2 for a given range
double calculate_partial_area(double start, double end, int num_steps, int rank, int size) {
double width = (end - start) / num_steps;
double partial_area = 0.0;
// Use OpenMP parallelization within each MPI process
#pragma omp parallel reduction(+:partial_area)
{
// Get the number of threads and the ID of each thread
int num_threads = omp_get_num_threads();
int thread_id = omp_get_thread_num();
// Loop over the subintervals assigned to the current thread within the MPI process
for (int i = thread_id + rank * num_threads; i < num_steps; i += size * num_threads) {
double x = start + i * width;
double height = x * x;
partial_area += height * width;
}
}
double total_area;
// Use MPI_Reduce to sum up the partial areas from all MPI processes
MPI_Reduce(&partial_area, &total_area, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
return total_area;
}
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int num_steps = 1000000; // Adjust based on the required precision
// Measure execution time
double start_time = MPI_Wtime();
// Calculate the total area using MPI + OpenMP hybrid parallelization
double total_area = calculate_partial_area(0.0, 1.0, num_steps, rank, size);
// Stop measuring time
double end_time = MPI_Wtime();
double elapsed_time = end_time - start_time;
if (rank == 0) {
// Print the results at the root process
printf("Total Area (MPI + OpenMP): %f\n", total_area);
printf("Time (MPI + OpenMP): %f seconds\n", elapsed_time);
}
MPI_Finalize();
return 0;
}
master
master
master
master
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