RayTracing.
[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 const int deviceId = 5;
58 initCuda(deviceId);
59
60 vector<string> args;
61 for (int i = 1; i < argc; ++i)
62 args.push_back(argv[i]);
63
64 int isOk = start(args);
65
66 //cudaDeviceReset causes the driver to clean up all state.
67 // While not mandatory in normal operation, it is good practice.
68 HANDLE_ERROR(cudaDeviceReset());
69
70 return isOk;
71 }
72 else
73 {
74 return EXIT_FAILURE;
75 }
76 }
77
78 /*--------------------------------------*\
79 |* Private *|
80 \*-------------------------------------*/
81
82 void initCuda(int deviceId)
83 {
84 // Check deviceId area
85 int nbDevice = Device::getDeviceCount();
86 assert(deviceId >= 0 && deviceId < nbDevice);
87
88 // Choose current device (state of host-thread)
89 HANDLE_ERROR(cudaSetDevice(deviceId));
90
91 // Enable Interoperabilit� OpenGL:
92 // - Create a cuda specifique contexte, shared between Cuda and GL
93 // - To be called before first call to kernel
94 // - cudaSetDevice ou cudaGLSetGLDevice are mutualy exclusive
95 HANDLE_ERROR(cudaGLSetGLDevice(deviceId));
96
97 // It can be usefull to preload driver, by example to practice benchmarking! (sometimes slow under linux)
98 Device::loadCudaDriver(deviceId);
99 // Device::loadCudaDriverAll();// Force driver to be load for all GPU
100 }
101
102 int start(const vector<string>& args)
103 {
104 Device::printCurrent();
105
106 bool IS_GL = true;
107
108 if (IS_GL)
109 {
110 return mainGL(args); // Bloquant, Tant qu'une fenêtre est ouverte.
111 }
112 else
113 {
114 return mainFreeGL();
115 }
116 }
117
118 /*----------------------------------------------------------------------*\
119 |* End *|
120 \*---------------------------------------------------------------------*/
121