Compute Library
 19.08
Dimensions.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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_DIMENSIONS_H__
25 #define __ARM_COMPUTE_DIMENSIONS_H__
26 
27 #include "arm_compute/core/Error.h"
28 
29 #include <algorithm>
30 #include <array>
31 #include <functional>
32 #include <numeric>
33 
34 namespace arm_compute
35 {
37 constexpr size_t MAX_DIMS = 6;
38 
40 template <typename T>
42 {
43 public:
45  static constexpr size_t num_max_dimensions = MAX_DIMS;
46 
51  template <typename... Ts>
52  explicit Dimensions(Ts... dims)
53  : _id{ { static_cast<T>(dims)... } }, _num_dimensions{ sizeof...(dims) }
54  {
55  }
56 
58  Dimensions(const Dimensions &) = default;
59 
61  Dimensions &operator=(const Dimensions &) = default;
62 
64  Dimensions(Dimensions &&) = default;
65 
67  Dimensions &operator=(Dimensions &&) = default;
68 
74  void set(size_t dimension, T value)
75  {
77  _id[dimension] = value;
78  _num_dimensions = std::max(_num_dimensions, dimension + 1);
79  }
81  T x() const
82  {
83  return _id[0];
84  }
86  T y() const
87  {
88  return _id[1];
89  }
91  T z() const
92  {
93  return _id[2];
94  }
103  const T &operator[](size_t dimension) const
104  {
106  return _id[dimension];
107  }
116  T &operator[](size_t dimension)
117  {
119  return _id[dimension];
120  }
122  unsigned int num_dimensions() const
123  {
124  return _num_dimensions;
125  }
126 
129  {
130  _num_dimensions = num_dimensions;
131  }
132 
138  void collapse(const size_t n, const size_t first = 0)
139  {
140  ARM_COMPUTE_ERROR_ON(first + n > _id.size());
141 
142  const size_t last = std::min(_num_dimensions, first + n);
143 
144  if(last > (first + 1))
145  {
146  // Collapse dimensions into the first
147  _id[first] = std::accumulate(&_id[first], &_id[last], 1, std::multiplies<T>());
148  // Shift the remaining dimensions down
149  std::copy(&_id[last], &_id[_num_dimensions], &_id[first + 1]);
150  // Reduce the number of dimensions
151  const size_t old_num_dimensions = _num_dimensions;
152  _num_dimensions -= last - first - 1;
153  // Fill the now empty dimensions with zero
154  std::fill(&_id[_num_dimensions], &_id[old_num_dimensions], 0);
155  }
156  }
157 
162  void collapse_from(size_t start)
163  {
165 
166  collapse(num_dimensions() - start, start);
167  }
168 
175  void remove(size_t idx)
176  {
177  ARM_COMPUTE_ERROR_ON(_num_dimensions < 1);
178  if(idx >= _num_dimensions)
179  {
180  return;
181  }
182 
183  std::copy(_id.begin() + idx + 1, _id.end(), _id.begin() + idx);
184  _num_dimensions--;
185 
186  // Make sure all empty dimensions are filled with 0
187  std::fill(_id.begin() + _num_dimensions, _id.end(), 0);
188  }
189 
194  typename std::array<T, num_max_dimensions>::iterator begin()
195  {
196  return _id.begin();
197  }
202  typename std::array<T, num_max_dimensions>::const_iterator begin() const
203  {
204  return _id.begin();
205  }
210  typename std::array<T, num_max_dimensions>::const_iterator cbegin() const
211  {
212  return begin();
213  }
218  typename std::array<T, num_max_dimensions>::iterator end()
219  {
220  return _id.end();
221  }
226  typename std::array<T, num_max_dimensions>::const_iterator end() const
227  {
228  return _id.end();
229  }
234  typename std::array<T, num_max_dimensions>::const_iterator cend() const
235  {
236  return end();
237  }
238 
239 protected:
241  ~Dimensions() = default;
242 
243  std::array<T, num_max_dimensions> _id;
244  size_t _num_dimensions{ 0 };
245 };
246 
254 template <typename T>
255 inline bool operator==(const Dimensions<T> &lhs, const Dimensions<T> &rhs)
256 {
257  return ((lhs.num_dimensions() == rhs.num_dimensions()) && std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin()));
258 }
266 template <typename T>
267 inline bool operator!=(const Dimensions<T> &lhs, const Dimensions<T> &rhs)
268 {
269  return !(lhs == rhs);
270 }
271 }
272 #endif /*__ARM_COMPUTE_DIMENSIONS_H__*/
std::array< T, num_max_dimensions >::const_iterator begin() const
Returns a read-only (constant) iterator that points to the first element in the dimension array.
Definition: Dimensions.h:202
T & operator[](size_t dimension)
Generic accessor to get the size of any dimension.
Definition: Dimensions.h:116
Dimensions & operator=(const Dimensions &)=default
Allow instances of this class to be copied.
bool operator!=(const Dimensions< T > &lhs, const Dimensions< T > &rhs)
Check that given dimensions are not equal.
Definition: Dimensions.h:267
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
void remove(size_t idx)
Remove dimension of a given index.
Definition: Dimensions.h:175
bool operator==(const Dimensions< T > &lhs, const Dimensions< T > &rhs)
Check that given dimensions are equal.
Definition: Dimensions.h:255
SimpleTensor< T > copy(const SimpleTensor< T > &src, const TensorShape &output_shape)
Definition: Copy.cpp:37
void set(size_t dimension, T value)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:74
void collapse(const size_t n, const size_t first=0)
Collapse dimensions.
Definition: Dimensions.h:138
Copyright (c) 2017-2018 ARM Limited.
constexpr size_t MAX_DIMS
Constant value used to indicate maximum dimensions of a Window, TensorShape and Coordinates.
Definition: Dimensions.h:37
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:81
void collapse_from(size_t start)
Collapse dimensions starting from a given point.
Definition: Dimensions.h:162
library fill(src, distribution, 0)
T z() const
Alias to access the size of the third dimension.
Definition: Dimensions.h:91
Dimensions with dimensionality.
Definition: Dimensions.h:41
std::array< T, num_max_dimensions >::iterator begin()
Returns a read/write iterator that points to the first element in the dimension array.
Definition: Dimensions.h:194
std::array< T, num_max_dimensions >::const_iterator cend() const
Returns a read-only (constant) iterator that points one past the last element in the dimension array.
Definition: Dimensions.h:234
std::array< T, num_max_dimensions >::const_iterator cbegin() const
Returns a read-only (constant) iterator that points to the first element in the dimension array.
Definition: Dimensions.h:210
const T & operator[](size_t dimension) const
Generic accessor to get the size of any dimension.
Definition: Dimensions.h:103
std::array< T, num_max_dimensions >::iterator end()
Returns a read/write iterator that points one past the last element in the dimension array.
Definition: Dimensions.h:218
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
Dimensions(Ts... dims)
Constructor to initialize the tensor shape.
Definition: Dimensions.h:52
__kernel void accumulate(__global uchar *input_ptr, uint input_stride_x, uint input_step_x, uint input_stride_y, uint input_step_y, uint input_offset_first_element_in_bytes, __global uchar *accu_ptr, uint accu_stride_x, uint accu_step_x, uint accu_stride_y, uint accu_step_y, uint accu_offset_first_element_in_bytes)
This function accumulates an input image into output image.
Definition: accumulate.cl:41
std::array< T, num_max_dimensions >::const_iterator end() const
Returns a read-only (constant) iterator that points one past the last element in the dimension array.
Definition: Dimensions.h:226
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:128
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:86
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45