From 6a98a5afa7035c2b05e0855df8839e5030abd85b Mon Sep 17 00:00:00 2001 From: kmazouzi Date: Thu, 26 Mar 2015 14:13:01 +0100 Subject: [PATCH] lab1 --- lab1/Makefile | 35 ++++++++++++++++++++++++++++++++ lab1/README | 33 ++++++++++++++++++++++++++++++ lab1/pi_matlab.m | 13 ++++++++++++ lab1/pi_mpi.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lab1/pi_omp.c | 38 +++++++++++++++++++++++++++++++++++ lab1/pi_ser.c | 35 ++++++++++++++++++++++++++++++++ lab1/pi_task.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 262 insertions(+) create mode 100644 lab1/Makefile create mode 100644 lab1/README create mode 100644 lab1/pi_matlab.m create mode 100644 lab1/pi_mpi.c create mode 100644 lab1/pi_omp.c create mode 100644 lab1/pi_ser.c create mode 100644 lab1/pi_task.c diff --git a/lab1/Makefile b/lab1/Makefile new file mode 100644 index 0000000..aa7f0df --- /dev/null +++ b/lab1/Makefile @@ -0,0 +1,35 @@ +GCC = gcc +MPI = mpicc +CFLAGS = -O3 -fopenmp +OMP_FLAG = -fopenmp +RM = rm -rf + + +EXE = pi_ser pi_omp pi_task pi_mpi + +all : $(EXE) + +#.PHONY: all clean purge + + +pi_ser: pi_ser.o + $(GCC) $(CFLAGS) -o $@ $^ + +pi_task: pi_task.o + $(GCC) $(CFLAGS) -o $@ $^ + +pi_omp: pi_omp.o + $(GCC) $(CFLAGS) $(OMP_FLAG) -o $@ $^ + +pi_mpi: pi_mpi.o + $(MPI) $(CFLAGS) -o $@ $^ + +%.o :%.c + $(GCC) $(CFLAGS) -c -o $@ $< + + +clean: + $(RM) *.o + +purge: clean + $(RM) $(EXE) diff --git a/lab1/README b/lab1/README new file mode 100644 index 0000000..140ab0b --- /dev/null +++ b/lab1/README @@ -0,0 +1,33 @@ +============= +MESOCENTRE FC +============= + +-------------------------LAB 1---------------------------- + +Compute PI with numerical integration (Trapezoidal rule) of: + + / 1 + | 4 + | --------- dx + | 1 + x^2 + / 0 + +Provided solution +----------------- +- pi_ser : serial version +- pi_task : parallel array task version +- pi_omp : parallel OpenMP version +- pi_mpi : parallel MPI version +- pi_matlab : Matlab version + +Howto? +------ +1- use Make to compile +2- submit jobs using SGE +3- For matlab example : + +module load matlab +matlab -nojvm -nodisplay -nosplash -r 'Pi_matlab(100000000);exit' + + +HF diff --git a/lab1/pi_matlab.m b/lab1/pi_matlab.m new file mode 100644 index 0000000..c8433bd --- /dev/null +++ b/lab1/pi_matlab.m @@ -0,0 +1,13 @@ + +function pi = Pi_matlab( n ) + +h = 1.0/n; +pi = 0.0; + +for i=1:n + x = (i + 0.5)*h; + pi = pi+ (4.0/(1.0 + x*x)); + end + pi=pi*h; + disp(pi) +end diff --git a/lab1/pi_mpi.c b/lab1/pi_mpi.c new file mode 100644 index 0000000..e0f3299 --- /dev/null +++ b/lab1/pi_mpi.c @@ -0,0 +1,59 @@ +#include +#include +#include "mpi/mpi.h" +#define N 100000000 + +int main (int argc, char** argv) +{ + int rank; + int size; + long long int n; + long long int i; + + double l_sum,total_sum, x, h; + + MPI_Init(NULL, NULL); + + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + n=N; + + if(rank==0) + { + if(argc==2) + { + n = atoll(argv[1]); + } + + printf("MPI version with process = %d\n", size); + printf("Number of intervals: %lld\n", n); + } + + + MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); + + + h = 1.0/n; + + l_sum = 0.0; + + for (i = rank; i < n; i += size) + { + x = (i + 0.5)*h; + l_sum += 4.0/(1.0 + x*x); + } + + l_sum *= h; + + MPI_Reduce(&l_sum,&total_sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD); + + if(rank==0) + { + printf("Pi=%0.12g\n", total_sum); + + } + + MPI_Finalize(); + return 0; +} diff --git a/lab1/pi_omp.c b/lab1/pi_omp.c new file mode 100644 index 0000000..b3684b5 --- /dev/null +++ b/lab1/pi_omp.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +#define N 100000000 +int main (int argc, char** argv) +{ + long long int n; + long long int i; + + double l_sum, x, h; + + n=N; + + if(argc==2) + { + n=atol(argv[1]); + } + + + h = 1.0/n; + + l_sum = 0.0; + +#pragma omp parallel for private(i,x) reduction(+:l_sum) + for (i = 0; i < n; i ++) + { + x = (i + 0.5)*h; + l_sum += 4.0/(1.0 + x*x); + } + + l_sum *= h; + + printf("N=%lld,PI=%0.12g\n",n, l_sum); + + return 0; +} diff --git a/lab1/pi_ser.c b/lab1/pi_ser.c new file mode 100644 index 0000000..28ed7b5 --- /dev/null +++ b/lab1/pi_ser.c @@ -0,0 +1,35 @@ +#include +#include +#include +#define N 100000000 +int main (int argc, char** argv) +{ + long long int n; + long long int i; + + double l_sum, x, h; + + n=N; + + if(argc==2) + { + n=atol(argv[1]); + } + + + h = 1.0/n; + + l_sum = 0.0; + + for (i = 0; i < n; i ++) + { + x = (i + 0.5)*h; + l_sum += 4.0/(1.0 + x*x); + } + + l_sum *= h; + + printf("N=%lld, PI = %0.12g\n",n ,l_sum); + + return 0; +} diff --git a/lab1/pi_task.c b/lab1/pi_task.c new file mode 100644 index 0000000..6298901 --- /dev/null +++ b/lab1/pi_task.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#define N 100000000 + +int main (int argc, char** argv) +{ + int task_id; + int total_tasks; + long long int n; + long long int i; + + double l_sum, x, h; + + n=N; + + if(argc<3) + { + fprintf(stderr,"Usage : %s task_id total_tasks [n]\n",argv[0]); + exit(0); + } + + task_id = atoi(argv[1]); + total_tasks = atoi(argv[2]); + + if(argc==4) + { + n = atoll(argv[3]); + } + + fprintf(stderr, "task_id=%d total_tasks=%d n=%lld\n", task_id, total_tasks, n); + + h = 1.0/n; + + l_sum = 0.0; + + for (i = task_id; i < n; i += total_tasks) + { + x = (i + 0.5)*h; + l_sum += 4.0/(1.0 + x*x); + } + + l_sum *= h; + + printf("Pi=%0.12g\n", l_sum); + + return 0; +} -- 1.7.10.4