Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | 200b6e3 | 2018-01-16 17:21:45 +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_CLKERNELLIBRARY_H__ |
| 25 | #define __ARM_COMPUTE_CLKERNELLIBRARY_H__ |
| 26 | |
| 27 | #include "arm_compute/core/CL/OpenCL.h" |
| 28 | |
| 29 | #include <map> |
| 30 | #include <set> |
| 31 | #include <string> |
| 32 | #include <utility> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame] | 36 | /** Build options */ |
| 37 | class CLBuildOptions |
| 38 | { |
| 39 | using StringSet = std::set<std::string>; |
| 40 | |
| 41 | public: |
| 42 | /** Default constructor. */ |
| 43 | CLBuildOptions(); |
| 44 | /** Adds option to the existing build option list |
| 45 | * |
| 46 | * @param[in] option Option to add |
| 47 | */ |
| 48 | void add_option(std::string option); |
| 49 | /** Adds option if a given condition is true; |
| 50 | * |
| 51 | * @param[in] cond Condition to check |
| 52 | * @param[in] option Option to add if condition is true |
| 53 | */ |
| 54 | void add_option_if(bool cond, std::string option); |
| 55 | /** Adds first option if condition is true else the second one |
| 56 | * |
| 57 | * @param[in] cond Condition to check |
| 58 | * @param[in] option_true Option to add if condition is true |
| 59 | * @param[in] option_false Option to add if condition is false |
| 60 | */ |
| 61 | void add_option_if_else(bool cond, std::string option_true, std::string option_false); |
Chunosov | f450caa | 2017-11-08 16:09:35 +0700 | [diff] [blame] | 62 | /** Appends given build options to the current's objects options. |
| 63 | * |
| 64 | * @param[in] options Build options to append |
| 65 | */ |
| 66 | void add_options(const StringSet &options); |
| 67 | /** Appends given build options to the current's objects options if a given condition is true. |
| 68 | * |
| 69 | * @param[in] cond Condition to check |
| 70 | * @param[in] options Option to add if condition is true |
| 71 | */ |
| 72 | void add_options_if(bool cond, const StringSet &options); |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame] | 73 | /** Gets the current options list set |
| 74 | * |
| 75 | * @return Build options set |
| 76 | */ |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 77 | const StringSet &options() const; |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | StringSet _build_opts; /**< Build options set */ |
| 81 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | /** Program class */ |
| 83 | class Program |
| 84 | { |
| 85 | public: |
| 86 | /** Default constructor. */ |
| 87 | Program(); |
| 88 | /** Construct program from source file. |
| 89 | * |
| 90 | * @param[in] context CL context used to create the program. |
| 91 | * @param[in] name Program name. |
| 92 | * @param[in] source Program source. |
| 93 | */ |
| 94 | Program(cl::Context context, std::string name, std::string source); |
| 95 | /** Construct program from binary file. |
| 96 | * |
| 97 | * @param[in] context CL context used to create the program. |
| 98 | * @param[in] device CL device for which the programs are created. |
| 99 | * @param[in] name Program name. |
| 100 | * @param[in] binary Program binary. |
| 101 | */ |
| 102 | Program(cl::Context context, cl::Device device, std::string name, std::vector<unsigned char> binary); |
| 103 | /** Default Copy Constructor. */ |
| 104 | Program(const Program &) = default; |
| 105 | /** Default Move Constructor. */ |
| 106 | Program(Program &&) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 107 | /** Default copy assignment operator */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | Program &operator=(const Program &) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 109 | /** Default move assignment operator */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | Program &operator=(Program &&) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 111 | /** Returns program name. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | * |
| 113 | * @return Program's name. |
| 114 | */ |
| 115 | std::string name() const |
| 116 | { |
| 117 | return _name; |
| 118 | } |
| 119 | /** User-defined conversion to the underlying CL program. |
| 120 | * |
| 121 | * @return The CL program object. |
| 122 | */ |
| 123 | explicit operator cl::Program() const; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 124 | /** Build the given CL program. |
| 125 | * |
| 126 | * @param[in] program The CL program to build. |
| 127 | * @param[in] build_options Options to build the CL program. |
| 128 | * |
| 129 | * @return True if the CL program builds successfully. |
| 130 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | static bool build(const cl::Program &program, const std::string &build_options = ""); |
| 132 | /** Build the underlying CL program. |
| 133 | * |
| 134 | * @param[in] build_options Options used to build the CL program. |
| 135 | * |
| 136 | * @return A reference to itself. |
| 137 | */ |
| 138 | cl::Program build(const std::string &build_options = "") const; |
| 139 | |
| 140 | private: |
| 141 | cl::Context _context; /**< Underlying CL context. */ |
| 142 | cl::Device _device; /**< CL device for which the programs are created. */ |
| 143 | bool _is_binary; /**< Create program from binary? */ |
| 144 | std::string _name; /**< Program name. */ |
| 145 | std::string _source; /**< Source code for the program. */ |
| 146 | std::vector<unsigned char> _binary; /**< Binary from which to create the program. */ |
| 147 | }; |
| 148 | |
| 149 | /** Kernel class */ |
| 150 | class Kernel |
| 151 | { |
| 152 | public: |
| 153 | /** Default Constructor. */ |
| 154 | Kernel(); |
| 155 | /** Default Copy Constructor. */ |
| 156 | Kernel(const Kernel &) = default; |
| 157 | /** Default Move Constructor. */ |
| 158 | Kernel(Kernel &&) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 159 | /** Default copy assignment operator */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | Kernel &operator=(const Kernel &) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 161 | /** Default move assignment operator */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | Kernel &operator=(Kernel &&) = default; |
| 163 | /** Constructor. |
| 164 | * |
| 165 | * @param[in] name Kernel name. |
| 166 | * @param[in] program Built program. |
| 167 | */ |
| 168 | Kernel(std::string name, const cl::Program &program); |
| 169 | /** Returns kernel name. |
| 170 | * |
| 171 | * @return Kernel's name. |
| 172 | */ |
| 173 | std::string name() const |
| 174 | { |
| 175 | return _name; |
| 176 | } |
| 177 | /** Returns OpenCL kernel. |
| 178 | * |
| 179 | * @return OpenCL Kernel. |
| 180 | */ |
| 181 | explicit operator cl::Kernel() const |
| 182 | { |
| 183 | return _kernel; |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | std::string _name; /**< Kernel name */ |
| 188 | cl::Kernel _kernel; /**< OpenCL Kernel */ |
| 189 | }; |
| 190 | |
| 191 | /** CLKernelLibrary class */ |
| 192 | class CLKernelLibrary |
| 193 | { |
| 194 | using StringSet = std::set<std::string>; |
| 195 | |
| 196 | private: |
| 197 | /** Default Constructor. */ |
| 198 | CLKernelLibrary(); |
| 199 | |
| 200 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 201 | /** Prevent instances of this class from being copied */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 202 | CLKernelLibrary(const CLKernelLibrary &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 203 | /** Prevent instances of this class from being copied */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | const CLKernelLibrary &operator=(const CLKernelLibrary &) = delete; |
| 205 | /** Access the KernelLibrary singleton. |
| 206 | * @return The KernelLibrary instance. |
| 207 | */ |
| 208 | static CLKernelLibrary &get(); |
| 209 | /** Initialises the kernel library. |
| 210 | * |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 211 | * @param[in] kernel_path Path of the directory from which kernel sources are loaded. |
| 212 | * @param[in] context CL context used to create programs. |
| 213 | * @param[in] device CL device for which the programs are created. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | */ |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 215 | void init(std::string kernel_path, cl::Context context, cl::Device device) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | { |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 217 | _kernel_path = std::move(kernel_path); |
| 218 | _context = std::move(context); |
| 219 | _device = std::move(device); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 220 | } |
| 221 | /** Sets the path that the kernels reside in. |
| 222 | * |
| 223 | * @param[in] kernel_path Path of the kernel. |
| 224 | */ |
| 225 | void set_kernel_path(const std::string &kernel_path) |
| 226 | { |
| 227 | _kernel_path = kernel_path; |
| 228 | }; |
Michalis Spyrou | d7e8281 | 2017-06-20 15:00:14 +0100 | [diff] [blame] | 229 | /** Gets the path that the kernels reside in. |
| 230 | */ |
| 231 | std::string get_kernel_path() |
| 232 | { |
| 233 | return _kernel_path; |
| 234 | }; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 235 | /** Gets the source of the selected program. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 236 | * |
| 237 | * @param[in] program_name Program name. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 238 | * |
| 239 | * @return Source of the selected program. |
Michalis Spyrou | d7e8281 | 2017-06-20 15:00:14 +0100 | [diff] [blame] | 240 | */ |
| 241 | std::string get_program_source(const std::string &program_name); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 242 | /** Sets the CL context used to create programs. |
| 243 | * |
| 244 | * @note Setting the context also resets the device to the |
| 245 | * first one available in the new context. |
| 246 | * |
| 247 | * @param[in] context A CL context. |
| 248 | */ |
| 249 | void set_context(cl::Context context) |
| 250 | { |
| 251 | _context = std::move(context); |
Anthony Barbier | ef9e059 | 2018-05-11 11:55:09 +0100 | [diff] [blame] | 252 | if(_context.get() == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 253 | { |
| 254 | _device = cl::Device(); |
| 255 | } |
| 256 | else |
| 257 | { |
Anthony Barbier | ef9e059 | 2018-05-11 11:55:09 +0100 | [diff] [blame] | 258 | const auto cl_devices = _context.getInfo<CL_CONTEXT_DEVICES>(); |
| 259 | |
| 260 | if(cl_devices.empty()) |
| 261 | { |
| 262 | _device = cl::Device(); |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | _device = cl_devices[0]; |
| 267 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 268 | } |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 269 | } |
Anthony Barbier | 4dcb583 | 2018-05-08 11:29:05 +0100 | [diff] [blame] | 270 | |
| 271 | /** Accessor for the associated CL context. |
| 272 | * |
| 273 | * @return A CL context. |
| 274 | */ |
| 275 | cl::Context &context() |
| 276 | { |
| 277 | return _context; |
| 278 | } |
| 279 | |
Giorgio Arena | 6200fa4 | 2018-07-06 17:06:36 +0100 | [diff] [blame] | 280 | /** Gets the CL device for which the programs are created. */ |
| 281 | cl::Device &get_device() |
| 282 | { |
| 283 | return _device; |
| 284 | } |
| 285 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 286 | /** Sets the CL device for which the programs are created. |
| 287 | * |
| 288 | * @param[in] device A CL device. |
| 289 | */ |
| 290 | void set_device(cl::Device device) |
| 291 | { |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 292 | _device = std::move(device); |
| 293 | } |
Anthony Barbier | 847864d | 2018-03-07 11:35:53 +0000 | [diff] [blame] | 294 | |
| 295 | /** Return the device version |
| 296 | * |
| 297 | * @return The content of CL_DEVICE_VERSION |
| 298 | */ |
| 299 | std::string get_device_version(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 300 | /** Creates a kernel from the kernel library. |
| 301 | * |
| 302 | * @param[in] kernel_name Kernel name. |
| 303 | * @param[in] build_options_set Kernel build options as a set. |
| 304 | * |
| 305 | * @return The created kernel. |
| 306 | */ |
| 307 | Kernel create_kernel(const std::string &kernel_name, const StringSet &build_options_set = {}) const; |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 308 | /** Find the maximum number of local work items in a workgroup can be supported for the kernel. |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 309 | * |
| 310 | */ |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 311 | size_t max_local_workgroup_size(const cl::Kernel &kernel) const; |
| 312 | /** Return the default NDRange for the device. |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 313 | * |
| 314 | */ |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 315 | cl::NDRange default_ndrange() const; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 316 | |
Anthony Barbier | 200b6e3 | 2018-01-16 17:21:45 +0000 | [diff] [blame] | 317 | /** Clear the library's cache of binary programs |
| 318 | */ |
| 319 | void clear_programs_cache() |
| 320 | { |
| 321 | _programs_map.clear(); |
| 322 | _built_programs_map.clear(); |
| 323 | } |
| 324 | |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 325 | /** Access the cache of built OpenCL programs */ |
Anthony Barbier | 7da55aa | 2018-04-13 16:58:43 +0100 | [diff] [blame] | 326 | const std::map<std::string, cl::Program> &get_built_programs() const |
| 327 | { |
| 328 | return _built_programs_map; |
| 329 | } |
| 330 | |
| 331 | /** Add a new built program to the cache |
| 332 | * |
| 333 | * @param[in] built_program_name Name of the program |
| 334 | * @param[in] program Built program to add to the cache |
| 335 | */ |
| 336 | void add_built_program(const std::string &built_program_name, cl::Program program); |
| 337 | |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 338 | /** Returns true if FP16 is supported by the CL device |
| 339 | * |
| 340 | * @return true if the CL device supports FP16 |
| 341 | */ |
| 342 | bool fp16_supported() const; |
| 343 | |
Vidhya Sudhan Loganathan | 76c8564 | 2018-05-25 13:53:02 +0100 | [diff] [blame] | 344 | /** Returns true if int64_base_atomics extension is supported by the CL device |
| 345 | * |
| 346 | * @return true if the CL device supports int64_base_atomics extension |
| 347 | */ |
| 348 | bool int64_base_atomics_supported() const; |
| 349 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 350 | private: |
| 351 | /** Load program and its dependencies. |
| 352 | * |
| 353 | * @param[in] program_name Name of the program to load. |
| 354 | */ |
| 355 | const Program &load_program(const std::string &program_name) const; |
| 356 | /** Concatenates contents of a set into a single string. |
| 357 | * |
| 358 | * @param[in] s Input set to concatenate. |
| 359 | * |
| 360 | * @return Concatenated string. |
| 361 | */ |
| 362 | std::string stringify_set(const StringSet &s) const; |
| 363 | |
| 364 | cl::Context _context; /**< Underlying CL context. */ |
| 365 | cl::Device _device; /**< Underlying CL device. */ |
| 366 | std::string _kernel_path; /**< Path to the kernels folder. */ |
| 367 | mutable std::map<std::string, const Program> _programs_map; /**< Map with all already loaded program data. */ |
| 368 | mutable std::map<std::string, cl::Program> _built_programs_map; /**< Map with all already built program data. */ |
| 369 | static const std::map<std::string, std::string> _kernel_program_map; /**< Map that associates kernel names with programs. */ |
| 370 | static const std::map<std::string, std::string> _program_source_map; /**< Contains sources for all programs. |
| 371 | Used for compile-time kernel inclusion. >*/ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 372 | }; |
| 373 | } |
| 374 | #endif /* __ARM_COMPUTE_CLKERNELLIBRARY_H__ */ |