Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | b309fc2 | 2021-01-05 09:46:16 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_DIMENSIONS_H |
| 25 | #define ARM_COMPUTE_DIMENSIONS_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 36 | /** Constant value used to indicate maximum dimensions of a Window, TensorShape and Coordinates */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | constexpr size_t MAX_DIMS = 6; |
| 38 | |
| 39 | /** Dimensions with dimensionality */ |
| 40 | template <typename T> |
| 41 | class Dimensions |
| 42 | { |
| 43 | public: |
| 44 | /** Number of dimensions the tensor has */ |
| 45 | static constexpr size_t num_max_dimensions = MAX_DIMS; |
| 46 | |
| 47 | /** Constructor to initialize the tensor shape. |
| 48 | * |
| 49 | * @param[in] dims Values to initialize the dimensions. |
| 50 | */ |
| 51 | template <typename... Ts> |
Moritz Pflanzer | b6c8d24 | 2017-07-18 13:42:54 +0100 | [diff] [blame] | 52 | explicit Dimensions(Ts... dims) |
Anthony Barbier | a8a28f6 | 2018-02-26 19:16:32 +0000 | [diff] [blame] | 53 | : _id{ { static_cast<T>(dims)... } }, _num_dimensions{ sizeof...(dims) } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
| 57 | /** Allow instances of this class to be copy constructed */ |
| 58 | Dimensions(const Dimensions &) = default; |
| 59 | |
| 60 | /** Allow instances of this class to be copied */ |
| 61 | Dimensions &operator=(const Dimensions &) = default; |
| 62 | |
| 63 | /** Allow instances of this class to be move constructed */ |
| 64 | Dimensions(Dimensions &&) = default; |
| 65 | |
| 66 | /** Allow instances of this class to be moved */ |
| 67 | Dimensions &operator=(Dimensions &&) = default; |
| 68 | |
| 69 | /** Accessor to set the value of one of the dimensions. |
| 70 | * |
Giorgio Arena | 15bc848 | 2020-12-08 14:34:00 +0000 | [diff] [blame] | 71 | * @param[in] dimension Dimension for which the value is set. |
| 72 | * @param[in] value Value to be set for the dimension. |
Giorgio Arena | ec241b4 | 2020-12-11 13:39:02 +0000 | [diff] [blame] | 73 | * @param[in] increase_dim_unit (Optional) Set to true if new unit dimensions increase the number of dimensions (e.g. for Coordinates), false otherwise (e.g. for TensorShapes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | */ |
Giorgio Arena | 15bc848 | 2020-12-08 14:34:00 +0000 | [diff] [blame] | 75 | void set(size_t dimension, T value, bool increase_dim_unit = true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | { |
| 77 | ARM_COMPUTE_ERROR_ON(dimension >= num_max_dimensions); |
Giorgio Arena | 15bc848 | 2020-12-08 14:34:00 +0000 | [diff] [blame] | 78 | _id[dimension] = value; |
| 79 | // Don't increase the number of dimensions if the new dimension is 1 |
| 80 | if(increase_dim_unit || value != 1) |
| 81 | { |
| 82 | _num_dimensions = std::max(_num_dimensions, dimension + 1); |
| 83 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | } |
| 85 | /** Alias to access the size of the first dimension */ |
| 86 | T x() const |
| 87 | { |
| 88 | return _id[0]; |
| 89 | } |
| 90 | /** Alias to access the size of the second dimension */ |
| 91 | T y() const |
| 92 | { |
| 93 | return _id[1]; |
| 94 | } |
| 95 | /** Alias to access the size of the third dimension */ |
| 96 | T z() const |
| 97 | { |
| 98 | return _id[2]; |
| 99 | } |
Giorgio Arena | b309fc2 | 2021-01-05 09:46:16 +0000 | [diff] [blame] | 100 | /** Increments the given dimension by a step size, avoiding overflows |
| 101 | * |
| 102 | * @note Precondition: dim < _num_dimensions |
| 103 | * |
| 104 | * @param[in] dim Dimension to increment. |
| 105 | * @param[in] step Step to increment @p dim by. |
| 106 | */ |
| 107 | void increment(size_t dim, T step = 1) |
| 108 | { |
Giorgio Arena | 4d9383e | 2021-01-07 14:40:12 +0000 | [diff] [blame] | 109 | ARM_COMPUTE_ERROR_ON(dim >= _num_dimensions); |
| 110 | if((std::numeric_limits<T>::max() - _id[dim]) >= step) |
Giorgio Arena | b309fc2 | 2021-01-05 09:46:16 +0000 | [diff] [blame] | 111 | { |
| 112 | _id[dim] += step; |
| 113 | } |
| 114 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | /** Generic accessor to get the size of any dimension |
| 116 | * |
| 117 | * @note Precondition: dimension < Dimensions::num_max_dimensions |
| 118 | * |
| 119 | * @param[in] dimension Dimension of the wanted size |
| 120 | * |
| 121 | * @return The size of the requested dimension. |
| 122 | */ |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 123 | const T &operator[](size_t dimension) const |
| 124 | { |
| 125 | ARM_COMPUTE_ERROR_ON(dimension >= num_max_dimensions); |
| 126 | return _id[dimension]; |
| 127 | } |
| 128 | /** Generic accessor to get the size of any dimension |
| 129 | * |
| 130 | * @note Precondition: dimension < Dimensions::num_max_dimensions |
| 131 | * |
| 132 | * @param[in] dimension Dimension of the wanted size |
| 133 | * |
| 134 | * @return The size of the requested dimension. |
| 135 | */ |
| 136 | T &operator[](size_t dimension) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | { |
| 138 | ARM_COMPUTE_ERROR_ON(dimension >= num_max_dimensions); |
| 139 | return _id[dimension]; |
| 140 | } |
| 141 | /** Returns the effective dimensionality of the tensor */ |
| 142 | unsigned int num_dimensions() const |
| 143 | { |
| 144 | return _num_dimensions; |
| 145 | } |
| 146 | |
| 147 | /** Set number of dimensions */ |
| 148 | void set_num_dimensions(size_t num_dimensions) |
| 149 | { |
| 150 | _num_dimensions = num_dimensions; |
| 151 | } |
| 152 | |
| 153 | /** Collapse dimensions. |
| 154 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | * @param[in] n Number of dimensions to collapse into @p first. |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 156 | * @param[in] first Dimensions into which the following @p n are collapsed. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | */ |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 158 | void collapse(const size_t n, const size_t first = 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | { |
| 160 | ARM_COMPUTE_ERROR_ON(first + n > _id.size()); |
| 161 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 162 | const size_t last = std::min(_num_dimensions, first + n); |
Moritz Pflanzer | 1b31afc | 2017-07-05 15:54:42 +0100 | [diff] [blame] | 163 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 164 | if(last > (first + 1)) |
| 165 | { |
| 166 | // Collapse dimensions into the first |
| 167 | _id[first] = std::accumulate(&_id[first], &_id[last], 1, std::multiplies<T>()); |
| 168 | // Shift the remaining dimensions down |
| 169 | std::copy(&_id[last], &_id[_num_dimensions], &_id[first + 1]); |
| 170 | // Reduce the number of dimensions |
| 171 | const size_t old_num_dimensions = _num_dimensions; |
| 172 | _num_dimensions -= last - first - 1; |
| 173 | // Fill the now empty dimensions with zero |
| 174 | std::fill(&_id[_num_dimensions], &_id[old_num_dimensions], 0); |
| 175 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 178 | /** Collapse dimensions starting from a given point |
| 179 | * |
| 180 | * @param[in] start Starting point of collapsing dimensions |
| 181 | */ |
| 182 | void collapse_from(size_t start) |
| 183 | { |
| 184 | ARM_COMPUTE_ERROR_ON(start > num_dimensions()); |
| 185 | |
| 186 | collapse(num_dimensions() - start, start); |
| 187 | } |
| 188 | |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 189 | /** Remove dimension of a given index |
| 190 | * |
| 191 | * @note If index is greater than the number of dimensions no operation is performed |
| 192 | * |
| 193 | * @param[in] idx Dimension index to remove |
| 194 | */ |
| 195 | void remove(size_t idx) |
| 196 | { |
| 197 | ARM_COMPUTE_ERROR_ON(_num_dimensions < 1); |
| 198 | if(idx >= _num_dimensions) |
| 199 | { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | std::copy(_id.begin() + idx + 1, _id.end(), _id.begin() + idx); |
| 204 | _num_dimensions--; |
| 205 | |
| 206 | // Make sure all empty dimensions are filled with 0 |
| 207 | std::fill(_id.begin() + _num_dimensions, _id.end(), 0); |
| 208 | } |
| 209 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 210 | /** Returns a read/write iterator that points to the first element in the dimension array. |
| 211 | * |
| 212 | * @return an iterator. |
| 213 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | typename std::array<T, num_max_dimensions>::iterator begin() |
| 215 | { |
| 216 | return _id.begin(); |
| 217 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 218 | /** Returns a read-only (constant) iterator that points to the first element in the dimension array. |
| 219 | * |
| 220 | * @return an iterator. |
| 221 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 222 | typename std::array<T, num_max_dimensions>::const_iterator begin() const |
| 223 | { |
| 224 | return _id.begin(); |
| 225 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 226 | /** Returns a read-only (constant) iterator that points to the first element in the dimension array. |
| 227 | * |
| 228 | * @return an iterator. |
| 229 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 230 | typename std::array<T, num_max_dimensions>::const_iterator cbegin() const |
| 231 | { |
| 232 | return begin(); |
| 233 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 234 | /** Returns a read/write iterator that points one past the last element in the dimension array. |
| 235 | * |
| 236 | * @return an iterator. |
| 237 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 238 | typename std::array<T, num_max_dimensions>::iterator end() |
| 239 | { |
| 240 | return _id.end(); |
| 241 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 242 | /** Returns a read-only (constant) iterator that points one past the last element in the dimension array. |
| 243 | * |
| 244 | * @return an iterator. |
| 245 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 246 | typename std::array<T, num_max_dimensions>::const_iterator end() const |
| 247 | { |
| 248 | return _id.end(); |
| 249 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 250 | /** Returns a read-only (constant) iterator that points one past the last element in the dimension array. |
| 251 | * |
| 252 | * @return an iterator. |
| 253 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 254 | typename std::array<T, num_max_dimensions>::const_iterator cend() const |
| 255 | { |
| 256 | return end(); |
| 257 | } |
| 258 | |
| 259 | protected: |
| 260 | /** Protected destructor. */ |
| 261 | ~Dimensions() = default; |
| 262 | |
| 263 | std::array<T, num_max_dimensions> _id; |
| 264 | size_t _num_dimensions{ 0 }; |
| 265 | }; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 266 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 267 | /** Check that given dimensions are equal. |
| 268 | * |
| 269 | * @param[in] lhs Left-hand side Dimensions. |
| 270 | * @param[in] rhs Right-hand side Dimensions. |
| 271 | * |
| 272 | * @return True if the given dimensions are equal. |
| 273 | */ |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 274 | template <typename T> |
| 275 | inline bool operator==(const Dimensions<T> &lhs, const Dimensions<T> &rhs) |
| 276 | { |
| 277 | return ((lhs.num_dimensions() == rhs.num_dimensions()) && std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin())); |
| 278 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 279 | /** Check that given dimensions are not equal. |
| 280 | * |
| 281 | * @param[in] lhs Left-hand side Dimensions. |
| 282 | * @param[in] rhs Right-hand side Dimensions. |
| 283 | * |
| 284 | * @return True if the given dimensions are not equal. |
| 285 | */ |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 286 | template <typename T> |
| 287 | inline bool operator!=(const Dimensions<T> &lhs, const Dimensions<T> &rhs) |
| 288 | { |
| 289 | return !(lhs == rhs); |
| 290 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 291 | } |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 292 | #endif /*ARM_COMPUTE_DIMENSIONS_H*/ |