xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/runtime/CL/CLTuner.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2017-2022 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_CLTUNER_H
25*c217d954SCole Faust #define ARM_COMPUTE_CLTUNER_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/CL/OpenCL.h"
28*c217d954SCole Faust #include "arm_compute/core/utils/misc/Macros.h"
29*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTunerTypes.h"
30*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTuningParams.h"
31*c217d954SCole Faust #include "arm_compute/runtime/CL/ICLTuner.h"
32*c217d954SCole Faust 
33*c217d954SCole Faust #include <unordered_map>
34*c217d954SCole Faust 
35*c217d954SCole Faust namespace arm_compute
36*c217d954SCole Faust {
37*c217d954SCole Faust class ICLKernel;
38*c217d954SCole Faust 
39*c217d954SCole Faust /** Basic implementation of the OpenCL tuner interface */
40*c217d954SCole Faust class CLTuner : public ICLTuner
41*c217d954SCole Faust {
42*c217d954SCole Faust public:
43*c217d954SCole Faust     /** Constructor
44*c217d954SCole Faust      *
45*c217d954SCole Faust      * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ?
46*c217d954SCole Faust      * @param[in] tuning_info      (Optional) opencl parameters to tune
47*c217d954SCole Faust      *
48*c217d954SCole Faust      */
49*c217d954SCole Faust     CLTuner(bool tune_new_kernels = true, CLTuningInfo tuning_info = CLTuningInfo());
50*c217d954SCole Faust 
51*c217d954SCole Faust     /** Destructor */
52*c217d954SCole Faust     ~CLTuner() = default;
53*c217d954SCole Faust 
54*c217d954SCole Faust     /** Setter for tune_new_kernels option
55*c217d954SCole Faust      *
56*c217d954SCole Faust      * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ?
57*c217d954SCole Faust      */
58*c217d954SCole Faust     void set_tune_new_kernels(bool tune_new_kernels);
59*c217d954SCole Faust 
60*c217d954SCole Faust     /** Tune kernels that are not in the tuning parameters table
61*c217d954SCole Faust      *
62*c217d954SCole Faust      * @return True if tuning of new kernels is enabled.
63*c217d954SCole Faust      */
64*c217d954SCole Faust     bool tune_new_kernels() const;
65*c217d954SCole Faust 
66*c217d954SCole Faust     /** Setter for tune parameters option
67*c217d954SCole Faust      *
68*c217d954SCole Faust      * @param[in] tuning_info opencl parameters to tune
69*c217d954SCole Faust      */
70*c217d954SCole Faust     void set_tuning_parameters(CLTuningInfo tuning_info);
71*c217d954SCole Faust 
72*c217d954SCole Faust     /** Set OpenCL tuner mode
73*c217d954SCole Faust      *
74*c217d954SCole Faust      * @param[in] mode Indicates how exhaustive the search for the optimal tuning parameters should be while tuning. Default is Exhaustive mode
75*c217d954SCole Faust      */
76*c217d954SCole Faust     void set_tuner_mode(CLTunerMode mode);
77*c217d954SCole Faust 
78*c217d954SCole Faust     /** Manually add tuning parameters for a kernel
79*c217d954SCole Faust      *
80*c217d954SCole Faust      * @param[in] kernel_id             Unique identifiant of the kernel
81*c217d954SCole Faust      * @param[in] optimal_tuning_params Optimal tuning parameters to use for the given kernel
82*c217d954SCole Faust      */
83*c217d954SCole Faust     void add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params);
84*c217d954SCole Faust 
85*c217d954SCole Faust     /** Import tuning parameters table
86*c217d954SCole Faust      *
87*c217d954SCole Faust      * @param[in] tuning_params_table The unordered_map container to import
88*c217d954SCole Faust      */
89*c217d954SCole Faust     void import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table);
90*c217d954SCole Faust 
91*c217d954SCole Faust     /** Give read access to the tuning params table
92*c217d954SCole Faust      *
93*c217d954SCole Faust      * @return The tuning params table as unordered_map container
94*c217d954SCole Faust      */
95*c217d954SCole Faust     const std::unordered_map<std::string, CLTuningParams> &tuning_params_table() const;
96*c217d954SCole Faust 
97*c217d954SCole Faust     /** Set the OpenCL kernel event
98*c217d954SCole Faust      *
99*c217d954SCole Faust      * @note The interceptor can use this function to store the event associated to the OpenCL kernel
100*c217d954SCole Faust      *
101*c217d954SCole Faust      * @param[in] kernel_event The OpenCL kernel event
102*c217d954SCole Faust      */
103*c217d954SCole Faust     void set_cl_kernel_event(cl_event kernel_event);
104*c217d954SCole Faust 
105*c217d954SCole Faust     /** clEnqueueNDRangeKernel symbol */
106*c217d954SCole Faust     std::function<decltype(clEnqueueNDRangeKernel)> real_clEnqueueNDRangeKernel;
107*c217d954SCole Faust 
108*c217d954SCole Faust     /** Load the tuning parameters table from file. It also sets up the tuning read from the file
109*c217d954SCole Faust      *
110*c217d954SCole Faust      * @param[in] filename Load the tuning parameters table from this file.(Must exist)
111*c217d954SCole Faust      *
112*c217d954SCole Faust      */
113*c217d954SCole Faust     void load_from_file(const std::string &filename);
114*c217d954SCole Faust 
115*c217d954SCole Faust     /** Save the content of the tuning parameters table to file
116*c217d954SCole Faust      *
117*c217d954SCole Faust      * @param[in] filename Save the tuning parameters table to this file. (Content will be overwritten)
118*c217d954SCole Faust      *
119*c217d954SCole Faust      * @return true if the file was created
120*c217d954SCole Faust      */
121*c217d954SCole Faust     bool save_to_file(const std::string &filename) const;
122*c217d954SCole Faust 
123*c217d954SCole Faust     // Inherited methods overridden:
124*c217d954SCole Faust     void tune_kernel_static(ICLKernel &kernel) override;
125*c217d954SCole Faust     void tune_kernel_dynamic(ICLKernel &kernel) override;
126*c217d954SCole Faust     void tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors) override;
127*c217d954SCole Faust     /** Is the kernel_event set ?
128*c217d954SCole Faust      *
129*c217d954SCole Faust      * @return true if the kernel_event is set.
130*c217d954SCole Faust      */
131*c217d954SCole Faust     bool kernel_event_is_set() const;
132*c217d954SCole Faust 
133*c217d954SCole Faust     /** A wrapper wrapping tensors and other objects needed for running the kernel
134*c217d954SCole Faust      */
135*c217d954SCole Faust     struct IKernelData;
136*c217d954SCole Faust 
137*c217d954SCole Faust private:
138*c217d954SCole Faust     /** Perform tune_kernel_dynamic
139*c217d954SCole Faust      *
140*c217d954SCole Faust      * @param[in]     kernel OpenCL kernel to be tuned with tuning parameters
141*c217d954SCole Faust      * @param[in,out] data   IKernelData object wrapping tensors and other objects needed for running the kernel
142*c217d954SCole Faust      *
143*c217d954SCole Faust      */
144*c217d954SCole Faust     void do_tune_kernel_dynamic(ICLKernel &kernel, IKernelData *data);
145*c217d954SCole Faust     /** Find optimal tuning parameters using brute-force approach
146*c217d954SCole Faust      *
147*c217d954SCole Faust      * @param[in]     kernel OpenCL kernel to be tuned with tuning parameters
148*c217d954SCole Faust      * @param[in,out] data   IKernelData object wrapping tensors and other objects needed for running the kernel
149*c217d954SCole Faust      *
150*c217d954SCole Faust      * @return The optimal tuning parameters to use
151*c217d954SCole Faust      */
152*c217d954SCole Faust     CLTuningParams find_optimal_tuning_params(ICLKernel &kernel, IKernelData *data);
153*c217d954SCole Faust 
154*c217d954SCole Faust     std::unordered_map<std::string, CLTuningParams> _tuning_params_table;
155*c217d954SCole Faust     std::unordered_map<std::string, cl::NDRange>    _lws_table;
156*c217d954SCole Faust     cl::Event    _kernel_event;
157*c217d954SCole Faust     bool         _tune_new_kernels;
158*c217d954SCole Faust     CLTuningInfo _tuning_info;
159*c217d954SCole Faust };
160*c217d954SCole Faust } // namespace arm_compute
161*c217d954SCole Faust #endif /*ARM_COMPUTE_CLTUNER_H */
162