-
Notifications
You must be signed in to change notification settings - Fork 52
/
DevicesInfo.cpp
223 lines (219 loc) · 9.1 KB
/
DevicesInfo.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifdef USE_CLEW
#include "clew.h"
#else
#if defined(__APPLE__) || defined(__MACOSX)
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#endif
#include "EasyCL.h"
#include "DeviceInfo.h"
#include "DevicesInfo.h"
#include "deviceinfo_helper.h"
#include "platforminfo_helper.h"
#include <stdexcept>
using namespace std;
namespace easycl {
std::string errorMessage(cl_int error) {
return EasyCL::toString(error);
}
DeviceInfo DevicesInfo::getGpuInfo(int index) {
char *gpuOffsetStr = getenv("CL_GPUOFFSET");
if(gpuOffsetStr != 0) {
int gpuOffset = atoi(gpuOffsetStr);
if(gpuOffset != 0) {
int newIndex = index + gpuOffset;
// cout << "CL_GPUOFFSET var detected, increasing gpu index from " << index << " to " << newIndex << endl;
index = newIndex;
}
}
return getDeviceInfo(index, CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR);
}
DeviceInfo DevicesInfo::getDeviceInfo(int index) {
return getDeviceInfo(index, CL_DEVICE_TYPE_ALL);
}
DeviceInfo DevicesInfo::getDeviceInfo(int index, int types) {
cl_platform_id platformId;
cl_device_id deviceId;
getDeviceIds(index, types, &platformId, &deviceId);
DeviceInfo info;
info.populate(platformId, deviceId);
return info;
}
void DevicesInfo::getDeviceIds(int index, int types, cl_platform_id *p_platformId, cl_device_id *p_deviceId) {
#ifdef USE_CLEW
bool clpresent = 0 == clewInit();
if(!clpresent) {
throw std::runtime_error("OpenCL library not found");
}
#endif
cl_int error;
int currentGpuIndex = 0;
cl_platform_id platform_ids[10];
cl_uint num_platforms;
error = clGetPlatformIDs(10, platform_ids, &num_platforms);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting platforms ids: " + errorMessage(error));
}
if(num_platforms == 0) {
throw std::runtime_error("Error: no platforms available");
}
// int numDevices = 0;
for(int platform = 0; platform < (int)num_platforms; platform++) {
cl_platform_id platform_id = platform_ids[platform];
// cout << "checking platform id " << platform_id << endl;
// cl_device_id device_ids[100];
cl_uint num_devices;
error = clGetDeviceIDs(platform_id, types, 0, 0, &num_devices);
if (error != CL_SUCCESS) {
// continue;
throw std::runtime_error("Error getting num devices for platform " + EasyCL::toString(platform) + ": " +
errorMessage(error));
}
// cout << "num devices this platform " << num_devices << endl;
cl_device_id *device_ids = new cl_device_id[num_devices];
error = clGetDeviceIDs(platform_id, types, num_devices, device_ids, &num_devices);
if (error != CL_SUCCESS) {
// continue;
throw std::runtime_error("Error getting device ids for platform " + EasyCL::toString(platform) + ": " + errorMessage(error));
}
if(( index - currentGpuIndex) < (int)num_devices) {
*p_platformId = platform_id;
*p_deviceId = device_ids[(index - currentGpuIndex)];
delete[] device_ids;
return;
} else {
delete[] device_ids;
currentGpuIndex += num_devices;
}
}
if(index == 0) {
throw std::runtime_error("No devices found");
} else {
throw std::runtime_error("Not enough devices found to satisfy index: " + EasyCL::toString(index) );
}
}
int DevicesInfo::getNumGpus() {
char *gpuOffsetStr = getenv("CL_GPUOFFSET");
int gpuOffset = 0;
int numGpus = getNumDevices(CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR);
if(gpuOffsetStr != 0) {
gpuOffset = atoi(gpuOffsetStr);
if(gpuOffset != 0) {
int newNumGpus = numGpus - gpuOffset;
if(newNumGpus < 0) {
newNumGpus = 0;
}
// cout << "CL_GPUOFFSET var detected, decreasing num gpus from " << numGpus << " to " << newNumGpus << endl;
numGpus = newNumGpus;
}
}
return numGpus;
}
int DevicesInfo::getNumDevices() {
return getNumDevices(CL_DEVICE_TYPE_ALL);
}
int DevicesInfo::getNumDevices(int types) {
#ifdef USE_CLEW
bool clpresent = 0 == clewInit();
if(!clpresent) {
throw std::runtime_error("OpenCL library not found");
}
#endif
cl_int error;
// int currentGpuIndex = 0;
cl_platform_id platform_ids[10];
cl_uint num_platforms;
error = clGetPlatformIDs(10, platform_ids, &num_platforms);
if (error != CL_SUCCESS) {
return 0;
// throw std::runtime_error("Error getting platforms ids: " + errorMessage(error));
}
// cout << "num platforms " << num_platforms << endl;
if(num_platforms == 0) {
return 0;
// throw std::runtime_error("Error: no platforms available");
}
int numDevices = 0;
for(int platform = 0; platform < (int)num_platforms; platform++) {
cl_platform_id platform_id = platform_ids[platform];
// cout << "checking platform id " << platform_id << endl;
cl_uint num_devices;
error = clGetDeviceIDs(platform_id, types, 0, 0, &num_devices);
if (error != CL_SUCCESS) {
continue;
// throw std::runtime_error("Error getting num device ids for platform " + EasyCL::toString(platform) + ": " + errorMessage(error));
}
// cout << "num devices " << num_devices << endl;
// cl_device_id *device_ids = new cl_device_id[num_devices];
// error = clGetDeviceIDs(platform_id, types, num_devices, device_ids, &num_devices);
// cout << "num devices " << num_devices << endl;
// if (error != CL_SUCCESS) {
//// continue;
// throw std::runtime_error("Error getting device ids for platform " + EasyCL::toString(platform) + ": " + errorMessage(error));
// }
numDevices += num_devices;
// delete[] device_ids
}
return numDevices;
}
void DevicesInfo::getIdForIndexedGpu(int gpu, cl_platform_id *p_platformId, cl_device_id *p_deviceId) {
char *gpuOffsetStr = getenv("CL_GPUOFFSET");
int gpuOffset = 0;
if(gpuOffsetStr != 0) {
gpuOffset = atoi(gpuOffsetStr);
if(gpuOffset != 0) {
int newGpu = gpu + gpuOffset;
// if(newNumGpus < 0) {
// newNumGpus = 0;
// }
// cout << "CL_GPUOFFSET var detected, increasing gpu offset from " << gpu << " to " << newGpu << endl;
gpu = newGpu;
}
}
getDeviceIds(gpu, CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR, p_platformId, p_deviceId);
}
void DevicesInfo::getIdForIndexedDevice(int device, cl_platform_id *p_platformId, cl_device_id *p_deviceId) {
getDeviceIds(device, CL_DEVICE_TYPE_ALL, p_platformId, p_deviceId);
}
void DevicesInfo::getIdForIndexedPlatformDevice(int platform, int device, int types, cl_platform_id *p_platformId, cl_device_id *p_deviceId) {
#ifdef USE_CLEW
bool clpresent = 0 == clewInit();
if(!clpresent) {
throw std::runtime_error("OpenCL library not found");
}
#endif
cl_int error;
cl_platform_id platform_ids[10];
cl_uint num_platforms;
error = clGetPlatformIDs(10, platform_ids, &num_platforms);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting platforms ids: " + errorMessage(error));
}
if(num_platforms == 0) {
throw std::runtime_error("Error: no platforms available");
}
if(platform < 0) {
throw std::runtime_error("Error: platform index must be non-negative");
}
if(platform >= (int)num_platforms) {
throw runtime_error("Error: not enough platforms available to satisfy index");
}
cl_platform_id platform_id = platform_ids[platform];
cl_device_id device_ids[100];
cl_uint num_devices;
error = clGetDeviceIDs(platform_id, types, 100, device_ids, &num_devices);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting device ids for platform " + EasyCL::toString(platform) + ": " + errorMessage(error));
}
if(device < 0) {
throw runtime_error("Error: device must be non-negative");
}
if(device >= (int)num_devices) {
throw runtime_error("Error: device index greater than number available devices");
}
*p_platformId = platform_id;
*p_deviceId = device_ids[device];
}
}