Ajout de l'ensemble du workspace.
[GPU.git] / WCudaMSE / Student_OMP / src / cpp / core / omp / 02_pi / 04_pi_entrelacer_atomic.cpp
1 #include <omp.h>
2 #include "00_pi_tools.h"
3 #include "OmpTools.h"
4
5 /*----------------------------------------------------------------------*\
6 |* Declaration *|
7 \*---------------------------------------------------------------------*/
8
9 /*--------------------------------------*\
10 |* Imported *|
11 \*-------------------------------------*/
12
13
14 /*--------------------------------------*\
15 |* Public *|
16 \*-------------------------------------*/
17
18 bool isPiOMPEntrelacerAtomic_Ok(int n);
19
20 /*--------------------------------------*\
21 |* Private *|
22 \*-------------------------------------*/
23
24 static double piOMPEntrelacerAtomic(int n);
25
26 /*----------------------------------------------------------------------*\
27 |* Implementation *|
28 \*---------------------------------------------------------------------*/
29
30 /*--------------------------------------*\
31 |* Public *|
32 \*-------------------------------------*/
33
34 bool isPiOMPEntrelacerAtomic_Ok(int n)
35 {
36 return isAlgoPI_OK(piOMPEntrelacerAtomic, n, "Pi OMP Entrelacer atomic");
37 }
38
39 /*--------------------------------------*\
40 |* Private *|
41 \*-------------------------------------*/
42
43 /**
44 * Bonne performance, si!
45 */
46 double piOMPEntrelacerAtomic(int n)
47 {
48 const int NB_THREAD = OmpTools::setAndGetNaturalGranularity();
49 const double DX = 1.0/(double)n;
50 double sum = 0;
51
52 #pragma omp parallel
53 {
54 double xs;
55 double sumLocalThread = 0;
56 const int TID = OmpTools::getTid();
57 int s = TID;
58
59 while (s < n)
60 {
61 xs = s * DX;
62 sumLocalThread += fpi(xs);
63 s += NB_THREAD;
64 }
65
66 #pragma omp atomic
67 sum += sumLocalThread;
68
69 }
70
71 return sum * DX;
72 }
73
74 /*----------------------------------------------------------------------*\
75 |* End *|
76 \*---------------------------------------------------------------------*/
77