-
Notifications
You must be signed in to change notification settings - Fork 3
/
device.cu
86 lines (64 loc) · 1.42 KB
/
device.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cuda.h>
#include <exception>
#include <stdexcept>
#include <vector>
#include "device.h"
using std::runtime_error;
using std::vector;
// Cache number of devices
static int deviceCount = -1;
// Cache device properties
static vector<cudaDeviceProp> deviceProperties;
static void loadDeviceData()
{
cudaError_t err;
err = cudaGetDeviceCount(&deviceCount);
if (err != cudaSuccess)
{
throw runtime_error(cudaGetErrorString(err));
}
deviceProperties.reserve(deviceCount);
for (int device = 0; device < deviceCount; ++device)
{
cudaDeviceProp prop;
err = cudaGetDeviceProperties(&prop, device);
if (err != cudaSuccess)
{
throw runtime_error(cudaGetErrorString(err));
}
deviceProperties.push_back(prop);
}
}
bool isDeviceValid(int device)
{
if (deviceCount < 0)
{
loadDeviceData();
}
if (device < 0 || device >= deviceCount)
{
return false;
}
const cudaDeviceProp& prop = deviceProperties[device];
if (prop.computeMode == cudaComputeModeProhibited)
{
return false;
}
return true;
}
int countDevices()
{
if (deviceCount < 0)
{
loadDeviceData();
}
return deviceCount;
}
void loadDeviceProperties(int device, cudaDeviceProp& prop)
{
if (deviceCount < 0)
{
loadDeviceData();
}
prop = deviceProperties[device];
}