-
Notifications
You must be signed in to change notification settings - Fork 26
/
OpenCL_Device.cpp
160 lines (127 loc) · 5.55 KB
/
OpenCL_Device.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
#include "OpenCL_Error.h"
#include "OpenCL_Device.h"
OpenclDevice::OpenclDevice(const cl_device_id& p_handle) throw (std::exception)
: m_handle(p_handle) {
}
OpenclDevice::~OpenclDevice() throw () {
}
std::string OpenclDevice::getType() const throw (std::exception) {
cl_device_type type;
cl_int error = clGetDeviceInfo(m_handle, CL_DEVICE_TYPE, sizeof(cl_device_type), (void*)&type, 0);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve device type");
}
switch (type) {
case CL_DEVICE_TYPE_CPU:
return "CPU";
break;
case CL_DEVICE_TYPE_GPU:
return "GPU";
break;
case CL_DEVICE_TYPE_ACCELERATOR:
return "Accelerator";
break;
default:
return "Unknown";
}
}
std::string OpenclDevice::getName() const throw (std::exception) {
return getInfoString(CL_DEVICE_NAME);
}
std::string OpenclDevice::getVendor() const throw (std::exception) {
return getInfoString(CL_DEVICE_VENDOR);
}
std::string OpenclDevice::getVersion() const throw (std::exception) {
return getInfoString(CL_DEVICE_VERSION);
}
std::string OpenclDevice::getDriverVersion() const throw (std::exception) {
return getInfoString(CL_DRIVER_VERSION);
}
unsigned int OpenclDevice::getMaxClockFrequency() const throw (std::exception) {
return getInfoUint(CL_DEVICE_MAX_CLOCK_FREQUENCY);
}
unsigned int OpenclDevice::getMaxComputeUnits() const throw (std::exception) {
return getInfoUint(CL_DEVICE_MAX_COMPUTE_UNITS);
}
unsigned long long OpenclDevice::getGlobalMemorySize() const throw (std::exception) {
return getInfoUlong(CL_DEVICE_GLOBAL_MEM_SIZE);
}
unsigned long long OpenclDevice::getMaxMemoryAllocationSize() const throw (std::exception) {
return getInfoUlong(CL_DEVICE_MAX_MEM_ALLOC_SIZE);
}
std::size_t OpenclDevice::getMaxWorkGroupSize() const throw (std::exception) {
return getInfoSizet(CL_DEVICE_MAX_WORK_GROUP_SIZE);
}
unsigned long long OpenclDevice::getLocalMemorySize() const throw (std::exception) {
return getInfoUlong(CL_DEVICE_LOCAL_MEM_SIZE);
}
std::vector<std::size_t> OpenclDevice::getMaxWorkItemSizes() const throw (std::exception) {
cl_int error;
cl_uint maxWorkItemDimensions;
error = clGetDeviceInfo(m_handle, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), (void*)&maxWorkItemDimensions, 0);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve device max work item dimensions");
}
std::unique_ptr<std::size_t[]> maxWorkItemSizes(new std::size_t[maxWorkItemDimensions]);
error = clGetDeviceInfo(m_handle, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(std::size_t) * maxWorkItemDimensions, (void*)maxWorkItemSizes.get(), 0);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve device max work item sizes");
}
return std::vector<std::size_t>(maxWorkItemSizes.get(), maxWorkItemSizes.get() + maxWorkItemDimensions);
}
std::string OpenclDevice::getInfoString(const cl_platform_info& p_paramName) const throw (std::exception) {
cl_int error;
std::size_t size = 0;
error = clGetDeviceInfo(m_handle, p_paramName, 0, 0, &size);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve device info size");
}
std::unique_ptr<char[]> buffer(new char[size]);
error = clGetDeviceInfo(m_handle, p_paramName, size, (void*)buffer.get(), 0);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve device info value");
}
return std::string(buffer.get());
}
unsigned int OpenclDevice::getInfoUint(const cl_platform_info& p_paramName) const throw (std::exception) {
cl_uint value = 0;
cl_int error = clGetDeviceInfo(m_handle, p_paramName, sizeof(cl_uint), (void*)&value, 0);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve device info value");
}
return value;
}
std::size_t OpenclDevice::getInfoSizet(const cl_platform_info& p_paramName) const throw (std::exception) {
std::size_t value = 0;
cl_int error = clGetDeviceInfo(m_handle, p_paramName, sizeof(std::size_t), (void*)&value, 0);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve device info value");
}
return value;
}
unsigned long long OpenclDevice::getInfoUlong(const cl_platform_info& p_paramName) const throw (std::exception) {
cl_ulong value = 0;
cl_int error = clGetDeviceInfo(m_handle, p_paramName, sizeof(cl_ulong), (void*)&value, 0);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve device info value");
}
return value;
}
std::vector<std::shared_ptr<OpenclDevice>> OpenclDevice::list(const std::shared_ptr<OpenclPlatform>& p_platform) throw (std::exception) {
std::vector<std::shared_ptr<OpenclDevice>> list;
cl_int error;
cl_uint devicesNumber = 0;
error = clGetDeviceIDs(p_platform->getHandle(), CL_DEVICE_TYPE_ALL, 0, 0, &devicesNumber);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve the OpenCL devices number");
}
std::unique_ptr<cl_device_id[]> devices(new cl_device_id[devicesNumber]);
error = clGetDeviceIDs(p_platform->getHandle(), CL_DEVICE_TYPE_ALL, devicesNumber, devices.get(), 0);
if (error != CL_SUCCESS) {
throw OpenclError(error, "Unable to retrieve the OpenCL devices");
}
for (cl_uint i = 0; i < devicesNumber; ++i) {
list.push_back(std::shared_ptr<OpenclDevice>(new OpenclDevice(devices[i])));
}
return list;
}