Skip to content

Commit

Permalink
adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
adelbke committed Jan 13, 2022
1 parent 9692b06 commit fc8fea3
Show file tree
Hide file tree
Showing 17 changed files with 569 additions and 3 deletions.
Binary file added a.out
Binary file not shown.
67 changes: 64 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Même tâche que cae.sh sauf que ce dernier compile avec *-fopenmp* avant d'exé

## Exercices

### Exo1
### TP1
#### Exo1

- Les messages du thread ne s'ont pas affichés car le thread principal a terminé suffisamment rapidement avant que le thread crée puisse afficher les messages nécessaires
- Le rôle de la fonction thread_join est d'attendre la fin d'exécution du thread crée poour continuer ensuite l'execution
Expand All @@ -22,6 +23,66 @@ Même tâche que cae.sh sauf que ce dernier compile avec *-fopenmp* avant d'exé
- ![console output](scripts/td1/exo1/comp_seq_para.jpg)


### Exo2
#### Exo2

- ![console output](scripts/td1/exo2/time_comparison.jpg)

### TP2 (Open MP)

#### Exercice 1

1. [code](scripts/td2/exo1/sections.c) Modifier le programme parallèle pour ajouter des sections en utilisant la directive omp parallel
sections (autant de sections que de threads). Créer deux sections une pour un thread qui va afficherhello (num thread) et une qui affichera world (num thread). Tester avec nombre de threads =2 , 4

- nombre de threads est 2
![console output](scripts/td2/exo1/hello_world_threads_sections.png)

- nombre de threads est 4
![console output](scripts/td2/exo1/hello_world_threads4_sections.png)

2. [code](scripts/td2/exo1/test_prive.c) Tester les variables partagées, privées. Déclarer une variable a avant la section parallèle,
l’initialiser à 5 (par défaut elle sera partagée !). Dans la région parallèle, section 1, un des threads
va modifier la valeur de a un autre thread qui exécute la deuxième section va juste l’afficher. Tester
avec et sans utilisation de la clause critical.

- Test sans la clause critical
![console output](scripts/td2/exo1/test_partage_no_critical.png)

- Test Avec la clause critical

3. [code](scripts/td2/exo1/clause_private.c) Maintenant, toujours la variable a, on va la déclarer private pour chaque thread (clause private(a)).
Afficher la valeur de a au début et à la fin de chaque section (ajouter sleep pour ralentir
l’exécution).

- Affichage de a avant et après chaque section
![console output](scripts/td2/exo1/test_clause_private.png)

#### Exercice 2

1. Les instructions executées par tous les threads sont:
- `tid = omp_get_thread_num();`
- `printf("Thread %d: starting ...\n", tid);`
- ```C
for (size_t i = 0; i < SIZE; i++) {
c[i] = a[i] + b[i];
printf("Thread %d: c[%2zu] = %g\n", tid ,i, c[i]);
}
```
Les instructions Executés que par un seul thread (dans ce cas le Main thread)

- ```C
if (tid == 0)
printf("Nb threads = %d\n", omp_get_num_threads());
```
2. l'ordre d'exécution des instructions parait aléatoire et changes à chaque itération

3. En ajoutant un pipe après le programme vers sort on obtiens [Sortie standard trié](scripts/td2/exo2/sortie_standard_trie.txt)

4. La répartition des itérations n'est pas stable, la charge de travaille n'est pas distribué de manière uniforme

5. Oui la répartition est stable en utilisant la politique d'ordonnancement static [Sortie standard trié](scripts/td2/exo2/sortie_standard_trie_static.txt)

6. Le Choix de la politique d'ordonnancement est important car un mauvais choix peux engendrer la création de threads qui exécutent trop peu de tâches pour compenser le *temps perdu* de leur création où encore pire qu'ils n'aient même pas le temps de traiter comme dans le cas actuel

#### Exercice 3

- ![console output](scripts/td1/exo2/time_comparison.jpg)
29 changes: 29 additions & 0 deletions scripts/exercises/exo2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>

