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
66 /* Returns a string containing the supported extensions list for a device. */
get_device_extensions_string(cl_device_id device)67 std::string get_device_extensions_string(cl_device_id device)
68 {
69 return get_device_info_string(device, CL_DEVICE_EXTENSIONS);
70 }
71
72 /* Returns a string containing the supported IL version(s) for a device. */
get_device_il_version_string(cl_device_id device)73 std::string get_device_il_version_string(cl_device_id device)
74 {
75 return get_device_info_string(device, CL_DEVICE_IL_VERSION);
76 }
77
78 /* Returns a string containing the supported OpenCL version for a device. */
get_device_version_string(cl_device_id device)79 std::string get_device_version_string(cl_device_id device)
80 {
81 return get_device_info_string(device, CL_DEVICE_VERSION);
82 }
83
84 /* Returns a string containing the device name. */
get_device_name(cl_device_id device)85 std::string get_device_name(cl_device_id device)
86 {
87 return get_device_info_string(device, CL_DEVICE_NAME);
88 }
89
get_max_param_size(cl_device_id device)90 size_t get_max_param_size(cl_device_id device)
91 {
92 size_t ret(0);
93 if (clGetDeviceInfo(device, CL_DEVICE_MAX_PARAMETER_SIZE, sizeof(ret), &ret,
94 nullptr)
95 != CL_SUCCESS)
96 {
97 throw std::runtime_error("clGetDeviceInfo failed\n");
98 }
99 return ret;
100 }
101