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