blob: 5ca210a11262f35b6d2c1adfcbfb7b3d68d86890 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou5237e012018-01-17 09:40:27 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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_WINDOW_H__
25#define __ARM_COMPUTE_WINDOW_H__
26
27#include <algorithm>
28#include <array>
29#include <cstddef>
30
31#include "arm_compute/core/Coordinates.h"
32#include "arm_compute/core/Error.h"
33#include "arm_compute/core/ITensorInfo.h"
34#include "arm_compute/core/Utils.h"
35
36namespace arm_compute
37{
38/** Describe a multidimensional execution window. */
39class Window
40{
41public:
42 /** Alias for dimension 0 also known as X dimension */
43 static constexpr size_t DimX = 0;
44 /** Alias for dimension 1 also known as Y dimension */
45 static constexpr size_t DimY = 1;
46 /** Alias for dimension 2 also known as Z dimension */
47 static constexpr size_t DimZ = 2;
48
49 /** Default constructor: create a window containing a single element. */
50 constexpr Window()
Moritz Pflanzerc186b572017-09-07 09:48:04 +010051 : _dims()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 {
53 }
54 /** Copy constructor
55 *
56 * @param[in] src Copy the values from src to a new object
57 */
58 Window(const Window &src);
59
60 /** Describe one of the image's dimensions with a start, end and step.
61 *
62 * Iteration through the elements of the dimension is done like this:
63 * for(int v = start(); v < end(); v += step())
64 * {
65 * ...
66 * }
67 */
68 class Dimension
69 {
70 public:
71 /** Constructor, by default creates a dimension of 1.
72 *
73 * @param[in] start Start of the dimension
74 * @param[in] end End of the dimension
75 * @param[in] step Step between two elements of the dimension when iterating.
76 *
77 */
78 constexpr Dimension(int start = 0, int end = 1, int step = 1)
79 : _start(start), _end(end), _step(step)
80 {
81 }
82 /** Default assignment operator to allow dimensions to be copied */
83 Dimension &operator=(const Dimension &d) = default;
84 /** Return the start of the dimension */
85 constexpr int start() const
86 {
87 return _start;
88 }
89 /** Return the end of the dimension */
90 constexpr int end() const
91 {
92 return _end;
93 }
94 /** Return the step of the dimension */
95 constexpr int step() const
96 {
97 return _step;
98 }
99 /** Set the dimension's step
100 *
101 * @param[in] step The new step
102 */
103 void set_step(int step)
104 {
105 _step = step;
106 }
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000107 /** Set the dimension's end
108 *
109 * @param[in] end The new end
110 */
111 void set_end(int end)
112 {
113 _end = end;
114 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
116 private:
117 int _start; /**< Start of the dimension */
118 int _end; /**< End of the dimension */
119 int _step;
120 };
121
122 /** Read only access to a given dimension of the window
123 *
124 * @note Precondition: dimension < Coordinates::num_max_dimensions
125 *
126 * @param[in] dimension The dimension to access
127 *
128 * @return The requested dimension
129 */
130 constexpr const Dimension &operator[](size_t dimension) const;
131
132 /** Alias to access the first dimension of the window
133 *
134 * @return First dimension of the window
135 */
136 constexpr const Dimension &x() const
137 {
138 return _dims.at(Window::DimX);
139 }
140
141 /** Alias to access the second dimension of the window
142 *
143 * @return Second dimension of the window
144 */
145 constexpr const Dimension &y() const
146 {
147 return _dims.at(Window::DimY);
148 }
149
150 /** Alias to access the third dimension of the window
151 *
152 * @return Third dimension of the window
153 */
154 constexpr const Dimension &z() const
155 {
156 return _dims.at(Window::DimZ);
157 }
158
159 /** Set the values of a given dimension
160 *
161 * @param[in] dimension The dimension to set
162 * @param[in] dim The values to set the dimension to
163 */
164 void set(size_t dimension, const Dimension &dim);
165
166 /** Use the tensor's dimensions to fill the window dimensions.
167 *
SiCong Li86b53332017-08-23 11:02:43 +0100168 * @param[in] shape @ref TensorShape to copy the dimensions from.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 * @param[in] first_dimension Only copy dimensions which are greater or equal to this value.
170 */
SiCong Li86b53332017-08-23 11:02:43 +0100171 void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension = Window::DimX);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172
173 /** Shift the values of a given dimension by the given shift_value
174 *
175 * @param[in] dimension The dimension to shift
176 * @param[in] shift_value Value to shift the start and end values of.
177 */
178 void shift(size_t dimension, int shift_value);
179
Michalis Spyrou995f5522018-01-29 13:43:35 +0000180 /** Shift down all the dimensions of a window
181 *
182 * i.e new_dims[n] = old_dims[n+shift_value].
183 *
184 * @param[in] shift_value Number of dimensions to shift the window by.
185 *
186 * @return The window with the shifted dimensions.
187 */
188 Window shift_dimensions(unsigned int shift_value) const;
189
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 /** Adjust the start or end of a given dimension by the given value
191 *
192 * @param[in] dimension The dimension to adjust
193 * @param[in] adjust_value The adjusted value.
194 * @param[in] is_at_start The flag to indicate whether adjust the start or end of the dimension.
195 */
196 void adjust(size_t dimension, int adjust_value, bool is_at_start);
197
198 /** Scale the values of a given dimension by the given scale_value
199 *
200 * @note The end of the window is rounded up to be a multiple of step after the scaling.
201 *
202 * @param[in] dimension The dimension to scale
203 * @param[in] scale_value Value to scale the start, end and step values of.
204 */
205 void scale(size_t dimension, float scale_value);
206
207 /** Set the step of a given dimension.
208 *
209 * @param[in] dimension Dimension to update
210 * @param[in] step The new dimension's step value
211 */
212 void set_dimension_step(size_t dimension, int step);
213
214 /** Will validate all the window's dimensions' values when asserts are enabled
215 *
216 * No-op when asserts are disabled
217 */
218 void validate() const;
219
220 /** Return the number of iterations needed to iterate through a given dimension
221 *
222 * @param[in] dimension The requested dimension
223 *
224 * @return The number of iterations
225 */
226 constexpr size_t num_iterations(size_t dimension) const;
227
228 /** Split a window into a set of sub windows along a given dimension
229 *
230 * For example to split a window into 3 sub-windows along the Y axis, you would have to do:<br/>
231 * Window sub0 = window.split_window( 1, 0, 3);<br/>
232 * Window sub1 = window.split_window( 1, 1, 3);<br/>
233 * Window sub2 = window.split_window( 1, 2, 3);<br/>
234 *
235 * @param[in] dimension Dimension along which the split will be performed
236 * @param[in] id Id of the sub-window to return. Must be in the range (0, total-1)
237 * @param[in] total Total number of sub-windows the window will be split into.
238 *
239 * @return The subwindow "id" out of "total"
240 */
241 Window split_window(size_t dimension, size_t id, size_t total) const;
242 /** First 1D slice of the window
243 *
244 * @return The first slice of the window.
245 */
246 Window first_slice_window_1D() const
247 {
248 return first_slice_window<1>();
249 };
250 /** First 2D slice of the window
251 *
252 * @return The first slice of the window.
253 */
254 Window first_slice_window_2D() const
255 {
256 return first_slice_window<2>();
257 };
258 /** First 3D slice of the window
259 *
260 * @return The first slice of the window.
261 */
262 Window first_slice_window_3D() const
263 {
264 return first_slice_window<3>();
265 };
Michalis Spyrou5237e012018-01-17 09:40:27 +0000266 /** First 4D slice of the window
267 *
268 * @return The first slice of the window.
269 */
270 Window first_slice_window_4D() const
271 {
272 return first_slice_window<4>();
273 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100274 /** Slide the passed 1D window slice.
275 *
276 * If slice contains the last slice then it will remain unchanged and false will be returned.
277 *
278 * @param[in,out] slice Current slice, to be updated to the next slice.
279 *
280 * @return true if slice contains a new slice, false if slice already contained the last slice
281 */
282 bool slide_window_slice_1D(Window &slice) const
283 {
284 return slide_window_slice<1>(slice);
285 }
286 /** Slide the passed 2D window slice.
287 *
288 * If slice contains the last slice then it will remain unchanged and false will be returned.
289 *
290 * @param[in,out] slice Current slice, to be updated to the next slice.
291 *
292 * @return true if slice contains a new slice, false if slice already contained the last slice
293 */
294 bool slide_window_slice_2D(Window &slice) const
295 {
296 return slide_window_slice<2>(slice);
297 }
298 /** Slide the passed 3D window slice.
299 *
300 * If slice contains the last slice then it will remain unchanged and false will be returned.
301 *
302 * @param[in,out] slice Current slice, to be updated to the next slice.
303 *
304 * @return true if slice contains a new slice, false if slice already contained the last slice
305 */
306 bool slide_window_slice_3D(Window &slice) const
307 {
308 return slide_window_slice<3>(slice);
309 }
310 /** Slide the passed 4D window slice.
311 *
312 * If slice contains the last slice then it will remain unchanged and false will be returned.
313 *
314 * @param[in,out] slice Current slice, to be updated to the next slice.
315 *
316 * @return true if slice contains a new slice, false if slice already contained the last slice
317 */
318 bool slide_window_slice_4D(Window &slice) const
319 {
320 return slide_window_slice<4>(slice);
321 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000323 /* Collapse the dimensions between @p first and @p last if possible.
324 *
325 * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
326 *
327 * @param[in] full_window Full window @p window has been created from.
328 * @param[in] first Start dimension into which the following are collapsed.
329 * @param[in] last End (exclusive) dimension to collapse.
330 * @param[out] has_collapsed (Optional) Whether the window was collapsed.
331 *
332 * @return Collapsed window.
333 */
334 Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed = nullptr) const;
335
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100336 /* Collapse the dimensions higher than @p first if possible.
337 *
338 * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
339 *
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000340 * @param[in] full_window Full window @p window has been created from.
341 * @param[in] first Start dimension into which the following are collapsed.
342 * @param[out] has_collapsed (Optional) Whether the window was collapsed.
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100343 *
344 * @return Collapsed window.
345 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000346 Window collapse_if_possible(const Window &full_window, size_t first, bool *has_collapsed = nullptr) const
347 {
348 return collapse_if_possible(full_window, first, Coordinates::num_max_dimensions, has_collapsed);
349 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100350
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000351 /* Collapse the dimensions between @p first and @p last.
Michalis Spyrou5237e012018-01-17 09:40:27 +0000352 *
353 * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
354 *
355 * @param[in] full_window Full window @p window has been created from.
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000356 * @param[in] first Start dimension into which the following are collapsed.
357 * @param[in] last End (exclusive) dimension to collapse.
Michalis Spyrou5237e012018-01-17 09:40:27 +0000358 *
359 * @return Collapsed window if successful.
360 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000361 Window collapse(const Window &full_window, size_t first, size_t last = Coordinates::num_max_dimensions) const;
362
363 /* Don't advance in the dimension where @p shape is less equal to 1.
364 *
365 * @param[in] shape A TensorShape.
366 *
367 * @return Broadcast window.
368 */
369 Window broadcast_if_dimension_le_one(const TensorShape &shape) const;
370
371 /* Don't advance in the dimension where shape of @p info is less equal to 1.
372 *
373 * @param[in] info An ITensorInfo.
374 *
375 * @return Broadcast window.
376 */
377 Window broadcast_if_dimension_le_one(const ITensorInfo &info) const
378 {
379 return broadcast_if_dimension_le_one(info.tensor_shape());
380 }
Michalis Spyrou5237e012018-01-17 09:40:27 +0000381
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100382private:
383 /** First slice of the window
384 *
385 * @return The first slice of the window.
386 */
387 template <unsigned int window_dimension>
388 Window first_slice_window() const;
389
390 /** Slide the passed window slice.
391 *
392 * If slice contains the last slice then it will remain unchanged and false will be returned.
393 *
394 * @param[in,out] slice Current slice, to be updated to the next slice.
395 *
396 * @return true if slice contains a new slice, false if slice already contained the last slice
397 */
398 template <unsigned int window_dimension>
399 bool slide_window_slice(Window &slice) const;
400
401private:
402 std::array<Dimension, Coordinates::num_max_dimensions> _dims;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403};
404}
405#include "Window.inl"
406#endif /*__ARM_COMPUTE_WINDOW_H__ */