blob: 5e3907f1ea924d831e5b48767fcf58addd210cc1 [file] [log] [blame]
Manuel Bottinib56c1752020-11-18 17:56:30 +00001/*
SiCong Li47f177e2023-02-22 17:24:09 +00002 * Copyright (c) 2019-2021, 2023 Arm Limited.
Manuel Bottinib56c1752020-11-18 17:56:30 +00003 *
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#include "arm_compute/runtime/CL/tuners/CLTuningParametersList.h"
25
26namespace arm_compute
27{
28namespace cl_tuner
29{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030constexpr unsigned int max_lws_supported_x{64u};
31constexpr unsigned int max_lws_supported_y{32u};
32constexpr unsigned int max_lws_supported_z{32u};
Manuel Bottinib56c1752020-11-18 17:56:30 +000033
SiCong Li47f177e2023-02-22 17:24:09 +000034/** Non instantiable base class for Tuning parameters combinations that use Index2Coord mapping */
Manuel Bottinib56c1752020-11-18 17:56:30 +000035class CLTuningParametersList : public ICLTuningParametersList
36{
37protected:
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000038 /* Shape of 4-D search space */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 TensorShape search_space_shape{0, 0, 0, 0};
40 std::vector<unsigned int> _lws_x{0};
41 std::vector<unsigned int> _lws_y{0};
42 std::vector<unsigned int> _lws_z{0};
43 std::vector<int> _wbsm{0}; /* Modify the batches size of workgroups distributed to compute units.
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000044 The value is in the range [-31,+31].
45 When 0, the runtime-selected wbs used is unmodified. */
Manuel Bottinib56c1752020-11-18 17:56:30 +000046
47 /** Constructor */
48 CLTuningParametersList() = default;
49 /** Copy Constructor */
50 CLTuningParametersList(const CLTuningParametersList &) = default;
51 /** Move Constructor */
52 CLTuningParametersList(CLTuningParametersList &&) noexcept(true) = default;
53 /** Assignment */
54 CLTuningParametersList &operator=(const CLTuningParametersList &) = default;
55 /** Move Assignment */
56 CLTuningParametersList &operator=(CLTuningParametersList &&) noexcept(true) = default;
57 /** Destructor */
58 virtual ~CLTuningParametersList() = default;
59
60 // Inherited methods overridden:
61 virtual size_t size() override;
62};
63
64/** Exhaustive list of all possible Tuning parameters (lws) values */
65class CLTuningParametersListExhaustive : public CLTuningParametersList
66{
67public:
68 /** Prevent default constructor calls */
69 CLTuningParametersListExhaustive() = delete;
70 /** Constructor */
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000071 CLTuningParametersListExhaustive(const cl::NDRange &gws, CLTuningInfo tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +000072 /** Copy Constructor */
73 CLTuningParametersListExhaustive(const CLTuningParametersListExhaustive &) = default;
74 /** Move Constructor */
75 CLTuningParametersListExhaustive(CLTuningParametersListExhaustive &&) noexcept(true) = default;
76 /** Assignment */
77 CLTuningParametersListExhaustive &operator=(const CLTuningParametersListExhaustive &) = default;
78 /** Move Assignment */
79 CLTuningParametersListExhaustive &operator=(CLTuningParametersListExhaustive &&) noexcept(true) = default;
80 /** Destructor */
81 ~CLTuningParametersListExhaustive() = default;
82
83 // Inherited methods overridden:
84 CLTuningParams operator[](size_t) override;
85};
86
87/** A subset of LWS values that are either factors of gws when gws[2] < 16 or power of 2 */
88class CLTuningParametersListNormal : public CLTuningParametersList
89{
90public:
91 /** Constructor */
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000092 CLTuningParametersListNormal(const cl::NDRange &gws, CLTuningInfo tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +000093 /** Copy Constructor */
94 CLTuningParametersListNormal(const CLTuningParametersListNormal &) = default;
95 /** Move Constructor */
96 CLTuningParametersListNormal(CLTuningParametersListNormal &&) noexcept(true) = default;
97 /** Assignment */
98 CLTuningParametersListNormal &operator=(const CLTuningParametersListNormal &) = default;
99 /** Move Assignment */
100 CLTuningParametersListNormal &operator=(CLTuningParametersListNormal &&) noexcept(true) = default;
101 /** Destructor */
102 ~CLTuningParametersListNormal() = default;
103
104 // Inherited methods overridden:
105 CLTuningParams operator[](size_t) override;
106
Manuel Bottinib56c1752020-11-18 17:56:30 +0000107 /** Prevent default constructor calls */
108 CLTuningParametersListNormal() = default;
109
110private:
111 /** Utility function used to initialize the LWS values to test.
112 * Only the LWS values which are power of 2 or satisfy the modulo conditions with GWS are taken into account by the CLTuner
113 *
114 * @param[in, out] lws Vector of LWS to test
115 * @param[in] gws Size of the specific GWS
116 * @param[in] lws_max Max LWS value allowed to be tested
117 * @param[in] mod_let_one True if the results of the modulo operation between gws and the lws can be less than one.
118 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 void
120 initialize_lws_values(std::vector<unsigned int> &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000121};
122
123/** A minimal subset of LWS values that only have 1,2 and 4/8 */
124class CLTuningParametersListRapid : public CLTuningParametersListNormal
125{
126public:
127 /** Prevent default constructor calls */
128 CLTuningParametersListRapid() = delete;
129 /** Constructor */
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000130 CLTuningParametersListRapid(const cl::NDRange &gws, CLTuningInfo tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000131 /** Copy Constructor */
132 CLTuningParametersListRapid(const CLTuningParametersListRapid &) = default;
133 /** Move Constructor */
134 CLTuningParametersListRapid(CLTuningParametersListRapid &&) noexcept(true) = default;
135 /** Assignment */
136 CLTuningParametersListRapid &operator=(const CLTuningParametersListRapid &) = default;
137 /** Move Assignment */
138 CLTuningParametersListRapid &operator=(CLTuningParametersListRapid &&) noexcept(true) = default;
139 /** Destructor */
140 virtual ~CLTuningParametersListRapid() = default;
141
142private:
143 /** Utility function used to initialize the LWS values to test.
144 * Only the LWS values that have 1,2 and 4/8 for each dimension are taken into account by the CLTuner
145 *
146 * @param[in, out] lws Vector of LWS to test
147 * @param[in] lws_max Max LWS value allowed to be tested
148 */
149 void initialize_lws_values(std::vector<unsigned int> &lws, unsigned int lws_max);
150};
151
152size_t CLTuningParametersList::size()
153{
154 return search_space_shape.total_size();
155}
156
157CLTuningParams CLTuningParametersListExhaustive::operator[](size_t index)
158{
159 ARM_COMPUTE_ERROR_ON(index >= size());
160 auto coords = index2coords(search_space_shape, index);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000161 return CLTuningParams(coords[0] + 1U, coords[1] + 1U, coords[2] + 1U, static_cast<int>(coords[3]));
Manuel Bottinib56c1752020-11-18 17:56:30 +0000162}
163
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000164CLTuningParametersListExhaustive::CLTuningParametersListExhaustive(const cl::NDRange &gws, CLTuningInfo tuning_info)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000165{
SiCong Li47f177e2023-02-22 17:24:09 +0000166 const auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), max_lws_supported_x);
167 const auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), max_lws_supported_y);
168 const auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), max_lws_supported_z);
169
170 search_space_shape[0] = lws_x_max;
171 search_space_shape[1] = lws_y_max;
172 search_space_shape[2] = lws_z_max;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000173 search_space_shape[3] = 1;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100174 if (tuning_info.tune_wbsm)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000175 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100176 _wbsm = {-3, -2, -1, 0, 1, 2, 3};
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000177 search_space_shape[3] = _wbsm.size();
178 }
Manuel Bottinib56c1752020-11-18 17:56:30 +0000179}
180
181CLTuningParams CLTuningParametersListNormal::operator[](size_t index)
182{
183 ARM_COMPUTE_ERROR_ON(index >= size());
184 auto coords = index2coords(search_space_shape, index);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000185 return CLTuningParams(_lws_x[coords[0]], _lws_y[coords[1]], _lws_z[coords[2]], _wbsm[coords[3]]);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000186}
187
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000188CLTuningParametersListNormal::CLTuningParametersListNormal(const cl::NDRange &gws, CLTuningInfo tuning_info)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000189{
SiCong Li47f177e2023-02-22 17:24:09 +0000190 const auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), max_lws_supported_x);
191 const auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), max_lws_supported_y);
192 const auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), max_lws_supported_z);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000193
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000194 // Initialize the tuning parameters values to test
195 _lws_x = {};
196 _lws_y = {};
197 _lws_z = {};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198 initialize_lws_values(_lws_x, gws[0], lws_x_max,
199 gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16
200 initialize_lws_values(_lws_y, gws[1], lws_y_max,
201 gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16
Manuel Bottinib56c1752020-11-18 17:56:30 +0000202 initialize_lws_values(_lws_z, gws[2], lws_z_max, false);
203
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000204 search_space_shape[0] = _lws_x.size();
205 search_space_shape[1] = _lws_y.size();
206 search_space_shape[2] = _lws_z.size();
207 search_space_shape[3] = 1;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100208 if (tuning_info.tune_wbsm)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000209 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100210 _wbsm = {-2, -1, 0, 1, 2};
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000211 search_space_shape[3] = _wbsm.size();
212 }
Manuel Bottinib56c1752020-11-18 17:56:30 +0000213}
214
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100215void CLTuningParametersListNormal::initialize_lws_values(std::vector<unsigned int> &lws,
216 unsigned int gws,
217 unsigned int lws_max,
218 bool mod_let_one)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000219{
220 lws.push_back(1);
221
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100222 for (unsigned int i = 2; i <= lws_max; ++i)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000223 {
224 // Power of two condition
225 const bool is_power_of_two = (i & (i - 1)) == 0;
226
227 // Condition for the module accordingly with the mod_let_one flag
228 const bool mod_cond = mod_let_one ? (gws % i) <= 1 : (gws % i) == 0;
229
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100230 if (mod_cond || is_power_of_two)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000231 {
232 lws.push_back(i);
233 }
234 }
235}
236
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000237CLTuningParametersListRapid::CLTuningParametersListRapid(const cl::NDRange &gws, CLTuningInfo tuning_info)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000238{
SiCong Li47f177e2023-02-22 17:24:09 +0000239 const auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), 8u); // Limit exploration to 1 - 8
240 const auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), 4u); // Limit exploration to 1 - 4
241 const auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), 4u); // Limit exploration to 1 - 4
Manuel Bottinib56c1752020-11-18 17:56:30 +0000242
243 // Initialize the LWS values to test
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000244 _lws_x = {};
245 _lws_y = {};
246 _lws_z = {};
Manuel Bottinib56c1752020-11-18 17:56:30 +0000247 initialize_lws_values(_lws_x, lws_x_max);
248 initialize_lws_values(_lws_y, lws_y_max);
249 initialize_lws_values(_lws_z, lws_z_max);
250
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000251 search_space_shape[0] = _lws_x.size();
252 search_space_shape[1] = _lws_y.size();
253 search_space_shape[2] = _lws_z.size();
254 search_space_shape[3] = 1;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100255 if (tuning_info.tune_wbsm)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000256 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100257 _wbsm = {-1, 0, 1};
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000258 search_space_shape[3] = _wbsm.size();
259 }
Manuel Bottinib56c1752020-11-18 17:56:30 +0000260}
261
262void CLTuningParametersListRapid::initialize_lws_values(std::vector<unsigned int> &lws, unsigned int lws_max)
263{
264 lws.push_back(1);
265
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100266 for (unsigned int i = 2; i <= lws_max; i *= 4)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000267 {
268 lws.push_back(i);
269 }
270}
271
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000272std::unique_ptr<ICLTuningParametersList> get_tuning_parameters_list(CLTuningInfo tuning_info, const cl::NDRange &gws)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000273{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100274 switch (tuning_info.tuner_mode)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000275 {
276 case CLTunerMode::EXHAUSTIVE:
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000277 return std::make_unique<CLTuningParametersListExhaustive>(gws, tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000278 case CLTunerMode::NORMAL:
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000279 return std::make_unique<CLTuningParametersListNormal>(gws, tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000280 case CLTunerMode::RAPID:
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000281 return std::make_unique<CLTuningParametersListRapid>(gws, tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000282 default:
283 return nullptr;
284 }
285}
286} // namespace cl_tuner
287} // namespace arm_compute