blob: ded6bb84938055b53647899429e82b7796f3b342 [file] [log] [blame]
Michalis Spyrou11d49182020-03-26 10:31:32 +00001/*
Viet-Hoa Dof8bb0922022-05-30 15:15:15 +01002 * Copyright (c) 2020-2022 Arm Limited.
Michalis Spyrou11d49182020-03-26 10:31:32 +00003 *
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
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLTypes.h"
29#include "arm_compute/core/GPUTarget.h"
30#include "arm_compute/core/IDevice.h"
31
32#include <set>
Georgios Pinitasc6f95102021-03-30 10:03:01 +010033#include <sstream>
Michalis Spyrou11d49182020-03-26 10:31:32 +000034#include <string>
35
36namespace 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 * */
43class CLDevice : public IDevice
44{
45public:
46 /** Default Constructor */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 CLDevice() : _device(cl::Device()), _options()
Michalis Spyrou11d49182020-03-26 10:31:32 +000048 {
49 }
50
51 /** Constructor
52 *
53 * @param[in] cl_device OpenCL device
54 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 CLDevice(const cl::Device &cl_device) : _device(), _options()
Michalis Spyrou11d49182020-03-26 10:31:32 +000056 {
57 _device = cl_device;
58
59 // Get device target
60 std::string device_name = _device.getInfo<CL_DEVICE_NAME>();
61 _options.gpu_target = get_target_from_name(device_name);
62
63 // Fill extensions
64 std::string extensions = _device.getInfo<CL_DEVICE_EXTENSIONS>();
65
66 std::istringstream iss(extensions);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 for (std::string s; iss >> s;)
Michalis Spyrou11d49182020-03-26 10:31:32 +000068 {
69 _options.extensions.insert(s);
70 }
71
72 // SW workaround for G76
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 if (_options.gpu_target == GPUTarget::G76)
Michalis Spyrou11d49182020-03-26 10:31:32 +000074 {
75 _options.extensions.insert("cl_arm_integer_dot_product_int8");
76 }
77
78 // Get device version
79 _options.version = get_cl_version(_device);
80
81 // Get compute units
82 _options.compute_units = _device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>();
83
84 // Get device version
85 _options.device_version = _device.getInfo<CL_DEVICE_VERSION>();
86 }
87
88 /** Returns the GPU target of the cl device
89 *
90 * @return The GPU target
91 */
92 const GPUTarget &target() const
93 {
94 return _options.gpu_target;
95 }
96
97 /** Returns the number of compute units available
98 *
99 * @return Number of compute units
100 */
101 size_t compute_units() const
102 {
103 return _options.compute_units;
104 }
105
106 /** Returns the underlying cl device object
107 *
108 * @return A cl device
109 */
110 const cl::Device &cl_device() const
111 {
112 return _device;
113 }
114
115 /** Returns the device's CL version
116 *
117 * @return CLVersion of the device
118 */
119 CLVersion version() const
120 {
121 return _options.version;
122 }
123
124 /** Returns the device version as a string
125 *
126 * @return CLVersion of the device
127 */
128 std::string device_version() const
129 {
130 return _options.device_version;
131 }
132
133 // Inherrited methods
134 DeviceType type() const override
135 {
136 return DeviceType::CL;
137 }
138
139 bool supported(const std::string &extension) const override
140 {
141 return _options.extensions.count(extension) != 0;
142 }
143
Viet-Hoa Dof8bb0922022-05-30 15:15:15 +0100144 /** Returns whether non-uniform workgroup is supported and the build options.
145 *
146 * If the feature is supported, the appropriate build options will be
147 * appended to the specified string.
148 *
149 * @return A tuple (supported, build_options) indicating whether the feature
150 * is supported and the corresponding build options to enable it.
151 */
152 std::tuple<bool, std::string> is_non_uniform_workgroup_supported() const
153 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 if (version() == CLVersion::CL30 && get_cl_non_uniform_work_group_supported(_device))
Viet-Hoa Dof8bb0922022-05-30 15:15:15 +0100155 {
156 return {true, " -cl-std=CL3.0 "};
157 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158 else if (version() == CLVersion::CL20)
Viet-Hoa Dof8bb0922022-05-30 15:15:15 +0100159 {
160 return {true, " -cl-std=CL2.0 "};
161 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100162 else if (supported("cl_arm_non_uniform_work_group_size"))
Viet-Hoa Dof8bb0922022-05-30 15:15:15 +0100163 {
164 return {true, " -cl-arm-non-uniform-work-group-size "};
165 }
166
167 return {false, ""};
168 }
169
Michalis Spyrou11d49182020-03-26 10:31:32 +0000170private:
171 cl::Device _device; /**< OpenCL device. */
172 struct CLDeviceOptions _options; /**< OpenCL device options */
173};
174
175} // namespace arm_compute
176
177#endif /* ARM_COMPUTE_CLDEVICE_H */