Implémentation de RipplingMOO (entralecement + auto-for).
[GPU.git] / WCudaMSE / Tuto_Image / src / cpp / core / 01_Vague / b_moo / VagueMOO.cpp
1 #include <iostream>
2 #include <omp.h>
3 #include <math.h>
4
5 #include "VagueMOO.h"
6 #include "VagueMath.h"
7
8 #include "OmpTools.h"
9 #include "IndiceTools.h"
10
11 using std::cout;
12 using std::endl;
13
14 /*----------------------------------------------------------------------*\
15 |* Declaration *|
16 \*---------------------------------------------------------------------*/
17
18 /*--------------------------------------*\
19 |* Public *|
20 \*-------------------------------------*/
21
22 /*--------------------------------------*\
23 |* Private *|
24 \*-------------------------------------*/
25
26 /*----------------------------------------------------------------------*\
27 |* Implementation *|
28 \*---------------------------------------------------------------------*/
29
30 /*--------------------------------------*\
31 |* Public *|
32 \*-------------------------------------*/
33
34 VagueMOO::VagueMOO(unsigned int w, unsigned int h, float dt)
35 {
36 // Input
37 this->dt = dt;
38 this->w=w;
39
40
41 // Tools
42 this->t = 0;
43 this->isEntrelacement=true;
44
45
46 // OMP (facultatif)
47 const int NB_THREADS = OmpTools::setAndGetNaturalGranularity();
48 cout << "\n[VAGUE] nbThread = " << NB_THREADS << endl;
49 }
50
51 VagueMOO::~VagueMOO(void)
52 {
53 // rien
54 }
55
56 /*--------------------------------------*\
57 |* Public *|
58 \*-------------------------------------*/
59
60 void VagueMOO::process(uchar4* ptrTabPixels, int w, int h)
61 {
62 if (isEntrelacement)
63 {
64 entrelacementOMP(ptrTabPixels, w, h); // Plus lent
65 }
66 else
67 {
68 forAutoOMP(ptrTabPixels, w, h); // Plus rapide
69 }
70
71 isEntrelacement = !isEntrelacement; // Pour tester que les deux implementations fonctionnent
72 }
73
74 void VagueMOO::animationStep()
75 {
76 t+=dt;
77 }
78
79 /*--------------*\
80 |* get *|
81 \*-------------*/
82
83 float VagueMOO::getT()
84 {
85 return t;
86 }
87
88 float VagueMOO::getDT()
89 {
90 return dt;
91 }
92
93 /*--------------------------------------*\
94 |* Private *|
95 \*-------------------------------------*/
96
97
98 /**
99 * Code entrainement Cuda
100 */
101 void VagueMOO::entrelacementOMP(uchar4* ptrTabPixels, int w, int h)
102 {
103 VagueMath vagueMath(w,h); // ici pour preparer cuda
104
105 const int WH = w * h;
106
107 #pragma omp parallel
108 {
109 const int NB_THREAD = OmpTools::getNbThread();// dans region parallel
110 const int TID = OmpTools::getTid();
111 int s = TID; // in [0,...
112
113 int i;
114 int j;
115 while (s < WH)
116 {
117 IndiceTools::toIJ(s, w, &i, &j); // s[0,W*H[ --> i[0,H[ j[0,W[
118
119 vagueMath.colorIJ(&ptrTabPixels[s], i, j,t);
120
121 s += NB_THREAD;
122 }
123 }
124 }
125
126 /**
127 * Code naturel et direct OMP
128 */
129 void VagueMOO::forAutoOMP(uchar4* ptrTabPixels, int w, int h)
130 {
131 VagueMath vagueMath(w,h); // ici pour preparer cuda
132
133 #pragma omp parallel for
134 for (int i = 0; i < h; i++)
135 {
136 for (int j = 0; j < w; j++)
137 {
138 // int s = i * W + j;
139 int s = IndiceTools::toS(w, i, j); // i[0,H[ j[0,W[ --> s[0,W*H[
140
141 vagueMath.colorIJ(&ptrTabPixels[s], i, j,t);
142 }
143 }
144 }
145 /*----------------------------------------------------------------------*\
146 |* End *|
147 \*---------------------------------------------------------------------*/
148