Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_CLHELPERS_H__ |
| 25 | #define __ARM_COMPUTE_CLHELPERS_H__ |
| 26 | |
| 27 | #include "arm_compute/core/CL/OpenCL.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 29 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | |
| 31 | #include <string> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | enum class DataType; |
| 36 | enum class GPUTarget; |
| 37 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 38 | /** Enable bitwise operations on GPUTarget enumerations */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | template <> |
| 40 | struct enable_bitwise_ops<arm_compute::GPUTarget> |
| 41 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 42 | static constexpr bool value = true; /**< Enabled. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /** Max vector width of an OpenCL vector */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 46 | static constexpr unsigned int max_cl_vector_width = 16; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | |
| 48 | /** Translates a tensor data type to the appropriate OpenCL type. |
| 49 | * |
| 50 | * @param[in] dt @ref DataType to be translated to OpenCL type. |
| 51 | * |
| 52 | * @return The string specifying the OpenCL type to be used. |
| 53 | */ |
| 54 | std::string get_cl_type_from_data_type(const DataType &dt); |
| 55 | |
SiCong Li | c51b72f | 2017-07-28 14:46:20 +0100 | [diff] [blame] | 56 | /** Get the size of a data type in number of bits. |
| 57 | * |
| 58 | * @param[in] dt @ref DataType. |
| 59 | * |
| 60 | * @return Number of bits in the data type specified. |
| 61 | */ |
| 62 | std::string get_data_size_from_data_type(const DataType &dt); |
| 63 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 64 | /** Translates fixed point tensor data type to the underlying OpenCL type. |
| 65 | * |
| 66 | * @param[in] dt @ref DataType to be translated to OpenCL type. |
| 67 | * |
| 68 | * @return The string specifying the underlying OpenCL type to be used. |
| 69 | */ |
| 70 | std::string get_underlying_cl_type_from_data_type(const DataType &dt); |
| 71 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | /** Translates a given gpu device target to string. |
| 73 | * |
| 74 | * @param[in] target Given gpu target. |
| 75 | * |
| 76 | * @return The string describing the target. |
| 77 | */ |
| 78 | const std::string &string_from_target(GPUTarget target); |
| 79 | |
| 80 | /** Helper function to create and return a unique_ptr pointed to a CL kernel object |
| 81 | * It also calls the kernel's configuration. |
| 82 | * |
| 83 | * @param[in] args All the arguments that need pass to kernel's configuration. |
| 84 | * |
| 85 | * @return A unique pointer pointed to a CL kernel object |
| 86 | */ |
| 87 | template <typename Kernel, typename... T> |
| 88 | std::unique_ptr<Kernel> create_configure_kernel(T &&... args) |
| 89 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 90 | std::unique_ptr<Kernel> k = arm_compute::support::cpp14::make_unique<Kernel>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | k->configure(std::forward<T>(args)...); |
| 92 | return k; |
| 93 | } |
| 94 | |
| 95 | /** Helper function to create and return a unique_ptr pointed to a CL kernel object |
| 96 | * |
| 97 | * @return A unique pointer pointed to a CL kernel object |
| 98 | */ |
| 99 | template <typename Kernel> |
| 100 | std::unique_ptr<Kernel> create_kernel() |
| 101 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 102 | std::unique_ptr<Kernel> k = arm_compute::support::cpp14::make_unique<Kernel>(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | return k; |
| 104 | } |
| 105 | |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 106 | /** Helper function to get the GPU target from a device name |
| 107 | * |
| 108 | * @param[in] device_name A device name |
| 109 | * |
| 110 | * @return the GPU target |
| 111 | */ |
| 112 | GPUTarget get_target_from_name(const std::string &device_name); |
| 113 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | /** Helper function to get the GPU target from CL device |
| 115 | * |
| 116 | * @param[in] device A CL device |
| 117 | * |
| 118 | * @return the GPU target |
| 119 | */ |
| 120 | GPUTarget get_target_from_device(cl::Device &device); |
| 121 | |
| 122 | /** Helper function to get the GPU arch |
| 123 | * |
| 124 | * @param[in] target GPU target |
| 125 | * |
| 126 | * @return the GPU target which shows the arch |
| 127 | */ |
| 128 | GPUTarget get_arch_from_target(GPUTarget target); |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 129 | |
| 130 | /** Helper function to get the highest OpenCL version supported |
| 131 | * |
| 132 | * @param[in] device A CL device |
| 133 | * |
| 134 | * @return the highest OpenCL version supported |
| 135 | */ |
| 136 | CLVersion get_cl_version(const cl::Device &device); |
Vidhya Sudhan Loganathan | eb8a399 | 2018-04-10 12:23:22 +0100 | [diff] [blame^] | 137 | |
| 138 | /** Helper function to check whether a given extension is supported |
| 139 | * |
| 140 | * @param[in] device A CL device |
| 141 | * @param[in] extension_name Name of the extension to be checked |
| 142 | * |
| 143 | * @return True if the extension is supported |
| 144 | */ |
| 145 | bool device_supports_extension(const cl::Device &device, const char *extension_name); |
| 146 | |
Matthew Bentham | 6f31f8c | 2017-10-27 11:50:06 +0100 | [diff] [blame] | 147 | /** Helper function to check whether the cl_khr_fp16 extension is supported |
| 148 | * |
| 149 | * @param[in] device A CL device |
| 150 | * |
| 151 | * @return True if the extension is supported |
| 152 | */ |
| 153 | bool fp16_support(const cl::Device &device); |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 154 | /** Helper function to check whether the arm_non_uniform_work_group_size extension is supported |
| 155 | * |
| 156 | * @param[in] device A CL device |
| 157 | * |
| 158 | * @return True if the extension is supported |
| 159 | */ |
| 160 | bool non_uniform_workgroup_support(const cl::Device &device); |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 161 | /** Helper function to check whether a gpu target is equal to the provided targets |
| 162 | * |
| 163 | * @param[in] target_to_check gpu target to check |
| 164 | * @param[in] target First target to compare against |
| 165 | * @param[in] targets (Optional) Additional targets to compare with |
| 166 | * |
| 167 | * @return True if the target is equal with at least one of the targets. |
| 168 | */ |
| 169 | template <typename... Args> |
| 170 | bool gpu_target_is_in(GPUTarget target_to_check, GPUTarget target, Args... targets) |
| 171 | { |
| 172 | return (target_to_check == target) | gpu_target_is_in(target_to_check, targets...); |
| 173 | } |
| 174 | |
| 175 | /** Variant of gpu_target_is_in for comparing two targets */ |
| 176 | inline bool gpu_target_is_in(GPUTarget target_to_check, GPUTarget target) |
| 177 | { |
| 178 | return target_to_check == target; |
| 179 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | } |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 181 | #endif /* __ARM_COMPUTE_CLHELPERS_H__ */ |