Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 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_WINDOW_H |
| 25 | #define ARM_COMPUTE_WINDOW_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include <algorithm> |
| 28 | #include <array> |
| 29 | #include <cstddef> |
| 30 | |
| 31 | #include "arm_compute/core/Coordinates.h" |
| 32 | #include "arm_compute/core/Error.h" |
| 33 | #include "arm_compute/core/ITensorInfo.h" |
| 34 | #include "arm_compute/core/Utils.h" |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | /** Describe a multidimensional execution window. */ |
| 39 | class Window |
| 40 | { |
| 41 | public: |
| 42 | /** Alias for dimension 0 also known as X dimension */ |
| 43 | static constexpr size_t DimX = 0; |
| 44 | /** Alias for dimension 1 also known as Y dimension */ |
| 45 | static constexpr size_t DimY = 1; |
| 46 | /** Alias for dimension 2 also known as Z dimension */ |
| 47 | static constexpr size_t DimZ = 2; |
| 48 | |
| 49 | /** Default constructor: create a window containing a single element. */ |
| 50 | constexpr Window() |
Sang-Hoon Park | cecb0a7 | 2019-09-17 08:59:09 +0100 | [diff] [blame] | 51 | : _dims(), _is_broadcasted(utility::generate_array<bool, Coordinates::num_max_dimensions, false>::value) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | /** Copy constructor |
| 55 | * |
| 56 | * @param[in] src Copy the values from src to a new object |
| 57 | */ |
| 58 | Window(const Window &src); |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 59 | /** Copy assignment operator |
| 60 | * |
| 61 | * @param[in] rhs Copy the values from rhs to the current object |
| 62 | * |
| 63 | * @return Reference to the updated object |
| 64 | */ |
| 65 | Window &operator=(const Window &rhs); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | |
| 67 | /** Describe one of the image's dimensions with a start, end and step. |
| 68 | * |
| 69 | * Iteration through the elements of the dimension is done like this: |
| 70 | * for(int v = start(); v < end(); v += step()) |
| 71 | * { |
| 72 | * ... |
| 73 | * } |
| 74 | */ |
| 75 | class Dimension |
| 76 | { |
| 77 | public: |
| 78 | /** Constructor, by default creates a dimension of 1. |
| 79 | * |
| 80 | * @param[in] start Start of the dimension |
| 81 | * @param[in] end End of the dimension |
| 82 | * @param[in] step Step between two elements of the dimension when iterating. |
| 83 | * |
| 84 | */ |
| 85 | constexpr Dimension(int start = 0, int end = 1, int step = 1) |
| 86 | : _start(start), _end(end), _step(step) |
| 87 | { |
| 88 | } |
| 89 | /** Default assignment operator to allow dimensions to be copied */ |
| 90 | Dimension &operator=(const Dimension &d) = default; |
| 91 | /** Return the start of the dimension */ |
| 92 | constexpr int start() const |
| 93 | { |
| 94 | return _start; |
| 95 | } |
| 96 | /** Return the end of the dimension */ |
| 97 | constexpr int end() const |
| 98 | { |
| 99 | return _end; |
| 100 | } |
| 101 | /** Return the step of the dimension */ |
| 102 | constexpr int step() const |
| 103 | { |
| 104 | return _step; |
| 105 | } |
| 106 | /** Set the dimension's step |
| 107 | * |
| 108 | * @param[in] step The new step |
| 109 | */ |
| 110 | void set_step(int step) |
| 111 | { |
| 112 | _step = step; |
| 113 | } |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 114 | /** Set the dimension's end |
| 115 | * |
| 116 | * @param[in] end The new end |
| 117 | */ |
| 118 | void set_end(int end) |
| 119 | { |
| 120 | _end = end; |
| 121 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | int _start; /**< Start of the dimension */ |
| 125 | int _end; /**< End of the dimension */ |
| 126 | int _step; |
| 127 | }; |
| 128 | |
| 129 | /** Read only access to a given dimension of the window |
| 130 | * |
| 131 | * @note Precondition: dimension < Coordinates::num_max_dimensions |
| 132 | * |
| 133 | * @param[in] dimension The dimension to access |
| 134 | * |
| 135 | * @return The requested dimension |
| 136 | */ |
| 137 | constexpr const Dimension &operator[](size_t dimension) const; |
| 138 | |
| 139 | /** Alias to access the first dimension of the window |
| 140 | * |
| 141 | * @return First dimension of the window |
| 142 | */ |
| 143 | constexpr const Dimension &x() const |
| 144 | { |
| 145 | return _dims.at(Window::DimX); |
| 146 | } |
| 147 | |
| 148 | /** Alias to access the second dimension of the window |
| 149 | * |
| 150 | * @return Second dimension of the window |
| 151 | */ |
| 152 | constexpr const Dimension &y() const |
| 153 | { |
| 154 | return _dims.at(Window::DimY); |
| 155 | } |
| 156 | |
| 157 | /** Alias to access the third dimension of the window |
| 158 | * |
| 159 | * @return Third dimension of the window |
| 160 | */ |
| 161 | constexpr const Dimension &z() const |
| 162 | { |
| 163 | return _dims.at(Window::DimZ); |
| 164 | } |
| 165 | |
| 166 | /** Set the values of a given dimension |
| 167 | * |
| 168 | * @param[in] dimension The dimension to set |
| 169 | * @param[in] dim The values to set the dimension to |
| 170 | */ |
| 171 | void set(size_t dimension, const Dimension &dim); |
| 172 | |
Sang-Hoon Park | cecb0a7 | 2019-09-17 08:59:09 +0100 | [diff] [blame] | 173 | /** Set the dimension as broadcasted dimension |
| 174 | * |
| 175 | * @param[in] dimension The dimension to set |
| 176 | */ |
| 177 | void set_broadcasted(size_t dimension); |
| 178 | |
| 179 | /** Return whether a dimension has been broadcasted |
| 180 | * |
| 181 | * @param[in] dimension The requested dimension |
| 182 | * |
| 183 | * @return true if the dimension has been broadcasted |
| 184 | */ |
| 185 | bool is_broadcasted(size_t dimension) const; |
| 186 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | /** Use the tensor's dimensions to fill the window dimensions. |
| 188 | * |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 189 | * @param[in] shape @ref TensorShape to copy the dimensions from. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 190 | * @param[in] first_dimension Only copy dimensions which are greater or equal to this value. |
| 191 | */ |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 192 | void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension = Window::DimX); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 193 | |
| 194 | /** Shift the values of a given dimension by the given shift_value |
| 195 | * |
| 196 | * @param[in] dimension The dimension to shift |
| 197 | * @param[in] shift_value Value to shift the start and end values of. |
| 198 | */ |
| 199 | void shift(size_t dimension, int shift_value); |
| 200 | |
Michalis Spyrou | 995f552 | 2018-01-29 13:43:35 +0000 | [diff] [blame] | 201 | /** Shift down all the dimensions of a window |
| 202 | * |
| 203 | * i.e new_dims[n] = old_dims[n+shift_value]. |
| 204 | * |
| 205 | * @param[in] shift_value Number of dimensions to shift the window by. |
| 206 | * |
| 207 | * @return The window with the shifted dimensions. |
| 208 | */ |
| 209 | Window shift_dimensions(unsigned int shift_value) const; |
| 210 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 211 | /** Adjust the start or end of a given dimension by the given value |
| 212 | * |
| 213 | * @param[in] dimension The dimension to adjust |
| 214 | * @param[in] adjust_value The adjusted value. |
| 215 | * @param[in] is_at_start The flag to indicate whether adjust the start or end of the dimension. |
| 216 | */ |
| 217 | void adjust(size_t dimension, int adjust_value, bool is_at_start); |
| 218 | |
| 219 | /** Scale the values of a given dimension by the given scale_value |
| 220 | * |
| 221 | * @note The end of the window is rounded up to be a multiple of step after the scaling. |
| 222 | * |
| 223 | * @param[in] dimension The dimension to scale |
| 224 | * @param[in] scale_value Value to scale the start, end and step values of. |
| 225 | */ |
| 226 | void scale(size_t dimension, float scale_value); |
| 227 | |
| 228 | /** Set the step of a given dimension. |
| 229 | * |
| 230 | * @param[in] dimension Dimension to update |
| 231 | * @param[in] step The new dimension's step value |
| 232 | */ |
| 233 | void set_dimension_step(size_t dimension, int step); |
| 234 | |
| 235 | /** Will validate all the window's dimensions' values when asserts are enabled |
| 236 | * |
| 237 | * No-op when asserts are disabled |
| 238 | */ |
| 239 | void validate() const; |
| 240 | |
| 241 | /** Return the number of iterations needed to iterate through a given dimension |
| 242 | * |
| 243 | * @param[in] dimension The requested dimension |
| 244 | * |
| 245 | * @return The number of iterations |
| 246 | */ |
| 247 | constexpr size_t num_iterations(size_t dimension) const; |
Anthony Barbier | 671a11e | 2018-07-06 15:11:36 +0100 | [diff] [blame] | 248 | /** Return the total number of iterations needed to iterate through the entire window |
| 249 | * |
| 250 | * @return Number of total iterations |
| 251 | */ |
| 252 | size_t num_iterations_total() const; |
| 253 | /** Return the shape of the window in number of steps */ |
| 254 | TensorShape shape() const; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 255 | /** Split a window into a set of sub windows along a given dimension |
| 256 | * |
| 257 | * For example to split a window into 3 sub-windows along the Y axis, you would have to do:<br/> |
| 258 | * Window sub0 = window.split_window( 1, 0, 3);<br/> |
| 259 | * Window sub1 = window.split_window( 1, 1, 3);<br/> |
| 260 | * Window sub2 = window.split_window( 1, 2, 3);<br/> |
| 261 | * |
| 262 | * @param[in] dimension Dimension along which the split will be performed |
| 263 | * @param[in] id Id of the sub-window to return. Must be in the range (0, total-1) |
| 264 | * @param[in] total Total number of sub-windows the window will be split into. |
| 265 | * |
| 266 | * @return The subwindow "id" out of "total" |
| 267 | */ |
| 268 | Window split_window(size_t dimension, size_t id, size_t total) const; |
| 269 | /** First 1D slice of the window |
| 270 | * |
| 271 | * @return The first slice of the window. |
| 272 | */ |
| 273 | Window first_slice_window_1D() const |
| 274 | { |
| 275 | return first_slice_window<1>(); |
| 276 | }; |
| 277 | /** First 2D slice of the window |
| 278 | * |
| 279 | * @return The first slice of the window. |
| 280 | */ |
| 281 | Window first_slice_window_2D() const |
| 282 | { |
| 283 | return first_slice_window<2>(); |
| 284 | }; |
| 285 | /** First 3D slice of the window |
| 286 | * |
| 287 | * @return The first slice of the window. |
| 288 | */ |
| 289 | Window first_slice_window_3D() const |
| 290 | { |
| 291 | return first_slice_window<3>(); |
| 292 | }; |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 293 | /** First 4D slice of the window |
| 294 | * |
| 295 | * @return The first slice of the window. |
| 296 | */ |
| 297 | Window first_slice_window_4D() const |
| 298 | { |
| 299 | return first_slice_window<4>(); |
| 300 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 301 | /** Slide the passed 1D window slice. |
| 302 | * |
| 303 | * If slice contains the last slice then it will remain unchanged and false will be returned. |
| 304 | * |
| 305 | * @param[in,out] slice Current slice, to be updated to the next slice. |
| 306 | * |
| 307 | * @return true if slice contains a new slice, false if slice already contained the last slice |
| 308 | */ |
| 309 | bool slide_window_slice_1D(Window &slice) const |
| 310 | { |
| 311 | return slide_window_slice<1>(slice); |
| 312 | } |
| 313 | /** Slide the passed 2D window slice. |
| 314 | * |
| 315 | * If slice contains the last slice then it will remain unchanged and false will be returned. |
| 316 | * |
| 317 | * @param[in,out] slice Current slice, to be updated to the next slice. |
| 318 | * |
| 319 | * @return true if slice contains a new slice, false if slice already contained the last slice |
| 320 | */ |
| 321 | bool slide_window_slice_2D(Window &slice) const |
| 322 | { |
| 323 | return slide_window_slice<2>(slice); |
| 324 | } |
| 325 | /** Slide the passed 3D window slice. |
| 326 | * |
| 327 | * If slice contains the last slice then it will remain unchanged and false will be returned. |
| 328 | * |
| 329 | * @param[in,out] slice Current slice, to be updated to the next slice. |
| 330 | * |
| 331 | * @return true if slice contains a new slice, false if slice already contained the last slice |
| 332 | */ |
| 333 | bool slide_window_slice_3D(Window &slice) const |
| 334 | { |
| 335 | return slide_window_slice<3>(slice); |
| 336 | } |
| 337 | /** Slide the passed 4D window slice. |
| 338 | * |
| 339 | * If slice contains the last slice then it will remain unchanged and false will be returned. |
| 340 | * |
| 341 | * @param[in,out] slice Current slice, to be updated to the next slice. |
| 342 | * |
| 343 | * @return true if slice contains a new slice, false if slice already contained the last slice |
| 344 | */ |
| 345 | bool slide_window_slice_4D(Window &slice) const |
| 346 | { |
| 347 | return slide_window_slice<4>(slice); |
| 348 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 349 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 350 | /** Collapse the dimensions between @p first and @p last if possible. |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 351 | * |
| 352 | * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window |
| 353 | * |
| 354 | * @param[in] full_window Full window @p window has been created from. |
| 355 | * @param[in] first Start dimension into which the following are collapsed. |
| 356 | * @param[in] last End (exclusive) dimension to collapse. |
| 357 | * @param[out] has_collapsed (Optional) Whether the window was collapsed. |
| 358 | * |
| 359 | * @return Collapsed window. |
| 360 | */ |
| 361 | Window collapse_if_possible(const Window &full_window, size_t first, size_t last, bool *has_collapsed = nullptr) const; |
| 362 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 363 | /** Collapse the dimensions higher than @p first if possible. |
Anthony Barbier | be35b0e | 2017-07-11 18:13:08 +0100 | [diff] [blame] | 364 | * |
| 365 | * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window |
| 366 | * |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 367 | * @param[in] full_window Full window @p window has been created from. |
| 368 | * @param[in] first Start dimension into which the following are collapsed. |
| 369 | * @param[out] has_collapsed (Optional) Whether the window was collapsed. |
Anthony Barbier | be35b0e | 2017-07-11 18:13:08 +0100 | [diff] [blame] | 370 | * |
| 371 | * @return Collapsed window. |
| 372 | */ |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 373 | Window collapse_if_possible(const Window &full_window, size_t first, bool *has_collapsed = nullptr) const |
| 374 | { |
| 375 | return collapse_if_possible(full_window, first, Coordinates::num_max_dimensions, has_collapsed); |
| 376 | } |
Anthony Barbier | be35b0e | 2017-07-11 18:13:08 +0100 | [diff] [blame] | 377 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 378 | /** Collapse the dimensions between @p first and @p last. |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 379 | * |
| 380 | * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window |
| 381 | * |
| 382 | * @param[in] full_window Full window @p window has been created from. |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 383 | * @param[in] first Start dimension into which the following are collapsed. |
| 384 | * @param[in] last End (exclusive) dimension to collapse. |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 385 | * |
| 386 | * @return Collapsed window if successful. |
| 387 | */ |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 388 | Window collapse(const Window &full_window, size_t first, size_t last = Coordinates::num_max_dimensions) const; |
| 389 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 390 | /** Don't advance in the dimension where @p shape is less equal to 1. |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 391 | * |
| 392 | * @param[in] shape A TensorShape. |
| 393 | * |
| 394 | * @return Broadcast window. |
| 395 | */ |
| 396 | Window broadcast_if_dimension_le_one(const TensorShape &shape) const; |
| 397 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 398 | /** Don't advance in the dimension where shape of @p info is less equal to 1. |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 399 | * |
| 400 | * @param[in] info An ITensorInfo. |
| 401 | * |
| 402 | * @return Broadcast window. |
| 403 | */ |
| 404 | Window broadcast_if_dimension_le_one(const ITensorInfo &info) const |
| 405 | { |
| 406 | return broadcast_if_dimension_le_one(info.tensor_shape()); |
| 407 | } |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 408 | /** Friend function that swaps the contents of two windows |
| 409 | * |
| 410 | * @param[in] lhs First window to swap. |
| 411 | * @param[in] rhs Second window to swap. |
| 412 | */ |
| 413 | friend void swap(Window &lhs, Window &rhs); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 414 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 415 | private: |
| 416 | /** First slice of the window |
| 417 | * |
| 418 | * @return The first slice of the window. |
| 419 | */ |
| 420 | template <unsigned int window_dimension> |
| 421 | Window first_slice_window() const; |
| 422 | |
| 423 | /** Slide the passed window slice. |
| 424 | * |
| 425 | * If slice contains the last slice then it will remain unchanged and false will be returned. |
| 426 | * |
| 427 | * @param[in,out] slice Current slice, to be updated to the next slice. |
| 428 | * |
| 429 | * @return true if slice contains a new slice, false if slice already contained the last slice |
| 430 | */ |
| 431 | template <unsigned int window_dimension> |
| 432 | bool slide_window_slice(Window &slice) const; |
| 433 | |
| 434 | private: |
| 435 | std::array<Dimension, Coordinates::num_max_dimensions> _dims; |
Sang-Hoon Park | cecb0a7 | 2019-09-17 08:59:09 +0100 | [diff] [blame] | 436 | std::array<bool, Coordinates::num_max_dimensions> _is_broadcasted; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 437 | }; |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 438 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 439 | #include "Window.inl" |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 440 | #endif /*ARM_COMPUTE_WINDOW_H */ |