3fad4508230608599458964b5b377392ee579b07
[GPU.git] / WCudaMSE / Student_Cuda / 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 "LimitsTools.h"
8
9 using std::cout;
10 using std::endl;
11
12 /*----------------------------------------------------------------------*\
13 |* Declaration *|
14 \*---------------------------------------------------------------------*/
15
16 /*--------------------------------------*\
17 |* Imported *|
18 \*-------------------------------------*/
19
20 extern int mainCore();
21 extern int mainTest();
22
23 /*--------------------------------------*\
24 |* Public *|
25 \*-------------------------------------*/
26
27 int main(void);
28
29 /*--------------------------------------*\
30 |* Private *|
31 \*-------------------------------------*/
32
33 static void initCuda(int deviceId);
34 static int start(void);
35
36 /*----------------------------------------------------------------------*\
37 |* Implementation *|
38 \*---------------------------------------------------------------------*/
39
40 /*--------------------------------------*\
41 |* Public *|
42 \*-------------------------------------*/
43
44 int main(void)
45 {
46 cout << "main" << endl;
47
48 // LimitsTools::rappelTypeSize();
49
50 if (Device::isCuda())
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 int isOk = start();
60
61 //cudaDeviceReset causes the driver to clean up all state.
62 // While not mandatory in normal operation, it is good practice.
63 HANDLE_ERROR(cudaDeviceReset());
64
65 return isOk;
66 }
67 else
68 {
69 return EXIT_FAILURE;
70 }
71 }
72
73 void initCuda(int deviceId)
74 {
75 // Check deviceId area
76 int nbDevice = Device::getDeviceCount();
77 assert(deviceId >= 0 && deviceId < nbDevice);
78
79 //HANDLE_ERROR(cudaDeviceReset());
80
81 // Choose current device (state of host-thread)
82 HANDLE_ERROR(cudaSetDevice(deviceId));
83
84 // It can be usefull to preload driver, by example to practice benchmarking! (sometimes slow under linux)
85 Device::loadCudaDriver(deviceId);
86 // Device::loadCudaDriverAll();// Force driver to be load for all GPU
87 }
88
89 int start(void)
90 {
91 Device::printCurrent();
92
93 bool IS_TEST = true;
94
95 if (IS_TEST)
96 {
97 return mainTest();
98 }
99 else
100 {
101 return mainCore();
102 }
103 }
104
105 /*----------------------------------------------------------------------*\
106 |* End *|
107 \*---------------------------------------------------------------------*/
108