Ajout du TP HeatTransfert.
[GPU.git] / WCudaMSE / Student_Cuda_Image / src / cpp / core / 05_HeatTransfert / moo / device / HeatTransfertDevice.cu
diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/device/HeatTransfertDevice.cu b/WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/device/HeatTransfertDevice.cu
new file mode 100755 (executable)
index 0000000..03225a5
--- /dev/null
@@ -0,0 +1,126 @@
+#include "HeatTransfertDevice.h"\r
+\r
+#include <iostream>\r
+using namespace std;\r
+\r
+#include "Indice2D.h"\r
+#include "IndiceTools.h"\r
+#include "cudaTools.h"\r
+#include "Device.h"\r
+\r
+#include "ColorTools.h"\r
+\r
+#include "HeatImage.h"\r
+\r
+__global__\r
+void display(HeatImage image, uchar4* ptrDevPixels, CalibreurF calibreur)\r
+    {\r
+    const int TID = Indice2D::tid();\r
+    const int NB_THREAD = Indice2D::nbThread();\r
+    const int WH = image.getWCuda() * image.getHCuda();\r
+\r
+    uchar4 color;\r
+\r
+    int pixelI;\r
+    int pixelJ;\r
+\r
+    int s = TID;\r
+    while (s < WH)\r
+       {\r
+       IndiceTools::toIJ(s, image.getWCuda(), &pixelI, &pixelJ);\r
+\r
+       float heatValue = image.getCuda(pixelJ, pixelI);\r
+       calibreur.calibrer(heatValue);\r
+       ColorTools::HSB_TO_RVB(heatValue, &color);\r
+\r
+       ptrDevPixels[s] = color;\r
+       s += NB_THREAD;\r
+       }\r
+    }\r
+\r
+__global__\r
+void copyHeaters(HeatImage heaters, HeatImage destination)\r
+    {\r
+    const int TID = Indice2D::tid();\r
+    const int NB_THREAD = Indice2D::nbThread();\r
+    const int WH = heaters.getWCuda() * heaters.getHCuda();\r
+\r
+    int pixelI;\r
+    int pixelJ;\r
+\r
+    int s = TID;\r
+    while (s < WH)\r
+        {\r
+        IndiceTools::toIJ(s, heaters.getWCuda(), &pixelI, &pixelJ);\r
+\r
+        float heatValue = heaters.getCuda(pixelJ, pixelI);\r
+        if (heatValue > 0.0)\r
+            destination.setCuda(pixelJ, pixelI, heatValue);\r
+\r
+        s += NB_THREAD;\r
+        }\r
+    }\r
+\r
+__global__\r
+void diffuseMethode1(HeatImage from, HeatImage to)\r
+    {\r
+    const int TID = Indice2D::tid();\r
+    const int NB_THREAD = Indice2D::nbThread();\r
+\r
+    const int W = from.getWCuda() - 2;\r
+    const int H = from.getHCuda() - 2;\r
+    const int WH = W * H;\r
+\r
+    const float k = 0.1;\r
+\r
+    int pixelI;\r
+    int pixelJ;\r
+\r
+    int s = TID;\r
+    while (s < WH)\r
+        {\r
+        IndiceTools::toIJ(s, W, &pixelI, &pixelJ);\r
+        const int x = pixelJ + 1;\r
+        const int y = pixelI + 1;\r
+        const float t_old = from.getCuda(x, y);\r
+        const float t_up = from.getCuda(x, y - 1);\r
+        const float t_down = from.getCuda(x, y + 1);\r
+        const float t_left = from.getCuda(x - 1, y);\r
+        const float t_right = from.getCuda(x + 1, y);\r
+\r
+        to.setCuda(x, y, t_old + k * (t_up + t_down + t_left + t_right - 4.0 * t_old));\r
+\r
+        s += NB_THREAD;\r
+        }\r
+    }\r
+\r
+__global__\r
+void diffuseMethode2(HeatImage from, HeatImage to)\r
+    {\r
+    const int TID = Indice2D::tid();\r
+    const int NB_THREAD = Indice2D::nbThread();\r
+\r
+    const int W = from.getWCuda() - 2;\r
+    const int H = from.getHCuda() - 2;\r
+    const int WH = W * H;\r
+\r
+    int pixelI;\r
+    int pixelJ;\r
+\r
+    int s = TID;\r
+    while (s < WH)\r
+        {\r
+        IndiceTools::toIJ(s, W, &pixelI, &pixelJ);\r
+        const int x = pixelJ + 1;\r
+        const int y = pixelI + 1;\r
+        const float t_old = from.getCuda(x, y);\r
+        const float t_up = from.getCuda(x, y - 1);\r
+        const float t_down = from.getCuda(x, y + 1);\r
+        const float t_left = from.getCuda(x - 1, y);\r
+        const float t_right = from.getCuda(x + 1, y);\r
+\r
+        to.setCuda(x, y, (t_up + t_down + t_left + t_right + 4.0 * t_old) / 8.0);\r
+\r
+        s += NB_THREAD;\r
+        }\r
+    }\r