forked from bashbaug/SimpleOpenCLSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.hpp
More file actions
86 lines (76 loc) · 2.22 KB
/
util.hpp
File metadata and controls
86 lines (76 loc) · 2.22 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
/*
// Copyright (c) 2021-2026 Ben Ashbaugh
//
// SPDX-License-Identifier: MIT
*/
#pragma once
#include <CL/opencl.hpp>
#include <string>
static cl_version getDeviceOpenCLVersion(
const cl::Device& device)
{
cl_uint major = 0;
cl_uint minor = 0;
std::string version = device.getInfo<CL_DEVICE_VERSION>();
// The device version string has the form:
// OpenCL <Major>.<Minor> <Vendor Specific Info>
const std::string prefix{"OpenCL "};
if (!version.compare(0, prefix.length(), prefix)) {
const char* check = version.c_str() + prefix.length();
while (isdigit(check[0])) {
major *= 10;
major += check[0] - '0';
++check;
}
if (check[0] == '.') {
++check;
}
while (isdigit(check[0])) {
minor *= 10;
minor += check[0] - '0';
++check;
}
}
return CL_MAKE_VERSION(major, minor, 0);
}
static bool checkDeviceForExtension(
const cl::Device& device,
const char* extensionName)
{
bool supported = false;
if (extensionName && !strchr(extensionName, ' ')) {
std::string deviceExtensions = device.getInfo<CL_DEVICE_EXTENSIONS>();
const char* start = deviceExtensions.c_str();
while (true) {
const char* where = strstr(start, extensionName);
if (!where) {
break;
}
const char* terminator = where + strlen(extensionName);
if (where == start || *(where - 1) == ' ') {
if (*terminator == ' ' || *terminator == '\0') {
supported = true;
break;
}
}
start = terminator;
}
}
return supported;
}
static bool checkPlatformIndex(
const std::vector<cl::Platform>& platforms,
int platformIndex)
{
if (platforms.size() == 0) {
fprintf(stderr, "Error: No OpenCL platforms found.\n");
return false;
}
if (platformIndex >= (int)platforms.size()) {
fprintf(stderr, "Error: Invalid platform index %d specified (max %d)\n",
platformIndex,
(int)(platforms.size() - 1) );
return false;
}
return true;
}