Commit 6a98a5afa7035c2b05e0855df8839e5030abd85b
0 parents
Exists in
master
lab1
Showing 7 changed files with 262 additions and 0 deletions Inline Diff
lab1/Makefile
View file @
6a98a5a
File was created | 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 $@ $^ |
lab1/README
View file @
6a98a5a
File was created | 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 |
lab1/pi_matlab.m
View file @
6a98a5a
File was created | 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 |
lab1/pi_mpi.c
View file @
6a98a5a
File was created | 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) |
lab1/pi_omp.c
View file @
6a98a5a
File was created | 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 ++) |
lab1/pi_ser.c
View file @
6a98a5a
File was created | 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 | { |
lab1/pi_task.c
View file @
6a98a5a
File was created | 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 |