Commit 6a98a5afa7035c2b05e0855df8839e5030abd85b

Authored by kmazouzi
0 parents
Exists in master

lab1

Showing 7 changed files with 262 additions and 0 deletions Side-by-side Diff

lab1/Makefile View file @ 6a98a5a
... ... @@ -0,0 +1,35 @@
  1 +GCC = gcc
  2 +MPI = mpicc
  3 +CFLAGS = -O3 -fopenmp
  4 +OMP_FLAG = -fopenmp
  5 +RM = rm -rf
  6 +
  7 +
  8 +EXE = pi_ser pi_omp pi_task pi_mpi
  9 +
  10 +all : $(EXE)
  11 +
  12 +#.PHONY: all clean purge
  13 +
  14 +
  15 +pi_ser: pi_ser.o
  16 + $(GCC) $(CFLAGS) -o $@ $^
  17 +
  18 +pi_task: pi_task.o
  19 + $(GCC) $(CFLAGS) -o $@ $^
  20 +
  21 +pi_omp: pi_omp.o
  22 + $(GCC) $(CFLAGS) $(OMP_FLAG) -o $@ $^
  23 +
  24 +pi_mpi: pi_mpi.o
  25 + $(MPI) $(CFLAGS) -o $@ $^
  26 +
  27 +%.o :%.c
  28 + $(GCC) $(CFLAGS) -c -o $@ $<
  29 +
  30 +
  31 +clean:
  32 + $(RM) *.o
  33 +
  34 +purge: clean
  35 + $(RM) $(EXE)
... ... @@ -0,0 +1,33 @@
  1 +=============
  2 +MESOCENTRE FC
  3 +=============
  4 +
  5 +-------------------------LAB 1----------------------------
  6 +
  7 +Compute PI with numerical integration (Trapezoidal rule) of:
  8 +
  9 + / 1
  10 + | 4
  11 + | --------- dx
  12 + | 1 + x^2
  13 + / 0
  14 +
  15 +Provided solution
  16 +-----------------
  17 +- pi_ser : serial version
  18 +- pi_task : parallel array task version
  19 +- pi_omp : parallel OpenMP version
  20 +- pi_mpi : parallel MPI version
  21 +- pi_matlab : Matlab version
  22 +
  23 +Howto?
  24 +------
  25 +1- use Make to compile
  26 +2- submit jobs using SGE
  27 +3- For matlab example :
  28 +
  29 +module load matlab
  30 +matlab -nojvm -nodisplay -nosplash -r 'Pi_matlab(100000000);exit'
  31 +
  32 +
  33 +HF
lab1/pi_matlab.m View file @ 6a98a5a
... ... @@ -0,0 +1,13 @@
  1 +
  2 +function pi = Pi_matlab( n )
  3 +
  4 +h = 1.0/n;
  5 +pi = 0.0;
  6 +
  7 +for i=1:n
  8 + x = (i + 0.5)*h;
  9 + pi = pi+ (4.0/(1.0 + x*x));
  10 + end
  11 + pi=pi*h;
  12 + disp(pi)
  13 +end
