Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 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_CLKERNELLIBRARY_H |
| 25 | #define ARM_COMPUTE_CLKERNELLIBRARY_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLCompileContext.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/OpenCL.h" |
| 29 | |
| 30 | #include <map> |
| 31 | #include <set> |
| 32 | #include <string> |
| 33 | #include <utility> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | /** CLKernelLibrary class */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 38 | class CLKernelLibrary final |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | { |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 40 | private: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | /** Default Constructor. */ |
| 42 | CLKernelLibrary(); |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 43 | /** Prevent instances of this class from being copied */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | CLKernelLibrary(const CLKernelLibrary &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 45 | /** Prevent instances of this class from being copied */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | const CLKernelLibrary &operator=(const CLKernelLibrary &) = delete; |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 47 | |
| 48 | public: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | /** Access the KernelLibrary singleton. |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 50 | * This method has been deprecated and will be removed in the next release. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | * @return The KernelLibrary instance. |
| 52 | */ |
| 53 | static CLKernelLibrary &get(); |
| 54 | /** Initialises the kernel library. |
| 55 | * |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 56 | * @param[in] kernel_path Path of the directory from which kernel sources are loaded. |
| 57 | * @param[in] context CL context used to create programs. |
| 58 | * @param[in] device CL device for which the programs are created. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 60 | void init(std::string kernel_path, cl::Context context, cl::Device device); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | /** Sets the path that the kernels reside in. |
| 62 | * |
| 63 | * @param[in] kernel_path Path of the kernel. |
| 64 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 65 | void set_kernel_path(const std::string &kernel_path); |
Michalis Spyrou | d7e8281 | 2017-06-20 15:00:14 +0100 | [diff] [blame] | 66 | /** Gets the path that the kernels reside in. |
| 67 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 68 | std::string get_kernel_path(); |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 69 | /** Gets the source of the selected program. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 70 | * |
| 71 | * @param[in] program_name Program name. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 72 | * |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 73 | * @return A pair with the source (false) or the binary (true), of the selected program. |
Michalis Spyrou | d7e8281 | 2017-06-20 15:00:14 +0100 | [diff] [blame] | 74 | */ |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 75 | std::pair<std::string, bool> get_program(const std::string &program_name) const; |
Anthony Barbier | 4dcb583 | 2018-05-08 11:29:05 +0100 | [diff] [blame] | 76 | |
| 77 | /** Accessor for the associated CL context. |
| 78 | * |
| 79 | * @return A CL context. |
| 80 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 81 | cl::Context &context(); |
Anthony Barbier | 4dcb583 | 2018-05-08 11:29:05 +0100 | [diff] [blame] | 82 | |
Giorgio Arena | 6200fa4 | 2018-07-06 17:06:36 +0100 | [diff] [blame] | 83 | /** Gets the CL device for which the programs are created. */ |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 84 | const cl::Device &get_device(); |
Giorgio Arena | 6200fa4 | 2018-07-06 17:06:36 +0100 | [diff] [blame] | 85 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | /** Sets the CL device for which the programs are created. |
| 87 | * |
| 88 | * @param[in] device A CL device. |
| 89 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 90 | void set_device(cl::Device device); |
Anthony Barbier | 847864d | 2018-03-07 11:35:53 +0000 | [diff] [blame] | 91 | |
| 92 | /** Return the device version |
| 93 | * |
| 94 | * @return The content of CL_DEVICE_VERSION |
| 95 | */ |
| 96 | std::string get_device_version(); |
Giorgio Arena | 5d42b46 | 2019-07-26 15:54:20 +0100 | [diff] [blame] | 97 | /** Return the maximum number of compute units in the device |
| 98 | * |
| 99 | * @return The content of CL_DEVICE_MAX_COMPUTE_UNITS |
| 100 | */ |
| 101 | cl_uint get_num_compute_units(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | /** Creates a kernel from the kernel library. |
| 103 | * |
| 104 | * @param[in] kernel_name Kernel name. |
| 105 | * @param[in] build_options_set Kernel build options as a set. |
| 106 | * |
| 107 | * @return The created kernel. |
| 108 | */ |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 109 | Kernel create_kernel(const std::string &kernel_name, const std::set<std::string> &build_options_set = {}) const; |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 110 | /** 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] | 111 | * |
| 112 | */ |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 113 | size_t max_local_workgroup_size(const cl::Kernel &kernel) const; |
| 114 | /** Return the default NDRange for the device. |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 115 | * |
| 116 | */ |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 117 | cl::NDRange default_ndrange() const; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
Anthony Barbier | 200b6e3 | 2018-01-16 17:21:45 +0000 | [diff] [blame] | 119 | /** Clear the library's cache of binary programs |
| 120 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 121 | void clear_programs_cache(); |
Anthony Barbier | 200b6e3 | 2018-01-16 17:21:45 +0000 | [diff] [blame] | 122 | |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 123 | /** Access the cache of built OpenCL programs */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 124 | const std::map<std::string, cl::Program> &get_built_programs() const; |
Anthony Barbier | 7da55aa | 2018-04-13 16:58:43 +0100 | [diff] [blame] | 125 | |
| 126 | /** Add a new built program to the cache |
| 127 | * |
| 128 | * @param[in] built_program_name Name of the program |
| 129 | * @param[in] program Built program to add to the cache |
| 130 | */ |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 131 | void add_built_program(const std::string &built_program_name, const cl::Program &program); |
Anthony Barbier | 7da55aa | 2018-04-13 16:58:43 +0100 | [diff] [blame] | 132 | |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 133 | /** Returns true if FP16 is supported by the CL device |
| 134 | * |
| 135 | * @return true if the CL device supports FP16 |
| 136 | */ |
| 137 | bool fp16_supported() const; |
| 138 | |
Vidhya Sudhan Loganathan | 76c8564 | 2018-05-25 13:53:02 +0100 | [diff] [blame] | 139 | /** Returns true if int64_base_atomics extension is supported by the CL device |
| 140 | * |
| 141 | * @return true if the CL device supports int64_base_atomics extension |
| 142 | */ |
| 143 | bool int64_base_atomics_supported() const; |
| 144 | |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 145 | /** Returns the program name given a kernel name |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | * |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 147 | * @return Program name |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | */ |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 149 | std::string get_program_name(const std::string &kernel_name) const; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 151 | /** Sets the CL context used to create programs. |
| 152 | * |
| 153 | * @note Setting the context also resets the device to the |
| 154 | * first one available in the new context. |
| 155 | * |
| 156 | * @param[in] context A CL context. |
| 157 | */ |
| 158 | void set_context(cl::Context context); |
| 159 | |
| 160 | /** Gets the compile context used |
| 161 | * |
| 162 | * @return The used compile context |
| 163 | */ |
| 164 | CLCompileContext &get_compile_context(); |
| 165 | |
| 166 | private: |
| 167 | CLCompileContext _compile_context; /**< Compile Context. */ |
| 168 | std::string _kernel_path; /**< Path to the kernels folder. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | static const std::map<std::string, std::string> _kernel_program_map; /**< Map that associates kernel names with programs. */ |
| 170 | static const std::map<std::string, std::string> _program_source_map; /**< Contains sources for all programs. |
Michalis Spyrou | 11d4918 | 2020-03-26 10:31:32 +0000 | [diff] [blame] | 171 | Used for compile-time kernel inclusion. >*/ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | }; |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 173 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 174 | #endif /* ARM_COMPUTE_CLKERNELLIBRARY_H */ |