blob: 6f3e32491a873c652b104afdd14c97327079aa73 [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{
30constexpr unsigned int max_lws_supported_x{ 64u };
31constexpr unsigned int max_lws_supported_y{ 32u };
32constexpr unsigned int max_lws_supported_z{ 32u };
33
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 */
39 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.
44 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 */
119 void initialize_lws_values(std::vector<unsigned int> &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one);
120};
121
122/** A minimal subset of LWS values that only have 1,2 and 4/8 */
123class CLTuningParametersListRapid : public CLTuningParametersListNormal
124{
125public:
126 /** Prevent default constructor calls */
127 CLTuningParametersListRapid() = delete;
128 /** Constructor */
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000129 CLTuningParametersListRapid(const cl::NDRange &gws, CLTuningInfo tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000130 /** Copy Constructor */
131 CLTuningParametersListRapid(const CLTuningParametersListRapid &) = default;
132 /** Move Constructor */
133 CLTuningParametersListRapid(CLTuningParametersListRapid &&) noexcept(true) = default;
134 /** Assignment */
135 CLTuningParametersListRapid &operator=(const CLTuningParametersListRapid &) = default;
136 /** Move Assignment */
137 CLTuningParametersListRapid &operator=(CLTuningParametersListRapid &&) noexcept(true) = default;
138 /** Destructor */
139 virtual ~CLTuningParametersListRapid() = default;
140
141private:
142 /** Utility function used to initialize the LWS values to test.
143 * Only the LWS values that have 1,2 and 4/8 for each dimension are taken into account by the CLTuner
144 *
145 * @param[in, out] lws Vector of LWS to test
146 * @param[in] lws_max Max LWS value allowed to be tested
147 */
148 void initialize_lws_values(std::vector<unsigned int> &lws, unsigned int lws_max);
149};
150
151size_t CLTuningParametersList::size()
152{
153 return search_space_shape.total_size();
154}
155
156CLTuningParams CLTuningParametersListExhaustive::operator[](size_t index)
157{
158 ARM_COMPUTE_ERROR_ON(index >= size());
159 auto coords = index2coords(search_space_shape, index);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000160 return CLTuningParams(coords[0] + 1U, coords[1] + 1U, coords[2] + 1U, static_cast<int>(coords[3]));
Manuel Bottinib56c1752020-11-18 17:56:30 +0000161}
162
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000163CLTuningParametersListExhaustive::CLTuningParametersListExhaustive(const cl::NDRange &gws, CLTuningInfo tuning_info)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000164{
SiCong Li47f177e2023-02-22 17:24:09 +0000165 const auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), max_lws_supported_x);
166 const auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), max_lws_supported_y);
167 const auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), max_lws_supported_z);
168
169 search_space_shape[0] = lws_x_max;
170 search_space_shape[1] = lws_y_max;
171 search_space_shape[2] = lws_z_max;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000172 search_space_shape[3] = 1;
173 if(tuning_info.tune_wbsm)
174 {
175 _wbsm = { -3, -2, -1, 0, 1, 2, 3 };
176 search_space_shape[3] = _wbsm.size();
177 }
Manuel Bottinib56c1752020-11-18 17:56:30 +0000178}
179
180CLTuningParams CLTuningParametersListNormal::operator[](size_t index)
181{
182 ARM_COMPUTE_ERROR_ON(index >= size());
183 auto coords = index2coords(search_space_shape, index);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000184 return CLTuningParams(_lws_x[coords[0]], _lws_y[coords[1]], _lws_z[coords[2]], _wbsm[coords[3]]);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000185}
186
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000187CLTuningParametersListNormal::CLTuningParametersListNormal(const cl::NDRange &gws, CLTuningInfo tuning_info)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000188{
SiCong Li47f177e2023-02-22 17:24:09 +0000189 const auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), max_lws_supported_x);
190 const auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), max_lws_supported_y);
191 const auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), max_lws_supported_z);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000192
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000193 // Initialize the tuning parameters values to test
194 _lws_x = {};
195 _lws_y = {};
196 _lws_z = {};
Manuel Bottinib56c1752020-11-18 17:56:30 +0000197 initialize_lws_values(_lws_x, gws[0], lws_x_max, gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16
198 initialize_lws_values(_lws_y, gws[1], lws_y_max, gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16
199 initialize_lws_values(_lws_z, gws[2], lws_z_max, false);
200
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000201 search_space_shape[0] = _lws_x.size();
202 search_space_shape[1] = _lws_y.size();
203 search_space_shape[2] = _lws_z.size();
204 search_space_shape[3] = 1;
205 if(tuning_info.tune_wbsm)
206 {
207 _wbsm = { -2, -1, 0, 1, 2 };
208 search_space_shape[3] = _wbsm.size();
209 }
Manuel Bottinib56c1752020-11-18 17:56:30 +0000210}
211
212void CLTuningParametersListNormal::initialize_lws_values(std::vector<unsigned int> &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one)
213{
214 lws.push_back(1);
215
216 for(unsigned int i = 2; i <= lws_max; ++i)
217 {
218 // Power of two condition
219 const bool is_power_of_two = (i & (i - 1)) == 0;
220
221 // Condition for the module accordingly with the mod_let_one flag
222 const bool mod_cond = mod_let_one ? (gws % i) <= 1 : (gws % i) == 0;
223
224 if(mod_cond || is_power_of_two)
225 {
226 lws.push_back(i);
227 }
228 }
229}
230
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000231CLTuningParametersListRapid::CLTuningParametersListRapid(const cl::NDRange &gws, CLTuningInfo tuning_info)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000232{
SiCong Li47f177e2023-02-22 17:24:09 +0000233 const auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), 8u); // Limit exploration to 1 - 8
234 const auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), 4u); // Limit exploration to 1 - 4
235 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 +0000236
237 // Initialize the LWS values to test
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000238 _lws_x = {};
239 _lws_y = {};
240 _lws_z = {};
Manuel Bottinib56c1752020-11-18 17:56:30 +0000241 initialize_lws_values(_lws_x, lws_x_max);
242 initialize_lws_values(_lws_y, lws_y_max);
243 initialize_lws_values(_lws_z, lws_z_max);
244
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000245 search_space_shape[0] = _lws_x.size();
246 search_space_shape[1] = _lws_y.size();
247 search_space_shape[2] = _lws_z.size();
248 search_space_shape[3] = 1;
249 if(tuning_info.tune_wbsm)
250 {
251 _wbsm = { -1, 0, 1 };
252 search_space_shape[3] = _wbsm.size();
253 }
Manuel Bottinib56c1752020-11-18 17:56:30 +0000254}
255
256void CLTuningParametersListRapid::initialize_lws_values(std::vector<unsigned int> &lws, unsigned int lws_max)
257{
258 lws.push_back(1);
259
260 for(unsigned int i = 2; i <= lws_max; i *= 4)
261 {
262 lws.push_back(i);
263 }
264}
265
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000266std::unique_ptr<ICLTuningParametersList> get_tuning_parameters_list(CLTuningInfo tuning_info, const cl::NDRange &gws)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000267{
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000268 switch(tuning_info.tuner_mode)
Manuel Bottinib56c1752020-11-18 17:56:30 +0000269 {
270 case CLTunerMode::EXHAUSTIVE:
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000271 return std::make_unique<CLTuningParametersListExhaustive>(gws, tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000272 case CLTunerMode::NORMAL:
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000273 return std::make_unique<CLTuningParametersListNormal>(gws, tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000274 case CLTunerMode::RAPID:
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000275 return std::make_unique<CLTuningParametersListRapid>(gws, tuning_info);
Manuel Bottinib56c1752020-11-18 17:56:30 +0000276 default:
277 return nullptr;
278 }
279}
280} // namespace cl_tuner
281} // namespace arm_compute