blob: 654f5ed4f8cbf604ed2f0b8446e6bab49aeee89c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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#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 }
107
108 private:
109 int _start; /**< Start of the dimension */
110 int _end; /**< End of the dimension */
111 int _step;
112 };
113
114 /** Read only access to a given dimension of the window
115 *
116 * @note Precondition: dimension < Coordinates::num_max_dimensions
117 *
118 * @param[in] dimension The dimension to access
119 *
120 * @return The requested dimension
121 */
122 constexpr const Dimension &operator[](size_t dimension) const;
123
124 /** Alias to access the first dimension of the window
125 *
126 * @return First dimension of the window
127 */
128 constexpr const Dimension &x() const
129 {
130 return _dims.at(Window::DimX);
131 }
132
133 /** Alias to access the second dimension of the window
134 *
135 * @return Second dimension of the window
136 */
137 constexpr const Dimension &y() const
138 {
139 return _dims.at(Window::DimY);
140 }
141
142 /** Alias to access the third dimension of the window
143 *
144 * @return Third dimension of the window
145 */
146 constexpr const Dimension &z() const
147 {
148 return _dims.at(Window::DimZ);
149 }
150
151 /** Set the values of a given dimension
152 *
153 * @param[in] dimension The dimension to set
154 * @param[in] dim The values to set the dimension to
155 */
156 void set(size_t dimension, const Dimension &dim);
157
158 /** Use the tensor's dimensions to fill the window dimensions.
159 *
SiCong Li86b53332017-08-23 11:02:43 +0100160 * @param[in] shape @ref TensorShape to copy the dimensions from.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 * @param[in] first_dimension Only copy dimensions which are greater or equal to this value.
162 */
SiCong Li86b53332017-08-23 11:02:43 +0100163 void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension = Window::DimX);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164
165 /** Shift the values of a given dimension by the given shift_value
166 *
167 * @param[in] dimension The dimension to shift
168 * @param[in] shift_value Value to shift the start and end values of.
169 */
170 void shift(size_t dimension, int shift_value);
171
172 /** Adjust the start or end of a given dimension by the given value
173 *
174 * @param[in] dimension The dimension to adjust
175 * @param[in] adjust_value The adjusted value.
176 * @param[in] is_at_start The flag to indicate whether adjust the start or end of the dimension.
177 */
178 void adjust(size_t dimension, int adjust_value, bool is_at_start);
179
180 /** Scale the values of a given dimension by the given scale_value
181 *
182 * @note The end of the window is rounded up to be a multiple of step after the scaling.
183 *
184 * @param[in] dimension The dimension to scale
185 * @param[in] scale_value Value to scale the start, end and step values of.
186 */
187 void scale(size_t dimension, float scale_value);
188
189 /** Set the step of a given dimension.
190 *
191 * @param[in] dimension Dimension to update
192 * @param[in] step The new dimension's step value
193 */
194 void set_dimension_step(size_t dimension, int step);
195
196 /** Will validate all the window's dimensions' values when asserts are enabled
197 *
198 * No-op when asserts are disabled
199 */
200 void validate() const;
201
202 /** Return the number of iterations needed to iterate through a given dimension
203 *
204 * @param[in] dimension The requested dimension
205 *
206 * @return The number of iterations
207 */
208 constexpr size_t num_iterations(size_t dimension) const;
209
210 /** Split a window into a set of sub windows along a given dimension
211 *
212 * For example to split a window into 3 sub-windows along the Y axis, you would have to do:<br/>
213 * Window sub0 = window.split_window( 1, 0, 3);<br/>
214 * Window sub1 = window.split_window( 1, 1, 3);<br/>
215 * Window sub2 = window.split_window( 1, 2, 3);<br/>
216 *
217 * @param[in] dimension Dimension along which the split will be performed
218 * @param[in] id Id of the sub-window to return. Must be in the range (0, total-1)
219 * @param[in] total Total number of sub-windows the window will be split into.
220 *
221 * @return The subwindow "id" out of "total"
222 */
223 Window split_window(size_t dimension, size_t id, size_t total) const;
224 /** First 1D slice of the window
225 *
226 * @return The first slice of the window.
227 */
228 Window first_slice_window_1D() const
229 {
230 return first_slice_window<1>();
231 };
232 /** First 2D slice of the window
233 *
234 * @return The first slice of the window.
235 */
236 Window first_slice_window_2D() const
237 {
238 return first_slice_window<2>();
239 };
240 /** First 3D slice of the window
241 *
242 * @return The first slice of the window.
243 */
244 Window first_slice_window_3D() const
245 {
246 return first_slice_window<3>();
247 };
248 /** Slide the passed 1D window slice.
249 *
250 * If slice contains the last slice then it will remain unchanged and false will be returned.
251 *
252 * @param[in,out] slice Current slice, to be updated to the next slice.
253 *
254 * @return true if slice contains a new slice, false if slice already contained the last slice
255 */
256 bool slide_window_slice_1D(Window &slice) const
257 {
258 return slide_window_slice<1>(slice);
259 }
260 /** Slide the passed 2D window slice.
261 *
262 * If slice contains the last slice then it will remain unchanged and false will be returned.
263 *
264 * @param[in,out] slice Current slice, to be updated to the next slice.
265 *
266 * @return true if slice contains a new slice, false if slice already contained the last slice
267 */
268 bool slide_window_slice_2D(Window &slice) const
269 {
270 return slide_window_slice<2>(slice);
271 }
272 /** Slide the passed 3D window slice.
273 *
274 * If slice contains the last slice then it will remain unchanged and false will be returned.
275 *
276 * @param[in,out] slice Current slice, to be updated to the next slice.
277 *
278 * @return true if slice contains a new slice, false if slice already contained the last slice
279 */
280 bool slide_window_slice_3D(Window &slice) const
281 {
282 return slide_window_slice<3>(slice);
283 }
284 /** Slide the passed 4D window slice.
285 *
286 * If slice contains the last slice then it will remain unchanged and false will be returned.
287 *
288 * @param[in,out] slice Current slice, to be updated to the next slice.
289 *
290 * @return true if slice contains a new slice, false if slice already contained the last slice
291 */
292 bool slide_window_slice_4D(Window &slice) const
293 {
294 return slide_window_slice<4>(slice);
295 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100296
Anthony Barbierbe35b0e2017-07-11 18:13:08 +0100297 /* Collapse the dimensions higher than @p first if possible.
298 *
299 * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
300 *
301 * @param[in] full_window Full window @p window has been created from.
302 * @param[in] first Dimensions into which the following are collapsed.
303 *
304 * @return Collapsed window.
305 */
306 Window collapse_if_possible(const Window &full_window, size_t first) const;
307
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308private:
309 /** First slice of the window
310 *
311 * @return The first slice of the window.
312 */
313 template <unsigned int window_dimension>
314 Window first_slice_window() const;
315
316 /** Slide the passed window slice.
317 *
318 * If slice contains the last slice then it will remain unchanged and false will be returned.
319 *
320 * @param[in,out] slice Current slice, to be updated to the next slice.
321 *
322 * @return true if slice contains a new slice, false if slice already contained the last slice
323 */
324 template <unsigned int window_dimension>
325 bool slide_window_slice(Window &slice) const;
326
327private:
328 std::array<Dimension, Coordinates::num_max_dimensions> _dims;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100329};
330}
331#include "Window.inl"
332#endif /*__ARM_COMPUTE_WINDOW_H__ */