blob: fc3f4d5db06d41baa37d878ea9790ce8eb054c77 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou7317e392020-01-17 11:27:49 +00002 * Copyright (c) 2016-2020 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
33namespace arm_compute
34{
Pablo Tellodb8485a2019-09-24 11:03:47 +010035class CLCoreRuntimeContext;
Michalis Spyrou11d49182020-03-26 10:31:32 +000036class CLCompileContext;
Pablo Tellodb8485a2019-09-24 11:03:47 +010037class CLBuildOptions;
38
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039enum class DataType;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
41/** Max vector width of an OpenCL vector */
Anthony Barbier7068f992017-10-26 15:23:08 +010042static constexpr unsigned int max_cl_vector_width = 16;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043
44/** Translates a tensor data type to the appropriate OpenCL type.
45 *
46 * @param[in] dt @ref DataType to be translated to OpenCL type.
47 *
48 * @return The string specifying the OpenCL type to be used.
49 */
50std::string get_cl_type_from_data_type(const DataType &dt);
51
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010052/** Translates a tensor data type to the appropriate OpenCL promoted type.
53 *
54 * @param[in] dt @ref DataType to be used to get the promoted OpenCL type.
55 *
56 * @return The string specifying the OpenCL type to be used.
57 */
58std::string get_cl_promoted_type_from_data_type(const DataType &dt);
59
60/** Translates the element size to an unsigned integer data type
61 *
62 * @param[in] element_size Size in bytes of an element.
63 *
64 * @return The string specifying the OpenCL type to be used.
65 */
66std::string get_cl_unsigned_type_from_element_size(size_t element_size);
67
Michalis Spyrou7317e392020-01-17 11:27:49 +000068/** Translates the element size to an signed integer data type
69 *
70 * @param[in] element_size Size in bytes of an element.
71 *
72 * @return The string specifying the OpenCL type to be used.
73 */
74std::string get_cl_signed_type_from_element_size(size_t element_size);
75
Giorgio Arena73023022018-09-04 14:55:55 +010076/** Translates a tensor data type to the appropriate OpenCL select type.
77 *
78 * @param[in] dt @ref DataType to be translated to OpenCL select type.
79 *
80 * @return The string specifying the OpenCL select type to be used.
81 */
82std::string get_cl_select_type_from_data_type(const DataType &dt);
83
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000084/** Translates a tensor data type to the appropriate OpenCL dot8 accumulator type.
85 *
86 * @param[in] dt @ref DataType to be translated to OpenCL dot8 accumulator type.
87 *
88 * @return The string specifying the OpenCL dot8 accumulator type to be used.
89 */
90std::string get_cl_dot8_acc_type_from_data_type(const DataType &dt);
91
SiCong Lic51b72f2017-07-28 14:46:20 +010092/** Get the size of a data type in number of bits.
93 *
94 * @param[in] dt @ref DataType.
95 *
96 * @return Number of bits in the data type specified.
97 */
98std::string get_data_size_from_data_type(const DataType &dt);
99
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100100/** Translates fixed point tensor data type to the underlying OpenCL type.
101 *
102 * @param[in] dt @ref DataType to be translated to OpenCL type.
103 *
104 * @return The string specifying the underlying OpenCL type to be used.
105 */
106std::string get_underlying_cl_type_from_data_type(const DataType &dt);
107
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108/** Helper function to get the GPU target from CL device
109 *
110 * @param[in] device A CL device
111 *
112 * @return the GPU target
113 */
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100114GPUTarget get_target_from_device(const cl::Device &device);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
steniu0134702472017-07-11 09:22:58 +0100116/** Helper function to get the highest OpenCL version supported
117 *
118 * @param[in] device A CL device
119 *
120 * @return the highest OpenCL version supported
121 */
122CLVersion get_cl_version(const cl::Device &device);
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100123
Gian Marco Iodicea98dee22020-06-02 12:12:35 +0100124/** Helper function to get the cl_image pitch alignment in pixels
125 *
126 * @param[in] device A CL device
127 *
128 * @return the cl_image pitch alignment in pixels. If an error occurs, the function will return 0
129 */
130size_t get_cl_image_pitch_alignment(const cl::Device &device);
131
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +0100132/** Helper function to check whether a given extension is supported
133 *
134 * @param[in] device A CL device
135 * @param[in] extension_name Name of the extension to be checked
136 *
137 * @return True if the extension is supported
138 */
139bool device_supports_extension(const cl::Device &device, const char *extension_name);
140
Matthew Bentham6f31f8c2017-10-27 11:50:06 +0100141/** Helper function to check whether the cl_khr_fp16 extension is supported
142 *
143 * @param[in] device A CL device
144 *
145 * @return True if the extension is supported
146 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100147bool fp16_supported(const cl::Device &device);
steniu0134702472017-07-11 09:22:58 +0100148/** Helper function to check whether the arm_non_uniform_work_group_size extension is supported
149 *
150 * @param[in] device A CL device
151 *
152 * @return True if the extension is supported
153 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100154bool arm_non_uniform_workgroup_supported(const cl::Device &device);
Michalis Spyroue03342e2018-01-15 14:39:13 +0000155/** Helper function to check whether the cl_arm_integer_dot_product_int8 extension is supported
156 *
157 * @param[in] device A CL device
158 *
159 * @return True if the extension is supported
160 */
161bool dot8_supported(const cl::Device &device);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100162
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100163/** Helper function to check whether the cl_arm_integer_dot_product_accumulate_int8 extension is supported
164 *
165 * @param[in] device A CL device
166 *
167 * @return True if the extension is supported
168 */
169bool dot8_acc_supported(const cl::Device &device);
170
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100171/** This function checks if the Winograd configuration (defined through the output tile, kernel size and the data layout) is supported on OpenCL
172 *
173 * @param[in] output_tile Output tile for the Winograd filtering algorithm
174 * @param[in] kernel_size Kernel size for the Winograd filtering algorithm
175 * @param[in] data_layout Data layout of the input tensor
176 *
177 * @return True if the configuration is supported
178 */
179bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout);
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000180
181/** Helper function to get the preferred native vector width size for built-in scalar types that can be put into vectors
182 *
183 * @param[in] device A CL device
184 * @param[in] dt data type
185 *
186 * @return preferred vector width
187 */
188size_t preferred_vector_width(const cl::Device &device, DataType dt);
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000189
190/** Helper function to check if "dummy work-items" are preferred to have a power of two NDRange
191 * In case dummy work-items is enabled, it is OpenCL kernel responsibility to check if the work-item is out-of range or not
192 *
193 * @param[in] device A CL device
194 *
195 * @return True if dummy work-items should be preferred to dispatch the NDRange
196 */
197bool preferred_dummy_work_items_support(const cl::Device &device);
Pablo Tellodb8485a2019-09-24 11:03:47 +0100198
Gian Marco Iodicea98dee22020-06-02 12:12:35 +0100199/** Helper function to check whether the cl_khr_image2d_from_buffer extension is supported
200 *
201 * @param[in] device A CL device
202 *
203 * @return True if the extension is supported
204 */
205bool image2d_from_buffer_supported(const cl::Device &device);
206
Pablo Tellodb8485a2019-09-24 11:03:47 +0100207/** Creates an opencl kernel
208 *
209 * @param[in] ctx A context to be used to create the opencl kernel.
210 * @param[in] kernel_name The kernel name.
211 * @param[in] build_opts The build options to be used for the opencl kernel compilation.
212 *
213 * @return An opencl kernel
214 */
215cl::Kernel create_opencl_kernel(CLCoreRuntimeContext *ctx, const std::string &kernel_name, const CLBuildOptions &build_opts);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100216
Michalis Spyrou11d49182020-03-26 10:31:32 +0000217/** Creates an opencl kernel using a compile context
218 *
219 * @param[in] ctx A compile context to be used to create the opencl kernel.
220 * @param[in] kernel_name The kernel name.
221 * @param[in] build_opts The build options to be used for the opencl kernel compilation.
222 *
223 * @return An opencl kernel
224 */
Manuel Bottini256c0b92020-04-21 13:29:30 +0100225cl::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 +0000226
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100227/** Creates a suitable LWS hint object for parallel implementations. Sets the number of WG based on the input size.
228 * If input width is smaller than 128 we can use fewer threads than 8.
229 *
230 * @param[in] input_dimension number of elements along the dimension to apply the parallellization
231 * @param[in] vector_size size of the vector in OpenCL
232 *
233 * @return An LWS hint object
234 */
235cl::NDRange create_lws_hint_parallel_implementations(unsigned int input_dimension, unsigned int vector_size);
236
Georgios Pinitas7ae80a92019-10-25 18:25:17 +0100237} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000238#endif /* ARM_COMPUTE_CLHELPERS_H */