Anthony Barbier | 671a11e | 2018-07-06 15:11:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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_WINDOW_ITERATOR_H__ |
| 25 | #define __ARM_COMPUTE_WINDOW_ITERATOR_H__ |
| 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/Window.h" |
| 29 | |
| 30 | //FIXME: Delete the "PRINTF" before the release. In the meantime it's probably going to be useful to debug |
| 31 | //#define PRINTF printf |
| 32 | #define PRINTF(...) |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | /** Convert an offset in window steps into absolute coordinates. |
| 37 | * |
| 38 | * @param[in] w Window @p offset is related to. |
| 39 | * @param[in] offset Offset inside the window expressed in number of window steps. |
| 40 | * |
| 41 | * @return Absolute coordinates. |
| 42 | */ |
| 43 | inline Coordinates convert_window_coord_to_position(const Window &w, const Coordinates &offset) |
| 44 | { |
| 45 | Coordinates position; |
| 46 | for(unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i) |
| 47 | { |
| 48 | position.set(i, w[i].start() + offset[i] * w[i].step()); |
| 49 | } |
| 50 | return position; |
| 51 | } |
| 52 | |
| 53 | /** Tensor accessors to make it easier to interface with arm_gemm */ |
| 54 | template <typename T> |
| 55 | class TensorAccessor |
| 56 | { |
| 57 | public: |
| 58 | /** Constructor: |
| 59 | * |
| 60 | * @param[in] tensor Source tensor, must be allocated. |
| 61 | */ |
| 62 | TensorAccessor(const ITensor &tensor) |
| 63 | : _first(tensor.ptr_to_element(Coordinates())), _strides(tensor.info()->strides_in_bytes()) |
| 64 | { |
| 65 | } |
| 66 | /** Get the stride of the dimension dim expressed in number of Ts. |
| 67 | * |
| 68 | * @param[in] dim Dimension of the wanted stride. |
| 69 | * |
| 70 | * @return Stride in number of Ts. |
| 71 | */ |
| 72 | inline size_t stride(size_t dim) const |
| 73 | { |
| 74 | return _strides[dim] / sizeof(T); |
| 75 | } |
| 76 | |
| 77 | /** Returns a pointer to the element at coordinates (x,y,z,w) |
| 78 | * |
| 79 | * @param[in] x X coordinates |
| 80 | * @param[in] y (optional) Y coordinates |
| 81 | * @param[in] z (optional) Z coordinates |
| 82 | * @param[in] w (optional) W coordinates |
| 83 | */ |
| 84 | inline T *get_ptr(unsigned int x, unsigned int y = 0, unsigned int z = 0, unsigned int w = 0) |
| 85 | { |
| 86 | return reinterpret_cast<T *>(_first + x * _strides[0] + y * _strides[1] + z * _strides[2] + w * _strides[3]); |
| 87 | } |
| 88 | |
| 89 | /** Returns a pointer to the element at coordinates (x,y,z,w) |
| 90 | * |
| 91 | * @param[in] x X coordinates |
| 92 | * @param[in] y (optional) Y coordinates |
| 93 | * @param[in] z (optional) Z coordinates |
| 94 | * @param[in] w (optional) W coordinates |
| 95 | */ |
| 96 | inline T *operator()(unsigned int x, unsigned int y = 0, unsigned int z = 0, unsigned int w = 0) |
| 97 | { |
| 98 | return get_ptr(x, y, z, w); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | uint8_t *_first; /**< Pointer to the first element of the tensor.*/ |
| 103 | const Strides &_strides; /**< Strides in bytes of the tensor */ |
| 104 | }; |
| 105 | |
| 106 | /** Iterate over a portion of a Window */ |
| 107 | template <typename L> |
| 108 | class WindowIterator |
| 109 | { |
| 110 | public: |
| 111 | /** Construct a WindowIterator object |
| 112 | * |
| 113 | * @param[in] w Window to use for the iteration |
| 114 | * @param[in] start Where to start iterating from (In Window coordinates) |
| 115 | * @param[in] end Where to stop iterating (In Window coordinates). |
| 116 | * @param[in] lambda_function Lambda function to call for every iteration between start and end. (It will be called last for end - 1) |
| 117 | */ |
| 118 | WindowIterator(const Window &w, const Coordinates &start, const Coordinates &end, L &&lambda_function) |
| 119 | : _lambda_function(std::move(lambda_function)), |
| 120 | _position(convert_window_coord_to_position(w, start)), |
| 121 | _end(convert_window_coord_to_position(w, end)), |
| 122 | _w(w) |
| 123 | { |
| 124 | } |
| 125 | /** Iterate over the lowest 3 dimensions of the window. |
| 126 | * |
| 127 | * @param[in] on_new_row_size Callback to be called before lambda_function every time the width of the row processed changes. |
| 128 | */ |
| 129 | template <typename M> |
| 130 | void iterate_3D(M &&on_new_row_size) |
| 131 | { |
| 132 | while(_end.z() != _position.z()) |
| 133 | { |
| 134 | PRINTF("New slice %d\n", _position.z()); |
| 135 | iterate_2D_internal(on_new_row_size, _w.x().end() - _w.x().step(), _w.y().end() - _w.y().step()); |
| 136 | _position[2] += _w.z().step(); |
| 137 | _position[1] = _w.y().start(); |
| 138 | _position[0] = _w.x().start(); |
| 139 | } |
| 140 | // Left over: |
| 141 | PRINTF("Left over slice\n"); |
| 142 | iterate_2D(on_new_row_size); |
| 143 | } |
| 144 | |
| 145 | /** Iterate over the lowest 2 dimensions of the window. |
| 146 | * |
| 147 | * @param[in] on_new_row_size Callback to be called before lambda_function every time the width of the row processed changes. |
| 148 | */ |
| 149 | template <typename M> |
| 150 | void iterate_2D(M &&on_new_row_size) |
| 151 | { |
| 152 | iterate_2D_internal(on_new_row_size, _end.x(), _end.y()); |
| 153 | } |
| 154 | |
| 155 | /** Change the step used for the iteration. |
| 156 | * |
| 157 | * @note Does not affect the start and end points. |
| 158 | * |
| 159 | * @param[in] dim Dimension to change |
| 160 | * @param[in] step New step to use for the given dimension. |
| 161 | */ |
| 162 | inline void set_step(size_t dim, int step) |
| 163 | { |
| 164 | _w.set_dimension_step(dim, step); |
| 165 | } |
| 166 | |
| 167 | /** Returns the coordinates in absolute coordinates of the end position |
| 168 | * |
| 169 | * @return End position coordinates. |
| 170 | */ |
| 171 | const Coordinates &end_position() const |
| 172 | { |
| 173 | return _end; |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | template <typename M> |
| 178 | void iterate_2D_internal(M &&on_new_row_size, int end_x, int end_y) |
| 179 | { |
| 180 | //Is there more than one row to process ? |
| 181 | if(end_y == _position.y()) |
| 182 | { |
| 183 | // Single row: |
| 184 | PRINTF("Partial row only\n"); |
| 185 | // Both start and end belong to the same row: |
| 186 | iterate_over_dim0(end_x + _w.x().step(), on_new_row_size); |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | // Do we start from the beginning of the row ? |
| 191 | if(_w.x().start() != _position.x()) |
| 192 | { |
| 193 | //Start in the middle of a row: process left-over X |
| 194 | PRINTF("Partial row first\n"); |
| 195 | iterate_over_dim0(_w.x().end(), on_new_row_size); |
| 196 | _position[1] += _w.y().step(); |
| 197 | } |
| 198 | |
| 199 | //Middle rows |
| 200 | bool no_leftover = end_x + _w.x().step() == _w.x().end(); |
| 201 | if(no_leftover) |
| 202 | { |
| 203 | PRINTF("no left over\n"); |
| 204 | //Switch to full row size: |
| 205 | on_new_row_size(_w[0].start(), _w.x().end()); |
| 206 | // Shouldn't be possible to reach that point and not have at least one entire row to process |
| 207 | ARM_COMPUTE_ERROR_ON(_w.y().end() == _position.y()); |
| 208 | // No leftover: all the rows lefts to process are full width: |
| 209 | iterate_over_dim1(_w.y().end()); |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | PRINTF("with left over\n"); |
| 214 | // Are there full rows to process ? |
| 215 | if(_position[1] != end_y) |
| 216 | { |
| 217 | PRINTF("full rows\n"); |
| 218 | //Switch to full row size: |
| 219 | on_new_row_size(_w[0].start(), _w.x().end()); |
| 220 | iterate_over_dim1(_w.y().end() - _w.y().step()); |
| 221 | } |
| 222 | |
| 223 | PRINTF("Final leftover\n"); |
| 224 | //Leftover end x |
| 225 | _position[0] = _w.x().start(); |
| 226 | iterate_over_dim0(end_x + _w.x().step(), on_new_row_size); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** Process full rows below 'end' |
| 232 | * |
| 233 | * @param[in] end Y position to stop at. |
| 234 | */ |
| 235 | void iterate_over_dim1(int end) |
| 236 | { |
| 237 | for(; _position[1] != end; _position[1] += _w[1].step()) |
| 238 | { |
| 239 | _position[0] = _w[0].start(); |
| 240 | iterate_over_dim0(_w[0].end()); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** Process elements of a given row up to 'end' |
| 245 | * |
| 246 | * @param[in] end X position to stop at. |
| 247 | * @param[in] on_new_row_size Callback to call before starting iterating |
| 248 | */ |
| 249 | template <typename M> |
| 250 | void iterate_over_dim0(int end, M &&on_new_row_size) |
| 251 | { |
| 252 | on_new_row_size(_position.x(), end); |
| 253 | iterate_over_dim0(end); |
| 254 | } |
| 255 | |
| 256 | /** Process elements of a given row up to 'end' |
| 257 | * |
| 258 | * @param[in] end X position to stop at. |
| 259 | */ |
| 260 | void iterate_over_dim0(int end) |
| 261 | { |
| 262 | PRINTF("X [%d, %d, %d]\n", _position.x(), end, _w[0].step()); |
| 263 | // Both start and end belong to the same row: |
| 264 | ARM_COMPUTE_ERROR_ON(_position[0] > end); |
| 265 | for(; _position.x() < end; _position[0] += _w[0].step()) |
| 266 | { |
| 267 | _lambda_function(_position); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | L _lambda_function; /**< Function to call for each iteration */ |
| 272 | Coordinates _position; /**< Absolute coordinates of the current position */ |
| 273 | Coordinates _end; /**< Absolute coordinates of the point after the last iteration */ |
| 274 | Window _w; /**< Window to iterate over */ |
| 275 | }; |
| 276 | |
| 277 | /** Create a WindowIterator object |
| 278 | * |
| 279 | * @param[in] w Window to use for the iteration |
| 280 | * @param[in] start Where to start iterating from (In Window coordinates) |
| 281 | * @param[in] end Where to stop iterating (In Window coordinates). |
| 282 | * @param[in] lambda_function Lambda function to call for every iteration between start and end. (It will be called last for end - 1) |
| 283 | * |
| 284 | * @return A WindowIterator object. |
| 285 | */ |
| 286 | template <typename L> |
| 287 | WindowIterator<L> create_window_iterator(const Window &w, const Coordinates &start, const Coordinates &end, L &&lambda_function) |
| 288 | { |
| 289 | return WindowIterator<L>(w, start, end, std::move(lambda_function)); |
| 290 | } |
| 291 | } |
| 292 | #endif /*__ARM_COMPUTE_WINDOW_ITERATOR_H__*/ |