Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLHELPERS_H |
| 25 | #define ARM_COMPUTE_CLHELPERS_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Michele Di Giorgio | b8fc60f | 2018-04-25 11:58:07 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLTypes.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/OpenCL.h" |
Georgios Pinitas | c6f9510 | 2021-03-30 10:03:01 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 31 | #include <set> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include <string> |
| 33 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 34 | /* CL Device capabilities */ |
| 35 | #define ARM_COMPUTE_LIBRARY_OPENCL_DEVICE_CAPABILITIES_ARM 0x41E4 |
| 36 | /* Workgroup Batch Size Modifier */ |
| 37 | #define ARM_COMPUTE_LIBRARY_OPENCL_EXEC_WBSM_ARM 0x41E6 |
| 38 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 41 | class CLCoreRuntimeContext; |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 42 | class CLCompileContext; |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 43 | class CLBuildOptions; |
| 44 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | enum class DataType; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | |
| 47 | /** Max vector width of an OpenCL vector */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 48 | static constexpr unsigned int max_cl_vector_width = 16; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | |
| 50 | /** Translates a tensor data type to the appropriate OpenCL type. |
| 51 | * |
| 52 | * @param[in] dt @ref DataType to be translated to OpenCL type. |
| 53 | * |
| 54 | * @return The string specifying the OpenCL type to be used. |
| 55 | */ |
| 56 | std::string get_cl_type_from_data_type(const DataType &dt); |
| 57 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 58 | /** Translates a tensor data type to the appropriate OpenCL promoted type. |
| 59 | * |
| 60 | * @param[in] dt @ref DataType to be used to get the promoted OpenCL type. |
| 61 | * |
| 62 | * @return The string specifying the OpenCL type to be used. |
| 63 | */ |
| 64 | std::string get_cl_promoted_type_from_data_type(const DataType &dt); |
| 65 | |
| 66 | /** Translates the element size to an unsigned integer data type |
| 67 | * |
| 68 | * @param[in] element_size Size in bytes of an element. |
| 69 | * |
| 70 | * @return The string specifying the OpenCL type to be used. |
| 71 | */ |
| 72 | std::string get_cl_unsigned_type_from_element_size(size_t element_size); |
| 73 | |
Michalis Spyrou | 7317e39 | 2020-01-17 11:27:49 +0000 | [diff] [blame] | 74 | /** Translates the element size to an signed integer data type |
| 75 | * |
| 76 | * @param[in] element_size Size in bytes of an element. |
| 77 | * |
| 78 | * @return The string specifying the OpenCL type to be used. |
| 79 | */ |
| 80 | std::string get_cl_signed_type_from_element_size(size_t element_size); |
| 81 | |
Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 82 | /** Translates a tensor data type to the appropriate OpenCL select type. |
| 83 | * |
| 84 | * @param[in] dt @ref DataType to be translated to OpenCL select type. |
| 85 | * |
| 86 | * @return The string specifying the OpenCL select type to be used. |
| 87 | */ |
| 88 | std::string get_cl_select_type_from_data_type(const DataType &dt); |
| 89 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 90 | /** Translates a tensor data type to the appropriate OpenCL dot8 accumulator type. |
| 91 | * |
| 92 | * @param[in] dt @ref DataType to be translated to OpenCL dot8 accumulator type. |
| 93 | * |
| 94 | * @return The string specifying the OpenCL dot8 accumulator type to be used. |
| 95 | */ |
| 96 | std::string get_cl_dot8_acc_type_from_data_type(const DataType &dt); |
| 97 | |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 98 | /** Get the size of a data type in number of bits. |
| 99 | * |
| 100 | * @param[in] dt @ref DataType. |
| 101 | * |
| 102 | * @return Number of bits in the data type specified. |
| 103 | */ |
| 104 | std::string get_data_size_from_data_type(const DataType &dt); |
| 105 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | /** Helper function to get the GPU target from CL device |
| 107 | * |
| 108 | * @param[in] device A CL device |
| 109 | * |
| 110 | * @return the GPU target |
| 111 | */ |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 112 | GPUTarget get_target_from_device(const cl::Device &device); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 114 | /** Helper function to get the highest OpenCL version supported |
| 115 | * |
| 116 | * @param[in] device A CL device |
| 117 | * |
| 118 | * @return the highest OpenCL version supported |
| 119 | */ |
| 120 | CLVersion get_cl_version(const cl::Device &device); |
Vidhya Sudhan Loganathan | eb8a399 | 2018-04-10 12:23:22 +0100 | [diff] [blame] | 121 | |
Gian Marco Iodice | a98dee2 | 2020-06-02 12:12:35 +0100 | [diff] [blame] | 122 | /** Helper function to get the cl_image pitch alignment in pixels |
| 123 | * |
| 124 | * @param[in] device A CL device |
| 125 | * |
| 126 | * @return the cl_image pitch alignment in pixels. If an error occurs, the function will return 0 |
| 127 | */ |
| 128 | size_t get_cl_image_pitch_alignment(const cl::Device &device); |
| 129 | |
Vidhya Sudhan Loganathan | eb8a399 | 2018-04-10 12:23:22 +0100 | [diff] [blame] | 130 | /** Helper function to check whether a given extension is supported |
| 131 | * |
| 132 | * @param[in] device A CL device |
| 133 | * @param[in] extension_name Name of the extension to be checked |
| 134 | * |
| 135 | * @return True if the extension is supported |
| 136 | */ |
| 137 | bool device_supports_extension(const cl::Device &device, const char *extension_name); |
| 138 | |
Matthew Bentham | 6f31f8c | 2017-10-27 11:50:06 +0100 | [diff] [blame] | 139 | /** Helper function to check whether the cl_khr_fp16 extension is supported |
| 140 | * |
| 141 | * @param[in] device A CL device |
| 142 | * |
| 143 | * @return True if the extension is supported |
| 144 | */ |
Anthony Barbier | d727e85 | 2018-04-20 11:05:29 +0100 | [diff] [blame] | 145 | bool fp16_supported(const cl::Device &device); |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 146 | /** Helper function to check whether the arm_non_uniform_work_group_size extension is supported |
| 147 | * |
| 148 | * @param[in] device A CL device |
| 149 | * |
| 150 | * @return True if the extension is supported |
| 151 | */ |
Anthony Barbier | d727e85 | 2018-04-20 11:05:29 +0100 | [diff] [blame] | 152 | bool arm_non_uniform_workgroup_supported(const cl::Device &device); |
Michalis Spyrou | e03342e | 2018-01-15 14:39:13 +0000 | [diff] [blame] | 153 | /** Helper function to check whether the cl_arm_integer_dot_product_int8 extension is supported |
| 154 | * |
| 155 | * @param[in] device A CL device |
| 156 | * |
| 157 | * @return True if the extension is supported |
| 158 | */ |
| 159 | bool dot8_supported(const cl::Device &device); |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 160 | |
Giorgio Arena | eff8d95 | 2018-07-02 15:29:57 +0100 | [diff] [blame] | 161 | /** Helper function to check whether the cl_arm_integer_dot_product_accumulate_int8 extension is supported |
| 162 | * |
| 163 | * @param[in] device A CL device |
| 164 | * |
| 165 | * @return True if the extension is supported |
| 166 | */ |
| 167 | bool dot8_acc_supported(const cl::Device &device); |
| 168 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 169 | /** This function checks if the Winograd configuration (defined through the output tile, kernel size and the data layout) is supported on OpenCL |
| 170 | * |
| 171 | * @param[in] output_tile Output tile for the Winograd filtering algorithm |
| 172 | * @param[in] kernel_size Kernel size for the Winograd filtering algorithm |
| 173 | * @param[in] data_layout Data layout of the input tensor |
| 174 | * |
| 175 | * @return True if the configuration is supported |
| 176 | */ |
| 177 | bool cl_winograd_convolution_layer_supported(const Size2D &output_tile, const Size2D &kernel_size, DataLayout data_layout); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 178 | |
| 179 | /** Helper function to get the preferred native vector width size for built-in scalar types that can be put into vectors |
| 180 | * |
| 181 | * @param[in] device A CL device |
| 182 | * @param[in] dt data type |
| 183 | * |
| 184 | * @return preferred vector width |
| 185 | */ |
| 186 | size_t preferred_vector_width(const cl::Device &device, DataType dt); |
Gian Marco Iodice | b0c5037 | 2019-03-15 10:13:05 +0000 | [diff] [blame] | 187 | |
| 188 | /** Helper function to check if "dummy work-items" are preferred to have a power of two NDRange |
| 189 | * In case dummy work-items is enabled, it is OpenCL kernel responsibility to check if the work-item is out-of range or not |
| 190 | * |
| 191 | * @param[in] device A CL device |
| 192 | * |
| 193 | * @return True if dummy work-items should be preferred to dispatch the NDRange |
| 194 | */ |
| 195 | bool preferred_dummy_work_items_support(const cl::Device &device); |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 196 | |
Gian Marco Iodice | a98dee2 | 2020-06-02 12:12:35 +0100 | [diff] [blame] | 197 | /** Helper function to check whether the cl_khr_image2d_from_buffer extension is supported |
| 198 | * |
| 199 | * @param[in] device A CL device |
| 200 | * |
| 201 | * @return True if the extension is supported |
| 202 | */ |
| 203 | bool image2d_from_buffer_supported(const cl::Device &device); |
| 204 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 205 | /** Creates an opencl kernel |
| 206 | * |
| 207 | * @param[in] ctx A context to be used to create the opencl kernel. |
| 208 | * @param[in] kernel_name The kernel name. |
| 209 | * @param[in] build_opts The build options to be used for the opencl kernel compilation. |
| 210 | * |
| 211 | * @return An opencl kernel |
| 212 | */ |
| 213 | cl::Kernel create_opencl_kernel(CLCoreRuntimeContext *ctx, const std::string &kernel_name, const CLBuildOptions &build_opts); |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 214 | |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 215 | /** Creates an opencl kernel using a compile context |
| 216 | * |
| 217 | * @param[in] ctx A compile context to be used to create the opencl kernel. |
| 218 | * @param[in] kernel_name The kernel name. |
| 219 | * @param[in] build_opts The build options to be used for the opencl kernel compilation. |
| 220 | * |
| 221 | * @return An opencl kernel |
| 222 | */ |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 223 | cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set<std::string> &build_opts = std::set<std::string>()); |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 224 | |
Manuel Bottini | 7b9998d | 2019-10-21 17:59:07 +0100 | [diff] [blame] | 225 | /** Creates a suitable LWS hint object for parallel implementations. Sets the number of WG based on the input size. |
| 226 | * If input width is smaller than 128 we can use fewer threads than 8. |
| 227 | * |
| 228 | * @param[in] input_dimension number of elements along the dimension to apply the parallellization |
| 229 | * @param[in] vector_size size of the vector in OpenCL |
| 230 | * |
| 231 | * @return An LWS hint object |
| 232 | */ |
| 233 | cl::NDRange create_lws_hint_parallel_implementations(unsigned int input_dimension, unsigned int vector_size); |
| 234 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 235 | /* Helper function to check if the workgroup batch size modifier parameter is supported on the cl device |
| 236 | * |
| 237 | * @param[in] device cl device to check for support |
| 238 | * |
| 239 | * @return true if the workgroup batch size modifier parameter is supported, false otherwise |
| 240 | */ |
| 241 | bool get_wbsm_support_info(const cl::Device &device); |
| 242 | |
| 243 | /* Helper function to set the workgroup batch size modifier parameter in the kernel |
| 244 | * |
| 245 | * @param[in] kernel cl kernel to set the workgroup batch size modifier parameter |
| 246 | * @param[in] wbsm_hint workgroup batch size modifier to use |
| 247 | */ |
| 248 | void set_wbsm(cl::Kernel &kernel, cl_int wbsm_hint); |
| 249 | |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 250 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 251 | #endif /* ARM_COMPUTE_CLHELPERS_H */ |