lab1/pi_mpi.c View file @ 6a98a5a
... ... @@ -0,0 +1,59 @@
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include "mpi/mpi.h"
  4 +#define N 100000000
  5 +
  6 +int main (int argc, char** argv)
  7 +{
  8 + int rank;
  9 + int size;
  10 + long long int n;
  11 + long long int i;
  12 +
  13 + double l_sum,total_sum, x, h;
  14 +
  15 + MPI_Init(NULL, NULL);
  16 +
  17 + MPI_Comm_size(MPI_COMM_WORLD, &size);
  18 + MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  19 +
  20 + n=N;
  21 +
  22 + if(rank==0)
  23 + {
  24 + if(argc==2)
  25 + {
  26 + n = atoll(argv[1]);
  27 + }
  28 +
  29 + printf("MPI version with process = %d\n", size);
  30 + printf("Number of intervals: %lld\n", n);
  31 + }
  32 +
  33 +
  34 + MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  35 +
  36 +
  37 + h = 1.0/n;
  38 +
  39 + l_sum = 0.0;
  40 +
  41 + for (i = rank; i < n; i += size)
  42 + {
  43 + x = (i + 0.5)*h;
  44 + l_sum += 4.0/(1.0 + x*x);
  45 + }
  46 +
  47 + l_sum *= h;
  48 +
  49 + MPI_Reduce(&l_sum,&total_sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
  50 +
  51 + if(rank==0)
  52 + {
  53 + printf("Pi=%0.12g\n", total_sum);
  54 +
  55 + }
  56 +
  57 + MPI_Finalize();
  58 + return 0;
  59 +}
lab1/pi_omp.c View file @ 6a98a5a
... ... @@ -0,0 +1,38 @@
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <stdlib.h>
  4 +#include <omp.h>
  5 +
  6 +#define N 100000000
  7 +int main (int argc, char** argv)
  8 +{
  9 + long long int n;
  10 + long long int i;
  11 +
  12 + double l_sum, x, h;
  13 +
  14 + n=N;
  15 +
  16 + if(argc==2)
  17 + {
  18 + n=atol(argv[1]);
  19 + }
  20 +
  21 +
  22 + h = 1.0/n;
  23 +
  24 + l_sum = 0.0;
  25 +
  26 +#pragma omp parallel for private(i,x) reduction(+:l_sum)
  27 + for (i = 0; i < n; i ++)
  28 + {
  29 + x = (i + 0.5)*h;
  30 + l_sum += 4.0/(1.0 + x*x);
  31 + }
  32 +
  33 + l_sum *= h;
  34 +
  35 + printf("N=%lld,PI=%0.12g\n",n, l_sum);
  36 +
  37 + return 0;
  38 +}
lab1/pi_ser.c View file @ 6a98a5a
... ... @@ -0,0 +1,35 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <string.h>
  4 +#define N 100000000
  5 +int main (int argc, char** argv)
  6 +{
  7 + long long int n;
  8 + long long int i;
  9 +
  10 + double l_sum, x, h;
  11 +
  12 + n=N;
  13 +
  14 + if(argc==2)
  15 + {
  16 + n=atol(argv[1]);
  17 + }
  18 +
  19 +
  20 + h = 1.0/n;
  21 +
  22 + l_sum = 0.0;
  23 +
  24 + for (i = 0; i < n; i ++)
  25 + {
  26 + x = (i + 0.5)*h;
  27 + l_sum += 4.0/(1.0 + x*x);
  28 + }
  29 +
  30 + l_sum *= h;
  31 +
  32 + printf("N=%lld, PI = %0.12g\n",n ,l_sum);
  33 +
  34 + return 0;
  35 +}
lab1/pi_task.c View file @ 6a98a5a
... ... @@ -0,0 +1,49 @@
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <stdlib.h>
  4 +
  5 +#define N 100000000
  6 +
  7 +int main (int argc, char** argv)
  8 +{
  9 + int task_id;
  10 + int total_tasks;
  11 + long long int n;
  12 + long long int i;
  13 +
  14 + double l_sum, x, h;
  15 +
  16 + n=N;
  17 +
  18 + if(argc<3)
  19 + {
  20 + fprintf(stderr,"Usage : %s task_id total_tasks [n]\n",argv[0]);
  21 + exit(0);
  22 + }
  23 +
  24 + task_id = atoi(argv[1]);
  25 + total_tasks = atoi(argv[2]);
  26 +
  27 + if(argc==4)
  28 + {
  29 + n = atoll(argv[3]);
  30 + }
  31 +
  32 + fprintf(stderr, "task_id=%d total_tasks=%d n=%lld\n", task_id, total_tasks, n);
  33 +
  34 + h = 1.0/n;
  35 +
  36 + l_sum = 0.0;
  37 +
  38 + for (i = task_id; i < n; i += total_tasks)
  39 + {
  40 + x = (i + 0.5)*h;
  41 + l_sum += 4.0/(1.0 + x*x);
  42 + }
  43 +
  44 + l_sum *= h;
  45 +
  46 + printf("Pi=%0.12g\n", l_sum);
  47 +
  48 + return 0;
  49 +}