Compute Library
 19.08
Window.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019 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 {
39 class Window
40 {
41 public:
43  static constexpr size_t DimX = 0;
45  static constexpr size_t DimY = 1;
47  static constexpr size_t DimZ = 2;
48 
50  constexpr Window()
51  : _dims()
52  {
53  }
58  Window(const Window &src);
65  Window &operator=(const Window &rhs);
66 
75  class Dimension
76  {
77  public:
85  constexpr Dimension(int start = 0, int end = 1, int step = 1)
86  : _start(start), _end(end), _step(step)
87  {
88  }
90  Dimension &operator=(const Dimension &d) = default;
92  constexpr int start() const
93  {
94  return _start;
95  }
97  constexpr int end() const
98  {
99  return _end;
100  }
102  constexpr int step() const
103  {
104  return _step;
105  }
110  void set_step(int step)
111  {
112  _step = step;
113  }
118  void set_end(int end)
119  {
120  _end = end;
121  }
122 
123  private:
124  int _start;
125  int _end;
126  int _step;
127  };
128 
137  constexpr const Dimension &operator[](size_t dimension) const;
138 
143  constexpr const Dimension &x() const
144  {
145  return _dims.at(Window::DimX);
146  }
147 
152  constexpr const Dimension &y() const
153  {
154  return _dims.at(Window::DimY);
155  }
156 
161  constexpr const Dimension &z() const
162  {
163  return _dims.at(Window::DimZ);
164  }
165 
171  void set(size_t dimension, const Dimension &dim);
172 
178  void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension = Window::DimX);
179 
185  void shift(size_t dimension, int shift_value);
186 
195  Window shift_dimensions(unsigned int shift_value) const;
196 
203  void adjust(size_t dimension, int adjust_value, bool is_at_start);
204 
212  void scale(size_t dimension, float scale_value);
213 
219  void set_dimension_step(size_t dimension, int step);
220 
225  void validate() const;
226 
233  constexpr size_t num_iterations(size_t dimension) const;
238  size_t num_iterations_total() const;
240  TensorShape shape() const;
254  Window split_window(size_t dimension, size_t id, size_t total) const;
260  {
261  return first_slice_window<1>();
262  };
268  {
269  return first_slice_window<2>();
270  };
276  {
277  return first_slice_window<3>();
278  };
284  {
285  return first_slice_window<4>();
286  };
296  {
297  return slide_window_slice<1>(slice);
298  }
308  {
309  return slide_window_slice<2>(slice);
310  }
320  {
321  return slide_window_slice<3>(slice);
322  }
332  {
333  return slide_window_slice<4>(slice);
334  }
335 
347  Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed = nullptr) const;
348 
359  Window collapse_if_possible(const Window &full_window, size_t first, bool *has_collapsed = nullptr) const
360  {
361  return collapse_if_possible(full_window, first, Coordinates::num_max_dimensions, has_collapsed);
362  }
363 
374  Window collapse(const Window &full_window, size_t first, size_t last = Coordinates::num_max_dimensions) const;
375 
383 
391  {
392  return broadcast_if_dimension_le_one(info.tensor_shape());
393  }
399  friend void swap(Window &lhs, Window &rhs);
400 
401 private:
406  template <unsigned int window_dimension>
407  Window first_slice_window() const;
408 
417  template <unsigned int window_dimension>
418  bool slide_window_slice(Window &slice) const;
419 
420 private:
421  std::array<Dimension, Coordinates::num_max_dimensions> _dims;
422 };
423 } // namespace arm_compute
424 #include "Window.inl"
425 #endif /*__ARM_COMPUTE_WINDOW_H__ */
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:267
void scale(size_t dimension, float scale_value)
Scale the values of a given dimension by the given scale_value.
Definition: Window.inl:141
Shape of a tensor.
Definition: TensorShape.h:39
void set_end(int end)
Set the dimension's end.
Definition: Window.h:118
void shift(size_t dimension, int shift_value)
Shift the values of a given dimension by the given shift_value.
Definition: Window.inl:119
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:102
friend void swap(Window &lhs, Window &rhs)
Friend function that swaps the contents of two windows.
Definition: Window.inl:278
Store the tensor'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:175
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
void validate() const
Will validate all the window's dimensions' values when asserts are enabled.
Definition: Window.inl:159
constexpr const Dimension & z() const
Alias to access the third dimension of the window.
Definition: Window.h:161
constexpr size_t num_iterations(size_t dimension) const
Return the number of iterations needed to iterate through a given dimension.
Definition: Window.inl:168
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
Definition: Window.inl:250
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:307
Copyright (c) 2017-2018 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:97
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
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:54
Window & operator=(const Window &rhs)
Copy assignment operator.
Definition: Window.inl:35
constexpr const Dimension & operator[](size_t dimension) const
Read only access to a given dimension of the window.
Definition: Window.inl:42
constexpr Window()
Default constructor: create a window containing a single element.
Definition: Window.h:50
void set_step(int step)
Set the dimension's step.
Definition: Window.h:110
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:48
Window broadcast_if_dimension_le_one(const TensorShape &shape) const
Don't advance in the dimension where shape is less equal to 1.
Definition: Window.inl:106
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:319
constexpr Dimension(int start=0, int end=1, int step=1)
Constructor, by default creates a dimension of 1.
Definition: Window.h:85
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:153
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:359
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:152
Window broadcast_if_dimension_le_one(const ITensorInfo &info) const
Don't advance in the dimension where shape of info is less equal to 1.
Definition: Window.h:390
Window first_slice_window_4D() const
First 4D slice of the window.
Definition: Window.h:283
bool slide_window_slice_4D(Window &slice) const
Slide the passed 4D window slice.
Definition: Window.h:331
TensorShape shape() const
Return the shape of the window in number of steps.
Definition: Window.inl:258
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:97
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:126
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:275
bool slide_window_slice_1D(Window &slice) const
Slide the passed 1D window slice.
Definition: Window.h:295
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:92
size_t num_iterations_total() const
Return the total number of iterations needed to iterate through the entire window.
Definition: Window.inl:268
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:87
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:259
cast configure & src
Definition: Cast.cpp:169
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:143