blob: 7f63078192e15ba08518220d605b7078415b94c6 [file] [log] [blame]
Manuel Bottinib56c1752020-11-18 17:56:30 +00001/*
2 * Copyright (c) 2019-2021 Arm Limited.
3 *
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
34/** Non instantiable base class for Tuning parameters combinations that use Index2Cooard mapping */
35class CLTuningParametersList : public ICLTuningParametersList
36{
37protected:
38 /* Shape of 3-D search space */
39 TensorShape search_space_shape{ 0, 0, 0 };
40
41 /** Constructor */
42 CLTuningParametersList() = default;
43 /** Copy Constructor */
44 CLTuningParametersList(const CLTuningParametersList &) = default;
45 /** Move Constructor */
46 CLTuningParametersList(CLTuningParametersList &&) noexcept(true) = default;
47 /** Assignment */
48 CLTuningParametersList &operator=(const CLTuningParametersList &) = default;
49 /** Move Assignment */
50 CLTuningParametersList &operator=(CLTuningParametersList &&) noexcept(true) = default;
51 /** Destructor */
52 virtual ~CLTuningParametersList() = default;
53
54 // Inherited methods overridden:
55 virtual size_t size() override;
56};
57
58/** Exhaustive list of all possible Tuning parameters (lws) values */
59class CLTuningParametersListExhaustive : public CLTuningParametersList
60{
61public:
62 /** Prevent default constructor calls */
63 CLTuningParametersListExhaustive() = delete;
64 /** Constructor */
65 CLTuningParametersListExhaustive(const cl::NDRange &gws);
66 /** Copy Constructor */
67 CLTuningParametersListExhaustive(const CLTuningParametersListExhaustive &) = default;
68 /** Move Constructor */
69 CLTuningParametersListExhaustive(CLTuningParametersListExhaustive &&) noexcept(true) = default;
70 /** Assignment */
71 CLTuningParametersListExhaustive &operator=(const CLTuningParametersListExhaustive &) = default;
72 /** Move Assignment */
73 CLTuningParametersListExhaustive &operator=(CLTuningParametersListExhaustive &&) noexcept(true) = default;
74 /** Destructor */
75 ~CLTuningParametersListExhaustive() = default;
76
77 // Inherited methods overridden:
78 CLTuningParams operator[](size_t) override;
79};
80
81/** A subset of LWS values that are either factors of gws when gws[2] < 16 or power of 2 */
82class CLTuningParametersListNormal : public CLTuningParametersList
83{
84public:
85 /** Constructor */
86 CLTuningParametersListNormal(const cl::NDRange &gws);
87 /** Copy Constructor */
88 CLTuningParametersListNormal(const CLTuningParametersListNormal &) = default;
89 /** Move Constructor */
90 CLTuningParametersListNormal(CLTuningParametersListNormal &&) noexcept(true) = default;
91 /** Assignment */
92 CLTuningParametersListNormal &operator=(const CLTuningParametersListNormal &) = default;
93 /** Move Assignment */
94 CLTuningParametersListNormal &operator=(CLTuningParametersListNormal &&) noexcept(true) = default;
95 /** Destructor */
96 ~CLTuningParametersListNormal() = default;
97
98 // Inherited methods overridden:
99 CLTuningParams operator[](size_t) override;
100
101protected:
102 std::vector<unsigned int> _lws_x{};
103 std::vector<unsigned int> _lws_y{};
104 std::vector<unsigned int> _lws_z{};
105
106 /** Prevent default constructor calls */
107 CLTuningParametersListNormal() = default;
108
109private:
110 /** Utility function used to initialize the LWS values to test.
111 * Only the LWS values which are power of 2 or satisfy the modulo conditions with GWS are taken into account by the CLTuner
112 *
113 * @param[in, out] lws Vector of LWS to test
114 * @param[in] gws Size of the specific GWS
115 * @param[in] lws_max Max LWS value allowed to be tested
116 * @param[in] mod_let_one True if the results of the modulo operation between gws and the lws can be less than one.
117 */
118 void initialize_lws_values(std::vector<unsigned int> &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one);
119};
120
121/** A minimal subset of LWS values that only have 1,2 and 4/8 */
122class CLTuningParametersListRapid : public CLTuningParametersListNormal
123{
124public:
125 /** Prevent default constructor calls */
126 CLTuningParametersListRapid() = delete;
127 /** Constructor */
128 CLTuningParametersListRapid(const cl::NDRange &gws);
129 /** Copy Constructor */
130 CLTuningParametersListRapid(const CLTuningParametersListRapid &) = default;
131 /** Move Constructor */
132 CLTuningParametersListRapid(CLTuningParametersListRapid &&) noexcept(true) = default;
133 /** Assignment */
134 CLTuningParametersListRapid &operator=(const CLTuningParametersListRapid &) = default;
135 /** Move Assignment */
136 CLTuningParametersListRapid &operator=(CLTuningParametersListRapid &&) noexcept(true) = default;
137 /** Destructor */
138 virtual ~CLTuningParametersListRapid() = default;
139
140private:
141 /** Utility function used to initialize the LWS values to test.
142 * Only the LWS values that have 1,2 and 4/8 for each dimension are taken into account by the CLTuner
143 *
144 * @param[in, out] lws Vector of LWS to test
145 * @param[in] lws_max Max LWS value allowed to be tested
146 */
147 void initialize_lws_values(std::vector<unsigned int> &lws, unsigned int lws_max);
148};
149
150size_t CLTuningParametersList::size()
151{
152 return search_space_shape.total_size();
153}
154
155CLTuningParams CLTuningParametersListExhaustive::operator[](size_t index)
156{
157 ARM_COMPUTE_ERROR_ON(index >= size());
158 auto coords = index2coords(search_space_shape, index);
159 return CLTuningParams(coords[0] + 1U, coords[1] + 1U, coords[2] + 1U);
160}
161
162CLTuningParametersListExhaustive::CLTuningParametersListExhaustive(const cl::NDRange &gws)
163{
164 ARM_COMPUTE_UNUSED(gws);
165 search_space_shape = TensorShape(max_lws_supported_x,
166 max_lws_supported_y,
167 max_lws_supported_z);
168}
169
170CLTuningParams CLTuningParametersListNormal::operator[](size_t index)
171{
172 ARM_COMPUTE_ERROR_ON(index >= size());
173 auto coords = index2coords(search_space_shape, index);
174 return CLTuningParams(_lws_x[coords[0]], _lws_y[coords[1]], _lws_z[coords[2]]);
175}
176
177CLTuningParametersListNormal::CLTuningParametersListNormal(const cl::NDRange &gws)
178{
179 auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), max_lws_supported_x);
180 auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), max_lws_supported_y);
181 auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), max_lws_supported_z);
182
183 // Initialize the LWS values to test
184 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
185 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
186 initialize_lws_values(_lws_z, gws[2], lws_z_max, false);
187
188 search_space_shape = TensorShape(_lws_x.size(), _lws_y.size(), _lws_z.size());
189}
190
191void CLTuningParametersListNormal::initialize_lws_values(std::vector<unsigned int> &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one)
192{
193 lws.push_back(1);
194
195 for(unsigned int i = 2; i <= lws_max; ++i)
196 {
197 // Power of two condition
198 const bool is_power_of_two = (i & (i - 1)) == 0;
199
200 // Condition for the module accordingly with the mod_let_one flag
201 const bool mod_cond = mod_let_one ? (gws % i) <= 1 : (gws % i) == 0;
202
203 if(mod_cond || is_power_of_two)
204 {
205 lws.push_back(i);
206 }
207 }
208}
209
210CLTuningParametersListRapid::CLTuningParametersListRapid(const cl::NDRange &gws)
211{
212 auto lws_x_max = std::min(static_cast<unsigned int>(gws[0]), 8u); // Limit exploration to 1 - 8
213 auto lws_y_max = std::min(static_cast<unsigned int>(gws[1]), 4u); // Limit exploration to 1 - 4
214 auto lws_z_max = std::min(static_cast<unsigned int>(gws[2]), 4u); // Limit exploration to 1 - 4
215
216 // Initialize the LWS values to test
217 initialize_lws_values(_lws_x, lws_x_max);
218 initialize_lws_values(_lws_y, lws_y_max);
219 initialize_lws_values(_lws_z, lws_z_max);
220
221 search_space_shape = TensorShape(_lws_x.size(), _lws_y.size(), _lws_z.size());
222}
223
224void CLTuningParametersListRapid::initialize_lws_values(std::vector<unsigned int> &lws, unsigned int lws_max)
225{
226 lws.push_back(1);
227
228 for(unsigned int i = 2; i <= lws_max; i *= 4)
229 {
230 lws.push_back(i);
231 }
232}
233
234std::unique_ptr<ICLTuningParametersList> get_tuning_parameters_list(CLTunerMode mode, const cl::NDRange &gws)
235{
236 switch(mode)
237 {
238 case CLTunerMode::EXHAUSTIVE:
239 return std::make_unique<CLTuningParametersListExhaustive>(gws);
240 case CLTunerMode::NORMAL:
241 return std::make_unique<CLTuningParametersListNormal>(gws);
242 case CLTunerMode::RAPID:
243 return std::make_unique<CLTuningParametersListRapid>(gws);
244 default:
245 return nullptr;
246 }
247}
248} // namespace cl_tuner
249} // namespace arm_compute