• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-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_WINDOW_ITERATOR_H
25 #define ARM_COMPUTE_WINDOW_ITERATOR_H
26 #include "arm_compute/core/Coordinates.h"
27 #include "arm_compute/core/Error.h"
28 #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 
35 namespace 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  */
convert_window_coord_to_position(const Window & w,const Coordinates & offset)44 inline 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 */
55 template <typename T>
56 class TensorAccessor
57 {
58 public:
59     /** Constructor:
60      *
61      * @param[in] tensor Source tensor, must be allocated.
62      */
TensorAccessor(const ITensor & tensor)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      */
stride(size_t dim)73     inline size_t stride(size_t dim) const
74     {
75         ARM_COMPUTE_ERROR_ON(_strides[dim] % sizeof(T) != 0);
76         return _strides[dim] / sizeof(T);
77     }
78 
79     /** 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      */
set_stride(size_t dim,size_t size)84     void set_stride(size_t dim, size_t size)
85     {
86         _strides[dim] = size;
87     }
88 
89     /** Manually set the strides
90      *
91      * @param[in] strides Strides to set
92      */
set_strides(const Strides & strides)93     void set_strides(const Strides &strides)
94     {
95         _strides = strides;
96     }
97 
98     /** Returns a pointer to the element at coordinates (x,y,z,w)
99      *
100      * @param[in] x X coordinates
101      * @param[in] y (optional) Y coordinates
102      * @param[in] z (optional) Z coordinates
103      * @param[in] w (optional) W coordinates
104      */
105     inline T *get_ptr(unsigned int x, unsigned int y = 0, unsigned int z = 0, unsigned int w = 0)
106     {
107         return reinterpret_cast<T *>(_first + x * _strides[0] + y * _strides[1] + z * _strides[2] + w * _strides[3]);
108     }
109 
110     /** Returns a pointer to the element at coordinates (x,y,z,w)
111      *
112      * @param[in] x X coordinates
113      * @param[in] y (optional) Y coordinates
114      * @param[in] z (optional) Z coordinates
115      * @param[in] w (optional) W coordinates
116      */
operator()117     inline T *operator()(unsigned int x, unsigned int y = 0, unsigned int z = 0, unsigned int w = 0)
118     {
119         return get_ptr(x, y, z, w);
120     }
121 
122     /** Returns a pointer to the first element of the tensor
123      *
124      * @return Pointer to the first element.
125      */
first_element()126     inline T *first_element()
127     {
128         return reinterpret_cast<T *>(_first);
129     }
130 
131     /** Returns a pointer to the first element of the tensor
132      *
133      * @return Pointer to the first element.
134      */
operator()135     inline T *operator()()
136     {
137         return first_element();
138     }
139 
140 private:
141     uint8_t *_first;   /**< Pointer to the first element of the tensor.*/
142     Strides  _strides; /**< Strides in bytes of the tensor */
143 };
144 
145 /** Iterate over a portion of a Window */
146 template <typename L>
147 class WindowIterator
148 {
149 public:
150     /** Construct a WindowIterator object
151      *
152      * @param[in] w               Window to use for the iteration
153      * @param[in] start           Where to start iterating from (In Window coordinates)
154      * @param[in] end             Where to stop iterating (In Window coordinates).
155      * @param[in] lambda_function Lambda function to call for every iteration between start and end. (It will be called last for end - 1)
156      */
WindowIterator(const Window & w,const Coordinates & start,const Coordinates & end,L && lambda_function)157     WindowIterator(const Window &w, const Coordinates &start, const Coordinates &end, L &&lambda_function)
158         : _lambda_function(std::move(lambda_function)),
159           _position(convert_window_coord_to_position(w, start)),
160           _end(convert_window_coord_to_position(w, end)),
161           _w(w)
162     {
163     }
164     /** Iterate over the lowest 3 dimensions of the window.
165      *
166      * @param[in] on_new_row_size Callback to be called before lambda_function every time the width of the row processed changes.
167      */
168     template <typename M>
iterate_3D(M && on_new_row_size)169     void iterate_3D(M &&on_new_row_size)
170     {
171         while(_end.z() != _position.z())
172         {
173             PRINTF("New slice %d\n", _position.z());
174             iterate_2D_internal(on_new_row_size, _w.x().end() - _w.x().step(), _w.y().end() - _w.y().step());
175             _position[2] += _w.z().step();
176             _position[1] = _w.y().start();
177             _position[0] = _w.x().start();
178         }
179         // Left over:
180         PRINTF("Left over slice\n");
181         iterate_2D(on_new_row_size);
182     }
183 
184     /** Iterate over the lowest 2 dimensions of the window.
185      *
186      * @param[in] on_new_row_size Callback to be called before lambda_function every time the width of the row processed changes.
187      */
188     template <typename M>
iterate_2D(M && on_new_row_size)189     void iterate_2D(M &&on_new_row_size)
190     {
191         iterate_2D_internal(on_new_row_size, _end.x(), _end.y());
192     }
193 
194     /** Change the step used for the iteration.
195      *
196      * @note Does not affect the start and end points.
197      *
198      * @param[in] dim  Dimension to change
199      * @param[in] step New step to use for the given dimension.
200      */
set_step(size_t dim,int step)201     inline void set_step(size_t dim, int step)
202     {
203         _w.set_dimension_step(dim, step);
204     }
205 
206     /** Returns the coordinates in absolute coordinates of the end position
207          *
208          * @return End position coordinates.
209          */
end_position()210     const Coordinates &end_position() const
211     {
212         return _end;
213     }
214 
215 private:
216     template <typename M>
iterate_2D_internal(M && on_new_row_size,int end_x,int end_y)217     void iterate_2D_internal(M &&on_new_row_size, int end_x, int end_y)
218     {
219         //Is there more than one row to process ?
220         if(end_y == _position.y())
221         {
222             // Single row:
223             PRINTF("Partial row only\n");
224             // Both start and end belong to the same row:
225             iterate_over_dim0(end_x + _w.x().step(), on_new_row_size);
226         }
227         else
228         {
229             // Do we start from the beginning of the row ?
230             if(_w.x().start() != _position.x())
231             {
232                 //Start in the middle of a row: process left-over X
233                 PRINTF("Partial row first\n");
234                 iterate_over_dim0(_w.x().end(), on_new_row_size);
235                 _position[1] += _w.y().step();
236             }
237 
238             //Middle rows
239             bool no_leftover = end_x + _w.x().step() == _w.x().end();
240             if(no_leftover)
241             {
242                 PRINTF("no left over\n");
243                 //Switch to full row size:
244                 on_new_row_size(_w[0].start(), _w.x().end());
245                 // Shouldn't be possible to reach that point and not have at least one entire row to process
246                 ARM_COMPUTE_ERROR_ON(_w.y().end() == _position.y());
247                 // No leftover: all the rows lefts to process are full width:
248                 iterate_over_dim1(end_y + _w.y().step());
249             }
250             else
251             {
252                 PRINTF("with left over\n");
253                 // Are there full rows to process ?
254                 if(_position[1] != end_y)
255                 {
256                     PRINTF("full rows\n");
257                     //Switch to full row size:
258                     on_new_row_size(_w[0].start(), _w.x().end());
259                     iterate_over_dim1(end_y);
260                 }
261 
262                 PRINTF("Final leftover\n");
263                 //Leftover end x
264                 _position[0] = _w.x().start();
265                 iterate_over_dim0(end_x + _w.x().step(), on_new_row_size);
266             }
267         }
268     }
269 
270     /** Process full rows below 'end'
271      *
272      * @param[in] end Y position to stop at.
273      */
iterate_over_dim1(int end)274     void iterate_over_dim1(int end)
275     {
276         for(; _position[1] != end; _position[1] += _w[1].step())
277         {
278             _position[0] = _w[0].start();
279             iterate_over_dim0(_w[0].end());
280         }
281     }
282 
283     /** Process elements of a given row up to 'end'
284      *
285      * @param[in] end             X position to stop at.
286      * @param[in] on_new_row_size Callback to call before starting iterating
287      */
288     template <typename M>
iterate_over_dim0(int end,M && on_new_row_size)289     void iterate_over_dim0(int end, M &&on_new_row_size)
290     {
291         on_new_row_size(_position.x(), end);
292         iterate_over_dim0(end);
293     }
294 
295     /** Process elements of a given row up to 'end'
296      *
297      * @param[in] end X position to stop at.
298      */
iterate_over_dim0(int end)299     void iterate_over_dim0(int end)
300     {
301         PRINTF("X [%d, %d, %d]\n", _position.x(), end, _w[0].step());
302         // Both start and end belong to the same row:
303         ARM_COMPUTE_ERROR_ON(_position[0] > end);
304         for(; _position.x() < end; _position[0] += _w[0].step())
305         {
306             _lambda_function(_position);
307         }
308     }
309 
310     L           _lambda_function; /**< Function to call for each iteration */
311     Coordinates _position;        /**< Absolute coordinates of the current position */
312     Coordinates _end;             /**< Absolute coordinates of the point after the last iteration */
313     Window      _w;               /**< Window to iterate over */
314 };
315 
316 /** Create a WindowIterator object
317  *
318  * @param[in] w               Window to use for the iteration
319  * @param[in] start           Where to start iterating from (In Window coordinates)
320  * @param[in] end             Where to stop iterating (In Window coordinates).
321  * @param[in] lambda_function Lambda function to call for every iteration between start and end. (It will be called last for end - 1)
322  *
323  * @return A WindowIterator object.
324  */
325 template <typename L>
create_window_iterator(const Window & w,const Coordinates & start,const Coordinates & end,L && lambda_function)326 WindowIterator<L> create_window_iterator(const Window &w, const Coordinates &start, const Coordinates &end, L &&lambda_function)
327 {
328     return WindowIterator<L>(w, start, end, std::move(lambda_function));
329 }
330 }
331 #endif /*ARM_COMPUTE_WINDOW_ITERATOR_H*/
332