Compute Library
 20.11
Helpers.inl
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 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 #include "arm_compute/core/Error.h"
25 
26 #include <cmath>
27 #include <numeric>
28 
29 namespace arm_compute
30 {
31 template <size_t dimension>
32 struct IncrementIterators
33 {
34  template <typename T, typename... Ts>
35  static void unroll(T &&it, Ts &&... iterators)
36  {
37  auto increment = [](T && it)
38  {
39  it.increment(dimension);
40  };
41  utility::for_each(increment, std::forward<T>(it), std::forward<Ts>(iterators)...);
42  }
43  static void unroll()
44  {
45  // End of recursion
46  }
47 };
48 
49 template <size_t dim>
50 struct ForEachDimension
51 {
52  template <typename L, typename... Ts>
53  static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&... iterators)
54  {
55  const auto &d = w[dim - 1];
56 
57  for(auto v = d.start(); v < d.end(); v += d.step(), IncrementIterators < dim - 1 >::unroll(iterators...))
58  {
59  id.set(dim - 1, v);
60  ForEachDimension < dim - 1 >::unroll(w, id, lambda_function, iterators...);
61  }
62  }
63 };
64 
65 template <>
66 struct ForEachDimension<0>
67 {
68  template <typename L, typename... Ts>
69  static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&... iterators)
70  {
71  ARM_COMPUTE_UNUSED(w, iterators...);
72  lambda_function(id);
73  }
74 };
75 
76 template <typename L, typename... Ts>
77 inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
78 {
79  w.validate();
80 
81  for(unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i)
82  {
83  ARM_COMPUTE_ERROR_ON(w[i].step() == 0);
84  }
85 
86  Coordinates id;
87  ForEachDimension<Coordinates::num_max_dimensions>::unroll(w, id, std::forward<L>(lambda_function), std::forward<Ts>(iterators)...);
88 }
89 
90 inline constexpr Iterator::Iterator()
91  : _ptr(nullptr), _dims()
92 {
93 }
94 
95 inline Iterator::Iterator(const ITensor *tensor, const Window &win)
96  : Iterator()
97 {
98  ARM_COMPUTE_ERROR_ON(tensor == nullptr);
99  ARM_COMPUTE_ERROR_ON(tensor->info() == nullptr);
100 
101  const ITensorInfo *info = tensor->info();
102  const Strides &strides = info->strides_in_bytes();
103 
104  _ptr = tensor->buffer() + info->offset_first_element_in_bytes();
105 
106  //Initialize the stride for each dimension and calculate the position of the first element of the iteration:
107  for(unsigned int n = 0; n < info->num_dimensions(); ++n)
108  {
109  _dims[n]._stride = win[n].step() * strides[n];
110  std::get<0>(_dims)._dim_start += static_cast<size_t>(strides[n]) * win[n].start();
111  }
112 
113  //Copy the starting point to all the dimensions:
114  for(unsigned int n = 1; n < Coordinates::num_max_dimensions; ++n)
115  {
116  _dims[n]._dim_start = std::get<0>(_dims)._dim_start;
117  }
118 
119  ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(win, info->num_dimensions());
120 }
121 
122 inline void Iterator::increment(const size_t dimension)
123 {
125 
126  _dims[dimension]._dim_start += _dims[dimension]._stride;
127 
128  for(unsigned int n = 0; n < dimension; ++n)
129  {
130  _dims[n]._dim_start = _dims[dimension]._dim_start;
131  }
132 }
133 
134 inline constexpr size_t Iterator::offset() const
135 {
136  return _dims.at(0)._dim_start;
137 }
138 
139 inline constexpr uint8_t *Iterator::ptr() const
140 {
141  return _ptr + _dims.at(0)._dim_start;
142 }
143 
144 inline void Iterator::reset(const size_t dimension)
145 {
147 
148  _dims[dimension]._dim_start = _dims[dimension + 1]._dim_start;
149 
150  for(unsigned int n = 0; n < dimension; ++n)
151  {
152  _dims[n]._dim_start = _dims[dimension]._dim_start;
153  }
154 }
155 
156 inline Coordinates index2coords(const TensorShape &shape, int index)
157 {
158  int num_elements = shape.total_size();
159 
160  ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]!");
161  ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape!");
162 
163  Coordinates coord{ 0 };
164 
165  for(int d = shape.num_dimensions() - 1; d >= 0; --d)
166  {
167  num_elements /= shape[d];
168  coord.set(d, index / num_elements);
169  index %= num_elements;
170  }
171 
172  return coord;
173 }
174 
175 inline int coords2index(const TensorShape &shape, const Coordinates &coord)
176 {
177  int num_elements = shape.total_size();
178  ARM_COMPUTE_UNUSED(num_elements);
179  ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create linear index from empty shape!");
180 
181  int index = 0;
182  int stride = 1;
183 
184  for(unsigned int d = 0; d < coord.num_dimensions(); ++d)
185  {
186  index += coord[d] * stride;
187  stride *= shape[d];
188  }
189 
190  return index;
191 }
192 
193 inline size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
194 {
195  ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
196 
197  /* Return the index based on the data layout
198  * [N C H W]
199  * [3 2 1 0]
200  * [N H W C]
201  */
202  switch(data_layout_dimension)
203  {
205  return (data_layout == DataLayout::NCHW) ? 2 : 0;
206  break;
208  return (data_layout == DataLayout::NCHW) ? 1 : 2;
209  break;
211  return (data_layout == DataLayout::NCHW) ? 0 : 1;
212  break;
214  return 3;
215  break;
216  default:
217  break;
218  }
219  ARM_COMPUTE_ERROR("Data layout index not supported!");
220 }
221 
223 {
224  ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
225 
226  /* Return the index based on the data layout
227  * [N C H W]
228  * [3 2 1 0]
229  * [N H W C]
230  */
231  switch(index)
232  {
233  case 0:
235  break;
236  case 1:
238  break;
239  case 2:
241  break;
242  case 3:
244  break;
245  default:
246  ARM_COMPUTE_ERROR("Index value not supported!");
247  break;
248  }
249 }
250 } // namespace arm_compute
SimpleTensor< float > w
Definition: DFT.cpp:156
Shape of a tensor.
Definition: TensorShape.h:39
Coordinates index2coords(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
Definition: Helpers.inl:156
void increment(size_t dimension)
Increment the iterator along the specified dimension of the step value associated to the dimension.
Definition: Helpers.inl:122
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
DataLayoutDimension
[DataLayout enum definition]
Definition: Types.h:129
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
const DataLayout data_layout
Definition: Im2Col.cpp:146
Store the tensor's metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(w, md)
Definition: Validate.h:263
Interface for NEON tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2020 Arm Limited.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Coordinates of an item.
Definition: Coordinates.h:37
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
int coords2index(const TensorShape &shape, const Coordinates &coord)
Convert n-dimensional coordinates into a linear index.
Definition: Helpers.inl:175
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
DataLayoutDimension get_index_data_layout_dimension(const DataLayout data_layout, const size_t index)
Get the DataLayoutDimension of a given index and layout.
Definition: Helpers.inl:222
constexpr Iterator()
Default constructor to create an empty iterator.
Definition: Helpers.inl:90
Num samples, channels, height, width.
void for_each(F &&)
Base case of for_each.
Definition: Utility.h:108
Strides of an item in bytes.
Definition: Strides.h:37
void reset(size_t dimension)
Move the iterator back to the beginning of the specified dimension.
Definition: Helpers.inl:144
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
constexpr int step
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
constexpr size_t offset() const
Return the offset in bytes from the first element to the current position of the iterator.
Definition: Helpers.inl:134
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
Describe a multidimensional execution window.
Definition: Window.h:39