blob: 5ea99d360a6dedaa830c6efb2ce8e8769e8be45d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gunes Bayirbbeef722023-03-20 10:19:10 +00002 * Copyright (c) 2016-2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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#include "arm_compute/core/CL/CLHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Gian Marco Iodice8155c022021-04-16 15:08:59 +010026#include "arm_compute/core/CL/CLKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/CLTypes.h"
28#include "arm_compute/core/Error.h"
Georgios Pinitas3faea252017-10-30 14:13:50 +000029#include "arm_compute/core/Log.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Types.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "arm_compute/core/utils/DataTypeUtils.h"
Georgios Pinitas908f6162021-05-04 10:11:09 +010032
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033#include "src/gpu/cl/ClCompileContext.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010034#include "src/gpu/cl/ClKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010036#include <utility>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include <vector>
38
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039namespace arm_compute
40{
41std::string get_cl_type_from_data_type(const DataType &dt)
42{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043 switch (dt)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 {
45 case DataType::U8:
Michel Iwaniec00633802017-10-12 14:14:15 +010046 case DataType::QASYMM8:
47 return "uchar";
Georgios Pinitas3d13af82019-06-04 13:04:16 +010048 case DataType::S8:
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000049 case DataType::QASYMM8_SIGNED:
Georgios Pinitas3d13af82019-06-04 13:04:16 +010050 case DataType::QSYMM8:
Michalis Spyrou3f632f32019-08-22 16:52:00 +010051 case DataType::QSYMM8_PER_CHANNEL:
Georgios Pinitas3d13af82019-06-04 13:04:16 +010052 return "char";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010054 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 return "ushort";
56 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +010057 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 return "short";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 case DataType::U32:
60 return "uint";
61 case DataType::S32:
62 return "int";
63 case DataType::U64:
64 return "ulong";
65 case DataType::S64:
66 return "long";
67 case DataType::F16:
68 return "half";
69 case DataType::F32:
70 return "float";
71 default:
72 ARM_COMPUTE_ERROR("Unsupported input data type.");
73 return "";
74 }
75}
76
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010077std::string get_cl_promoted_type_from_data_type(const DataType &dt)
78{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 switch (dt)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010080 {
81 case DataType::U8:
82 case DataType::QASYMM8:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010083 return "ushort";
84 case DataType::S8:
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000085 case DataType::QASYMM8_SIGNED:
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010086 case DataType::QSYMM8:
87 case DataType::QSYMM8_PER_CHANNEL:
88 return "short";
89 case DataType::U16:
90 case DataType::QASYMM16:
91 return "uint";
92 case DataType::S16:
93 case DataType::QSYMM16:
94 return "int";
95 case DataType::U32:
96 return "ulong";
97 case DataType::S32:
98 return "long";
99 case DataType::F16:
100 return "float";
101 default:
102 ARM_COMPUTE_ERROR("Cannot get promoted OpenCL type for the input data type.");
103 return "";
104 }
105}
106
107std::string get_cl_unsigned_type_from_element_size(size_t element_size)
108{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 switch (element_size)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100110 {
111 case 1:
112 return "uchar";
113 case 2:
114 return "ushort";
115 case 4:
116 return "uint";
117 case 8:
118 return "ulong";
119 default:
120 ARM_COMPUTE_ERROR("Data type not supported");
121 return "";
122 }
123}
124
Michalis Spyrou7317e392020-01-17 11:27:49 +0000125std::string get_cl_signed_type_from_element_size(size_t element_size)
126{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100127 switch (element_size)
Michalis Spyrou7317e392020-01-17 11:27:49 +0000128 {
129 case 1:
130 return "char";
131 case 2:
132 return "short";
133 case 4:
134 return "int";
135 case 8:
136 return "long";
137 default:
138 ARM_COMPUTE_ERROR("Data type not supported");
139 return "";
140 }
141}
142
Giorgio Arena73023022018-09-04 14:55:55 +0100143std::string get_cl_select_type_from_data_type(const DataType &dt)
144{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 switch (dt)
Giorgio Arena73023022018-09-04 14:55:55 +0100146 {
147 case DataType::U8:
Giorgio Arena73023022018-09-04 14:55:55 +0100148 case DataType::QASYMM8:
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100149 case DataType::S8:
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000150 case DataType::QASYMM8_SIGNED:
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100151 case DataType::QSYMM8:
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100152 case DataType::QSYMM8_PER_CHANNEL:
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100153 return "char";
Giorgio Arena73023022018-09-04 14:55:55 +0100154 case DataType::U16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100155 case DataType::QASYMM16:
Giorgio Arena73023022018-09-04 14:55:55 +0100156 return "ushort";
157 case DataType::F16:
158 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +0100159 case DataType::QSYMM16:
Giorgio Arena73023022018-09-04 14:55:55 +0100160 return "short";
161 case DataType::U32:
162 return "uint";
163 case DataType::F32:
164 case DataType::S32:
165 return "int";
166 case DataType::U64:
167 return "ulong";
168 case DataType::S64:
169 return "long";
170 default:
171 ARM_COMPUTE_ERROR("Unsupported input data type.");
172 return "";
173 }
174}
175
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000176std::string get_cl_dot8_acc_type_from_data_type(const DataType &dt)
177{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100178 switch (dt)
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000179 {
180 case DataType::U8:
181 case DataType::QASYMM8:
182 return "uint";
183 case DataType::S8:
184 case DataType::QASYMM8_SIGNED:
185 case DataType::QSYMM8:
186 case DataType::QSYMM8_PER_CHANNEL:
187 return "int";
188 default:
189 ARM_COMPUTE_ERROR("Unsupported data type.");
190 return "";
191 }
192}
193
SiCong Lic51b72f2017-07-28 14:46:20 +0100194std::string get_data_size_from_data_type(const DataType &dt)
195{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100196 switch (dt)
SiCong Lic51b72f2017-07-28 14:46:20 +0100197 {
198 case DataType::U8:
SiCong Lic51b72f2017-07-28 14:46:20 +0100199 case DataType::S8:
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100200 case DataType::QSYMM8:
Michel Iwaniec00633802017-10-12 14:14:15 +0100201 case DataType::QASYMM8:
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000202 case DataType::QASYMM8_SIGNED:
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100203 case DataType::QSYMM8_PER_CHANNEL:
SiCong Lic51b72f2017-07-28 14:46:20 +0100204 return "8";
205 case DataType::U16:
206 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +0100207 case DataType::QSYMM16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100208 case DataType::QASYMM16:
SiCong Lic51b72f2017-07-28 14:46:20 +0100209 case DataType::F16:
210 return "16";
211 case DataType::U32:
212 case DataType::S32:
213 case DataType::F32:
214 return "32";
215 case DataType::U64:
216 case DataType::S64:
217 return "64";
218 default:
219 ARM_COMPUTE_ERROR("Unsupported input data type.");
220 return "0";
221 }
222}
223
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100224GPUTarget get_target_from_device(const cl::Device &device)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 // Query device name size
Anthony Barbier30a63422018-02-28 18:18:24 +0000227 std::string device_name = device.getInfo<CL_DEVICE_NAME>();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228
Michalis Spyroua9676112018-02-22 18:07:43 +0000229 return get_target_from_name(device_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230}
231
Anthony Barbierd727e852018-04-20 11:05:29 +0100232bool arm_non_uniform_workgroup_supported(const cl::Device &device)
steniu0134702472017-07-11 09:22:58 +0100233{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100234 return device_supports_extension(device, "cl_arm_non_uniform_work_group_size");
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100235}
steniu0134702472017-07-11 09:22:58 +0100236
Anthony Barbierd727e852018-04-20 11:05:29 +0100237bool fp16_supported(const cl::Device &device)
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100238{
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100239 return device_supports_extension(device, "cl_khr_fp16");
steniu0134702472017-07-11 09:22:58 +0100240}
241
Michalis Spyroue03342e2018-01-15 14:39:13 +0000242bool dot8_supported(const cl::Device &device)
243{
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100244 std::string device_name = device.getInfo<CL_DEVICE_NAME>();
245 const GPUTarget gpu_target = get_target_from_name(device_name);
246
247 // SW_WORKAROUND: Workaround for DDK revision r14p0.to enable cl_arm_integer_dot_product_int8
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100248 std::set<GPUTarget> sw_workaround_issue = {GPUTarget::G76};
249 return (device_supports_extension(device, "cl_arm_integer_dot_product_int8") ||
250 sw_workaround_issue.count(gpu_target) != 0);
Michalis Spyroue03342e2018-01-15 14:39:13 +0000251}
252
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100253bool dot8_acc_supported(const cl::Device &device)
254{
255 return device_supports_extension(device, "cl_arm_integer_dot_product_accumulate_int8");
256}
257
steniu0134702472017-07-11 09:22:58 +0100258CLVersion get_cl_version(const cl::Device &device)
259{
Anthony Barbier30a63422018-02-28 18:18:24 +0000260 std::string version_str = device.getInfo<CL_DEVICE_VERSION>();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100261 if (version_str.find("OpenCL 3") != std::string::npos)
Viet-Hoa Dof8bb0922022-05-30 15:15:15 +0100262 {
263 return CLVersion::CL30;
264 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265 else if (version_str.find("OpenCL 2") != std::string::npos)
steniu0134702472017-07-11 09:22:58 +0100266 {
267 return CLVersion::CL20;
268 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100269 else if (version_str.find("OpenCL 1.2") != std::string::npos)
steniu0134702472017-07-11 09:22:58 +0100270 {
271 return CLVersion::CL12;
272 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100273 else if (version_str.find("OpenCL 1.1") != std::string::npos)
steniu0134702472017-07-11 09:22:58 +0100274 {
275 return CLVersion::CL11;
276 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100277 else if (version_str.find("OpenCL 1.0") != std::string::npos)
steniu0134702472017-07-11 09:22:58 +0100278 {
279 return CLVersion::CL10;
280 }
281
282 return CLVersion::UNKNOWN;
283}
284
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100285bool device_supports_extension(const cl::Device &device, const char *extension_name)
286{
287 std::string extensions = device.getInfo<CL_DEVICE_EXTENSIONS>();
288 auto pos = extensions.find(extension_name);
289 return (pos != std::string::npos);
290}
291
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292bool cl_winograd_convolution_layer_supported(const Size2D &output_tile,
293 const Size2D &kernel_size,
294 DataLayout data_layout)
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100295{
296 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
297
298 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
299
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100300 std::vector<WinogradConfiguration> winograd_configs_nchw = {
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100301 WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 3)),
302 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)),
303 WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(3, 1)),
304 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)),
305 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
306 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
Gian Marco Iodice876be2a2018-07-03 12:22:09 +0100307 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
308 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1)),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100309 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5))};
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100310
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100311 std::vector<WinogradConfiguration> winograd_configs_nhwc = {
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100312 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(3, 3)),
Giorgio Arena149fdf32018-07-04 17:03:33 +0100313 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 3)),
314 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(3, 1)),
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100315 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)),
Gian Marco Iodiced28b7512018-07-06 12:59:28 +0100316 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
317 WinogradConfiguration(std::pair<int, int>(4, 1), std::pair<int, int>(5, 1)),
Michele Di Giorgiof955d512019-02-27 14:26:51 +0000318 WinogradConfiguration(std::pair<int, int>(1, 4), std::pair<int, int>(1, 5)),
319 WinogradConfiguration(std::pair<int, int>(1, 2), std::pair<int, int>(1, 7)),
320 WinogradConfiguration(std::pair<int, int>(2, 1), std::pair<int, int>(7, 1)),
321 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7)),
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100322 };
323
324 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
325 std::pair<int, int>(kernel_size.width, kernel_size.height));
326
327 // Return true if supported
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100328 if (data_layout == DataLayout::NCHW)
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100329 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100330 return (std::find(winograd_configs_nchw.begin(), winograd_configs_nchw.end(), p) !=
331 winograd_configs_nchw.end());
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100332 }
333 else
334 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100335 return (std::find(winograd_configs_nhwc.begin(), winograd_configs_nhwc.end(), p) !=
336 winograd_configs_nhwc.end());
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100337 }
338}
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000339
340size_t preferred_vector_width(const cl::Device &device, const DataType dt)
341{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100342 switch (dt)
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000343 {
344 case DataType::U8:
345 case DataType::S8:
346 case DataType::QASYMM8:
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000347 case DataType::QASYMM8_SIGNED:
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100348 case DataType::QSYMM8:
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100349 case DataType::QSYMM8_PER_CHANNEL:
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000350 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR>();
351 case DataType::U16:
352 case DataType::S16:
Michele Di Giorgio6997fc92019-06-18 10:23:22 +0100353 case DataType::QSYMM16:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100354 case DataType::QASYMM16:
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000355 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT>();
356 case DataType::U32:
357 case DataType::S32:
358 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT>();
359 case DataType::F16:
360 case DataType::F32:
361 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>();
362 case DataType::U64:
363 case DataType::S64:
364 return device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG>();
365 default:
366 return 1;
367 }
368}
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000369
370bool preferred_dummy_work_items_support(const cl::Device &device)
371{
372 ARM_COMPUTE_UNUSED(device);
373 // TODO (COMPMID-2044)
374 return true;
375}
Pablo Tellodb8485a2019-09-24 11:03:47 +0100376
Gian Marco Iodicea98dee22020-06-02 12:12:35 +0100377bool image2d_from_buffer_supported(const cl::Device &device)
378{
379 return device_supports_extension(device, "cl_khr_image2d_from_buffer");
380}
381
382size_t get_cl_image_pitch_alignment(const cl::Device &device)
383{
384 cl_uint pixel_aligment = 0;
385
386 cl_int err = clGetDeviceInfo(device(), CL_DEVICE_IMAGE_PITCH_ALIGNMENT, sizeof(cl_uint), &pixel_aligment, nullptr);
387
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100388 if (err == CL_SUCCESS)
Gian Marco Iodicea98dee22020-06-02 12:12:35 +0100389 {
390 return pixel_aligment;
391 }
392 else
393 {
394 return 0;
395 }
396}
397
Viet-Hoa Dof8bb0922022-05-30 15:15:15 +0100398bool get_cl_non_uniform_work_group_supported(const cl::Device &device)
399{
400 cl_bool supported = CL_FALSE;
401
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100402 cl_int err =
403 clGetDeviceInfo(device(), CL_DEVICE_NON_UNIFORM_WORK_GROUP_SUPPORT, sizeof(cl_bool), &supported, nullptr);
Viet-Hoa Dof8bb0922022-05-30 15:15:15 +0100404
405 return (err == CL_SUCCESS && supported == CL_TRUE);
406}
407
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100408cl::Kernel
409create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set<std::string> &build_opts)
Michalis Spyrou11d49182020-03-26 10:31:32 +0000410{
Georgios Pinitas908f6162021-05-04 10:11:09 +0100411 opencl::ClKernelLibrary &klib = opencl::ClKernelLibrary::get();
412
413 const std::string program_name = klib.program_name(kernel_name);
414 auto kernel_src = klib.program(program_name);
415 const std::string kernel_path = klib.kernel_path();
416
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100417 return static_cast<cl::Kernel>(ctx.create_kernel(kernel_name, program_name, kernel_src.program, kernel_path,
418 build_opts, kernel_src.is_binary));
Michalis Spyrou11d49182020-03-26 10:31:32 +0000419}
420
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100421cl::NDRange create_lws_hint_parallel_implementations(unsigned int input_dimension, unsigned int vector_size)
422{
423 const unsigned int width_leftover = input_dimension % vector_size;
424 const unsigned int border_width = (width_leftover != 0) ? vector_size - width_leftover : 0;
425 const unsigned int num_of_threads = ((input_dimension + border_width) / 16);
426 return cl::NDRange(std::min(8U, num_of_threads));
427}
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000428
429bool get_wbsm_support_info(const cl::Device &device)
430{
431 cl_bitfield capabilities = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100432 cl_int err = clGetDeviceInfo(device.get(), CL_DEVICE_SCHEDULING_CONTROLS_CAPABILITIES_ARM, sizeof(cl_bitfield),
433 &capabilities, nullptr);
434 if ((err == CL_SUCCESS) && (capabilities & CL_KERNEL_EXEC_INFO_WORKGROUP_BATCH_SIZE_MODIFIER_ARM))
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000435 {
436 return true;
437 }
438 return false;
439}
440
441void set_wbsm(cl::Kernel &kernel, cl_int wbsm_hint)
442{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100443 cl_int err = clSetKernelExecInfo(kernel.get(), CL_KERNEL_EXEC_INFO_WORKGROUP_BATCH_SIZE_MODIFIER_ARM,
444 sizeof(cl_int), &wbsm_hint);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000445 ARM_COMPUTE_UNUSED(err);
446 ARM_COMPUTE_ERROR_ON(err != CL_SUCCESS);
447}
448
Gian Marco Iodicead9a7ed2022-09-16 14:14:21 +0100449bool export_to_cl_image(const ITensorInfo *tensor)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100450{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100451 if (tensor->tensor_shape()[0] % 4 != 0)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100452 {
453 return false;
454 }
455
456 // If not floating point
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100457 if (!is_data_type_float(tensor->data_type()))
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100458 {
459 return false;
460 }
461
462 // Check if the cl_khr_image2d_from_buffer extension is supported on the target platform
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100463 if (!image2d_from_buffer_supported(CLKernelLibrary::get().get_device()))
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100464 {
465 return false;
466 }
467
468 // Check cl image pitch alignment
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100469 if (get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()) == 0)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100470 {
471 return false;
472 }
473
474 const size_t image_w = tensor->tensor_shape()[0] / 4;
Gunes Bayirbbeef722023-03-20 10:19:10 +0000475 const size_t image_h = tensor->tensor_shape().total_size() / tensor->tensor_shape()[0];
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100476 const size_t max_image_w = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>();
477 const size_t max_image_h = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>();
478
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100479 if (image_w > max_image_w || image_h > max_image_h)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100480 {
481 return false;
482 }
483
484 return true;
485}
486
Gian Marco Iodice5e281812021-07-06 13:19:41 +0100487void set_unroll_with_pragma(CLBuildOptions &built_opts, std::initializer_list<int> values)
488{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100489 for (const int value : values)
Gian Marco Iodice5e281812021-07-06 13:19:41 +0100490 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100491 if (value > max_manual_loop_unrolling)
Gian Marco Iodice5e281812021-07-06 13:19:41 +0100492 {
493 built_opts.add_option("-DUNROLL_WITH_PRAGMA");
494 return;
495 }
496 }
497}
498
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000499bool arm_matrix_multiply_supported(const cl::Device &device)
500{
501 return device_supports_extension(device, "cl_arm_matrix_multiply");
502}
Viet-Hoa Do48b6d172023-08-11 17:48:54 +0100503
504bool command_buffer_supported(const cl::Device &device)
505{
506 return device_supports_extension(device, "cl_khr_command_buffer");
507}
508
509bool command_buffer_mutable_dispatch_supported(const cl::Device &device)
510{
511 return device_supports_extension(device, "cl_khr_command_buffer_mutable_dispatch");
512}
513
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100514} // namespace arm_compute