Compute Library
 21.08
CLDevice.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_CLDEVICE_H
25 #define ARM_COMPUTE_CLDEVICE_H
26 
31 
32 #include <set>
33 #include <sstream>
34 #include <string>
35 
36 namespace arm_compute
37 {
38 /** OpenCL device type class
39  *
40  * Initializes and stores all the information about a cl device,
41  * working mainly as a cache mechanism.
42  * */
43 class CLDevice : public IDevice
44 {
45 public:
46  /** Default Constructor */
48  : _device(cl::Device()), _options()
49  {
50  }
51 
52  /** Constructor
53  *
54  * @param[in] cl_device OpenCL device
55  */
56  CLDevice(const cl::Device &cl_device)
57  : _device(), _options()
58  {
59  _device = cl_device;
60 
61  // Get device target
62  std::string device_name = _device.getInfo<CL_DEVICE_NAME>();
63  _options.gpu_target = get_target_from_name(device_name);
64 
65  // Fill extensions
66  std::string extensions = _device.getInfo<CL_DEVICE_EXTENSIONS>();
67 
68  std::istringstream iss(extensions);
69  for(std::string s; iss >> s;)
70  {
71  _options.extensions.insert(s);
72  }
73 
74  // SW workaround for G76
75  if(_options.gpu_target == GPUTarget::G76)
76  {
77  _options.extensions.insert("cl_arm_integer_dot_product_int8");
78  }
79 
80  // Get device version
81  _options.version = get_cl_version(_device);
82 
83  // Get compute units
84  _options.compute_units = _device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>();
85 
86  // Get device version
87  _options.device_version = _device.getInfo<CL_DEVICE_VERSION>();
88  }
89 
90  /** Returns the GPU target of the cl device
91  *
92  * @return The GPU target
93  */
94  const GPUTarget &target() const
95  {
96  return _options.gpu_target;
97  }
98 
99  /** Returns the number of compute units available
100  *
101  * @return Number of compute units
102  */
103  size_t compute_units() const
104  {
105  return _options.compute_units;
106  }
107 
108  /** Returns the underlying cl device object
109  *
110  * @return A cl device
111  */
112  const cl::Device &cl_device() const
113  {
114  return _device;
115  }
116 
117  /** Returns the device's CL version
118  *
119  * @return CLVersion of the device
120  */
122  {
123  return _options.version;
124  }
125 
126  /** Returns the device version as a string
127  *
128  * @return CLVersion of the device
129  */
130  std::string device_version() const
131  {
132  return _options.device_version;
133  }
134 
135  // Inherrited methods
136  DeviceType type() const override
137  {
138  return DeviceType::CL;
139  }
140 
141  bool supported(const std::string &extension) const override
142  {
143  return _options.extensions.count(extension) != 0;
144  }
145 
146 private:
147  cl::Device _device; /**< OpenCL device. */
148  struct CLDeviceOptions _options; /**< OpenCL device options */
149 };
150 
151 } // namespace arm_compute
152 
153 #endif /* ARM_COMPUTE_CLDEVICE_H */
size_t compute_units
Number of compute units.
Definition: CLTypes.h:57
Definition: ICLTensor.h:33
std::string device_version() const
Returns the device version as a string.
Definition: CLDevice.h:130
Interface for device object.
Definition: IDevice.h:40
OpenCL device options.
Definition: CLTypes.h:49
DeviceType type() const override
Device type accessor.
Definition: CLDevice.h:136
CLDevice(const cl::Device &cl_device)
Constructor.
Definition: CLDevice.h:56
CLVersion
Available OpenCL Version.
Definition: CLTypes.h:39
GPUTarget get_target_from_name(const std::string &device_name)
Helper function to get the GPU target from a device name.
Definition: GPUTarget.cpp:145
GPUTarget gpu_target
GPU target architecture/instance.
Definition: CLTypes.h:55
Copyright (c) 2017-2021 Arm Limited.
std::set< std::string > extensions
List of supported extensions.
Definition: CLTypes.h:53
CLVersion version() const
Returns the device&#39;s CL version.
Definition: CLDevice.h:121
const GPUTarget & target() const
Returns the GPU target of the cl device.
Definition: CLDevice.h:94
OpenCL device type class.
Definition: CLDevice.h:43
bool supported(const std::string &extension) const override
Check if extensions on a device are supported.
Definition: CLDevice.h:141
CLDevice()
Default Constructor.
Definition: CLDevice.h:47
CLVersion get_cl_version(const cl::Device &device)
Helper function to get the highest OpenCL version supported.
Definition: CLHelpers.cpp:256
size_t compute_units() const
Returns the number of compute units available.
Definition: CLDevice.h:103
std::string device_version
Device version string.
Definition: CLTypes.h:52
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
const cl::Device & cl_device() const
Returns the underlying cl device object.
Definition: CLDevice.h:112
CLVersion version
Device OpenCL version.
Definition: CLTypes.h:56
DeviceType
Device types.
Definition: IDevice.h:33