Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
fortierq committed Aug 29, 2024
1 parent 242c174 commit be7cff1
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 87 deletions.
9 changes: 0 additions & 9 deletions docs/concurrence/0_Rappels de programmation/2_debug.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
hide_table_of_contents: true
hide_title: true
title: Erreurs classiques
title: Rappels de programmation
---

<div class="container4x3">
Expand Down
File renamed without changes.
35 changes: 35 additions & 0 deletions docs/concurrence/0_Rappels/2_debug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
hide_table_of_contents: true
hide_title: true
title: Débugueur C
---

<div class="container16x9">
<iframe src="https://www.youtube.com/embed/ua2RgVO-s6U" class="responsive-iframe" title="YouTube video player" allowFullScreen></iframe>
</div>

```c
#include <stdbool.h>
#include <stdio.h>

bool dichotomie(int* t, int e, int i, int j) {
if (i > j) {
return false;
}
int m = (i + j) / 2;
if (t[m] == e) {
return true;
}
if (t[m] < e) {
return dichotomie(t, e, m + 1, j);
}
return dichotomie(t, e, i, m - 1);
}

int main() {
int t[10] = {-4, -2, 0, 1, 3, 5, 7, 9, 11, 13};
printf("%d\n", dichotomie(t, 13, 0, 10));
printf("%d\n", dichotomie(t, 13, 0, 9));
return 0;
}
```
File renamed without changes.
9 changes: 9 additions & 0 deletions docs/concurrence/1_Concurrence/1_concurrence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
hide_table_of_contents: true
hide_title: true
title: Concurrence et parallélisme
---

<div class="container4x3">
<iframe src={require('./concurrence.pdf#zoom=page-fit&pagemode=none').default + "#zoom=page-fit&pagemode=none"} class="responsive-iframe" allowFullScreen></iframe>
</div>
184 changes: 184 additions & 0 deletions docs/concurrence/1_Concurrence/2_code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
---
hide_table_of_contents: false
hide_title: true
title: Code
---

# Exemples avec threads

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs>
<TabItem value="c" label="C">

```c
#include <pthread.h> // threads POSIX (standard Linux)
#include <stdio.h>

void *f(void *x) {
int *n = (int *)x; // Conversion du type
for(int i = 0; i < 100000; i++) {
if(i % 20000 == 0)
printf("%d %d\n", *n, i);
}
}

int main() {
pthread_t t1, t2;
int n1 = 1, n2 = 2;
pthread_create(&t1, NULL, f, (void *)&n1);
pthread_create(&t2, NULL, f, (void *)&n2);
pthread_join(t1, NULL); // Attendre la fin de t1
pthread_join(t2, NULL); // Attendre la fin de t2
}
```
Compilation : `gcc -pthread exemple.c`
</TabItem>
<TabItem value="OCaml" label="OCaml">
```ocaml
let counter = ref 0
let increment n =
for i = 0 to n - 1 do
counter := !counter + 1;
done
let main () =
let n = 1_000_000 in
let t0 = Thread.create increment n in
let t1 = Thread.create increment n in
Thread.join t0;
Thread.join t1;
Printf.printf "counter = %d\n" !counter
let () = main ()
```

Compilation : `ocamlopt -I +unix -I +threads unix.cmxa threads.cmxa exemple.ml`

</TabItem>
</Tabs>

## Incrémentation d'un compteur

<Tabs>
<TabItem value="c" label="C">

```c
#include <stdio.h>
#include <pthread.h>

int counter;

void *increment(void *arg){
for (int i = 1; i <= 1000000; i++) {
counter++;
}
return NULL;
}

int main(void){
counter = 0;
pthread_t t1, t2;
pthread_create(&t1, NULL, increment, NULL);
pthread_create(&t2, NULL, increment, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("Counter = %d\n", counter);
return 0;
}
```
</TabItem>
<TabItem value="OCaml" label="OCaml">
```ocaml
let counter = ref 0
let increment n =
for i = 0 to n - 1 do
counter := !counter + 1;
done
let main () =
let n = 1_000_000 in
let t0 = Thread.create increment n in
let t1 = Thread.create increment n in
Thread.join t0;
Thread.join t1;
Printf.printf "counter = %d\n" !counter
let () = main ()
```

</TabItem>
</Tabs>





```c title="Produit de matrice"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define N 1000
#define NTHREADS 1000

long long timeInMilliseconds(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);
}

int u[N][N], v[N][N], w[N][N];

void* sum(void* arg) {
int a = *(int*)arg;
for(int b = a; b < N*N; b += NTHREADS) {
int i = b / N;
int j = b % N;
w[i][j] = 0;
for(int k = 0; k < N; k++) {
w[i][j] += u[i][k] * v[k][j];
}
}
return NULL;
}

int main(void) {
pthread_t threads[N*N];
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
u[i][j] = i;
v[i][j] = j;
}
}
long long t = timeInMilliseconds();
for(int i = 0; i < NTHREADS; i++) {
int* arg = malloc(sizeof(int));
*arg = i;
pthread_create(&threads[i], NULL, sum, arg);
free(arg);
}
for(int i = 0; i < NTHREADS; i++) {
pthread_join(threads[i], NULL);
}
printf("En parallèle : %lld ms\n", timeInMilliseconds() - t);
t = timeInMilliseconds();
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
w[i][j] = 0;
for(int k = 0; k < N; k++) {
w[i][j] += u[i][k] * v[k][j];
}
}
}
printf("En séquentiel : %lld ms\n", timeInMilliseconds() - t);
return 0;
}
```
File renamed without changes.
73 changes: 0 additions & 73 deletions docs/concurrence/1_concurrence.md

This file was deleted.

11 changes: 8 additions & 3 deletions docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ const config: Config = {
{to: 'https://prepas.org/index.php?document=73', label: 'Programme officiel'},
{
type: "doc",
docId: "concurrence/concurrence",
docId: "concurrence/Concurrence/concurrence",
position: "left",
label: "Concurrence",
sidebarId: "concurrence",
},
{
href: 'https://mpi-lamartin.github.io',
label: 'Site de la classe',
label: 'Classe',
position: 'right',
},
{
href: 'https://mp2i-info.github.io',
label: 'MP2I',
position: 'right',
},
{
Expand All @@ -88,6 +92,7 @@ const config: Config = {
prism: {
theme: prismThemes.github,
darkTheme: prismThemes.dracula,
additionalLanguages: ['ocaml'],
},
} satisfies Preset.ThemeConfig,
};
Expand Down
2 changes: 1 addition & 1 deletion sidebars.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const sidebars = {};

["programmation", "concurrence"].forEach((e) => {
["concurrence"].forEach((e) => {
sidebars[e] = [
{
type: "autogenerated",
Expand Down

0 comments on commit be7cff1

Please sign in to comment.