Initialize le tableau à zero.
[GPU.git] / WCudaMSE / Student_OMP / src / cpp / core / omp / 02_pi / 02_pi_entrelacer_promotionTab.cpp
1 #include <omp.h>
2
3 #include <cstring>
4
5 #include "00_pi_tools.h"
6 #include "OmpTools.h"
7
8 /*----------------------------------------------------------------------*\
9 |* Declaration *|
10 \*---------------------------------------------------------------------*/
11
12 /*--------------------------------------*\
13 |* Imported *|
14 \*-------------------------------------*/
15
16
17 /*--------------------------------------*\
18 |* Public *|
19 \*-------------------------------------*/
20
21 bool isPiOMPEntrelacerPromotionTab_Ok(int n);
22
23 /*--------------------------------------*\
24 |* Private *|
25 \*-------------------------------------*/
26
27 static double piOMPEntrelacerPromotionTab(int n);
28
29 /*----------------------------------------------------------------------*\
30 |* Implementation *|
31 \*---------------------------------------------------------------------*/
32
33 /*--------------------------------------*\
34 |* Public *|
35 \*-------------------------------------*/
36
37 bool isPiOMPEntrelacerPromotionTab_Ok(int n)
38 {
39 return isAlgoPI_OK(piOMPEntrelacerPromotionTab, n, "Pi OMP Entrelacer promotionTab");
40 }
41
42 /*--------------------------------------*\
43 |* Private *|
44 \*-------------------------------------*/
45
46 /**
47 * pattern cuda : excellent!
48 */
49 double piOMPEntrelacerPromotionTab(int n)
50 {
51 const int NB_THREAD = OmpTools::setAndGetNaturalGranularity();
52
53 double* tabSums = new double[NB_THREAD];
54 memset(tabSums, 0, NB_THREAD * sizeof(double));
55
56 const double DX = 1.0/(double)n;
57
58 #pragma omp parallel
59 {
60 double xs;
61 const int TID = OmpTools::getTid();
62 int s = TID;
63
64 while (s < n)
65 {
66 xs = s * DX;
67 tabSums[TID] += fpi(xs);
68
69 s += NB_THREAD;
70 }
71 }
72
73 double sum = 0;
74 for (int i = 0; i < NB_THREAD; i++)
75 sum += tabSums[i];
76
77 delete[] tabSums;
78
79 return sum * DX;
80 }
81
82 /*----------------------------------------------------------------------*\
83 |* End *|
84 \*---------------------------------------------------------------------*/
85