Ajout du TP HeatTransfert.
[GPU.git] / WCudaMSE / Student_Cuda_Image / src / cpp / core / 05_HeatTransfert / moo / device / HeatTransfertDevice.cu
1 #include "HeatTransfertDevice.h"\r
2 \r
3 #include <iostream>\r
4 using namespace std;\r
5 \r
6 #include "Indice2D.h"\r
7 #include "IndiceTools.h"\r
8 #include "cudaTools.h"\r
9 #include "Device.h"\r
10 \r
11 #include "ColorTools.h"\r
12 \r
13 #include "HeatImage.h"\r
14 \r
15 __global__\r
16 void display(HeatImage image, uchar4* ptrDevPixels, CalibreurF calibreur)\r
17     {\r
18     const int TID = Indice2D::tid();\r
19     const int NB_THREAD = Indice2D::nbThread();\r
20     const int WH = image.getWCuda() * image.getHCuda();\r
21 \r
22     uchar4 color;\r
23 \r
24     int pixelI;\r
25     int pixelJ;\r
26 \r
27     int s = TID;\r
28     while (s < WH)\r
29         {\r
30         IndiceTools::toIJ(s, image.getWCuda(), &pixelI, &pixelJ);\r
31 \r
32         float heatValue = image.getCuda(pixelJ, pixelI);\r
33         calibreur.calibrer(heatValue);\r
34         ColorTools::HSB_TO_RVB(heatValue, &color);\r
35 \r
36         ptrDevPixels[s] = color;\r
37         s += NB_THREAD;\r
38         }\r
39     }\r
40 \r
41 __global__\r
42 void copyHeaters(HeatImage heaters, HeatImage destination)\r
43     {\r
44     const int TID = Indice2D::tid();\r
45     const int NB_THREAD = Indice2D::nbThread();\r
46     const int WH = heaters.getWCuda() * heaters.getHCuda();\r
47 \r
48     int pixelI;\r
49     int pixelJ;\r
50 \r
51     int s = TID;\r
52     while (s < WH)\r
53         {\r
54         IndiceTools::toIJ(s, heaters.getWCuda(), &pixelI, &pixelJ);\r
55 \r
56         float heatValue = heaters.getCuda(pixelJ, pixelI);\r
57         if (heatValue > 0.0)\r
58             destination.setCuda(pixelJ, pixelI, heatValue);\r
59 \r
60         s += NB_THREAD;\r
61         }\r
62     }\r
63 \r
64 __global__\r
65 void diffuseMethode1(HeatImage from, HeatImage to)\r
66     {\r
67     const int TID = Indice2D::tid();\r
68     const int NB_THREAD = Indice2D::nbThread();\r
69 \r
70     const int W = from.getWCuda() - 2;\r
71     const int H = from.getHCuda() - 2;\r
72     const int WH = W * H;\r
73 \r
74     const float k = 0.1;\r
75 \r
76     int pixelI;\r
77     int pixelJ;\r
78 \r
79     int s = TID;\r
80     while (s < WH)\r
81         {\r
82         IndiceTools::toIJ(s, W, &pixelI, &pixelJ);\r
83         const int x = pixelJ + 1;\r
84         const int y = pixelI + 1;\r
85         const float t_old = from.getCuda(x, y);\r
86         const float t_up = from.getCuda(x, y - 1);\r
87         const float t_down = from.getCuda(x, y + 1);\r
88         const float t_left = from.getCuda(x - 1, y);\r
89         const float t_right = from.getCuda(x + 1, y);\r
90 \r
91         to.setCuda(x, y, t_old + k * (t_up + t_down + t_left + t_right - 4.0 * t_old));\r
92 \r
93         s += NB_THREAD;\r
94         }\r
95     }\r
96 \r
97 __global__\r
98 void diffuseMethode2(HeatImage from, HeatImage to)\r
99     {\r
100     const int TID = Indice2D::tid();\r
101     const int NB_THREAD = Indice2D::nbThread();\r
102 \r
103     const int W = from.getWCuda() - 2;\r
104     const int H = from.getHCuda() - 2;\r
105     const int WH = W * H;\r
106 \r
107     int pixelI;\r
108     int pixelJ;\r
109 \r
110     int s = TID;\r
111     while (s < WH)\r
112         {\r
113         IndiceTools::toIJ(s, W, &pixelI, &pixelJ);\r
114         const int x = pixelJ + 1;\r
115         const int y = pixelI + 1;\r
116         const float t_old = from.getCuda(x, y);\r
117         const float t_up = from.getCuda(x, y - 1);\r
118         const float t_down = from.getCuda(x, y + 1);\r
119         const float t_left = from.getCuda(x - 1, y);\r
120         const float t_right = from.getCuda(x + 1, y);\r
121 \r
122         to.setCuda(x, y, (t_up + t_down + t_left + t_right + 4.0 * t_old) / 8.0);\r
123 \r
124         s += NB_THREAD;\r
125         }\r
126     }\r