Compute Library
 22.05
Window.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020, 2022 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 
32 #include "arm_compute/core/Error.h"
34 #include "arm_compute/core/Utils.h"
35 
36 namespace arm_compute
37 {
38 /** Describe a multidimensional execution window. */
39 class Window
40 {
41 public:
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  /** Alias for dimension 3 also known as W dimension */
49  static constexpr size_t DimW = 3;
50  /** Alias for dimension 4 also known as V dimension */
51  static constexpr size_t DimV = 4;
52 
53  /** Default constructor: create a window containing a single element. */
54  constexpr Window()
55  : _dims(), _is_broadcasted(utility::generate_array<bool, Coordinates::num_max_dimensions, false>::value)
56  {
57  }
58  /** Copy constructor
59  *
60  * @param[in] src Copy the values from src to a new object
61  */
62  Window(const Window &src);
63  /** Copy assignment operator
64  *
65  * @param[in] rhs Copy the values from rhs to the current object
66  *
67  * @return Reference to the updated object
68  */
69  Window &operator=(const Window &rhs);
70 
71  /** Describe one of the image's dimensions with a start, end and step.
72  *
73  * Iteration through the elements of the dimension is done like this:
74  * for(int v = start(); v < end(); v += step())
75  * {
76  * ...
77  * }
78  */
79  class Dimension
80  {
81  public:
82  /** Constructor, by default creates a dimension of 1.
83  *
84  * @param[in] start Start of the dimension
85  * @param[in] end End of the dimension
86  * @param[in] step Step between two elements of the dimension when iterating.
87  *
88  */
89  constexpr Dimension(int start = 0, int end = 1, int step = 1)
90  : _start(start), _end(end), _step(step)
91  {
92  }
93  /** Default assignment operator to allow dimensions to be copied */
94  Dimension &operator=(const Dimension &d) = default;
95  /** Return the start of the dimension */
96  constexpr int start() const
97  {
98  return _start;
99  }
100  /** Return the end of the dimension */
101  constexpr int end() const
102  {
103  return _end;
104  }
105  /** Return the step of the dimension */
106  constexpr int step() const
107  {
108  return _step;
109  }
110  /** Set the dimension's step
111  *
112  * @param[in] step The new step
113  */
114  void set_step(int step)
115  {
116  _step = step;
117  }
118  /** Set the dimension's end
119  *
120  * @param[in] end The new end
121  */
122  void set_end(int end)
123  {
124  _end = end;
125  }
126  /** Check whether two Dimensions are equal.
127  *
128  * @param[in] lhs LHS Dimensions
129  * @param[in] rhs RHS Dimensions
130  *
131  * @return True if the Dimensions are the same.
132  */
133  friend bool operator==(const Dimension &lhs, const Dimension &rhs)
134  {
135  return (lhs._start == rhs._start) && (lhs._end == rhs._end) && (lhs._step == rhs._step);
136  }
137 
138  private:
139  int _start; /**< Start of the dimension */
140  int _end; /**< End of the dimension */
141  int _step;
142  };
143 
144  /** Read only access to a given dimension of the window
145  *
146  * @note Precondition: dimension < Coordinates::num_max_dimensions
147  *
148  * @param[in] dimension The dimension to access
149  *
150  * @return The requested dimension
151  */
152  constexpr const Dimension &operator[](size_t dimension) const;
153 
154  /** Alias to access the first dimension of the window
155  *
156  * @return First dimension of the window
157  */
158  constexpr const Dimension &x() const
159  {
160  return _dims.at(Window::DimX);
161  }
162 
163  /** Alias to access the second dimension of the window
164  *
165  * @return Second dimension of the window
166  */
167  constexpr const Dimension &y() const
168  {
169  return _dims.at(Window::DimY);
170  }
171 
172  /** Alias to access the third dimension of the window
173  *
174  * @return Third dimension of the window
175  */
176  constexpr const Dimension &z() const
177  {
178  return _dims.at(Window::DimZ);
179  }
180 
181  /** Set the values of a given dimension
182  *
183  * @param[in] dimension The dimension to set
184  * @param[in] dim The values to set the dimension to
185  */
186  void set(size_t dimension, const Dimension &dim);
187 
188  /** Set the dimension as broadcasted dimension
189  *
190  * @param[in] dimension The dimension to set
191  */
192  void set_broadcasted(size_t dimension);
193 
194  /** Return whether a dimension has been broadcasted
195  *
196  * @param[in] dimension The requested dimension
197  *
198  * @return true if the dimension has been broadcasted
199  */
200  bool is_broadcasted(size_t dimension) const;
201 
202  /** Use the tensor's dimensions to fill the window dimensions.
203  *
204  * @param[in] shape @ref TensorShape to copy the dimensions from.
205  * @param[in] first_dimension Only copy dimensions which are greater or equal to this value.
206  */
207  void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension = Window::DimX);
208 
209  /** Shift the values of a given dimension by the given shift_value
210  *
211  * @param[in] dimension The dimension to shift
212  * @param[in] shift_value Value to shift the start and end values of.
213  */
214  void shift(size_t dimension, int shift_value);
215 
216  /** Shift down all the dimensions of a window
217  *
218  * i.e new_dims[n] = old_dims[n+shift_value].
219  *
220  * @param[in] shift_value Number of dimensions to shift the window by.
221  *
222  * @return The window with the shifted dimensions.
223  */
224  Window shift_dimensions(unsigned int shift_value) const;
225 
226  /** Adjust the start or end of a given dimension by the given value
227  *
228  * @param[in] dimension The dimension to adjust
229  * @param[in] adjust_value The adjusted value.
230  * @param[in] is_at_start The flag to indicate whether adjust the start or end of the dimension.
231  */
232  void adjust(size_t dimension, int adjust_value, bool is_at_start);
233 
234  /** Scale the values of a given dimension by the given scale_value
235  *
236  * @note The end of the window is rounded up to be a multiple of step after the scaling.
237  *
238  * @param[in] dimension The dimension to scale
239  * @param[in] scale_value Value to scale the start, end and step values of.
240  */
241  void scale(size_t dimension, float scale_value);
242 
243  /** Set the step of a given dimension.
244  *
245  * @param[in] dimension Dimension to update
246  * @param[in] step The new dimension's step value
247  */
248  void set_dimension_step(size_t dimension, int step);
249 
250  /** Will validate all the window's dimensions' values when asserts are enabled
251  *
252  * No-op when asserts are disabled
253  */
254  void validate() const;
255 
256  /** Return the number of iterations needed to iterate through a given dimension
257  *
258  * @param[in] dimension The requested dimension
259  *
260  * @return The number of iterations
261  */
262  constexpr size_t num_iterations(size_t dimension) const;
263  /** Return the total number of iterations needed to iterate through the entire window
264  *
265  * @return Number of total iterations
266  */
267  size_t num_iterations_total() const;
268  /** Return the shape of the window in number of steps */
269  TensorShape shape() const;
270  /** Split a window into a set of sub windows along a given dimension
271  *
272  * For example to split a window into 3 sub-windows along the Y axis, you would have to do:<br/>
273  * Window sub0 = window.split_window( 1, 0, 3);<br/>
274  * Window sub1 = window.split_window( 1, 1, 3);<br/>
275  * Window sub2 = window.split_window( 1, 2, 3);<br/>
276  *
277  * @param[in] dimension Dimension along which the split will be performed
278  * @param[in] id Id of the sub-window to return. Must be in the range (0, total-1)
279  * @param[in] total Total number of sub-windows the window will be split into.
280  *
281  * @return The subwindow "id" out of "total"
282  */
283  Window split_window(size_t dimension, size_t id, size_t total) const;
284  /** First 1D slice of the window
285  *
286  * @return The first slice of the window.
287  */
289  {
290  return first_slice_window<1>();
291  };
292  /** First 2D slice of the window
293  *
294  * @return The first slice of the window.
295  */
297  {
298  return first_slice_window<2>();
299  };
300  /** First 3D slice of the window
301  *
302  * @return The first slice of the window.
303  */
305  {
306  return first_slice_window<3>();
307  };
308  /** First 4D slice of the window
309  *
310  * @return The first slice of the window.
311  */
313  {
314  return first_slice_window<4>();
315  };
316  /** Slide the passed 1D 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  */
325  {
326  return slide_window_slice<1>(slice);
327  }
328  /** Slide the passed 2D window slice.
329  *
330  * If slice contains the last slice then it will remain unchanged and false will be returned.
331  *
332  * @param[in,out] slice Current slice, to be updated to the next slice.
333  *
334  * @return true if slice contains a new slice, false if slice already contained the last slice
335  */
337  {
338  return slide_window_slice<2>(slice);
339  }
340  /** Slide the passed 3D window slice.
341  *
342  * If slice contains the last slice then it will remain unchanged and false will be returned.
343  *
344  * @param[in,out] slice Current slice, to be updated to the next slice.
345  *
346  * @return true if slice contains a new slice, false if slice already contained the last slice
347  */
349  {
350  return slide_window_slice<3>(slice);
351  }
352  /** Slide the passed 4D window slice.
353  *
354  * If slice contains the last slice then it will remain unchanged and false will be returned.
355  *
356  * @param[in,out] slice Current slice, to be updated to the next slice.
357  *
358  * @return true if slice contains a new slice, false if slice already contained the last slice
359  */
361  {
362  return slide_window_slice<4>(slice);
363  }
364  /** Collapse the dimensions between @p first and @p last if possible.
365  *
366  * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
367  *
368  * @param[in] full_window Full window @p window has been created from.
369  * @param[in] first Start dimension into which the following are collapsed.
370  * @param[in] last End (exclusive) dimension to collapse.
371  * @param[out] has_collapsed (Optional) Whether the window was collapsed.
372  *
373  * @return Collapsed window.
374  */
375  Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed = nullptr) const;
376 
377  /** Collapse the dimensions higher than @p first if possible.
378  *
379  * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
380  *
381  * @param[in] full_window Full window @p window has been created from.
382  * @param[in] first Start dimension into which the following are collapsed.
383  * @param[out] has_collapsed (Optional) Whether the window was collapsed.
384  *
385  * @return Collapsed window.
386  */
387  Window collapse_if_possible(const Window &full_window, size_t first, bool *has_collapsed = nullptr) const
388  {
389  return collapse_if_possible(full_window, first, Coordinates::num_max_dimensions, has_collapsed);
390  }
391 
392  /** Collapse the dimensions between @p first and @p last.
393  *
394  * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
395  *
396  * @param[in] full_window Full window @p window has been created from.
397  * @param[in] first Start dimension into which the following are collapsed.
398  * @param[in] last End (exclusive) dimension to collapse.
399  *
400  * @return Collapsed window if successful.
401  */
402  Window collapse(const Window &full_window, size_t first, size_t last = Coordinates::num_max_dimensions) const;
403 
404  /** Don't advance in the dimension where @p shape is less equal to 1.
405  *
406  * @param[in] shape A TensorShape.
407  *
408  * @return Broadcast window.
409  */
411 
412  /** Don't advance in the dimension where shape of @p info is less equal to 1.
413  *
414  * @param[in] info An ITensorInfo.
415  *
416  * @return Broadcast window.
417  */
419  {
421  }
422  /** Friend function that swaps the contents of two windows
423  *
424  * @param[in] lhs First window to swap.
425  * @param[in] rhs Second window to swap.
426  */
427  friend void swap(Window &lhs, Window &rhs);
428  /** Check whether two Windows are equal.
429  *
430  * @param[in] lhs LHS window
431  * @param[in] rhs RHS window
432  *
433  * @return True if the given windows are the same.
434  */
435  friend bool operator==(const Window &lhs, const Window &rhs);
436 
437 private:
438  /** First slice of the window
439  *
440  * @return The first slice of the window.
441  */
442  template <unsigned int window_dimension>
443  Window first_slice_window() const;
444 
445  /** Slide the passed window slice.
446  *
447  * If slice contains the last slice then it will remain unchanged and false will be returned.
448  *
449  * @param[in,out] slice Current slice, to be updated to the next slice.
450  *
451  * @return true if slice contains a new slice, false if slice already contained the last slice
452  */
453  template <unsigned int window_dimension>
454  bool slide_window_slice(Window &slice) const;
455 
456 private:
457  std::array<Dimension, Coordinates::num_max_dimensions> _dims;
458  std::array<bool, Coordinates::num_max_dimensions> _is_broadcasted;
459 };
460 } // namespace arm_compute
461 #include "Window.inl"
462 #endif /*ARM_COMPUTE_WINDOW_H */
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:296
void scale(size_t dimension, float scale_value)
Scale the values of a given dimension by the given scale_value.
Definition: Window.inl:155
Shape of a tensor.
Definition: TensorShape.h:39
void set_end(int end)
Set the dimension&#39;s end.
Definition: Window.h:122
void shift(size_t dimension, int shift_value)
Shift the values of a given dimension by the given shift_value.
Definition: Window.inl:133
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:106
friend void swap(Window &lhs, Window &rhs)
Friend function that swaps the contents of two windows.
Definition: Window.inl:304
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Window split_window(size_t dimension, size_t id, size_t total) const
Split a window into a set of sub windows along a given dimension.
Definition: Window.inl:189
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
void validate() const
Will validate all the window&#39;s dimensions&#39; values when asserts are enabled.
Definition: Window.inl:173
constexpr const Dimension & z() const
Alias to access the third dimension of the window.
Definition: Window.h:176
constexpr size_t num_iterations(size_t dimension) const
Return the number of iterations needed to iterate through a given dimension.
Definition: Window.inl:182
friend bool operator==(const Dimension &lhs, const Dimension &rhs)
Check whether two Dimensions are equal.
Definition: Window.h:133
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor&#39;s dimensions to fill the window dimensions.
Definition: Window.inl:276
SimpleTensor< float > src
Definition: DFT.cpp:155
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:336
Copyright (c) 2017-2022 Arm Limited.
Window collapse(const Window &full_window, size_t first, size_t last=Coordinates::num_max_dimensions) const
Collapse the dimensions between first and last.
Definition: Window.inl:111
Dimension & operator=(const Dimension &d)=default
Default assignment operator to allow dimensions to be copied.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
static constexpr size_t DimV
Alias for dimension 4 also known as V dimension.
Definition: Window.h:51
Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed=nullptr) const
Collapse the dimensions between first and last if possible.
Definition: Window.inl:68
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Coordinates of an item.
Definition: Coordinates.h:37
Window & operator=(const Window &rhs)
Copy assignment operator.
Definition: Window.inl:36
constexpr const Dimension & operator[](size_t dimension) const
Read only access to a given dimension of the window.
Definition: Window.inl:43
constexpr Window()
Default constructor: create a window containing a single element.
Definition: Window.h:54
void set_step(int step)
Set the dimension&#39;s step.
Definition: Window.h:114
static constexpr size_t DimW
Alias for dimension 3 also known as W dimension.
Definition: Window.h:49
Window broadcast_if_dimension_le_one(const TensorShape &shape) const
Don&#39;t advance in the dimension where shape is less equal to 1.
Definition: Window.inl:120
bool is_broadcasted(size_t dimension) const
Return whether a dimension has been broadcasted.
Definition: Window.inl:62
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:348
constexpr Dimension(int start=0, int end=1, int step=1)
Constructor, by default creates a dimension of 1.
Definition: Window.h:89
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void set_dimension_step(size_t dimension, int step)
Set the step of a given dimension.
Definition: Window.inl:167
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Window collapse_if_possible(const Window &full_window, size_t first, bool *has_collapsed=nullptr) const
Collapse the dimensions higher than first if possible.
Definition: Window.h:387
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:167
Window broadcast_if_dimension_le_one(const ITensorInfo &info) const
Don&#39;t advance in the dimension where shape of info is less equal to 1.
Definition: Window.h:418
Window first_slice_window_4D() const
First 4D slice of the window.
Definition: Window.h:312
bool slide_window_slice_4D(Window &slice) const
Slide the passed 4D window slice.
Definition: Window.h:360
TensorShape shape() const
Return the shape of the window in number of steps.
Definition: Window.inl:284
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:101
void adjust(size_t dimension, int adjust_value, bool is_at_start)
Adjust the start or end of a given dimension by the given value.
Definition: Window.inl:140
void set_broadcasted(size_t dimension)
Set the dimension as broadcasted dimension.
Definition: Window.inl:55
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:304
bool slide_window_slice_1D(Window &slice) const
Slide the passed 1D window slice.
Definition: Window.h:324
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:96
size_t num_iterations_total() const
Return the total number of iterations needed to iterate through the entire window.
Definition: Window.inl:294
Describe a multidimensional execution window.
Definition: Window.h:39
Window shift_dimensions(unsigned int shift_value) const
Shift down all the dimensions of a window.
Definition: Window.inl:101
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:288
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:158