Ajout de l'ensemble du workspace.
[GPU.git] / WCudaMSE / Tuto_Image_Cuda / src / cpp / main.cpp
1 #include <stdlib.h>
2 #include <assert.h>
3
4 #include "cudaTools.h"
5 #include "Device.h"
6 #include "cudaTools.h"
7 #include "GLUTImageViewers_GPU.h"
8
9 /*----------------------------------------------------------------------*\
10 |* Declaration *|
11 \*---------------------------------------------------------------------*/
12
13 /*--------------------------------------*\
14 |* Imported *|
15 \*-------------------------------------*/
16
17 extern int mainGL(void);
18 extern int mainFreeGL(void);
19
20 /*--------------------------------------*\
21 |* Public *|
22 \*-------------------------------------*/
23
24 int main(int argc, char** argv);
25
26 /*--------------------------------------*\
27 |* Private *|
28 \*-------------------------------------*/
29
30 static int start(void);
31 static void initCuda(int deviceId);
32
33 /*----------------------------------------------------------------------*\
34 |* Implementation *|
35 \*---------------------------------------------------------------------*/
36
37 /*--------------------------------------*\
38 |* Public *|
39 \*-------------------------------------*/
40
41 int main(int argc, char** argv)
42 {
43 if (Device::isCuda())
44 {
45 gpu::GLUTImageViewers::init(argc, argv);
46
47 // Device::printAll();
48 Device::printAllSimple();
49
50 // Server Cuda1: in [0,5]
51 // Server Cuda2: in [0,2]
52 int deviceId = 0;
53
54 initCuda(deviceId);
55
56 int isOk = start();
57
58 //cudaDeviceReset causes the driver to clean up all state.
59 // While not mandatory in normal operation, it is good practice.
60 HANDLE_ERROR(cudaDeviceReset());
61
62 return isOk;
63 }
64 else
65 {
66 return EXIT_FAILURE;
67 }
68 }
69
70 /*--------------------------------------*\
71 |* Private *|
72 \*-------------------------------------*/
73
74 void initCuda(int deviceId)
75 {
76 // Check deviceId area
77 int nbDevice = Device::getDeviceCount();
78 assert(deviceId >= 0 && deviceId < nbDevice);
79
80 // Choose current device (state of host-thread)
81 HANDLE_ERROR(cudaSetDevice(deviceId));
82
83 // Enable Interoperabilité OpenGL:
84 // - Create a cuda specifique contexte, shared between Cuda and GL
85 // - To be called before first call to kernel
86 // - cudaSetDevice ou cudaGLSetGLDevice are mutualy exclusive
87 HANDLE_ERROR(cudaGLSetGLDevice(deviceId));
88
89 // It can be usefull to preload driver, by example to practice benchmarking! (sometimes slow under linux)
90 Device::loadCudaDriver(deviceId);
91 // Device::loadCudaDriverAll();// Force driver to be load for all GPU
92 }
93
94 int start(void)
95 {
96 Device::printCurrent();
97
98 bool IS_GL = true;
99
100 if (IS_GL)
101 {
102 return mainGL(); // Bloquant, Tant qu'une fenetre est ouverte
103 }
104 else
105 {
106 return mainFreeGL();
107 }
108 }
109
110 /*----------------------------------------------------------------------*\
111 |* End *|
112 \*---------------------------------------------------------------------*/
113