float i_func(float x){
return 4.0/(1.0 + x * x);
}

int main(int argc, char const *argv[])
{
float start = 0, stop = 1;
float range = stop - start;
int interval_count = 100;
float interval_width = range / (float) interval_count;

float result = 0.0;

for (int i = 0; i < interval_count; i++)
{
float curr_start = start + (interval_width * (float) i);
result += i_func(curr_start) * interval_width;
printf("\nResult[%d]= %f",i,result);

}

printf("\nResult: %f", result);

return 0;
}
45 changes: 45 additions & 0 deletions scripts/exercises/exo2_par.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#define steps 100
#define thread_num 4
float i_func(float x){
return 4.0/(1.0 + x * x);
}

int main(int argc, char const *argv[])
{
omp_set_num_threads(thread_num);

float start = 0, stop = 1;
float interval_width = (stop - start) / (float) steps;

float* results = malloc(sizeof(float) * thread_num);

float sum= 0.0;

#pragma omp parallel
{
int p_id = omp_get_thread_num();
float result = 0.0;
for (int i = p_id * (steps / thread_num); i < (p_id + 1) * (steps / thread_num); i++)
{
result += i_func(start + (interval_width * (float) i )) * interval_width;
}

#pragma omp critical
sum += result;
}

// for (int i = 0; i < thread_num; i++)
// {
// /* code */
// printf("\nResult[%d]= %f",i,results[i]);
// sum+= results[i];
// }

printf("\nResult: %f", sum);


return 0;
}
24 changes: 24 additions & 0 deletions scripts/mpi/produit_matrice.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <math.h>
#include <mpi.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
int rank;

MPI_Init(NULL, NULL);

// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

// Get the rank of the process
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

printf("Hello world from processor, rank %d out of %d processors\n", rank, world_size);

MPI_Finalize();

}
26 changes: 26 additions & 0 deletions scripts/td2/exo1/clause_private.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>



int main(int argc, char const *argv[])
{
int a = 5;

#pragma omp parallel sections private(a)
{
#pragma omp section
{
printf("\nA avant section 1: %d",a);
a = 15;
printf("\nA après section 1: %d",a);
}

#pragma omp section
{
printf("\n Affichage: %d", a);
}
}
return 0;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/td2/exo1/hello_world_threads_sections.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions scripts/td2/exo1/sections.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>



int main(int argc, char const *argv[])
{
// Two threads
// omp_set_num_threads(2);
// Four threads
omp_set_num_threads(4);
#pragma omp parallel sections
{
#pragma omp section
{
printf("\n Hello %d",omp_get_thread_num());
}

#pragma omp section
{
printf("\n World %d", omp_get_thread_num());
}
}
return 0;
}
Binary file added scripts/td2/exo1/test_clause_private.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/td2/exo1/test_partage_no_critical.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions scripts/td2/exo1/test_prive.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>



int main(int argc, char const *argv[])
{
int a = 5;

#pragma omp parallel sections
{
#pragma omp section
{
// with critical or not the output is the same, misunderstanding ?
#pragma omp critical
{
a = 15;
}
}

#pragma omp section
{
printf("\n Affichage: %d", a);
}
}
return 0;
}
25 changes: 25 additions & 0 deletions scripts/td2/exo2/schedule_clause.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <omp.h>
#define SIZE 100
#define CHUNK 10
int main()
{
int tid;
double a[SIZE], b[SIZE], c[SIZE];
for (size_t i = 0; i < SIZE; i++)
a[i] = b[i] = i;
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
if (tid == 0)
printf("Nb threads = %d\n", omp_get_num_threads());
printf("Thread %d: starting ...\n", tid);
#pragma omp for schedule(static, CHUNK)
for (size_t i = 0; i < SIZE; i++)
{
c[i] = a[i] + b[i];
printf("Thread %d: c[%2zu] = %g\n", tid, i, c[i]);
}
}
return 0;
}
Loading

0 comments on commit fc8fea3

Please sign in to comment.