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