Ajout de l'ensemble du workspace.
[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 * Code naturel et direct OMP
99 */
100 void VagueMOO::forAutoOMP(uchar4* ptrTabPixels, int w, int h)
101 {
102 VagueMath vagueMath(w,h); // ici pour preparer cuda
103
104 #pragma omp parallel for
105 for (int i = 0; i < h; i++)
106 {
107 for (int j = 0; j < w; j++)
108 {
109 // int s = i * W + j;
110 int s = IndiceTools::toS(w, i, j); // i[0,H[ j[0,W[ --> s[0,W*H[
111
112 vagueMath.colorIJ(&ptrTabPixels[s], i, j,t);
113 }
114 }
115 }
116
117 /**
118 * Code entrainement Cuda
119 */
120 void VagueMOO::entrelacementOMP(uchar4* ptrTabPixels, int w, int h)
121 {
122 VagueMath vagueMath(w,h); // ici pour preparer cuda
123
124 const int WH = w * h;
125
126
127 #pragma omp parallel
128 {
129 const int NB_THREAD = OmpTools::getNbThread();// dans region parallel
130 const int TID = OmpTools::getTid();
131 int s = TID; // in [0,...
132
133 int i;
134 int j;
135 while (s < WH)
136 {
137 IndiceTools::toIJ(s, w, &i, &j); // s[0,W*H[ --> i[0,H[ j[0,W[
138
139 vagueMath.colorIJ(&ptrTabPixels[s], i, j,t);
140
141 s += NB_THREAD;
142 }
143 }
144 }
145
146
147 /*----------------------------------------------------------------------*\
148 |* End *|
149 \*---------------------------------------------------------------------*/
150