blob: 251848814dae0cc1fd614ae9e129a8796a6c8a22 [file] [log] [blame]
Gian Marcode691f02017-09-08 16:13:11 +01001/*
Gian Marco85e6f512018-02-01 16:57:48 +00002 * Copyright (c) 2017-2018 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 */
24#ifndef __ARM_COMPUTE_CLTUNER_H__
25#define __ARM_COMPUTE_CLTUNER_H__
26
27#include "arm_compute/core/CL/OpenCL.h"
28#include "arm_compute/runtime/CL/ICLTuner.h"
29
30#include <unordered_map>
31
32namespace arm_compute
33{
34class ICLKernel;
35
36/** Basic implementation of the OpenCL tuner interface */
37class CLTuner : public ICLTuner
38{
39public:
Anthony Barbier8db83182018-02-27 13:08:00 +000040 /** Constructor
41 *
42 * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ?
43 *
44 */
45 CLTuner(bool tune_new_kernels = true);
Gian Marcode691f02017-09-08 16:13:11 +010046
47 /** Destructor */
48 ~CLTuner() = default;
49
Anthony Barbier8db83182018-02-27 13:08:00 +000050 /* Setter for tune_new_kernels option
51 *
52 * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ?
53 */
54 void set_tune_new_kernels(bool tune_new_kernels);
55 /** Manually add a LWS for a kernel
56 *
57 * @param[in] kernel_id Unique identifiant of the kernel
58 * @param[in] optimal_lws Optimal local workgroup size to use for the given kernel
59 */
60 void add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws);
Gian Marcode691f02017-09-08 16:13:11 +010061 /** Import LWS table
62 *
63 * @param[in] lws_table The unordered_map container to import
64 */
65 void import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table);
66
67 /** Export LWS table
68 *
69 * return The lws table as unordered_map container
70 */
Anthony Barbier8db83182018-02-27 13:08:00 +000071 const std::unordered_map<std::string, cl::NDRange> &export_lws_table() const;
Gian Marcode691f02017-09-08 16:13:11 +010072
Gian Marco85e6f512018-02-01 16:57:48 +000073 /** Set the OpenCL kernel event
74 *
75 * @note The interceptor can use this function to store the event associated to the OpenCL kernel
76 *
77 * @param[in] kernel_event The OpenCL kernel event
78 */
79 void set_cl_kernel_event(cl_event kernel_event);
80
Anthony Barbier8db83182018-02-27 13:08:00 +000081 std::function<decltype(clEnqueueNDRangeKernel)> real_clEnqueueNDRangeKernel;
82
83 // Inherited methods overridden:
84 void tune_kernel(ICLKernel &kernel) override;
Gian Marco85e6f512018-02-01 16:57:48 +000085
Gian Marcode691f02017-09-08 16:13:11 +010086private:
87 /** Find optimal LWS using brute-force approach
88 *
89 * @param[in] kernel OpenCL kernel to be tuned with LWS
90 *
91 * @return The optimal LWS to use
92 */
93 cl::NDRange find_optimal_lws(ICLKernel &kernel);
94
95 std::unordered_map<std::string, cl::NDRange> _lws_table;
Gian Marco85e6f512018-02-01 16:57:48 +000096 cl::CommandQueue _queue;
97 cl::CommandQueue _queue_profiler;
98 cl::Event _kernel_event;
Anthony Barbier8db83182018-02-27 13:08:00 +000099 bool _tune_new_kernels;
Gian Marco85e6f512018-02-01 16:57:48 +0000100};
101
Anthony Barbier8db83182018-02-27 13:08:00 +0000102class CLFileTuner : public CLTuner
Gian Marco85e6f512018-02-01 16:57:48 +0000103{
104public:
Anthony Barbier8db83182018-02-27 13:08:00 +0000105 /** Constructor
Gian Marco85e6f512018-02-01 16:57:48 +0000106 *
Anthony Barbier8db83182018-02-27 13:08:00 +0000107 * @param[in] file_path File to load/store the tuning information from
108 * @param[in] update_file If true, save the new LWS table to the file on exit.
109 * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ?
Gian Marco85e6f512018-02-01 16:57:48 +0000110 */
Anthony Barbier8db83182018-02-27 13:08:00 +0000111 CLFileTuner(std::string file_path = "acl_tuner.csv", bool update_file = false, bool tune_new_kernels = false);
112
113 /** Save the content of the LWS table to file
114 */
115 void save_to_file() const;
116 /* Setter for update_file option
117 *
118 * @param[in] update_file If true, save the new LWS table to the file on exit.
119 */
120 void set_update_file(bool update_file);
121 /** Destructor
122 *
123 * Will save the LWS table to the file if the CLFileTuner was created with update_file enabled.
124 */
125 ~CLFileTuner();
126 const std::string filename;
Gian Marco85e6f512018-02-01 16:57:48 +0000127
128private:
Anthony Barbier8db83182018-02-27 13:08:00 +0000129 bool _update_file;
Gian Marcode691f02017-09-08 16:13:11 +0100130};
131}
132#endif /*__ARM_COMPUTE_CLTUNER_H__ */