From be7cff155ee70d8f2126d2648aaea872a1f7763d Mon Sep 17 00:00:00 2001 From: Quentin Fortier Date: Thu, 29 Aug 2024 21:13:32 +0200 Subject: [PATCH] Update --- .../0_Rappels de programmation/2_debug.md | 9 - .../1_rappels_programmation.md} | 2 +- .../1_resume.md | 0 docs/concurrence/0_Rappels/2_debug.md | 35 ++++ .../rappels_programmation.pdf | Bin .../resume_prog.pdf | Bin .../1_Concurrence/1_concurrence.md | 9 + docs/concurrence/1_Concurrence/2_code.md | 184 ++++++++++++++++++ .../{ => 1_Concurrence}/concurrence.pdf | Bin docs/concurrence/1_concurrence.md | 73 ------- docusaurus.config.ts | 11 +- sidebars.ts | 2 +- 12 files changed, 238 insertions(+), 87 deletions(-) delete mode 100644 docs/concurrence/0_Rappels de programmation/2_debug.md rename docs/concurrence/{0_Rappels de programmation/0_rappels_programmation.md => 0_Rappels/1_rappels_programmation.md} (89%) rename docs/concurrence/{0_Rappels de programmation => 0_Rappels}/1_resume.md (100%) create mode 100644 docs/concurrence/0_Rappels/2_debug.md rename docs/concurrence/{0_Rappels de programmation => 0_Rappels}/rappels_programmation.pdf (100%) rename docs/concurrence/{0_Rappels de programmation => 0_Rappels}/resume_prog.pdf (100%) create mode 100644 docs/concurrence/1_Concurrence/1_concurrence.md create mode 100644 docs/concurrence/1_Concurrence/2_code.md rename docs/concurrence/{ => 1_Concurrence}/concurrence.pdf (100%) delete mode 100644 docs/concurrence/1_concurrence.md diff --git a/docs/concurrence/0_Rappels de programmation/2_debug.md b/docs/concurrence/0_Rappels de programmation/2_debug.md deleted file mode 100644 index 176152a..0000000 --- a/docs/concurrence/0_Rappels de programmation/2_debug.md +++ /dev/null @@ -1,9 +0,0 @@ ---- -hide_table_of_contents: true -hide_title: true -title: Débugueur C ---- - -
- -
diff --git a/docs/concurrence/0_Rappels de programmation/0_rappels_programmation.md b/docs/concurrence/0_Rappels/1_rappels_programmation.md similarity index 89% rename from docs/concurrence/0_Rappels de programmation/0_rappels_programmation.md rename to docs/concurrence/0_Rappels/1_rappels_programmation.md index 993572a..e09b6d3 100644 --- a/docs/concurrence/0_Rappels de programmation/0_rappels_programmation.md +++ b/docs/concurrence/0_Rappels/1_rappels_programmation.md @@ -1,7 +1,7 @@ --- hide_table_of_contents: true hide_title: true -title: Erreurs classiques +title: Rappels de programmation ---
diff --git a/docs/concurrence/0_Rappels de programmation/1_resume.md b/docs/concurrence/0_Rappels/1_resume.md similarity index 100% rename from docs/concurrence/0_Rappels de programmation/1_resume.md rename to docs/concurrence/0_Rappels/1_resume.md diff --git a/docs/concurrence/0_Rappels/2_debug.md b/docs/concurrence/0_Rappels/2_debug.md new file mode 100644 index 0000000..a9bbc22 --- /dev/null +++ b/docs/concurrence/0_Rappels/2_debug.md @@ -0,0 +1,35 @@ +--- +hide_table_of_contents: true +hide_title: true +title: Débugueur C +--- + +
+ +
+ +```c +#include +#include + +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; +} +``` \ No newline at end of file diff --git a/docs/concurrence/0_Rappels de programmation/rappels_programmation.pdf b/docs/concurrence/0_Rappels/rappels_programmation.pdf similarity index 100% rename from docs/concurrence/0_Rappels de programmation/rappels_programmation.pdf rename to docs/concurrence/0_Rappels/rappels_programmation.pdf diff --git a/docs/concurrence/0_Rappels de programmation/resume_prog.pdf b/docs/concurrence/0_Rappels/resume_prog.pdf similarity index 100% rename from docs/concurrence/0_Rappels de programmation/resume_prog.pdf rename to docs/concurrence/0_Rappels/resume_prog.pdf diff --git a/docs/concurrence/1_Concurrence/1_concurrence.md b/docs/concurrence/1_Concurrence/1_concurrence.md new file mode 100644 index 0000000..03ba52d --- /dev/null +++ b/docs/concurrence/1_Concurrence/1_concurrence.md @@ -0,0 +1,9 @@ +--- +hide_table_of_contents: true +hide_title: true +title: Concurrence et parallélisme +--- + +
+ +
diff --git a/docs/concurrence/1_Concurrence/2_code.md b/docs/concurrence/1_Concurrence/2_code.md new file mode 100644 index 0000000..5521273 --- /dev/null +++ b/docs/concurrence/1_Concurrence/2_code.md @@ -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'; + + + + +```c +#include // threads POSIX (standard Linux) +#include + +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` + + + + +```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` + + + + +## Incrémentation d'un compteur + + + + +```c +#include +#include + +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; +} +``` + + + + +```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 () +``` + + + + + + + + +```c title="Produit de matrice" +#include +#include +#include +#include +#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; +} +``` diff --git a/docs/concurrence/concurrence.pdf b/docs/concurrence/1_Concurrence/concurrence.pdf similarity index 100% rename from docs/concurrence/concurrence.pdf rename to docs/concurrence/1_Concurrence/concurrence.pdf diff --git a/docs/concurrence/1_concurrence.md b/docs/concurrence/1_concurrence.md deleted file mode 100644 index 9caa4af..0000000 --- a/docs/concurrence/1_concurrence.md +++ /dev/null @@ -1,73 +0,0 @@ ---- -hide_table_of_contents: true -hide_title: true -title: Concurrence et parallélisme ---- - -
- -
- -## Produit de matrice - -```c -#include -#include -#include -#include -#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; -} -``` diff --git a/docusaurus.config.ts b/docusaurus.config.ts index ff63063..8750efb 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -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', }, { @@ -88,6 +92,7 @@ const config: Config = { prism: { theme: prismThemes.github, darkTheme: prismThemes.dracula, + additionalLanguages: ['ocaml'], }, } satisfies Preset.ThemeConfig, }; diff --git a/sidebars.ts b/sidebars.ts index 02ef950..37a0a3c 100644 --- a/sidebars.ts +++ b/sidebars.ts @@ -1,6 +1,6 @@ const sidebars = {}; -["programmation", "concurrence"].forEach((e) => { +["concurrence"].forEach((e) => { sidebars[e] = [ { type: "autogenerated",