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