blob: cca12c9efe793b01b903576af3da7477422a027c [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
180 /** Adjust the start or end of a given dimension by the given value
181 *
182 * @param[in] dimension The dimension to adjust
183 * @param[in] adjust_value The adjusted value.
184 * @param[in] is_at_start The flag to indicate whether adjust the start or end of the dimension.
185 */
186 void adjust(size_t dimension, int adjust_value, bool is_at_start);
187
188 /** Scale the values of a given dimension by the given scale_value
189 *
190 * @note The end of the window is rounded up to be a multiple of step after the scaling.
191 *
192 * @param[in] dimension The dimension to scale
193 * @param[in] scale_value Value to scale the start, end and step values of.
194 */
195 void scale(size_t dimension, float scale_value);
196
197 /** Set the step of a given dimension.
198 *
199 * @param[in] dimension Dimension to update
200 * @param[in] step The new dimension's step value
201 */
202 void set_dimension_step(size_t dimension, int step);
203
204 /** Will validate all the window's dimensions' values when asserts are enabled
205 *
206 * No-op when asserts are disabled
207 */
208 void validate() const;
209
210 /** Return the number of iterations needed to iterate through a given dimension
211 *
212 * @param[in] dimension The requested dimension
213 *
214 * @return The number of iterations
215 */
216 constexpr size_t num_iterations(size_t dimension) const;
217
218 /** Split a window into a set of sub windows along a given dimension
219 *
220 * For example to split a window into 3 sub-windows along the Y axis, you would have to do:<br/>
221 * Window sub0 = window.split_window( 1, 0, 3);<br/>
222 * Window sub1 = window.split_window( 1, 1, 3);<br/>
223 * Window sub2 = window.split_window( 1, 2, 3);<br/>
224 *
225 * @param[in] dimension Dimension along which the split will be performed
226 * @param[in] id Id of the sub-window to return. Must be in the range (0, total-1)
227 * @param[in] total Total number of sub-windows the window will be split into.
228 *
229 * @return The subwindow "id" out of "total"
230 */
231 Window split_window(size_t dimension, size_t id, size_t total) const;
232 /** First 1D slice of the window
233 *
234 * @return The first slice of the window.
235 */
236 Window first_slice_window_1D() const
237 {
238 return first_slice_window<1>();
239 };
240 /** First 2D slice of the window
241 *
242 * @return The first slice of the window.
243 */
244 Window first_slice_window_2D() const
245 {
246 return first_slice_window<2>();
247 };
248 /** First 3D slice of the window
249 *
250 * @return The first slice of the window.
251 */
252 Window first_slice_window_3D() const
253 {
254 return first_slice_window<3>();
255 };
Michalis Spyrou5237e012018-01-17 09:40:27 +0000256 /** First 4D slice of the window
257 *
258 * @return The first slice of the window.
259 */
260 Window first_slice_window_4D() const
261 {
262 return first_slice_window<4>();
263 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264 /** Slide the passed 1D window slice.
265 *
266 * If slice contains the last slice then it will remain unchanged and false will be returned.
267 *
268 * @param[in,out] slice Current slice, to be updated to the next slice.
269 *
270 * @return true if slice contains a new slice, false if slice already contained the last slice
271 */
272 bool slide_window_slice_1D(Window &slice) const
273 {
274 return slide_window_slice<1>(slice);
275 }
276 /** Slide the passed 2D window slice.
277 *
278 * If slice contains the last slice then it will remain unchanged and false will be returned.
279 *
280 * @param[in,out] slice Current slice, to be updated to the next slice.
281 *
282 * @return true if slice contains a new slice, false if slice already contained the last slice
283 */
284 bool slide_window_slice_2D(Window &slice) const
285 {
286 return slide_window_slice<2>(slice);
287 }
288 /** Slide the passed 3D window slice.
289 *
290 * If slice contains the last slice then it will remain unchanged and false will be returned.
291 *
292 * @param[in,out] slice Current slice, to be updated to the next slice.
293 *
294 * @return true if slice contains a new slice, false if slice already contained the last slice
295 */
296 bool slide_window_slice_3D(Window &slice) const
297 {
298 return slide_window_slice<3>(slice);
299 }
300 /** Slide the passed 4D window slice.
301 *
302 * If slice contains the last slice then it will remain unchanged and false will be returned.
303 *
304 * @param[in,out] slice Current slice, to be updated to the next slice.
305 *
306 * @return true if slice contains a new slice, false if slice already contained the last slice
307 */
308 bool slide_window_slice_4D(Window &slice) const
309 {
310 return slide_window_slice<4>(slice);
311 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100312
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000313 /* Collapse the dimensions between @p first and @p last if possible.
314 *
315 * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
316 *
317 * @param[in] full_window Full window @p window has been created from.
318 * @param[in] first Start dimension into which the following are collapsed.
319 * @param[in] last End (exclusive) dimension to collapse.
320 * @param[out] has_collapsed (Optional) Whether the window was collapsed.
321 *
322 * @return Collapsed window.
323 */
324 Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed = nullptr) const;
325
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100326 /* Collapse the dimensions higher than @p first if possible.
327 *
328 * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
329 *
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000330 * @param[in] full_window Full window @p window has been created from.
331 * @param[in] first Start dimension into which the following are collapsed.
332 * @param[out] has_collapsed (Optional) Whether the window was collapsed.
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100333 *
334 * @return Collapsed window.
335 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000336 Window collapse_if_possible(const Window &full_window, size_t first, bool *has_collapsed = nullptr) const
337 {
338 return collapse_if_possible(full_window, first, Coordinates::num_max_dimensions, has_collapsed);
339 }
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100340
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000341 /* Collapse the dimensions between @p first and @p last.
Michalis Spyrou5237e012018-01-17 09:40:27 +0000342 *
343 * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
344 *
345 * @param[in] full_window Full window @p window has been created from.
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000346 * @param[in] first Start dimension into which the following are collapsed.
347 * @param[in] last End (exclusive) dimension to collapse.
Michalis Spyrou5237e012018-01-17 09:40:27 +0000348 *
349 * @return Collapsed window if successful.
350 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000351 Window collapse(const Window &full_window, size_t first, size_t last = Coordinates::num_max_dimensions) const;
352
353 /* Don't advance in the dimension where @p shape is less equal to 1.
354 *
355 * @param[in] shape A TensorShape.
356 *
357 * @return Broadcast window.
358 */
359 Window broadcast_if_dimension_le_one(const TensorShape &shape) const;
360
361 /* Don't advance in the dimension where shape of @p info is less equal to 1.
362 *
363 * @param[in] info An ITensorInfo.
364 *
365 * @return Broadcast window.
366 */
367 Window broadcast_if_dimension_le_one(const ITensorInfo &info) const
368 {
369 return broadcast_if_dimension_le_one(info.tensor_shape());
370 }
Michalis Spyrou5237e012018-01-17 09:40:27 +0000371
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100372private:
373 /** First slice of the window
374 *
375 * @return The first slice of the window.
376 */
377 template <unsigned int window_dimension>
378 Window first_slice_window() const;
379
380 /** Slide the passed window slice.
381 *
382 * If slice contains the last slice then it will remain unchanged and false will be returned.
383 *
384 * @param[in,out] slice Current slice, to be updated to the next slice.
385 *
386 * @return true if slice contains a new slice, false if slice already contained the last slice
387 */
388 template <unsigned int window_dimension>
389 bool slide_window_slice(Window &slice) const;
390
391private:
392 std::array<Dimension, Coordinates::num_max_dimensions> _dims;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100393};
394}
395#include "Window.inl"
396#endif /*__ARM_COMPUTE_WINDOW_H__ */