blob: 0e9aa5d6e5950505f02e2f3b48d41369cbc5073d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottinibe9f9f92021-01-25 15:07:17 +00002 * Copyright (c) 2016-2021 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLHELPERS_H
25#define ARM_COMPUTE_CLHELPERS_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +010027#include "arm_compute/core/CL/CLTypes.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/OpenCL.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
Michalis Spyrou11d49182020-03-26 10:31:32 +000030#include <set>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include <string>
32
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000033/* CL Device capabilities */
34#define ARM_COMPUTE_LIBRARY_OPENCL_DEVICE_CAPABILITIES_ARM 0x41E4
35/* Workgroup Batch Size Modifier */
36#define ARM_COMPUTE_LIBRARY_OPENCL_EXEC_WBSM_ARM 0x41E6
37
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038namespace arm_compute
39{
Pablo Tellodb8485a2019-09-24 11:03:47 +010040class CLCoreRuntimeContext;
Michalis Spyrou11d49182020-03-26 10:31:32 +000041class CLCompileContext;
Pablo Tellodb8485a2019-09-24 11:03:47 +010042class CLBuildOptions;
43
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044enum class DataType;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045
46/** Max vector width of an OpenCL vector */
Anthony Barbier7068f992017-10-26 15:23:08 +010047static constexpr unsigned int max_cl_vector_width = 16;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048
49/** Translates a tensor data type to the appropriate OpenCL type.
50 *
51 * @param[in] dt @ref DataType to be translated to OpenCL type.
52 *
53 * @return The string specifying the OpenCL type to be used.
54 */
55std::string get_cl_type_from_data_type(const DataType &dt);
56
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010057/** Translates a tensor data type to the appropriate OpenCL promoted type.
58 *
59 * @param[in] dt @ref DataType to be used to get the promoted OpenCL type.
60 *
61 * @return The string specifying the OpenCL type to be used.
62 */
63std::string get_cl_promoted_type_from_data_type(const DataType &dt);
64
65/** Translates the element size to an unsigned integer data type
66 *
67 * @param[in] element_size Size in bytes of an element.
68 *
69 * @return The string specifying the OpenCL type to be used.
70 */
71std::string get_cl_unsigned_type_from_element_size(size_t element_size);
72
Michalis Spyrou7317e392020-01-17 11:27:49 +000073/** Translates the element size to an signed integer data type
74 *
75 * @param[in] element_size Size in bytes of an element.
76 *
77 * @return The string specifying the OpenCL type to be used.
78 */
79std::string get_cl_signed_type_from_element_size(size_t element_size);
80
Giorgio Arena73023022018-09-04 14:55:55 +010081/** Translates a tensor data type to the appropriate OpenCL select type.
82 *
83 * @param[in] dt @ref DataType to be translated to OpenCL select type.
84 *
85 * @return The string specifying the OpenCL select type to be used.
86 */
87std::string get_cl_select_type_from_data_type(const DataType &dt);
88
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000089/** Translates a tensor data type to the appropriate OpenCL dot8 accumulator type.
90 *
91 * @param[in] dt @ref DataType to be translated to OpenCL dot8 accumulator type.
92 *
93 * @return The string specifying the OpenCL dot8 accumulator type to be used.
94 */
95std::string get_cl_dot8_acc_type_from_data_type(const DataType &dt);
96
SiCong Lic51b72f2017-07-28 14:46:20 +010097/** Get the size of a data type in number of bits.
98 *
99 * @param[in] dt @ref DataType.
100 *
101 * @return Number of bits in the data type specified.
102 */
103std::string get_data_size_from_data_type(const DataType &dt);
104
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105/** Helper function to get the GPU target from CL device
106 *
107 * @param[in] device A CL device
108 *
109 * @return the GPU target
110 */
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100111GPUTarget get_target_from_device(const cl::Device &device);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
steniu0134702472017-07-11 09:22:58 +0100113/** Helper function to get the highest OpenCL version supported
114 *
115 * @param[in] device A CL device
116 *
117 * @return the highest OpenCL version supported
118 */
119CLVersion get_cl_version(const cl::Device &device);
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100120
Gian Marco Iodicea98dee22020-06-02 12:12:35 +0100121/** Helper function to get the cl_image pitch alignment in pixels
122 *
123 * @param[in] device A CL device
124 *
125 * @return the cl_image pitch alignment in pixels. If an error occurs, the function will return 0
126 */
127size_t get_cl_image_pitch_alignment(const cl::Device &device);
128
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100129/** Helper function to check whether a given extension is supported
130 *
131 * @param[in] device A CL device
132 * @param[in] extension_name Name of the extension to be checked
133 *
134 * @return True if the extension is supported
135 */
136bool device_supports_extension(const cl::Device &device, const char *extension_name);
137
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100138/** Helper function to check whether the cl_khr_fp16 extension is supported
139 *
140 * @param[in] device A CL device
141 *
142 * @return True if the extension is supported
143 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100144bool fp16_supported(const cl::Device &device);
steniu0134702472017-07-11 09:22:58 +0100145/** Helper function to check whether the arm_non_uniform_work_group_size extension is supported
146 *
147 * @param[in] device A CL device
148 *
149 * @return True if the extension is supported
150 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100151bool arm_non_uniform_workgroup_supported(const cl::Device &device);
Michalis Spyroue03342e2018-01-15 14:39:13 +0000152/** Helper function to check whether the cl_arm_integer_dot_product_int8 extension is supported
153 *
154 * @param[in] device A CL device
155 *
156 * @return True if the extension is supported
157 */
158bool dot8_supported(const cl::Device &device);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100159
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100160/** Helper function to check whether the cl_arm_integer_dot_product_accumulate_int8 extension is supported
161 *
162 * @param[in] device A CL device
163 *
164 * @return True if the extension is supported
165 */
166bool dot8_acc_supported(const cl::Device &device);
167
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100168/** This function checks if the Winograd configuration (defined through the output tile, kernel size and the data layout) is supported on OpenCL
169 *
170 * @param[in] output_tile Output tile for the Winograd filtering algorithm
171 * @param[in] kernel_size Kernel size for the Winograd filtering algorithm
172 * @param[in] data_layout Data layout of the input tensor
173 *
174 * @return True if the configuration is supported
175 */
176bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout);
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000177
178/** Helper function to get the preferred native vector width size for built-in scalar types that can be put into vectors
179 *
180 * @param[in] device A CL device
181 * @param[in] dt data type
182 *
183 * @return preferred vector width
184 */
185size_t preferred_vector_width(const cl::Device &device, DataType dt);
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000186
187/** Helper function to check if "dummy work-items" are preferred to have a power of two NDRange
188 * In case dummy work-items is enabled, it is OpenCL kernel responsibility to check if the work-item is out-of range or not
189 *
190 * @param[in] device A CL device
191 *
192 * @return True if dummy work-items should be preferred to dispatch the NDRange
193 */
194bool preferred_dummy_work_items_support(const cl::Device &device);
Pablo Tellodb8485a2019-09-24 11:03:47 +0100195
Gian Marco Iodicea98dee22020-06-02 12:12:35 +0100196/** Helper function to check whether the cl_khr_image2d_from_buffer extension is supported
197 *
198 * @param[in] device A CL device
199 *
200 * @return True if the extension is supported
201 */
202bool image2d_from_buffer_supported(const cl::Device &device);
203
Pablo Tellodb8485a2019-09-24 11:03:47 +0100204/** Creates an opencl kernel
205 *
206 * @param[in] ctx A context to be used to create the opencl kernel.
207 * @param[in] kernel_name The kernel name.
208 * @param[in] build_opts The build options to be used for the opencl kernel compilation.
209 *
210 * @return An opencl kernel
211 */
212cl::Kernel create_opencl_kernel(CLCoreRuntimeContext *ctx, const std::string &kernel_name, const CLBuildOptions &build_opts);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100213
Michalis Spyrou11d49182020-03-26 10:31:32 +0000214/** Creates an opencl kernel using a compile context
215 *
216 * @param[in] ctx A compile context to be used to create the opencl kernel.
217 * @param[in] kernel_name The kernel name.
218 * @param[in] build_opts The build options to be used for the opencl kernel compilation.
219 *
220 * @return An opencl kernel
221 */
Manuel Bottini256c0b92020-04-21 13:29:30 +0100222cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set<std::string> &build_opts = std::set<std::string>());
Michalis Spyrou11d49182020-03-26 10:31:32 +0000223
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100224/** Creates a suitable LWS hint object for parallel implementations. Sets the number of WG based on the input size.
225 * If input width is smaller than 128 we can use fewer threads than 8.
226 *
227 * @param[in] input_dimension number of elements along the dimension to apply the parallellization
228 * @param[in] vector_size size of the vector in OpenCL
229 *
230 * @return An LWS hint object
231 */
232cl::NDRange create_lws_hint_parallel_implementations(unsigned int input_dimension, unsigned int vector_size);
233
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000234/* Helper function to check if the workgroup batch size modifier parameter is supported on the cl device
235 *
236 * @param[in] device cl device to check for support
237 *
238 * @return true if the workgroup batch size modifier parameter is supported, false otherwise
239 */
240bool get_wbsm_support_info(const cl::Device &device);
241
242/* Helper function to set the workgroup batch size modifier parameter in the kernel
243 *
244 * @param[in] kernel cl kernel to set the workgroup batch size modifier parameter
245 * @param[in] wbsm_hint workgroup batch size modifier to use
246 */
247void set_wbsm(cl::Kernel &kernel, cl_int wbsm_hint);
248
Georgios Pinitas7ae80a92019-10-25 18:25:17 +0100249} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000250#endif /* ARM_COMPUTE_CLHELPERS_H */