blob: 0592e07511eca96c1d40d937db359717545ff5d5 [file] [log] [blame]
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +00001/*
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01002 * Copyright (c) 2018-2020 Arm Limited.
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +00003 *
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
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010026#if defined(PAD00) && defined(PAD10) && defined(PAD20) && defined(PAD21) && defined(PAD30) && defined(DATA_TYPE) && defined(VEC_SIZE) // Compile time constants
27
28/** Perform a padded copy of input tensor to the output tensor. Padding values are defined at compile time
29 *
30 * @attention The following variables must be passed at compile time:
31 * -# -DPAD{d}{0,1} = padding before{0} and after{1} dimension d (d < 4)
32 * -# -DDEPTH = The third dimension (depth) of the tensor (it is needed only if d == 3)
33 * -# -DDATA_TYPE = Input and output datatypes.
34 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010035 * @param[in] in_ptr Pointer to the source tensor. Supported data types: All
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010036 * @param[in] in_stride_x Stride of the source tensor in X dimension (in bytes)
37 * @param[in] in_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
38 * @param[in] in_stride_y Stride of the source tensor in Y dimension (in bytes)
39 * @param[in] in_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
40 * @param[in] in_stride_z Stride of the source tensor in Z dimension (in bytes)
41 * @param[in] in_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
42 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source tensor
43 * @param[out] out_ptr Pointer to the destination tensor. Supported data types: same as @p in_ptr
44 * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes)
45 * @param[in] out_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
46 * @param[in] out_stride_y Stride of the destination tensor in Y dimension (in bytes)
47 * @param[in] out_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
48 * @param[in] out_stride_z Stride of the source tensor in Z dimension (in bytes)
49 * @param[in] out_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
50 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor
51 */
52__kernel void copy_pad_tensor(
53 TENSOR3D_DECLARATION(in),
54 TENSOR3D_DECLARATION(out))
55
56{
57 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(in);
58 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out);
59
60 const int offset_x = PAD00;
61 const int offset_y = PAD10;
62 const int offset_z = PAD20;
63
64#if PAD30 > 0
65 const size_t in_batch = get_global_id(2) / DEPTH;
66 const int total_depth = DEPTH + PAD20 + PAD21;
67 const int offset_w = PAD30 * total_depth + in_batch * (PAD20 + PAD21);
68#else // PAD30 == 0
69 const int offset_w = 0;
70#endif // PAD30
71
72 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
73 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
74
75 VSTORE(VEC_SIZE)
76 (data, 0, (__global DATA_TYPE *)tensor3D_offset(&out, offset_x, offset_y, offset_z + offset_w));
77}
78#endif // Compile time constants
79
George Wort894066d2019-02-15 15:12:52 +000080#if defined(DATA_TYPE)
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000081/** Performs a copy of input tensor to the output tensor.
82 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010083 * @param[in] in_ptr Pointer to the source tensor. Supported data types: All
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010084 * @param[in] in_stride_x Stride of the source tensor in X dimension (in bytes)
85 * @param[in] in_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
86 * @param[in] in_stride_y Stride of the source tensor in Y dimension (in bytes)
87 * @param[in] in_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
88 * @param[in] in_stride_z Stride of the source tensor in Z dimension (in bytes)
89 * @param[in] in_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
90 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source tensor
91 * @param[out] out_ptr Pointer to the destination tensor. Supported data types: same as @p in_ptr
92 * @param[in] out_stride_x Stride of the destination tensor in X dimension (in bytes)
93 * @param[in] out_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
94 * @param[in] out_stride_y Stride of the destination tensor in Y dimension (in bytes)
95 * @param[in] out_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
96 * @param[in] out_stride_z Stride of the source tensor in Z dimension (in bytes)
97 * @param[in] out_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
98 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination tensor
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +000099 */
100__kernel void copy_tensor(
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100101 TENSOR3D_DECLARATION(in),
102 TENSOR3D_DECLARATION(out))
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000103{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100104 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT(in);
105 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out);
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000106
George Wort894066d2019-02-15 15:12:52 +0000107#if defined(VEC_SIZE)
108
109#if defined(LAST_ACCESSED_X)
110 // Check if access on width gets out of bounds
111 // If it does then shift access vector to access elements within bounds
112 const int shift = max((int)(get_global_id(0) * VEC_SIZE) - (int)LAST_ACCESSED_X, 0);
113 in.ptr -= shift * in.stride_x;
114 out.ptr -= shift * out.stride_x;
115#endif // defined(LAST_ACCESSED_X)
116
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100117 // Load data
118 VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
119 data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)in.ptr);
Michalis Spyrou5c8e05c2018-03-22 11:56:01 +0000120
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100121 // Store result
122 VSTORE(VEC_SIZE)
123 (data, 0, (__global DATA_TYPE *)out.ptr);
George Wort894066d2019-02-15 15:12:52 +0000124#else // defined(VEC_SIZE)
125 *((__global DATA_TYPE *)(out.ptr)) = *((__global DATA_TYPE *)(in.ptr));
126#endif // defined(VEC_SIZE)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100127}
George Wort894066d2019-02-15 15:12:52 +0000128#endif // defined(DATA_TYPE)