Ajout du support du multi-GPU pour Mandelbrot.
[GPU.git] / WCudaMSE / Student_Cuda_Image / src / cpp / core / 02_Mandelbrot_Julia / moo / host / Fractal.cu
index bfaff4b..67c8b74 100755 (executable)
@@ -1,17 +1,19 @@
 #include "Fractal.h"\r
 \r
 #include <iostream>\r
+#include <sstream>\r
 #include <assert.h>\r
 using namespace std;\r
 \r
+#include <omp.h>\r
+\r
 #include "FractalDevice.h"\r
 #include "Device.h"\r
 \r
 Fractal::Fractal(int w, int h) :\r
         w(w), h(h),\r
         dg(8, 8, 1),\r
-        db(16, 16, 1),\r
-        title("Fractal Cuda")\r
+        db(16, 16, 1)\r
     {\r
     //print(dg, db);\r
     Device::assertDim(dg, db);\r
@@ -43,22 +45,38 @@ DomaineMath* Fractal::getDomaineMathInit()
     return this->ptrDomaineMathInit;\r
     }\r
 \r
-/**\r
- * Override\r
- */\r
-string Fractal::getTitle()\r
-    {\r
-    return this->title;\r
-    }\r
-\r
 /////\r
 \r
-FractalMandelbrot::FractalMandelbrot(int w, int h, int dn) :\r
+FractalMandelbrot::FractalMandelbrot(int w, int h, int dn, bool multiGPU) :\r
         Fractal(w, h),\r
         variateurAnimationN(IntervalI(10, 100), dn),\r
-        n(0)\r
+        n(0),\r
+        multiGPU(multiGPU)\r
     {\r
+    // Constuit le titre dynamiquement.\r
+    ostringstream titleStream;\r
+    titleStream << "Fractal Mandelbrot (multi-GPU " << (this->multiGPU ? "activated" : "not activated") << ")";\r
+    this->title = titleStream.str();\r
+\r
     this->ptrDomaineMathInit = new DomaineMath(-2, -1.3, 0.8, 1.3);\r
+\r
+    if (this->multiGPU)\r
+        {\r
+        const int nbDevice = Device::getDeviceCount();\r
+\r
+        this->hDevices = h / nbDevice;\r
+        this->hFirstDevice = h - ((nbDevice - 1) * this->hDevices);\r
+\r
+        // Allocation de la mémoire sur chaque GPU (sauf le premier pour lequel 'ptrDevPixels' est automatiquement alloué à l'appel de 'runGPU(..)').\r
+        this->ptrDevPixelsMultGPU = new uchar4*[nbDevice - 1];\r
+        for (int i = 0; i < nbDevice - 1; ++i)\r
+            {\r
+            HANDLE_ERROR(cudaSetDevice(i + 1));\r
+            HANDLE_ERROR(cudaMalloc(&this->ptrDevPixelsMultGPU[i], sizeof(uchar4) * w * this->hDevices));\r
+            }\r
+\r
+        HANDLE_ERROR(cudaSetDevice(0));\r
+        }\r
     }\r
 \r
 void FractalMandelbrot::animationStep()\r
@@ -78,9 +96,36 @@ void FractalMandelbrot::getValues(float* values)
     values[0] = float(this->n);\r
     }\r
 \r
+string FractalMandelbrot::getTitle()\r
+    {\r
+    return this->title;\r
+    }\r
+\r
 void FractalMandelbrot::runGPU(uchar4* ptrDevPixels, const DomaineMath& domaineMath)\r
     {\r
-    fractalMandelbrot<<<dg,db>>>(ptrDevPixels, this->w, this->h, domaineMath, static_cast<int>(this->n));\r
+\r
+    if (this->multiGPU)\r
+        {\r
+        HANDLE_ERROR(cudaSetDevice(0));\r
+        fractalMandelbrot<<<dg,db>>>(ptrDevPixels, this->w, 0, this->hFirstDevice, domaineMath, this->n);\r
+\r
+        const int nbDevice = Device::getDeviceCount();\r
+\r
+        // Rend chaque tranche par un GPU différent puis copie chaque tranche dans la mémoire du premier GPU.\r
+        #pragma omp parallel for\r
+        for (int i = 0; i < nbDevice - 1; ++i)\r
+            {\r
+            HANDLE_ERROR(cudaSetDevice(i + 1));\r
+            fractalMandelbrot<<<dg,db>>>(this->ptrDevPixelsMultGPU[i], this->w, i * this->hDevices + this->hFirstDevice, (i + 1) * this->hDevices + this->hFirstDevice, domaineMath, this->n);\r
+            HANDLE_ERROR(cudaMemcpy(ptrDevPixels + this->w * this->hFirstDevice + i * this->w * this->hDevices, this->ptrDevPixelsMultGPU[i], sizeof(uchar4) * this->w * this->hDevices, cudaMemcpyDeviceToDevice));\r
+            }\r
+\r
+        HANDLE_ERROR(cudaSetDevice(0));\r
+        }\r
+    else\r
+        {\r
+        fractalMandelbrot<<<dg,db>>>(ptrDevPixels, this->w, 0, this->h, domaineMath, this->n);\r
+        }\r
     }\r
 \r
 /////\r
@@ -116,6 +161,11 @@ void FractalJulia::getValues(float* values)
     values[1] = this->z_i;\r
     }\r
 \r
+string FractalJulia::getTitle()\r
+    {\r
+    return "Fractal Julia";\r
+    }\r
+\r
 void FractalJulia::runGPU(uchar4* ptrDevPixels, const DomaineMath& domaineMath)\r
     {\r
     fractalJulia<<<dg,db>>>(ptrDevPixels, this->w, this->h, domaineMath, this->n, this->z_r, this->z_i);\r