From 7753d7abc9c1cdf90793a2936221aa4951c574b3 Mon Sep 17 00:00:00 2001 From: gburri Date: Sun, 7 Dec 2014 20:59:16 +0100 Subject: [PATCH] Remplacement des 'powf(a, 2)' par 'a*a'. --- .../core/03_Newton/moo/device/NewtonDevice.cu | 10 +- .../core/03_Newton/moo/device/NewtonDevice.h | 9 ++ .../03_Newton/moo/device/math/NewtonMath.h | 111 ++++++++++-------- .../src/cpp/core/03_Newton/moo/host/Newton.cu | 18 +-- .../src/cpp/core/03_Newton/moo/host/Newton.h | 6 +- .../03_Newton/provider/NewtonProvider.cpp | 2 +- .../05_HeatTransfert/moo/host/HeatImage.cu | 12 -- .../05_HeatTransfert/moo/host/HeatImage.h | 10 +- .../src/cpp/core/mainGL.cpp | 4 +- 9 files changed, 105 insertions(+), 77 deletions(-) create mode 100644 WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/NewtonDevice.h delete mode 100644 WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/host/HeatImage.cu diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/NewtonDevice.cu b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/NewtonDevice.cu index 3399d92..534e37f 100755 --- a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/NewtonDevice.cu +++ b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/NewtonDevice.cu @@ -2,21 +2,23 @@ #include using namespace std; +#include "NewtonDevice.h" + #include "Indice2D.h" #include "IndiceTools.h" -#include "DomaineMath.h" #include "cudaTools.h" #include "Device.h" #include "NewtonMath.h" -__global__ void newton(uchar4* ptrDevPixels, int w, int h, DomaineMath domaineMath) +__global__ +void newton(uchar4* ptrDevPixels, int w, int h, DomaineMath domaineMath, int n, float epsilon) { const int TID = Indice2D::tid(); const int NB_THREAD = Indice2D::nbThread(); const int WH = w * h; - NewtonMath newtonMath; + NewtonMath newtonMath(n, epsilon); uchar4 color; color.w = 255; // Par défaut, l'image est opaque. @@ -31,7 +33,7 @@ __global__ void newton(uchar4* ptrDevPixels, int w, int h, DomaineMath domaineMa // (i,j) domaine écran. // (x,y) domaine math. - domaineMath.toXY(pixelI, pixelJ, &x, &y); // (i,j) -> (x,y) + domaineMath.toXY(pixelI, pixelJ, &x, &y); // (i,j) -> (x,y). newtonMath.colorXY(&color, x, y); diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/NewtonDevice.h b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/NewtonDevice.h new file mode 100644 index 0000000..f3c6341 --- /dev/null +++ b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/NewtonDevice.h @@ -0,0 +1,9 @@ +#ifndef NEWTON_DEVICE_H +#define NEWTON_DEVICE_H + +#include "DomaineMath.h" + +__global__ +void newton(uchar4* ptrDevPixels, int w, int h, DomaineMath domaineMath, int n, float epsilon); + +#endif diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/math/NewtonMath.h b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/math/NewtonMath.h index 3c76170..e9b0dd8 100755 --- a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/math/NewtonMath.h +++ b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/device/math/NewtonMath.h @@ -1,7 +1,6 @@ #ifndef NEWTON_MATH_H_ #define NEWTON_MATH_H_ -#include #include #include @@ -15,55 +14,13 @@ class NewtonMath C // (-1/2 -sqrt(3)/2) }; - /* - * Renvoie la valeur (x1, x2) de l'itération suivante (i+1). - */ - __device__ - static void nextX(float x1, float x2, float& x1_next, float& x2_next) - { - float f_x1 = powf(x1, 3.0) - 3.0 * x1 * powf(x2, 2.0) - 1.0; - float f_x2 = powf(x2, 3.0) - 3.0 * powf(x1, 2.0) * x2; - - // La matrice est représentée comme cela : - // a b - // c d - float jacobienne_f_x_a = 3.0 * powf(x1, 2.0) - 3.0 * powf(x2, 2.0); - float jacobienne_f_x_b = -6.0 * x1 * x2; - float jacobienne_f_x_c = -6.0 * x1 * x2; - float jacobienne_f_x_d = -3.0 * powf(x1, 2.0) + 3.0 * powf(x2, 2.0); - - float det_inverse_jacobienne = 1.0 / (jacobienne_f_x_a * jacobienne_f_x_d - jacobienne_f_x_b * jacobienne_f_x_c); - float jacobienne_f_x_a_inverse = jacobienne_f_x_d * det_inverse_jacobienne; - float jacobienne_f_x_b_inverse = -jacobienne_f_x_b * det_inverse_jacobienne; - float jacobienne_f_x_c_inverse = -jacobienne_f_x_c * det_inverse_jacobienne; - float jacobienne_f_x_d_inverse = jacobienne_f_x_a * det_inverse_jacobienne; - - x1_next = x1 - (jacobienne_f_x_a_inverse * f_x1 + jacobienne_f_x_b_inverse * f_x2); - x2_next = x2 - (jacobienne_f_x_c_inverse * f_x1 + jacobienne_f_x_d_inverse * f_x2); - } - - /* - * Renvoie la distance entre deux vecteurs a et b. - */ - __device__ - static float distance_carre(float a1, float a2, float b1, float b2) - { - return powf(a1 - b1, 2.0) + powf(a2 - b2, 2.0); - } - - __device__ - static float distance(float a1, float a2, float b1, float b2) - { - return (powf(a1 - b1, 2.0) + powf(a2 - b2, 2.0)) / (powf(b1, 2.0) + powf(b2, 2.0)); - } - public: /* * n est le nombre d'iteration. */ __device__ - NewtonMath(int n = 1000) - : n(n) + NewtonMath(int n, float epsilon) + : n(n), epsilon(epsilon) { } @@ -80,7 +37,7 @@ class NewtonMath const float C1 = -1.0 / 2.0; const float C2 = -sqrt(3.0) / 2.0; - const float epsilon = 0.001; + //const float epsilon = 0.1; float nearest_current_solution_distance = FLT_MAX; Solution nearest_current_solution = A; @@ -107,7 +64,18 @@ class NewtonMath nearest_current_solution_distance = distance_to_C; } - if (nearest_current_solution_distance < epsilon) + /*if (Indice2D::tid() == 0) + { + printf("nearest_current_solution_distance: %f\n", nearest_current_solution_distance); + }*/ + + /*printf("x1: %f, x2: %f\n", x1, x2); + printf("d to a: %f\n", distance_to_A); + printf("d to a: %f\n", distance_to_B); + printf("d to a: %f\n", distance_to_C); + }*/ + + if (nearest_current_solution_distance < this->epsilon) break; nextX(x1, x2, x1, x2); @@ -137,7 +105,56 @@ class NewtonMath } private: + + /* + * Renvoie la valeur (x1, x2) de l'itération suivante (i+1). + */ + __device__ + static void nextX(float x1, float x2, float& x1_next, float& x2_next) + { + float f_x1 = x1 * x1 * x2 - 3.0 * x1 * x2 * x2 - 1.0; + float f_x2 = x2 * x2 * x2 - 3.0 * x1 * x1 * x2; + + // La matrice est représentée comme cela : + // a b + // c d + float jacobienne_f_x_a = 3.0 * x1 * x1 - 3.0 * x2 * x2; + float jacobienne_f_x_b = -6.0 * x1 * x2; + float jacobienne_f_x_c = -6.0 * x1 * x2; + float jacobienne_f_x_d = -3.0 * x1 * x1 + 3.0 * x2 * x2; + + float det_inverse_jacobienne = 1.0 / (jacobienne_f_x_a * jacobienne_f_x_d - jacobienne_f_x_b * jacobienne_f_x_c); + float jacobienne_f_x_a_inverse = jacobienne_f_x_d * det_inverse_jacobienne; + float jacobienne_f_x_b_inverse = -jacobienne_f_x_b * det_inverse_jacobienne; + float jacobienne_f_x_c_inverse = -jacobienne_f_x_c * det_inverse_jacobienne; + float jacobienne_f_x_d_inverse = jacobienne_f_x_a * det_inverse_jacobienne; + + x1_next = x1 - (jacobienne_f_x_a_inverse * f_x1 + jacobienne_f_x_b_inverse * f_x2); + x2_next = x2 - (jacobienne_f_x_c_inverse * f_x1 + jacobienne_f_x_d_inverse * f_x2); + } + + /* + * Renvoie la distance entre deux vecteurs a et b. + */ + __device__ + static float distance_carre(float a1, float a2, float b1, float b2) + { + const float delta1 = a1 - b1; + const float delta2 = a2 - b2; + return delta1 * delta1 + delta2 * delta2; + } + + __device__ + static float distance(float a1, float a2, float b1, float b2) + { + const float delta1 = a1 - b1; + const float delta2 = a2 - b2; + + return (delta1 * delta1 + delta2 * delta2) / (b1 * b1 + b2 * b2); + } + int n; + float epsilon; }; #endif diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/host/Newton.cu b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/host/Newton.cu index 685fb5e..a468b05 100755 --- a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/host/Newton.cu +++ b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/host/Newton.cu @@ -5,18 +5,17 @@ using namespace std; #include "Newton.h" #include "Device.h" - -extern __global__ void newton(uchar4* ptrDevPixels, int w, int h, DomaineMath domaineMath); +#include "NewtonDevice.h" Newton::Newton(int w, int h) - : variateurAnimation(IntervalF(30, 100), 1), + : variateurN(IntervalI(5, 1000), 1), + variateurEpsilon(IntervalF(0.01, 10), 0.01), w(w), h(h), dg(8, 8, 1), - db(16, 16, 1), + db(32, 32, 1), ptrDomaineMathInit(new DomaineMath(-2, -2, 2, 2)), title("Fractal Newton") { - // print(dg, db); Device::assertDim(dg, db); } @@ -27,12 +26,15 @@ Newton::~Newton() void Newton::runGPU(uchar4* ptrDevPixels, const DomaineMath& domaineMath) { - newton<<>>(ptrDevPixels, this->w, this->h, domaineMath); + newton<<>>(ptrDevPixels, this->w, this->h, domaineMath, this->t, this->epsilon); + + HANDLE_ERROR(cudaDeviceSynchronize()); // Pour flusher les 'printf' (pour le DEBUG). } void Newton::animationStep() { - this->t = this->variateurAnimation.varierAndGet(); + this->t = this->variateurN.varierAndGet(); + this->epsilon = this->variateurEpsilon.varierAndGet(); } int Newton::getW() @@ -52,7 +54,7 @@ DomaineMath* Newton::getDomaineMathInit() float Newton::getT() { - return 0; + return this->t; } string Newton::getTitle() diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/host/Newton.h b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/host/Newton.h index ad86979..0b876f0 100755 --- a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/host/Newton.h +++ b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/moo/host/Newton.h @@ -4,6 +4,7 @@ #include "cudaTools.h" #include "AnimableFonctionel_I.h" #include "MathTools.h" +#include "VariateurI.h" #include "VariateurF.h" class Newton : public AnimableFonctionel_I @@ -24,8 +25,11 @@ class Newton : public AnimableFonctionel_I string getTitle(void) /*override*/; private: - VariateurF variateurAnimation; + VariateurI variateurN; // Variateur sur le nombre d'iteration max. + VariateurF variateurEpsilon; // Variateur sur epsilon. + float t; + float epsilon; // Inputs const int w; diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/provider/NewtonProvider.cpp b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/provider/NewtonProvider.cpp index 3a0637f..3a4821f 100755 --- a/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/provider/NewtonProvider.cpp +++ b/WCudaMSE/Student_Cuda_Image/src/cpp/core/03_Newton/provider/NewtonProvider.cpp @@ -3,7 +3,7 @@ Newton* NewtonProvider::create() { int dw = 16 * 50; - int dh = 16 * 30; + int dh = 16 * 50; return new Newton(dw, dh); } diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/host/HeatImage.cu b/WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/host/HeatImage.cu deleted file mode 100644 index e383fb6..0000000 --- a/WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/host/HeatImage.cu +++ /dev/null @@ -1,12 +0,0 @@ -#include "HeatImage.h" - -#include - -HeatImage::HeatImage(int w, int h, float* image) : - w(w), h(h), image(image) - { - } - -HeatImage::~HeatImage() - { - } diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/host/HeatImage.h b/WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/host/HeatImage.h index 00224c8..04c5bfa 100644 --- a/WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/host/HeatImage.h +++ b/WCudaMSE/Student_Cuda_Image/src/cpp/core/05_HeatTransfert/moo/host/HeatImage.h @@ -1,14 +1,20 @@ #ifndef HEAT_IMAGE_H #define HEAT_IMAGE_H +#include + /* * Une classe représentant une image de float. */ class HeatImage { public: - HeatImage(int w, int h, float* image = 0); - ~HeatImage(); + HeatImage(int w, int h, float* image = 0) : + w(w), h(h), image(image) + { + } + + ~HeatImage() {} inline void clear() { diff --git a/WCudaMSE/Student_Cuda_Image/src/cpp/core/mainGL.cpp b/WCudaMSE/Student_Cuda_Image/src/cpp/core/mainGL.cpp index 6eed6e3..9f1b84a 100755 --- a/WCudaMSE/Student_Cuda_Image/src/cpp/core/mainGL.cpp +++ b/WCudaMSE/Student_Cuda_Image/src/cpp/core/mainGL.cpp @@ -40,8 +40,8 @@ int mainGL(void) //Viewer rippling0(true, true, 10, 10); //Viewer fractalMandelbrot(true, true, 20, 20); //Viewer fractalJulia(true, true, 30, 30); - //Viewer newtown(true, true, 20, 20); - Viewer heatTransfert(true, false, 20, 20); + Viewer newtown(true, true, 20, 20); + //Viewer heatTransfert(true, false, 20, 20); GLUTImageViewers::runALL(); // Bloquant, Tant qu'une fenetre est ouverte -- 2.45.2