Compute Library
 23.11
Helpers.inl
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021, 2023 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) { it.increment(dimension); };
38  utility::for_each(increment, std::forward<T>(it), std::forward<Ts>(iterators)...);
39  }
40  static void unroll()
41  {
42  // End of recursion
43  }
44 };
45 
46 template <size_t dim>
47 struct ForEachDimension
48 {
49  template <typename L, typename... Ts>
50  static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&...iterators)
51  {
52  const auto &d = w[dim - 1];
53 
54  for (auto v = d.start(); v < d.end(); v += d.step(), IncrementIterators<dim - 1>::unroll(iterators...))
55  {
56  id.set(dim - 1, v);
57  ForEachDimension<dim - 1>::unroll(w, id, lambda_function, iterators...);
58  }
59  }
60 };
61 
62 template <>
63 struct ForEachDimension<0>
64 {
65  template <typename L, typename... Ts>
66  static void unroll(const Window &w, Coordinates &id, L &&lambda_function, Ts &&...iterators)
67  {
68  ARM_COMPUTE_UNUSED(w, iterators...);
69  lambda_function(id);
70  }
71 };
72 
73 template <typename L, typename... Ts>
74 inline void execute_window_loop(const Window &w, L &&lambda_function, Ts &&...iterators)
75 {
76  w.validate();
77 
78  for (unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i)
79  {
80  ARM_COMPUTE_ERROR_ON(w[i].step() == 0);
81  }
82 
83  Coordinates id;
84  ForEachDimension<Coordinates::num_max_dimensions>::unroll(w, id, std::forward<L>(lambda_function),
85  std::forward<Ts>(iterators)...);
86 }
87 
88 inline constexpr Iterator::Iterator() : _ptr(nullptr), _dims()
89 {
90 }
91 
92 inline Iterator::Iterator(const ITensor *tensor, const Window &win) : Iterator()
93 {
94  ARM_COMPUTE_ERROR_ON(tensor == nullptr);
95  ARM_COMPUTE_ERROR_ON(tensor->info() == nullptr);
96 
97  initialize(tensor->info()->num_dimensions(), tensor->info()->strides_in_bytes(), tensor->buffer(),
98  tensor->info()->offset_first_element_in_bytes(), win);
99 }
100 
101 inline Iterator::Iterator(size_t num_dims, const Strides &strides, uint8_t *buffer, size_t offset, const Window &win)
102  : Iterator()
103 {
104  initialize(num_dims, strides, buffer, offset, win);
105 }
106 
107 inline void
108 Iterator::initialize(size_t num_dims, const Strides &strides, uint8_t *buffer, size_t offset, const Window &win)
109 {
110  ARM_COMPUTE_ERROR_ON(buffer == nullptr);
111 
112  _ptr = buffer + offset;
113 
114  //Initialize the stride for each dimension and calculate the position of the first element of the iteration:
115  for (unsigned int n = 0; n < num_dims; ++n)
116  {
117  _dims[n]._stride = win[n].step() * strides[n];
118  std::get<0>(_dims)._dim_start += static_cast<size_t>(strides[n]) * win[n].start();
119  }
120 
121  //Copy the starting point to all the dimensions:
122  for (unsigned int n = 1; n < Coordinates::num_max_dimensions; ++n)
123  {
124  _dims[n]._dim_start = std::get<0>(_dims)._dim_start;
125  }
126 
128 }
129 
130 inline void Iterator::increment(const size_t dimension)
131 {
133 
134  _dims[dimension]._dim_start += _dims[dimension]._stride;
135 
136  for (unsigned int n = 0; n < dimension; ++n)
137  {
138  _dims[n]._dim_start = _dims[dimension]._dim_start;
139  }
140 }
141 
142 inline constexpr size_t Iterator::offset() const
143 {
144  return _dims.at(0)._dim_start;
145 }
146 
147 inline constexpr uint8_t *Iterator::ptr() const
148 {
149  return _ptr + _dims.at(0)._dim_start;
150 }
151 
152 inline void Iterator::reset(const size_t dimension)
153 {
155 
156  _dims[dimension]._dim_start = _dims[dimension + 1]._dim_start;
157 
158  for (unsigned int n = 0; n < dimension; ++n)
159  {
160  _dims[n]._dim_start = _dims[dimension]._dim_start;
161  }
162 }
163 
164 inline Coordinates index2coords(const TensorShape &shape, int index)
165 {
166  int num_elements = shape.total_size();
167 
168  ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]!");
169  ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape!");
170 
171  Coordinates coord{0};
172 
173  for (int d = shape.num_dimensions() - 1; d >= 0; --d)
174  {
175  num_elements /= shape[d];
176  coord.set(d, index / num_elements);
177  index %= num_elements;
178  }
179 
180  return coord;
181 }
182 
183 inline int coords2index(const TensorShape &shape, const Coordinates &coord)
184 {
185  int num_elements = shape.total_size();
186  ARM_COMPUTE_UNUSED(num_elements);
187  ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create linear index from empty shape!");
188 
189  int index = 0;
190  int stride = 1;
191 
192  for (unsigned int d = 0; d < coord.num_dimensions(); ++d)
193  {
194  index += coord[d] * stride;
195  stride *= shape[d];
196  }
197 
198  return index;
199 }
200 
202  const DataLayoutDimension &data_layout_dimension)
203 {
205  "Cannot retrieve the dimension index for an unknown layout!");
206  const auto &dims = get_layout_map().at(data_layout);
207  const auto &it = std::find(dims.cbegin(), dims.cend(), data_layout_dimension);
208  ARM_COMPUTE_ERROR_ON_MSG(it == dims.cend(), "Invalid dimension for the given layout.");
209  return it - dims.cbegin();
210 }
211 
213 {
215  "Cannot retrieve the layout dimension for an unknown layout!");
216  const auto &dims = get_layout_map().at(data_layout);
217  ARM_COMPUTE_ERROR_ON_MSG(index >= dims.size(), "Invalid index for the given layout.");
218  return dims[index];
219 }
220 } // namespace arm_compute
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:110
arm_compute::Iterator::increment
void increment(size_t dimension)
Increment the iterator along the specified dimension of the step value associated to the dimension.
Definition: Helpers.inl:130
arm_compute::DataLayoutDimension
DataLayoutDimension
[DataLayout enum definition]
Definition: CoreTypes.h:121
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::index2coords
Coordinates index2coords(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
Definition: Helpers.inl:164
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
Error.h
arm_compute::cpu::data_layout
constexpr auto data_layout
Definition: impl.h:36
arm_compute::Strides
Strides of an item in bytes.
Definition: Strides.h:38
arm_compute::test::validation::shape
shape
Definition: DFT.cpp:115
ARM_COMPUTE_ERROR_ON
#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
arm_compute::Iterator::ptr
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:147
arm_compute::get_layout_map
const std::map< DataLayout, std::vector< DataLayoutDimension > > & get_layout_map()
Returns a static map used to find an index or dimension based on a data layout.
Definition: Helpers.cpp:107
arm_compute::execute_window_loop
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:74
arm_compute::Iterator
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
ARM_COMPUTE_ERROR_ON_MSG
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
arm_compute::Iterator::Iterator
constexpr Iterator()
Default constructor to create an empty iterator.
Definition: Helpers.inl:88
arm_compute::test::validation::w
SimpleTensor< float > w
Definition: DFT.cpp:156
offset
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1128
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
tensor
CLTensor * tensor
Pointer to the auxiliary tensor.
Definition: ClWorkloadRuntime.cpp:67
arm_compute::Iterator::reset
void reset(size_t dimension)
Move the iterator back to the beginning of the specified dimension.
Definition: Helpers.inl:152
arm_compute::get_data_layout_dimension_index
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:201
arm_compute::coords2index
int coords2index(const TensorShape &shape, const Coordinates &coord)
Convert n-dimensional coordinates into a linear index.
Definition: Helpers.inl:183
arm_compute::Window
Describe a multidimensional execution window.
Definition: Window.h:39
arm_compute::Iterator::offset
constexpr size_t offset() const
Return the offset in bytes from the first element to the current position of the iterator.
Definition: Helpers.inl:142
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE
#define ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(w, md)
Definition: Validate.h:263
arm_compute::DataLayout::UNKNOWN
@ UNKNOWN
Unknown data layout.
arm_compute::cpu::step
constexpr int step
Definition: fp32.cpp:35
arm_compute::utility::for_each
void for_each(F &&)
Base case of for_each.
Definition: Utility.h:111
arm_compute::Dimensions::num_dimensions
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:142
arm_compute::get_index_data_layout_dimension
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:212
arm_compute::Dimensions< int >::num_max_dimensions
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46