blob: 93aa45adc19ac835914c09238c438271af4d7aae [file] [log] [blame]
Gian Marcode691f02017-09-08 16:13:11 +01001/*
SiCong Li0a486cf2022-04-07 17:41:51 +01002 * Copyright (c) 2017-2022 Arm Limited.
Gian Marcode691f02017-09-08 16:13:11 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLTUNER_H
25#define ARM_COMPUTE_CLTUNER_H
Gian Marcode691f02017-09-08 16:13:11 +010026
27#include "arm_compute/core/CL/OpenCL.h"
Manuel Bottinib56c1752020-11-18 17:56:30 +000028#include "arm_compute/core/utils/misc/Macros.h"
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010029#include "arm_compute/runtime/CL/CLTunerTypes.h"
Manuel Bottinib56c1752020-11-18 17:56:30 +000030#include "arm_compute/runtime/CL/CLTuningParams.h"
Gian Marcode691f02017-09-08 16:13:11 +010031#include "arm_compute/runtime/CL/ICLTuner.h"
32
33#include <unordered_map>
34
35namespace arm_compute
36{
37class ICLKernel;
38
39/** Basic implementation of the OpenCL tuner interface */
40class CLTuner : public ICLTuner
41{
42public:
Anthony Barbier8db83182018-02-27 13:08:00 +000043 /** Constructor
44 *
45 * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ?
Manuel Bottinib56c1752020-11-18 17:56:30 +000046 * @param[in] tuning_info (Optional) opencl parameters to tune
Anthony Barbier8db83182018-02-27 13:08:00 +000047 *
48 */
Manuel Bottinib56c1752020-11-18 17:56:30 +000049 CLTuner(bool tune_new_kernels = true, CLTuningInfo tuning_info = CLTuningInfo());
Gian Marcode691f02017-09-08 16:13:11 +010050
51 /** Destructor */
52 ~CLTuner() = default;
53
Anthony Barbier8b811952018-02-28 13:47:58 +000054 /** Setter for tune_new_kernels option
Anthony Barbier8db83182018-02-27 13:08:00 +000055 *
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 Bottinib56c1752020-11-18 17:56:30 +000059
60 /** Tune kernels that are not in the tuning parameters table
Anthony Barbier8b811952018-02-28 13:47:58 +000061 *
62 * @return True if tuning of new kernels is enabled.
63 */
64 bool tune_new_kernels() const;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010065
Manuel Bottinib56c1752020-11-18 17:56:30 +000066 /** 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 Loganathan050471e2019-04-25 09:27:24 +010072 /** Set OpenCL tuner mode
73 *
Manuel Bottinib56c1752020-11-18 17:56:30 +000074 * @param[in] mode Indicates how exhaustive the search for the optimal tuning parameters should be while tuning. Default is Exhaustive mode
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010075 */
76 void set_tuner_mode(CLTunerMode mode);
77
Manuel Bottinib56c1752020-11-18 17:56:30 +000078 /** Manually add tuning parameters for a kernel
79 *
80 * @param[in] kernel_id Unique identifiant of the kernel
81 * @param[in] optimal_tuning_params Optimal tuning parameters to use for the given kernel
82 */
83 void add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params);
84
Manuel Bottinib56c1752020-11-18 17:56:30 +000085 /** Import tuning parameters table
86 *
87 * @param[in] tuning_params_table The unordered_map container to import
88 */
89 void import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table);
90
Manuel Bottinib56c1752020-11-18 17:56:30 +000091 /** Give read access to the tuning params table
92 *
93 * @return The tuning params table as unordered_map container
94 */
95 const std::unordered_map<std::string, CLTuningParams> &tuning_params_table() const;
Gian Marcode691f02017-09-08 16:13:11 +010096
Gian Marco85e6f512018-02-01 16:57:48 +000097 /** Set the OpenCL kernel event
98 *
99 * @note The interceptor can use this function to store the event associated to the OpenCL kernel
100 *
101 * @param[in] kernel_event The OpenCL kernel event
102 */
103 void set_cl_kernel_event(cl_event kernel_event);
104
Alex Gildayc357c472018-03-21 13:54:09 +0000105 /** clEnqueueNDRangeKernel symbol */
Anthony Barbier8db83182018-02-27 13:08:00 +0000106 std::function<decltype(clEnqueueNDRangeKernel)> real_clEnqueueNDRangeKernel;
107
Manuel Bottinib56c1752020-11-18 17:56:30 +0000108 /** Load the tuning parameters table from file. It also sets up the tuning read from the file
Anthony Barbier8b811952018-02-28 13:47:58 +0000109 *
Manuel Bottinib56c1752020-11-18 17:56:30 +0000110 * @param[in] filename Load the tuning parameters table from this file.(Must exist)
111 *
Anthony Barbier8b811952018-02-28 13:47:58 +0000112 */
113 void load_from_file(const std::string &filename);
114
Manuel Bottinib56c1752020-11-18 17:56:30 +0000115 /** Save the content of the tuning parameters table to file
Anthony Barbier8b811952018-02-28 13:47:58 +0000116 *
Manuel Bottinib56c1752020-11-18 17:56:30 +0000117 * @param[in] filename Save the tuning parameters table to this file. (Content will be overwritten)
118 *
119 * @return true if the file was created
Anthony Barbier8b811952018-02-28 13:47:58 +0000120 */
Manuel Bottinib56c1752020-11-18 17:56:30 +0000121 bool save_to_file(const std::string &filename) const;
Anthony Barbier8b811952018-02-28 13:47:58 +0000122
Anthony Barbier8db83182018-02-27 13:08:00 +0000123 // Inherited methods overridden:
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000124 void tune_kernel_static(ICLKernel &kernel) override;
125 void tune_kernel_dynamic(ICLKernel &kernel) override;
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100126 void tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors) override;
Anthony Barbierf5dcf792018-02-28 18:04:45 +0000127 /** Is the kernel_event set ?
128 *
129 * @return true if the kernel_event is set.
130 */
131 bool kernel_event_is_set() const;
132
SiCong Li0a486cf2022-04-07 17:41:51 +0100133 /** A wrapper wrapping tensors and other objects needed for running the kernel
134 */
135 struct IKernelData;
136
Gian Marcode691f02017-09-08 16:13:11 +0100137private:
SiCong Li0a486cf2022-04-07 17:41:51 +0100138 /** Perform tune_kernel_dynamic
139 *
140 * @param[in] kernel OpenCL kernel to be tuned with tuning parameters
141 * @param[in,out] data IKernelData object wrapping tensors and other objects needed for running the kernel
142 *
143 */
144 void do_tune_kernel_dynamic(ICLKernel &kernel, IKernelData *data);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000145 /** Find optimal tuning parameters using brute-force approach
Gian Marcode691f02017-09-08 16:13:11 +0100146 *
SiCong Li0a486cf2022-04-07 17:41:51 +0100147 * @param[in] kernel OpenCL kernel to be tuned with tuning parameters
148 * @param[in,out] data IKernelData object wrapping tensors and other objects needed for running the kernel
Gian Marcode691f02017-09-08 16:13:11 +0100149 *
Manuel Bottinib56c1752020-11-18 17:56:30 +0000150 * @return The optimal tuning parameters to use
Gian Marcode691f02017-09-08 16:13:11 +0100151 */
SiCong Li0a486cf2022-04-07 17:41:51 +0100152 CLTuningParams find_optimal_tuning_params(ICLKernel &kernel, IKernelData *data);
Gian Marcode691f02017-09-08 16:13:11 +0100153
Manuel Bottinib56c1752020-11-18 17:56:30 +0000154 std::unordered_map<std::string, CLTuningParams> _tuning_params_table;
155 std::unordered_map<std::string, cl::NDRange> _lws_table;
156 cl::Event _kernel_event;
157 bool _tune_new_kernels;
158 CLTuningInfo _tuning_info;
Gian Marco85e6f512018-02-01 16:57:48 +0000159};
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000160} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000161#endif /*ARM_COMPUTE_CLTUNER_H */