From f8ae49d6668917ff4e046bb814146a61380631ce Mon Sep 17 00:00:00 2001 From: Klagarge Date: Sat, 18 Oct 2025 16:36:29 +0200 Subject: [PATCH] feat(omp): add lab omp realised during classroom --- .../core/omp/02_Slice/01_pi_sequentiel.cpp | 6 ++--- .../02_pi_entrelacer_promotionTab.cpp | 14 +++++------ .../02_Slice/03_pi_entrelacer_critique.cpp | 25 +++++++++++++++++-- .../omp/02_Slice/04_pi_entrelacer_atomic.cpp | 22 ++++++++++++++-- .../omp/02_Slice/07_pi_for_promotionTab.cpp | 13 +++++++++- .../core/omp/02_Slice/08_pi_for_reduction.cpp | 13 +++++++--- 6 files changed, 74 insertions(+), 19 deletions(-) diff --git a/Student_OMP/src/core/omp/02_Slice/01_pi_sequentiel.cpp b/Student_OMP/src/core/omp/02_Slice/01_pi_sequentiel.cpp index 4193561..21ca736 100755 --- a/Student_OMP/src/core/omp/02_Slice/01_pi_sequentiel.cpp +++ b/Student_OMP/src/core/omp/02_Slice/01_pi_sequentiel.cpp @@ -34,14 +34,14 @@ bool isPiSequentiel_OK(int n) \*-------------------------------------*/ double piSequentiel(int n) { - const double delta_x = 1 / (double)n; + const double dx = 1.0 / (double)n; double sum = 0; for (int i = 0; i < n; i++) { - double xi = i * delta_x; + double xi = i * dx; sum += fpi(xi); } - return sum * delta_x; + return sum * dx; } /*----------------------------------------------------------------------*\ diff --git a/Student_OMP/src/core/omp/02_Slice/02_pi_entrelacer_promotionTab.cpp b/Student_OMP/src/core/omp/02_Slice/02_pi_entrelacer_promotionTab.cpp index dcc1dbe..45a9014 100755 --- a/Student_OMP/src/core/omp/02_Slice/02_pi_entrelacer_promotionTab.cpp +++ b/Student_OMP/src/core/omp/02_Slice/02_pi_entrelacer_promotionTab.cpp @@ -45,22 +45,20 @@ double piOMPEntrelacerPromotionTab(int n) double sum[NB_THREAD]; // Reduction intra thread -#pragma omp parallel - { +#pragma omp parallel { const int TID = Omps::getTid(); int s = TID; double sum_thread = 0; - while (s < n) - { - double xi = s * delta_x; - sum_thread += fpi(xi); - s += NB_THREAD; + while (s < n) { + double xi = s * delta_x; + sum_thread += fpi(xi); + s += NB_THREAD; } sum[TID] = sum_thread; - } +} double sumTotal = 0; diff --git a/Student_OMP/src/core/omp/02_Slice/03_pi_entrelacer_critique.cpp b/Student_OMP/src/core/omp/02_Slice/03_pi_entrelacer_critique.cpp index 7941f8a..646d7f8 100755 --- a/Student_OMP/src/core/omp/02_Slice/03_pi_entrelacer_critique.cpp +++ b/Student_OMP/src/core/omp/02_Slice/03_pi_entrelacer_critique.cpp @@ -37,8 +37,29 @@ bool isPiOMPEntrelacerCritical_Ok(int n) double piOMPEntrelacerCritical(int n) { - //TODO - return -1; + const double dx = 1.0 / (double)n; + const int NB_THREAD = Omps::setAndGetNaturalGranularity(); + int total = 0; + +#pragma omp parallel + { + const int TID = Omps::getTid(); + int s = TID; + + double sum_thread = 0; + + while (s < n) + { + double xi = s*dx; + sum_thread += fpi(xi); + s+= NB_THREAD; + } +#pragma omp critical + { + total += sum_thread; + } + } + return total * dx; } /*----------------------------------------------------------------------*\ diff --git a/Student_OMP/src/core/omp/02_Slice/04_pi_entrelacer_atomic.cpp b/Student_OMP/src/core/omp/02_Slice/04_pi_entrelacer_atomic.cpp index 432f975..b356a8f 100755 --- a/Student_OMP/src/core/omp/02_Slice/04_pi_entrelacer_atomic.cpp +++ b/Student_OMP/src/core/omp/02_Slice/04_pi_entrelacer_atomic.cpp @@ -40,8 +40,26 @@ bool isPiOMPEntrelacerAtomic_Ok(int n) */ double piOMPEntrelacerAtomic(int n) { - // TODO - return -1; + const double dx = 1.0 / (double)n; + const int NB_THREAD = Omps::setAndGetNaturalGranularity(); + int total = 0; + +#pragma omp parallel + { + const int TID = Omps::getTid(); + int s = TID; + double sum_thread = 0; + while (s < n) + { + double xi = s * dx; + sum_thread += fpi(xi); + s += NB_THREAD; + } +#pragma omp atomic + total += sum_thread; + + } + return total * dx; } /*----------------------------------------------------------------------*\ diff --git a/Student_OMP/src/core/omp/02_Slice/07_pi_for_promotionTab.cpp b/Student_OMP/src/core/omp/02_Slice/07_pi_for_promotionTab.cpp index 9e71d20..a9d1453 100755 --- a/Student_OMP/src/core/omp/02_Slice/07_pi_for_promotionTab.cpp +++ b/Student_OMP/src/core/omp/02_Slice/07_pi_for_promotionTab.cpp @@ -43,7 +43,18 @@ bool isPiOMPforPromotionTab_Ok(int n) */ double piOMPforPromotionTab(int n) { - //TODO + const double dx = 1.0 / (double)n; + const int NB_THREAD = Omps::setAndGetNaturalGranularity(); + double total[NB_THREAD] = {0.0}; + +#pragma omp parallel for + for(int i = 0; i < n; i++) { + const int TID = Omps::getTid(); +// double xi = s*dx; +// total[TID] += fpi(xi); + } + + return -1; } diff --git a/Student_OMP/src/core/omp/02_Slice/08_pi_for_reduction.cpp b/Student_OMP/src/core/omp/02_Slice/08_pi_for_reduction.cpp index 114acf3..686a209 100755 --- a/Student_OMP/src/core/omp/02_Slice/08_pi_for_reduction.cpp +++ b/Student_OMP/src/core/omp/02_Slice/08_pi_for_reduction.cpp @@ -41,11 +41,18 @@ bool isPiOMPforReduction_Ok(int n) */ double piOMPforReduction(int n) { - //TODO - return -1; + const double dx = 1.0 / (double)n; + double sum = 0; +#pragma omp parallel for reduction(+:sum) + for (int i = 0; i < n; i++) + { + double xi = i * dx; + sum += fpi(xi); + } + + return sum * dx; } /*----------------------------------------------------------------------*\ |* End *| \*---------------------------------------------------------------------*/ -