blob: bc3df473450df25ddb3138e1f22ec06379b358cb [file] [log] [blame]
Georgios Pinitas77589b52018-08-21 14:41:35 +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#include "helpers.h"
25
26/** Perform a strided slice operation on a given input.
27 *
28 * @attention Supported tensor rank: up to 4
29 *
30 * @attention Data type can be passed using the -DDATA_TYPE compile flag, e.g. -DDATA_TYPE=float
31 * @attention Input and output tensor dephts should be given as a preprocessor arguments using -DSRC_DEPTH=size. and -DDST_DEPTH=size
32 * @attention Absolute start coordinates for each dimension should be given as preprocessor -DSTART_index=value e.g. -DSTART_0=2
33 * @attention Strides for each dimension should be given as preprocessor -DSTRIDE_index=value e.g. -DSTRIDE_1=1
34 *
35 * @param[in] input_ptr Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32
36 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
37 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
38 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
39 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
40 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
41 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
42 * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes)
43 * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes)
44 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
45 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
46 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
47 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
48 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
49 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
50 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
51 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
52 * @param[in] output_stride_w Stride of the destination tensor in W dimension (in bytes)
53 * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes)
54 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor
55 */
56__kernel void strided_slice(
57 TENSOR4D_DECLARATION(input),
58 TENSOR4D_DECLARATION(output))
59{
60 // Get pixels pointer
61 Tensor4D input = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input, SRC_DEPTH);
62 Tensor4D output = CONVERT_TO_TENSOR4D_STRUCT(output, DST_DEPTH);
63
Georgios Pinitasc1a72452018-08-24 11:25:32 +010064 int offset = 0;
Georgios Pinitas77589b52018-08-21 14:41:35 +010065
Georgios Pinitasc1a72452018-08-24 11:25:32 +010066 // Offset X
67#if defined(START_0) && defined(STRIDE_0) && defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
68 // Check if access on width gets out of bounds
69 // If it does shift access vector to access elements within bounds
70 const int xi = (int)(get_global_id(0) * VEC_SIZE);
71 offset = (int)START_0 + min(xi, (int)LAST_ACCESSED_X);
72 input.ptr += offset * input_stride_x;
73 output.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * output_stride_x;
74#elif defined(START_0) && defined(STRIDE_0)
75 offset = (int)START_0 + (int)get_global_id(0) * (int)STRIDE_0;
76 input.ptr += offset * input_stride_x;
Georgios Pinitas77589b52018-08-21 14:41:35 +010077#endif // defined(START_0) && defined(STRIDE_0)
Georgios Pinitasc1a72452018-08-24 11:25:32 +010078
79 // Offset Y
Georgios Pinitas77589b52018-08-21 14:41:35 +010080#if defined(START_1) && defined(STRIDE_1)
Georgios Pinitasc1a72452018-08-24 11:25:32 +010081 offset = (int)START_1 + (int)get_global_id(1) * (int)STRIDE_1;
82 input.ptr += offset * input_stride_y;
Georgios Pinitas77589b52018-08-21 14:41:35 +010083#endif // defined(START_1) && defined(STRIDE_1)
Georgios Pinitasc1a72452018-08-24 11:25:32 +010084
85 // Offset Z
Georgios Pinitas77589b52018-08-21 14:41:35 +010086#if defined(START_2) && defined(STRIDE_2)
Georgios Pinitasc1a72452018-08-24 11:25:32 +010087 offset = (int)START_2 + ((int)get_global_id(2) % (int)DST_DEPTH) * (int)STRIDE_2;
88 input.ptr += offset * input_stride_z;
Georgios Pinitas77589b52018-08-21 14:41:35 +010089#endif // defined(START_2) && defined(STRIDE_2)
Georgios Pinitasc1a72452018-08-24 11:25:32 +010090
91 // Offset depth
Georgios Pinitas77589b52018-08-21 14:41:35 +010092#if defined(START_3) && defined(STRIDE_3)
Georgios Pinitasc1a72452018-08-24 11:25:32 +010093 offset = (int)START_3 + ((int)get_global_id(2) / (int)DST_DEPTH) * (int)STRIDE_3;
94 input.ptr += offset * input_stride_w;
95#endif // defined(START_3) && defined(STRIDE_3)
Georgios Pinitas77589b52018-08-21 14:41:35 +010096
97 // Store result
Georgios Pinitasc1a72452018-08-24 11:25:32 +010098#if defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
99 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
100 val = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input.ptr));
101
102 VSTORE(VEC_SIZE)
103 (val, 0, (__global DATA_TYPE *)(output.ptr));
104#else // defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
105 *((__global DATA_TYPE *)(output.ptr)) = *((__global DATA_TYPE *)(input.ptr));
106#endif // defined(VEC_SIZE) && defined(LAST_ACCESSED_X)
Georgios Pinitas77589b52018-08-21 14:41:35 +0100107}