• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static 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     return std::string(info.begin(), info.end());
49 }
50 
51 /* Determines if an extension is supported by a device. */
is_extension_available(cl_device_id device,const char * extensionName)52 int is_extension_available(cl_device_id device, const char *extensionName)
53 {
54     std::string extString = get_device_extensions_string(device);
55     std::istringstream ss(extString);
56     while (ss)
57     {
58         std::string found;
59         ss >> found;
60         if (found == extensionName) return true;
61     }
62     return false;
63 }
64 
65 /* Returns a string containing the supported extensions list for a device. */
get_device_extensions_string(cl_device_id device)66 std::string get_device_extensions_string(cl_device_id device)
67 {
68     return get_device_info_string(device, CL_DEVICE_EXTENSIONS);
69 }
70 
71 /* Returns a string containing the supported IL version(s) for a device. */
get_device_il_version_string(cl_device_id device)72 std::string get_device_il_version_string(cl_device_id device)
73 {
74     return get_device_info_string(device, CL_DEVICE_IL_VERSION);
75 }
76 
77 /* Returns a string containing the supported OpenCL version for a device. */
get_device_version_string(cl_device_id device)78 std::string get_device_version_string(cl_device_id device)
79 {
80     return get_device_info_string(device, CL_DEVICE_VERSION);
81 }
82