xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/cl_kernels/common/gather.cl (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1/*
2 * Copyright (c) 2018-2021 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#if defined(DATA_TYPE) && defined(AXIS)
27
28/** Performs the Gather operation along the chosen axis
29 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
30 * @note Axis should be given as a preprocessor argument using -DAXIS=axis. e.g. -DAXIS=1
31 * @attention Output tensor depth should be given as a preprocessor argument using -DOUTPUT_DIM_Z=size. e.g. -DOUTPUT_DIM_Z=16
32 * @attention Input tensor depth should be given as a preprocessor argument using -DINPUT_DIM_Z=size. e.g. -DINPUT_DIM_Z=16
33 *
34 *
35 * @param[in]  input_ptr                             Pointer to the source tensor. Supported data types: All
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 work item (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 work item (in bytes)
40 * @param[in]  input_stride_z                        Stride of the source tensor in Y dimension (in bytes)
41 * @param[in]  input_step_z                          input_stride_z * number of elements along Z processed per work item (in bytes)
42 * @param[in]  input_stride_w                        Stride of the source tensor in Z dimension (in bytes)
43 * @param[in]  input_step_w                          input_stride_w * number of elements along W processed per work item (in bytes)
44 * @param[in]  input_offset_first_element_in_bytes   Offset of the first element in the source tensor
45 * @param[in]  indices_ptr                           Pointer to the indices vector. Supported data types: S32/U32.
46 * @param[in]  indices_stride_x                      Stride of the indices vector in X dimension (in bytes)
47 * @param[in]  indices_step_x                        input_stride_x * number of elements along X processed per work item (in bytes)
48 * @param[in]  indices_offset_first_element_in_bytes Offset of the first element in the indices vector
49 * @param[out] output_ptr                            Pointer to the destination tensor. Supported data types: same as @p input_ptr
50 * @param[in]  output_stride_x                       Stride of the destination tensor in X dimension (in bytes)
51 * @param[in]  output_step_x                         output_stride_x * number of elements along X processed per work item (in bytes)
52 * @param[in]  output_stride_y                       Stride of the destination tensor in Y dimension (in bytes)
53 * @param[in]  output_step_y                         output_stride_y * number of elements along Y processed per work item (in bytes)
54 * @param[in]  output_stride_z                       Stride of the destination tensor in Z dimension (in bytes)
55 * @param[in]  output_step_z                         output_stride_z * number of elements along Z processed per work item (in bytes)
56 * @param[in]  output_stride_w                       Stride of the destination tensor in W dimension (in bytes)
57 * @param[in]  output_step_w                         output_stride_w * number of elements along W processed per work item (in bytes)
58 * @param[in]  output_offset_first_element_in_bytes  Offset of the first element in the destination tensor
59 */
60__kernel void gather(
61    TENSOR4D_DECLARATION(input),
62    VECTOR_DECLARATION(indices),
63    TENSOR4D_DECLARATION(output))
64{
65    const int px = get_global_id(0);
66    const int py = get_global_id(1);
67    const int pz = get_global_id(2) % OUTPUT_DIM_Z;
68    const int pw = get_global_id(2) / OUTPUT_DIM_Z;
69
70    const Tensor4D input   = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input, INPUT_DIM_Z);
71    const Vector   indices = CONVERT_TO_VECTOR_STRUCT_NO_STEP(indices);
72    Tensor4D       output  = CONVERT_TO_TENSOR4D_STRUCT(output, OUTPUT_DIM_Z);
73
74#if AXIS == 0
75    const uint index                 = *(__global const uint *)vector_offset(&indices, px);
76    __global const uchar *input_addr = tensor4D_offset(&input, index, py, pz, pw);
77#elif AXIS == 1
78    const uint index                 = *(__global const uint *)vector_offset(&indices, py);
79    __global const uchar *input_addr = tensor4D_offset(&input, px, index, pz, pw);
80#elif AXIS == 2
81    const uint index                 = *(__global const uint *)vector_offset(&indices, pz);
82    __global const uchar *input_addr = tensor4D_offset(&input, px, py, index, pw);
83#elif AXIS == 3
84    const uint index                 = *(__global const uint *)vector_offset(&indices, pw);
85    __global const uchar *input_addr = tensor4D_offset(&input, px, py, pz, index);
86#endif //AXIS
87
88    *(__global DATA_TYPE *)output.ptr = *((__global const DATA_TYPE *)input_addr);
89}
90
91#endif //defined(DATA_TYPE) && defined(AXIS)