Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +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_CLTUNER_H |
| 25 | #define ARM_COMPUTE_CLTUNER_H |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/CL/OpenCL.h" |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/Macros.h" |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLTunerTypes.h" |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/CLTuningParams.h" |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/CL/ICLTuner.h" |
| 32 | |
| 33 | #include <unordered_map> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | class ICLKernel; |
| 38 | |
| 39 | /** Basic implementation of the OpenCL tuner interface */ |
| 40 | class CLTuner : public ICLTuner |
| 41 | { |
| 42 | public: |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 43 | /** Constructor |
| 44 | * |
| 45 | * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ? |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 46 | * @param[in] tuning_info (Optional) opencl parameters to tune |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 47 | * |
| 48 | */ |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 49 | CLTuner(bool tune_new_kernels = true, CLTuningInfo tuning_info = CLTuningInfo()); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 50 | |
| 51 | /** Destructor */ |
| 52 | ~CLTuner() = default; |
| 53 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 54 | /** Setter for tune_new_kernels option |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 55 | * |
| 56 | * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ? |
| 57 | */ |
| 58 | void set_tune_new_kernels(bool tune_new_kernels); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 59 | |
| 60 | /** Tune kernels that are not in the tuning parameters table |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 61 | * |
| 62 | * @return True if tuning of new kernels is enabled. |
| 63 | */ |
| 64 | bool tune_new_kernels() const; |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 65 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 66 | /** Setter for tune parameters option |
| 67 | * |
| 68 | * @param[in] tuning_info opencl parameters to tune |
| 69 | */ |
| 70 | void set_tuning_parameters(CLTuningInfo tuning_info); |
| 71 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 72 | /** Set OpenCL tuner mode |
| 73 | * |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 74 | * @param[in] mode Indicates how exhaustive the search for the optimal tuning parameters should be while tuning. Default is Exhaustive mode |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 75 | */ |
| 76 | void set_tuner_mode(CLTunerMode mode); |
| 77 | |
| 78 | /** Get the current OpenCL tuner mode |
| 79 | * |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 80 | * @return tuner_mode Indicates how exhaustive the search for the optimal tuning parameters should be while tuning |
| 81 | * |
| 82 | * @deprecated This function is deprecated and is intended to be removed in 21.05 release |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 83 | */ |
| 84 | CLTunerMode get_tuner_mode() const; |
| 85 | |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 86 | /** Manually add a LWS for a kernel |
| 87 | * |
| 88 | * @param[in] kernel_id Unique identifiant of the kernel |
| 89 | * @param[in] optimal_lws Optimal local workgroup size to use for the given kernel |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 90 | * |
| 91 | * @deprecated This function is deprecated and is intended to be removed in 21.05 release |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 92 | */ |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 93 | ARM_COMPUTE_DEPRECATED_REL_REPLACE(21.02, add_tuning_params) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 94 | void add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws); |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 95 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 96 | /** Manually add tuning parameters for a kernel |
| 97 | * |
| 98 | * @param[in] kernel_id Unique identifiant of the kernel |
| 99 | * @param[in] optimal_tuning_params Optimal tuning parameters to use for the given kernel |
| 100 | */ |
| 101 | void add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params); |
| 102 | |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 103 | /** Import LWS table |
| 104 | * |
| 105 | * @param[in] lws_table The unordered_map container to import |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 106 | * |
| 107 | * @deprecated This function is deprecated and is intended to be removed in 21.05 release |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 108 | */ |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 109 | ARM_COMPUTE_DEPRECATED_REL_REPLACE(21.02, import_tuning_params) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 110 | void import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table); |
| 111 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 112 | /** Import tuning parameters table |
| 113 | * |
| 114 | * @param[in] tuning_params_table The unordered_map container to import |
| 115 | */ |
| 116 | void import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table); |
| 117 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 118 | /** Give read access to the LWS table |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 119 | * |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 120 | * @return The lws table as unordered_map container |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 121 | * |
| 122 | * @deprecated This function is deprecated and is intended to be removed in 21.05 release |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 123 | */ |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 124 | ARM_COMPUTE_DEPRECATED_REL_REPLACE(21.02, tuning_params_table) |
| 125 | const std::unordered_map<std::string, cl::NDRange> &lws_table(); |
| 126 | |
| 127 | /** Give read access to the tuning params table |
| 128 | * |
| 129 | * @return The tuning params table as unordered_map container |
| 130 | */ |
| 131 | const std::unordered_map<std::string, CLTuningParams> &tuning_params_table() const; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 132 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 133 | /** Set the OpenCL kernel event |
| 134 | * |
| 135 | * @note The interceptor can use this function to store the event associated to the OpenCL kernel |
| 136 | * |
| 137 | * @param[in] kernel_event The OpenCL kernel event |
| 138 | */ |
| 139 | void set_cl_kernel_event(cl_event kernel_event); |
| 140 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 141 | /** clEnqueueNDRangeKernel symbol */ |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 142 | std::function<decltype(clEnqueueNDRangeKernel)> real_clEnqueueNDRangeKernel; |
| 143 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 144 | /** Load the tuning parameters table from file. It also sets up the tuning read from the file |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 145 | * |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 146 | * @param[in] filename Load the tuning parameters table from this file.(Must exist) |
| 147 | * |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 148 | */ |
| 149 | void load_from_file(const std::string &filename); |
| 150 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 151 | /** Save the content of the tuning parameters table to file |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 152 | * |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 153 | * @param[in] filename Save the tuning parameters table to this file. (Content will be overwritten) |
| 154 | * |
| 155 | * @return true if the file was created |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 156 | */ |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 157 | bool save_to_file(const std::string &filename) const; |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 158 | |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 159 | // Inherited methods overridden: |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 160 | void tune_kernel_static(ICLKernel &kernel) override; |
| 161 | void tune_kernel_dynamic(ICLKernel &kernel) override; |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 162 | void tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors) override; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 163 | |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 164 | /** Is the kernel_event set ? |
| 165 | * |
| 166 | * @return true if the kernel_event is set. |
| 167 | */ |
| 168 | bool kernel_event_is_set() const; |
| 169 | |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 170 | private: |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 171 | /** Find optimal tuning parameters using brute-force approach |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 172 | * |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 173 | * @param[in] kernel OpenCL kernel to be tuned with tuning parameters |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 174 | * @param[in,out] tensors Tensors for the kernel to operate on |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 175 | * |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 176 | * @return The optimal tuning parameters to use |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 177 | */ |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 178 | CLTuningParams find_optimal_tuning_params(ICLKernel &kernel, ITensorPack &tensors); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 179 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 180 | std::unordered_map<std::string, CLTuningParams> _tuning_params_table; |
| 181 | std::unordered_map<std::string, cl::NDRange> _lws_table; |
| 182 | cl::Event _kernel_event; |
| 183 | bool _tune_new_kernels; |
| 184 | CLTuningInfo _tuning_info; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 185 | }; |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 186 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 187 | #endif /*ARM_COMPUTE_CLTUNER_H */ |