X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=WCudaMSE%2FTuto_Image_Cuda%2Fsrc%2Fcpp%2Fmain.cpp;fp=WCudaMSE%2FTuto_Image_Cuda%2Fsrc%2Fcpp%2Fmain.cpp;h=9d091e1a7e205b421968254ddaa76e1d4aaee0b4;hb=8d08c12b29c2a14684f35c023ee39e694bb80d25;hp=0000000000000000000000000000000000000000;hpb=226de81f7e1f1fbf4ac79d0d089e8a05ec7159a0;p=GPU.git diff --git a/WCudaMSE/Tuto_Image_Cuda/src/cpp/main.cpp b/WCudaMSE/Tuto_Image_Cuda/src/cpp/main.cpp new file mode 100755 index 0000000..9d091e1 --- /dev/null +++ b/WCudaMSE/Tuto_Image_Cuda/src/cpp/main.cpp @@ -0,0 +1,113 @@ +#include +#include + +#include "cudaTools.h" +#include "Device.h" +#include "cudaTools.h" +#include "GLUTImageViewers_GPU.h" + +/*----------------------------------------------------------------------*\ + |* Declaration *| + \*---------------------------------------------------------------------*/ + +/*--------------------------------------*\ + |* Imported *| + \*-------------------------------------*/ + +extern int mainGL(void); +extern int mainFreeGL(void); + +/*--------------------------------------*\ + |* Public *| + \*-------------------------------------*/ + +int main(int argc, char** argv); + +/*--------------------------------------*\ + |* Private *| + \*-------------------------------------*/ + +static int start(void); +static void initCuda(int deviceId); + +/*----------------------------------------------------------------------*\ + |* Implementation *| + \*---------------------------------------------------------------------*/ + +/*--------------------------------------*\ + |* Public *| + \*-------------------------------------*/ + +int main(int argc, char** argv) + { + if (Device::isCuda()) + { + gpu::GLUTImageViewers::init(argc, argv); + + // Device::printAll(); + Device::printAllSimple(); + + // Server Cuda1: in [0,5] + // Server Cuda2: in [0,2] + int deviceId = 0; + + initCuda(deviceId); + + int isOk = start(); + + //cudaDeviceReset causes the driver to clean up all state. + // While not mandatory in normal operation, it is good practice. + HANDLE_ERROR(cudaDeviceReset()); + + return isOk; + } + else + { + return EXIT_FAILURE; + } + } + +/*--------------------------------------*\ + |* Private *| + \*-------------------------------------*/ + +void initCuda(int deviceId) + { + // Check deviceId area + int nbDevice = Device::getDeviceCount(); + assert(deviceId >= 0 && deviceId < nbDevice); + + // Choose current device (state of host-thread) + HANDLE_ERROR(cudaSetDevice(deviceId)); + + // Enable Interoperabilité OpenGL: + // - Create a cuda specifique contexte, shared between Cuda and GL + // - To be called before first call to kernel + // - cudaSetDevice ou cudaGLSetGLDevice are mutualy exclusive + HANDLE_ERROR(cudaGLSetGLDevice(deviceId)); + + // It can be usefull to preload driver, by example to practice benchmarking! (sometimes slow under linux) + Device::loadCudaDriver(deviceId); + // Device::loadCudaDriverAll();// Force driver to be load for all GPU + } + +int start(void) + { + Device::printCurrent(); + + bool IS_GL = true; + + if (IS_GL) + { + return mainGL(); // Bloquant, Tant qu'une fenetre est ouverte + } + else + { + return mainFreeGL(); + } + } + +/*----------------------------------------------------------------------*\ + |* End *| + \*---------------------------------------------------------------------*/ +