Commit 818b79fa authored by Ali Saeed's avatar Ali Saeed

openMP - CUDA

parents
This source diff could not be displayed because it is too large. You can view the blob instead.
File added
# Makefile for MPI program
# Source file
SRC = mpi.c
# number of process
NP = 8
# Compiler
CXX = mpicc
# Executable name
EXE = ./out
all: $(EXE)
$(EXE): $(SRC)
$(CXX) -o $(EXE) $(SRC)
clean:
rm -f $(EXE)
run:
mpirun -np $(NP) -f mpi_hosts $(EXE)
\ No newline at end of file
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
double total_width = 1.0;
double area = 0.0;
int num_processes, process_rank;
double start_time, end_time, execution_time;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
for ( int num_partitions = 10; num_partitions <=10000000; num_partitions *= 100)
{
double partition_width = total_width / num_partitions;
if (process_rank == 0) {
start_time = MPI_Wtime();
}
int local_start = (num_partitions / num_processes) * process_rank;
int local_end;
if (process_rank < num_partitions % num_processes) {
local_end = local_start + (num_partitions / num_processes) + 1;
} else {
local_end = local_start + (num_partitions / num_processes);
}
double local_area = 0.0;
for (int i = local_start; i < local_end; ++i) {
double x = (i + 0.5) * partition_width;
double height = x * x;
double partial_area = partition_width * height;
local_area += partial_area;
}
MPI_Reduce(&local_area, &area, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (process_rank == 0) {
end_time = MPI_Wtime();
execution_time = end_time - start_time;
printf("for num_partitions: %d\n", num_partitions);
printf("Total area under the curve: %f\n", area);
printf("Execution time: %f seconds\n", execution_time);
}
}
MPI_Finalize();
return 0;
}
master
slave1 user=mpiuser
slave2 user=mpiuser
# Makefile
# Source file
SRC = openMP.c
# Compiler
CC = gcc
# Executable name
EXE = ./out
all: $(EXE)
$(EXE): $(SRC)
$(CC) -fopenmp -o $(EXE) $(SRC)
clean:
rm -f $(EXE)
run:
$(EXE)
#include "omp.h"
#include "stdlib.h"
#include "stdio.h"
int main() {
double area = 0.0;
for (int num_partitions = 10; num_partitions <= 10000000; num_partitions *= 100) {
double total_width = 1.0;
double partition_width = total_width / num_partitions;
double start_time = omp_get_wtime();
#pragma omp parallel for reduction(+:area) num_threads(4)
for (int i = 0; i < num_partitions; ++i) {
double x = (i + 0.5) * partition_width;
double height = x * x;
double partial_area = partition_width * height;
area += partial_area;
}
double end_time = omp_get_wtime();
double execution_time = end_time - start_time;
printf("For num_partitions: %d\n", num_partitions);
printf("Total area under the curve: %f\n", area);
printf("Execution time: %f seconds\n", execution_time);
area = 0.0;
}
return 0;
}
\ No newline at end of file
# Makefile for MPI program
# Source file
SRC = mpi_openMP.c
# number of process
NP = 3
# Compiler
CXX = mpicc
# Executable name
EXE = ./out
all: $(EXE)
$(EXE): $(SRC)
$(CXX) -fopenmp -o $(EXE) $(SRC)
clean:
rm -f $(EXE)
run:
mpirun -np $(NP) -f mpi_hosts $(EXE)
\ No newline at end of file
master
slave1 user=mpiuser
slave2 user=mpiuser
#include <stdio.h>
#include <mpi.h>
#include <omp.h>
int main(int argc, char *argv[]) {
int num_processes, process_rank;
double start_time, end_time, execution_time;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
for (int num_partitions = 10; num_partitions <= 10000000; num_partitions *= 100) {
double total_width = 1.0;
double partition_width = total_width / num_partitions;
double area = 0.0;
if (process_rank == 0) {
start_time = MPI_Wtime();
}
int local_start = (num_partitions / num_processes) * process_rank;
int local_end;
if (process_rank < num_partitions % num_processes) {
local_end = local_start + (num_partitions / num_processes) + 1;
} else {
local_end = local_start + (num_partitions / num_processes);
}
double local_area = 0.0;
#pragma omp parallel for reduction(+:local_area) num_threads(4)
for (int j = local_start; j < local_end; ++j) { // Changed loop variable name to j
double x = (j + 0.5) * partition_width;
double height = x * x;
double partial_area = partition_width * height;
local_area += partial_area;
}
MPI_Reduce(&local_area, &area, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (process_rank == 0) {
end_time = MPI_Wtime();
execution_time = end_time - start_time;
printf("For num_partitions: %d\n", num_partitions);
printf("Total area under the curve: %f\n", area);
printf("Execution time: %f seconds\n", execution_time);
}
}
MPI_Finalize();
return 0;
}
\ No newline at end of file
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