blob: 3b7a42fa6a8dfdb9db11e91ff2cb481d305afcdf [file] [log] [blame]
Gian Marcode691f02017-09-08 16:13:11 +01001/*
Georgios Pinitas4632e5e2019-02-06 14:47:59 +00002 * Copyright (c) 2017-2019 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"
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010028#include "arm_compute/runtime/CL/CLTunerTypes.h"
Gian Marcode691f02017-09-08 16:13:11 +010029#include "arm_compute/runtime/CL/ICLTuner.h"
30
31#include <unordered_map>
32
33namespace arm_compute
34{
35class ICLKernel;
36
37/** Basic implementation of the OpenCL tuner interface */
38class CLTuner : public ICLTuner
39{
40public:
Anthony Barbier8db83182018-02-27 13:08:00 +000041 /** Constructor
42 *
43 * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ?
44 *
45 */
46 CLTuner(bool tune_new_kernels = true);
Gian Marcode691f02017-09-08 16:13:11 +010047
48 /** Destructor */
49 ~CLTuner() = default;
50
Anthony Barbier8b811952018-02-28 13:47:58 +000051 /** Setter for tune_new_kernels option
Anthony Barbier8db83182018-02-27 13:08:00 +000052 *
53 * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ?
54 */
55 void set_tune_new_kernels(bool tune_new_kernels);
Anthony Barbier8b811952018-02-28 13:47:58 +000056 /** Tune kernels that are not in the LWS table
57 *
58 * @return True if tuning of new kernels is enabled.
59 */
60 bool tune_new_kernels() const;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010061
62 /** Set OpenCL tuner mode
63 *
64 * @param[in] mode Indicates how exhaustive the search for the optimal LWS should be while tuning. Default is Exhaustive mode
65 */
66 void set_tuner_mode(CLTunerMode mode);
67
68 /** Get the current OpenCL tuner mode
69 *
70 * @return tuner_mode Indicates how exhaustive the search for the optimal LWS should be while tuning
71 */
72 CLTunerMode get_tuner_mode() const;
73
Anthony Barbier8db83182018-02-27 13:08:00 +000074 /** Manually add a LWS for a kernel
75 *
76 * @param[in] kernel_id Unique identifiant of the kernel
77 * @param[in] optimal_lws Optimal local workgroup size to use for the given kernel
78 */
79 void add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws);
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010080
Gian Marcode691f02017-09-08 16:13:11 +010081 /** Import LWS table
82 *
83 * @param[in] lws_table The unordered_map container to import
84 */
85 void import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table);
86
Anthony Barbier8b811952018-02-28 13:47:58 +000087 /** Give read access to the LWS table
Gian Marcode691f02017-09-08 16:13:11 +010088 *
Alex Gildayc357c472018-03-21 13:54:09 +000089 * @return The lws table as unordered_map container
Gian Marcode691f02017-09-08 16:13:11 +010090 */
Anthony Barbier8b811952018-02-28 13:47:58 +000091 const std::unordered_map<std::string, cl::NDRange> &lws_table() const;
Gian Marcode691f02017-09-08 16:13:11 +010092
Gian Marco85e6f512018-02-01 16:57:48 +000093 /** Set the OpenCL kernel event
94 *
95 * @note The interceptor can use this function to store the event associated to the OpenCL kernel
96 *
97 * @param[in] kernel_event The OpenCL kernel event
98 */
99 void set_cl_kernel_event(cl_event kernel_event);
100
Alex Gildayc357c472018-03-21 13:54:09 +0000101 /** clEnqueueNDRangeKernel symbol */
Anthony Barbier8db83182018-02-27 13:08:00 +0000102 std::function<decltype(clEnqueueNDRangeKernel)> real_clEnqueueNDRangeKernel;
103
Anthony Barbier8b811952018-02-28 13:47:58 +0000104 /** Load the LWS table from file
105 *
106 * @param[in] filename Load the LWS table from this file.(Must exist)
107 */
108 void load_from_file(const std::string &filename);
109
110 /** Save the content of the LWS table to file
111 *
112 * @param[in] filename Save the LWS table to this file. (Content will be overwritten)
113 */
114 void save_to_file(const std::string &filename) const;
115
Anthony Barbier8db83182018-02-27 13:08:00 +0000116 // Inherited methods overridden:
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000117 void tune_kernel_static(ICLKernel &kernel) override;
118 void tune_kernel_dynamic(ICLKernel &kernel) override;
Gian Marco85e6f512018-02-01 16:57:48 +0000119
Anthony Barbierf5dcf792018-02-28 18:04:45 +0000120 /** Is the kernel_event set ?
121 *
122 * @return true if the kernel_event is set.
123 */
124 bool kernel_event_is_set() const;
125
Gian Marcode691f02017-09-08 16:13:11 +0100126private:
127 /** Find optimal LWS using brute-force approach
128 *
129 * @param[in] kernel OpenCL kernel to be tuned with LWS
130 *
131 * @return The optimal LWS to use
132 */
133 cl::NDRange find_optimal_lws(ICLKernel &kernel);
134
135 std::unordered_map<std::string, cl::NDRange> _lws_table;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100136 cl::Event _kernel_event;
137 bool _tune_new_kernels;
138 CLTunerMode _tuner_mode;
Gian Marco85e6f512018-02-01 16:57:48 +0000139};
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000140} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000141#endif /*ARM_COMPUTE_CLTUNER_H */