Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 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 | */ |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 24 | #include "helpers.h" |
| 25 | |
| 26 | #if defined(DATA_TYPE) && defined(NUM_REVERSE_DIMS) |
| 27 | |
| 28 | #if NUM_REVERSE_DIMS > 4 |
| 29 | #error("Reversing more than 4 dimensions is not currently supported") |
| 30 | #endif /* NUM_REVERSE_DIMS > 4 */ |
| 31 | |
| 32 | /** Performs reverse along the specified axis. |
| 33 | * |
| 34 | * @note The data type must be given as a preprocessor argument using -DDATA_TYPE=num. e.g. -DDATA_TYPE=uint |
| 35 | * @note The number of dimensions to reverse must be given as a preprocessor argument using -DNUM_REVERSE_DIMS=num, e.g. -DNUM_REVERSE_DIMS=3 |
| 36 | * |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 37 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: All |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 38 | * @param[in] src_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 39 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 40 | * @param[in] src_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 41 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 42 | * @param[in] src_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 43 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 44 | * @param[in] src_stride_w Stride of the first source tensor in Z dimension (in bytes) |
| 45 | * @param[in] src_step_w src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 46 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 47 | * @param[in] axis_ptr Pointer to the source vector. Supported data types: U32 |
| 48 | * @param[in] axis_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 49 | * @param[in] axis_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 50 | * @param[in] axis_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 51 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 52 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 53 | * @param[in] dst_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 54 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 55 | * @param[in] dst_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 56 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 57 | * @param[in] dst_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 58 | * @param[in] dst_stride_w Stride of the destination tensor in Z dimension (in bytes) |
| 59 | * @param[in] dst_step_w output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 60 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 61 | */ |
| 62 | __kernel void reverse(TENSOR4D_DECLARATION(src), |
| 63 | VECTOR_DECLARATION(axis), |
| 64 | TENSOR4D_DECLARATION(dst), |
| 65 | const uint width, |
| 66 | const uint height, |
| 67 | const uint depth, |
| 68 | const uint batches) |
| 69 | { |
| 70 | Tensor4D src = CONVERT_TO_TENSOR4D_STRUCT(src, depth); |
| 71 | Vector axis = CONVERT_TO_VECTOR_STRUCT_NO_STEP(axis); |
| 72 | Tensor4D dst = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(dst, depth); |
| 73 | |
| 74 | const uint x_in = get_global_id(0); |
| 75 | const uint y_in = get_global_id(1); |
| 76 | const uint z_in = get_global_id(2) % depth; |
| 77 | const uint w_in = get_global_id(2) / depth; |
| 78 | |
| 79 | const uint4 dims = (uint4)(0, 1, 2, 3); |
| 80 | int4 to_reverse = (int4)(0, 0, 0, 0); |
| 81 | #if NUM_REVERSE_DIMS == 1 |
| 82 | const uint index = *((__global uint *)axis.ptr); |
| 83 | to_reverse = (uint4)index == dims; |
| 84 | #elif NUM_REVERSE_DIMS == 2 |
| 85 | const uint2 indices = vload2(0, (__global uint *)axis.ptr); |
| 86 | to_reverse = ((uint4)indices.s0 == dims) || ((uint4)indices.s1 == dims); |
| 87 | #elif NUM_REVERSE_DIMS == 3 |
| 88 | const uint2 indices01 = vload2(0, (__global uint *)axis.ptr); |
| 89 | const uint index2 = *((__global uint *)axis.ptr + 2); |
| 90 | to_reverse = ((uint4)indices01.s0 == dims) || ((uint4)indices01.s1 == dims) || ((uint4)index2 == dims); |
| 91 | #else /* NUM_REVERSE_DIMS == 3 */ |
| 92 | const uint4 indices = vload4(0, (__global uint *)axis.ptr); |
| 93 | to_reverse = ((uint4)indices.s0 == dims) || ((uint4)indices.s1 == dims) || ((uint4)indices.s2 == dims) || ((uint4)indices.s3 == dims); |
| 94 | #endif /* NUM_REVERSE_DIMS == 1 */ |
| 95 | const uint x_out = to_reverse.s0 ? width - x_in - 1 : x_in; |
| 96 | const uint y_out = to_reverse.s1 ? height - y_in - 1 : y_in; |
| 97 | const uint z_out = to_reverse.s2 ? depth - z_in - 1 : z_in; |
| 98 | const uint w_out = to_reverse.s3 ? batches - w_in - 1 : w_in; |
| 99 | |
| 100 | *((__global DATA_TYPE *)tensor4D_offset(&dst, x_out, y_out, z_out, w_out)) = *((__global DATA_TYPE *)src.ptr); |
| 101 | } |
| 102 | #endif // defined(DATA_TYPE) && defined(NUM_REVERSE_DIMS) |