forked from hughperkins/EasyCL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevicesInfo.cpp
More file actions
168 lines (164 loc) · 6.58 KB
/
DevicesInfo.cpp
File metadata and controls
168 lines (164 loc) · 6.58 KB
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
#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) {
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, 100, 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)];
return;
} else {
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() {
return getNumDevices(CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR);
}
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));
}
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_device_id device_ids[100];
cl_uint num_devices;
error = clGetDeviceIDs(platform_id, types, 100, device_ids, &num_devices);
if (error != CL_SUCCESS) {
continue;
// throw std::runtime_error("Error getting device ids for platform " + EasyCL::toString(platform) + ": " + errorMessage(error));
}
numDevices += num_devices;
}
return numDevices;
}
void DevicesInfo::getIdForIndexedGpu(int gpu, cl_platform_id *p_platformId, cl_device_id *p_deviceId) {
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];
}
}