-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathplatforminfo.cpp
More file actions
330 lines (307 loc) · 10.8 KB
/
platforminfo.cpp
File metadata and controls
330 lines (307 loc) · 10.8 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
*
* OpenCL hardware capability viewer
*
* Copyright (C) 2021 by Sascha Willems (www.saschawillems.de)
*
* This code is free software, you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 3 as published by the Free Software Foundation.
*
* Please review the following information to ensure the GNU Lesser
* General Public License version 3 requirements will be met:
* http://opensource.org/licenses/lgpl-3.0.html
*
* The code is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU LGPL 3.0 for more details.
*
*/
#include <unordered_map>
#include "platforminfo.h"
PlatformInfoValueDescriptor::PlatformInfoValueDescriptor()
{
this->name = 0;
this->valueType = clValueType::cl_char;
this->displayFunction = nullptr;
}
PlatformInfoValueDescriptor::PlatformInfoValueDescriptor(cl_platform_info name, clValueType valueType, PlatformInfoDisplayFn displayFunction)
{
this->name = name;
this->valueType = valueType;
this->displayFunction = displayFunction;
}
PlatformInfoValueDetailValue::PlatformInfoValueDetailValue(QString name, QVariant value, PlatformInfoDisplayFn displayFunction)
{
this->name = name;
this->detail = "";
this->value = value;
this->displayFunction = displayFunction;
}
PlatformInfoValueDetailValue::PlatformInfoValueDetailValue(QString name, QString detail, QVariant value, PlatformInfoDisplayFn displayFunction)
{
this->name = name;
this->detail = detail;
this->value = value;
this->displayFunction = displayFunction;
}
QString PlatformInfoValueDetailValue::getDisplayValue()
{
if (displayFunction) {
return displayFunction(value);
}
else {
return value.toString();
}
}
PlatformInfoValue::PlatformInfoValue(cl_platform_info info, QVariant value, QString extension, PlatformInfoDisplayFn displayFunction)
{
this->name = utils::platformInfoString(info);
this->value = value;
this->extension = extension;
this->enumValue = info;
this->displayFunction = displayFunction;
}
void PlatformInfoValue::addDetailValue(QString name, QVariant value, PlatformInfoDisplayFn displayFunction)
{
detailValues.push_back(PlatformInfoValueDetailValue(name, value, displayFunction));
}
void PlatformInfoValue::addDetailValue(QString name, QString detail, QVariant value, PlatformInfoDisplayFn displayFunction)
{
detailValues.push_back(PlatformInfoValueDetailValue(name, detail, value, displayFunction));
}
QString PlatformInfoValue::getDisplayValue()
{
if (displayFunction) {
return displayFunction(value);
} else {
return value.toString();
}
}
void PlatformInfo::readOpenCLVersion()
{
size_t valueSize;
_clGetPlatformInfo(this->platformId, CL_PLATFORM_VERSION, 0, nullptr, &valueSize);
std::string value;
value.resize(valueSize);
_clGetPlatformInfo(this->platformId, CL_PLATFORM_VERSION, valueSize, &value[0], nullptr);
// OpenCL<space><major_version.minor_version><space>
size_t versStart = value.find(' ', 0);
size_t versSplit = value.find('.', versStart + 1);
size_t versEnd = value.find(' ', versStart + 1);
std::string major, minor;
major = value.substr(versStart, versSplit - versStart);
minor = value.substr(versSplit + 1, versEnd - versSplit - 1);
clVersionMajor = std::stoi(major);
clVersionMinor = std::stoi(minor);
}
void PlatformInfo::readPlatformInfoValue(PlatformInfoValueDescriptor descriptor, QString extension)
{
qInfo() << "Reading platform info value for" << utils::platformInfoString(descriptor.name);
switch (descriptor.valueType)
{
case clValueType::cl_char:
{
size_t valueSize;
_clGetPlatformInfo(this->platformId, descriptor.name, 0, nullptr, &valueSize);
char* value = new char[valueSize];
_clGetPlatformInfo(this->platformId, descriptor.name, valueSize, &value[0], nullptr);
platformInfo.push_back(PlatformInfoValue(descriptor.name, QString::fromUtf8(value), extension));
delete[] value;
break;
}
case clValueType::cl_ulong:
{
cl_ulong value;
_clGetPlatformInfo(this->platformId, descriptor.name, sizeof(cl_ulong), &value, nullptr);
platformInfo.push_back(PlatformInfoValue(descriptor.name, QVariant::fromValue(value), extension));
break;
}
case clValueType::cl_version:
{
cl_version value;
_clGetPlatformInfo(this->platformId, descriptor.name, sizeof(cl_version), &value, nullptr);
platformInfo.push_back(PlatformInfoValue(descriptor.name, value, extension));
break;
}
case clValueType::cl_external_memory_handle_type_khr_array:
{
size_t valueSize{ 0 };
_clGetPlatformInfo(this->platformId, descriptor.name, 0, nullptr, &valueSize);
PlatformInfoValue infoValue(descriptor.name, 0, extension, descriptor.displayFunction);
if (valueSize > 0) {
std::vector<cl_external_memory_handle_type_khr> values;
values.resize(valueSize / sizeof(cl_external_memory_handle_type_khr));
infoValue.value = QVariant::fromValue(values.size());
_clGetPlatformInfo(this->platformId, descriptor.name, valueSize, &values[0], nullptr);
for (auto& value : values) {
infoValue.addDetailValue("Handle Type", QVariant::fromValue(value), utils::displayExternalMemoryHandleTypes);
}
}
platformInfo.push_back(infoValue);
break;
}
case clValueType::cl_external_semaphore_handle_type_khr:
{
size_t valueSize{ 0 };
_clGetPlatformInfo(this->platformId, descriptor.name, 0, nullptr, &valueSize);
PlatformInfoValue infoValue(descriptor.name, 0, extension, descriptor.displayFunction);
if (valueSize > 0) {
std::vector<cl_external_semaphore_handle_type_khr> values;
values.resize(valueSize / sizeof(cl_external_semaphore_handle_type_khr));
infoValue.value = QVariant::fromValue(values.size());
_clGetPlatformInfo(this->platformId, descriptor.name, valueSize, &values[0], nullptr);
for (auto& value : values) {
infoValue.addDetailValue("Handle type", QVariant::fromValue(value), utils::displayExternalSemaphoreHandleTypes);
}
}
platformInfo.push_back(infoValue);
break;
}
default:
qDebug("Unknown platform info type");
}
}
void PlatformInfo::readExtensions()
{
extensions.clear();
if (clVersionMajor >= 3) {
qInfo() << "Reading platform extension list with versions (CL >=3.0) for platform" << platformId;
size_t extSize;
_clGetPlatformInfo(this->platformId, CL_PLATFORM_EXTENSIONS_WITH_VERSION, 0, nullptr, &extSize);
cl_name_version* extensions = new cl_name_version[4096];
_clGetPlatformInfo(this->platformId, CL_PLATFORM_EXTENSIONS_WITH_VERSION, extSize, extensions, nullptr);
for (size_t i = 0; i < extSize / sizeof(cl_name_version); i++) {
PlatformExtension extension{};
extension.name = extensions[i].name;
extension.version = extensions[i].version;
this->extensions.push_back(extension);
}
delete[] extensions;
} else {
qInfo() << "Reading platform extension list (CL <3.0) for platform" << platformId;
size_t extStrSize;
_clGetPlatformInfo(this->platformId, CL_PLATFORM_EXTENSIONS, 0, nullptr, &extStrSize);
std::string extensionString;
extensionString.resize(extStrSize);
_clGetPlatformInfo(this->platformId, CL_PLATFORM_EXTENSIONS, extStrSize, &extensionString[0], nullptr);
std::vector<std::string> extensions;
extensions = utils::explode(extensionString, ' ');
for (size_t i = 0; i < extensions.size(); i++) {
PlatformExtension extension{};
// Remove trailing zeros
extensions[i].erase(std::find(extensions[i].begin(), extensions[i].end(), '\0'), extensions[i].end());
extension.name = QString::fromStdString(extensions[i]);
// Skip empty extension strings
if (extensions[i].length() == 0) {
continue;
}
extension.version = 0;
this->extensions.push_back(extension);
}
}
}
bool PlatformInfo::extensionSupported(const char* name)
{
for (auto& ext : extensions) {
if (ext.name == QLatin1String(name)) {
return true;
}
}
return false;
}
void PlatformInfo::read()
{
platformInfo.clear();
std::vector<PlatformInfoValueDescriptor> infoList = {
{ CL_PLATFORM_PROFILE, clValueType::cl_char },
{ CL_PLATFORM_VERSION, clValueType::cl_char },
{ CL_PLATFORM_NAME, clValueType::cl_char },
{ CL_PLATFORM_VENDOR, clValueType::cl_char },
};
for (auto &info : infoList)
{
readPlatformInfoValue(info);
}
readOpenCLVersion();
readExtensions();
readExtensionInfo();
// Version dependent information
if ((clVersionMajor == 2) & (clVersionMinor >= 1) || (clVersionMajor >= 3)) {
readPlatformInfoValue(PlatformInfoValueDescriptor(CL_PLATFORM_HOST_TIMER_RESOLUTION, clValueType::cl_ulong));
}
}
void PlatformInfo::readExtensionInfo()
{
qInfo() << "Reading extension info values for platform" << platformId;
// KHR
if (extensionSupported("cl_khr_icd")) {
std::vector<PlatformInfoValueDescriptor> infoList = {
{ CL_PLATFORM_ICD_SUFFIX_KHR, clValueType::cl_char },
};
for (auto &info : infoList) {
readPlatformInfoValue(info, "cl_khr_icd");
}
}
if (extensionSupported("cl_khr_external_memory")) {
std::vector<PlatformInfoValueDescriptor> infoList = {
{ CL_PLATFORM_EXTERNAL_MEMORY_IMPORT_HANDLE_TYPES_KHR, clValueType::cl_external_memory_handle_type_khr_array, utils::displayDetailValueArraySize },
};
for (auto &info : infoList) {
readPlatformInfoValue(info, "cl_khr_external_memory");
}
}
if (extensionSupported("cl_khr_external_semaphore")) {
std::vector<PlatformInfoValueDescriptor> infoList = {
{ CL_PLATFORM_SEMAPHORE_IMPORT_HANDLE_TYPES_KHR, clValueType::cl_external_semaphore_handle_type_khr, utils::displayDetailValueArraySize },
{ CL_PLATFORM_SEMAPHORE_EXPORT_HANDLE_TYPES_KHR, clValueType::cl_external_semaphore_handle_type_khr, utils::displayDetailValueArraySize },
};
for (auto& info : infoList) {
readPlatformInfoValue(info, "cl_khr_external_semaphore");
}
}
}
QJsonObject PlatformInfo::toJson()
{
QJsonObject jsonRoot;
// Extensions
QJsonArray jsonExtensions;
for (auto& ext : extensions)
{
QJsonObject jsonNode;
jsonNode["name"] = ext.name;
jsonNode["version"] = int(ext.version);
jsonExtensions.append(jsonNode);
}
jsonRoot["extensions"] = jsonExtensions;
// Platform info
QJsonArray jsonDeviceInfos;
for (auto& info : platformInfo)
{
QJsonObject jsonNode;
jsonNode["name"] = info.name;
jsonNode["extension"] = info.extension;
jsonNode["enumvalue"] = info.enumValue;
jsonNode["value"] = info.value.toJsonValue();
// Optional details for the platform info
if (info.detailValues.size() > 0) {
QJsonArray jsonDetails;
for (auto& detail : info.detailValues) {
QJsonObject jsonNodeDetail;
jsonNodeDetail["name"] = detail.name;
if (detail.detail.isEmpty()) {
jsonNodeDetail["detail"] = QJsonValue::Null;
}
else {
jsonNodeDetail["detail"] = detail.detail;
}
jsonNodeDetail["value"] = detail.value.toJsonValue();
jsonDetails.append(jsonNodeDetail);
}
jsonNode["details"] = jsonDetails;
}
jsonDeviceInfos.append(jsonNode);
}
jsonRoot["info"] = jsonDeviceInfos;
return jsonRoot;
}