1 //
2 // Copyright (c) 2017-2019 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <sstream>
18 #include <stdexcept>
19 #include <vector>
20
21 #include "deviceInfo.h"
22 #include "errorHelpers.h"
23 #include "typeWrappers.h"
24
25 /* Helper to return a string containing device information for the specified
26 * device info parameter. */
get_device_info_string(cl_device_id device,cl_device_info param_name)27 std::string get_device_info_string(cl_device_id device,
28 cl_device_info param_name)
29 {
30 size_t size = 0;
31 int err;
32
33 if ((err = clGetDeviceInfo(device, param_name, 0, NULL, &size))
34 != CL_SUCCESS
35 || size == 0)
36 {
37 throw std::runtime_error("clGetDeviceInfo failed\n");
38 }
39
40 std::vector<char> info(size);
41
42 if ((err = clGetDeviceInfo(device, param_name, size, info.data(), NULL))
43 != CL_SUCCESS)
44 {
45 throw std::runtime_error("clGetDeviceInfo failed\n");
46 }
47
48 /* The returned string does not include the null terminator. */
49 return std::string(info.data(), size - 1);
50 }
51
52 /* Determines if an extension is supported by a device. */
is_extension_available(cl_device_id device,const char * extensionName)53 int is_extension_available(cl_device_id device, const char *extensionName)
54 {
55 std::string extString = get_device_extensions_string(device);
56 std::istringstream ss(extString);
57 while (ss)
58 {
59 std::string found;
60 ss >> found;
61 if (found == extensionName) return true;
62 }
63 return false;
64 }
65
get_extension_version(cl_device_id device,const char * extensionName)66 cl_version get_extension_version(cl_device_id device, const char *extensionName)
67 {
68 cl_int err;
69 size_t size;
70
71 err = clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS_WITH_VERSION, 0, nullptr,
72 &size);
73 if (err != CL_SUCCESS)
74 {
75 throw std::runtime_error("clGetDeviceInfo(CL_DEVICE_EXTENSIONS_WITH_"
76 "VERSION) failed to return size\n");
77 }
78
79 std::vector<cl_name_version> extensions(size / sizeof(cl_name_version));
80 err = clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS_WITH_VERSION, size,
81 extensions.data(), &size);
82 if (err != CL_SUCCESS)
83 {
84 throw std::runtime_error("clGetDeviceInfo(CL_DEVICE_EXTENSIONS_WITH_"
85 "VERSION) failed to return value\n");
86 }
87
88 for (auto &ext : extensions)
89 {
90 if (!strcmp(extensionName, ext.name))
91 {
92 return ext.version;
93 }
94 }
95
96 throw std::runtime_error("Extension " + std::string(extensionName)
97 + " not supported by device!");
98 }
99
100 /* Returns a string containing the supported extensions list for a device. */
get_device_extensions_string(cl_device_id device)101 std::string get_device_extensions_string(cl_device_id device)
102 {
103 return get_device_info_string(device, CL_DEVICE_EXTENSIONS);
104 }
105
106 /* Returns a string containing the supported IL version(s) for a device. */
get_device_il_version_string(cl_device_id device)107 std::string get_device_il_version_string(cl_device_id device)
108 {
109 return get_device_info_string(device, CL_DEVICE_IL_VERSION);
110 }
111
112 /* Returns a string containing the supported OpenCL version for a device. */
get_device_version_string(cl_device_id device)113 std::string get_device_version_string(cl_device_id device)
114 {
115 return get_device_info_string(device, CL_DEVICE_VERSION);
116 }
117
118 /* Returns a string containing the device name. */
get_device_name(cl_device_id device)119 std::string get_device_name(cl_device_id device)
120 {
121 return get_device_info_string(device, CL_DEVICE_NAME);
122 }
123
get_max_param_size(cl_device_id device)124 size_t get_max_param_size(cl_device_id device)
125 {
126 size_t ret(0);
127 if (clGetDeviceInfo(device, CL_DEVICE_MAX_PARAMETER_SIZE, sizeof(ret), &ret,
128 nullptr)
129 != CL_SUCCESS)
130 {
131 throw std::runtime_error("clGetDeviceInfo failed\n");
132 }
133 return ret;
134 }
135