X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=WCudaMSE%2FStudent_Cuda%2Fsrc%2Fcpp%2Fcore%2F01_Hello%2F02_hello_add.cu;fp=WCudaMSE%2FStudent_Cuda%2Fsrc%2Fcpp%2Fcore%2F01_Hello%2F02_hello_add.cu;h=70f3908b8a24a025936e2fc7343c50af37d3a654;hb=8d08c12b29c2a14684f35c023ee39e694bb80d25;hp=0000000000000000000000000000000000000000;hpb=226de81f7e1f1fbf4ac79d0d089e8a05ec7159a0;p=GPU.git diff --git a/WCudaMSE/Student_Cuda/src/cpp/core/01_Hello/02_hello_add.cu b/WCudaMSE/Student_Cuda/src/cpp/core/01_Hello/02_hello_add.cu new file mode 100755 index 0000000..70f3908 --- /dev/null +++ b/WCudaMSE/Student_Cuda/src/cpp/core/01_Hello/02_hello_add.cu @@ -0,0 +1,104 @@ +// Attention : Extension .cu + +#include +#include +#include "cudaTools.h" +#include "Device.h" + +using std::cout; +using std::endl; + +/*----------------------------------------------------------------------*\ + |* Declaration *| + \*---------------------------------------------------------------------*/ + +/*--------------------------------------*\ + |* Imported *| + \*-------------------------------------*/ + +/*--------------------------------------*\ + |* Public *| + \*-------------------------------------*/ + +__host__ bool isAddScalarGPU_Ok(void); + +/*--------------------------------------*\ + |* Private *| + \*-------------------------------------*/ + +__host__ static int addScalarGPU(int a, int b); +__global__ static void addScalar(int a, int b, int* ptrC); + +/*----------------------------------------------------------------------*\ + |* Implementation *| + \*---------------------------------------------------------------------*/ + +/*--------------------------------------*\ + |* Public *| + \*-------------------------------------*/ + +__host__ bool isAddScalarGPU_Ok(void) + { + cout << endl << "[Hello Cuda 2]" << endl; + + int a = 2; + int b = 7; + + int sumGPU = addScalarGPU(a, b); + int sumTheorique = a + b; + + cout <<"\n[CPU] "<< a << " + " << b << " = " << sumGPU << endl; + + return sumGPU == sumTheorique; + } + +/*--------------------------------------*\ + |* Private *| + \*-------------------------------------*/ + +__host__ int addScalarGPU(int a, int b) + { + int c; + int* ptrC=&c; // on host (CPU) + int* ptrDev_c; // on device (GPU) + + // Specifier nb thread : ici 1 thread au total ! + dim3 dg = dim3(1,1,1); + dim3 db = dim3(1, 1, 1); + + // Debug + //Device::print(dg, db); + Device::checkDimError(dg,db); + + size_t size=sizeof(int); + HANDLE_ERROR(cudaMalloc((void**) &ptrDev_c, size)); // Device memory allocation (*) + + addScalar<<>>(a,b,ptrDev_c); // asynchrone !! + Device::checkKernelError("addScalar"); // facultatif + + //v1 + Device::synchronize();// Pour printf sur GPU + + //v2 + // cudaDeviceSynchronize(); // Pour printf sur GPU + + // memoryManagement => barrier de synchronisation + HANDLE_ERROR(cudaMemcpy(ptrC, ptrDev_c, size, cudaMemcpyDeviceToHost));// Device -> Host + HANDLE_ERROR(cudaFree(ptrDev_c)); // device dispose memory in (*) + + return c; + } + + +__global__ void addScalar(int a, int b, int* ptrC) + { + *ptrC = a + b; + + // debug + printf("[GPU] %d + %d = %d",a,b, *ptrC); + } + +/*----------------------------------------------------------------------*\ + |* End *| + \*---------------------------------------------------------------------*/ +