blob: 451b962b01d10264a812653b28c89298993b64d4 [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Manuel Bottinib412fab2018-12-10 17:40:23 +00002 * Copyright (c) 2016-2019 ARM Limited.
Michalis Spyrou04f089c2017-08-08 17:42:38 +01003 *
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/** Calculate square sum of a vector
27 *
28 * @param[in] input Pointer to the first pixel.
29 *
30 * @return square sum of vector.
31 */
32inline DATA_TYPE square_sum(__global const DATA_TYPE *input)
33{
34 VEC_DATA_TYPE(DATA_TYPE, 16)
35 in = vload16(0, input);
36
37 in *= in;
38
39 in.s01234567 += in.s89ABCDEF;
40 in.s0123 += in.s4567;
41 in.s01 += in.s23;
42
43 return (in.s0 + in.s1);
44}
45
46/** Calculate sum of a vector
47 *
48 * @param[in] input Pointer to the first pixel.
49 *
50 * @return sum of vector.
51 */
52inline DATA_TYPE sum(__global const DATA_TYPE *input)
53{
54 VEC_DATA_TYPE(DATA_TYPE, 16)
55 in = vload16(0, input);
56
57 in.s01234567 += in.s89ABCDEF;
58 in.s0123 += in.s4567;
59 in.s01 += in.s23;
60
61 return (in.s0 + in.s1);
62}
Manuel Bottinib412fab2018-12-10 17:40:23 +000063
64/** Calculate product of a vector
65 *
66 * @param[in] input Pointer to the first pixel.
67 *
68 * @return product of vector.
69 */
70inline DATA_TYPE product(__global const DATA_TYPE *input)
71{
72 VEC_DATA_TYPE(DATA_TYPE, 16)
73 in = vload16(0, input);
74
75 in.s01234567 *= in.s89ABCDEF;
76 in.s0123 *= in.s4567;
77 in.s01 *= in.s23;
78
79 return (in.s0 * in.s1);
80}
Manuel Bottini34f88dd2019-10-18 10:37:46 +000081#if defined(OPERATION)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010082/** This kernel performs parallel reduction given an operation on x-axis.
Michalis Spyrou04f089c2017-08-08 17:42:38 +010083 *
84 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Michalis Spyrou04f089c2017-08-08 17:42:38 +010085 * @note The operation we want to perform must be passed at compile time using -DOPERATION e.g. -DOPERATION=square_sum
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010086 * @note The mean flag must be passed at compile time using -DMEAN if we want to compute the mean value
Manuel Bottinib412fab2018-12-10 17:40:23 +000087 * @note The product flag must be passed at compile time using -DPROD if we want to compute the product, otherwise sum will be used
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010088 * @note The width size must be passed at compile time using -DWIDTH e.g. -DWIDTH=128 if we want to compute the mean value
Michalis Spyrou04f089c2017-08-08 17:42:38 +010089 *
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010090 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
Michalis Spyrou04f089c2017-08-08 17:42:38 +010091 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
92 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000093 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
94 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010095 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Manuel Bottinib412fab2018-12-10 17:40:23 +000096 * @param[in] partial_res_ptr The local buffer to hold partial result values. Supported data types: same as @p src_ptr
97 * @param[in] partial_res_stride_x Stride of the output tensor in X dimension (in bytes)
98 * @param[in] partial_res_step_x partial_res_stride_x * number of elements along X processed per workitem(in bytes)
99 * @param[in] partial_res_stride_y Stride of the output tensor in Y dimension (in bytes)
100 * @param[in] partial_res_step_y partial_res_stride_y * number of elements along Y processed per workitem(in bytes)
101 * @param[in] partial_res_offset_first_element_in_bytes The offset of the first element in the source tensor
102 * @param[in] local_results Local buffer for storing the partial result
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100103 */
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100104__kernel void reduction_operation_x(
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000105 IMAGE_DECLARATION(src),
Manuel Bottinib412fab2018-12-10 17:40:23 +0000106 IMAGE_DECLARATION(partial_res),
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000107 __local DATA_TYPE *local_results)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100108{
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000109 Image src = CONVERT_TO_IMAGE_STRUCT(src);
Manuel Bottinib412fab2018-12-10 17:40:23 +0000110 Image partial_res = CONVERT_TO_IMAGE_STRUCT(partial_res);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100111
112 unsigned int lsize = get_local_size(0);
113 unsigned int lid = get_local_id(0);
114
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000115 for(unsigned int y = 0; y < get_local_size(1); ++y)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100116 {
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000117 local_results[lid] = OPERATION((__global DATA_TYPE *)offset(&src, 0, y));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100118 barrier(CLK_LOCAL_MEM_FENCE);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100119
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000120 // Perform parallel reduction
121 for(unsigned int i = lsize >> 1; i > 0; i >>= 1)
122 {
123 if(lid < i)
124 {
Manuel Bottinib412fab2018-12-10 17:40:23 +0000125#if defined(PROD)
126 local_results[lid] *= local_results[lid + i];
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000127#else // !defined(PROD)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000128 local_results[lid] += local_results[lid + i];
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000129#endif // defined(PROD)
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000130 }
131 barrier(CLK_LOCAL_MEM_FENCE);
132 }
133
134 if(lid == 0)
135 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100136#if defined(MEAN) && defined(WIDTH)
137 if(y == get_local_size(1) - 1)
138 {
Manuel Bottinib412fab2018-12-10 17:40:23 +0000139 local_results[0] /= WIDTH;
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100140 }
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100141#endif // defined(MEAN) && defined(WIDTH)
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000142 ((__global DATA_TYPE *)offset(&partial_res, get_group_id(0), y))[0] = local_results[0];
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000143 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100144 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100145}
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000146#endif // defined(OPERATION)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100147
148#if defined(WIDTH)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000149/** This kernel performs reduction on x-axis. (Non parallel)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100150 *
Michalis Spyrou7930db42018-11-22 17:36:28 +0000151 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100152 * @note The width size must be passed at compile time using -DWIDTH e.g. -DWIDTH=128
Manuel Bottinib412fab2018-12-10 17:40:23 +0000153 * @note The product flag must be passed at compile time using -DPROD if we want to compute the product, otherwise sum will be used
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100154 * @note In case of MIN and MAX the condition data type must be passed at compile time using -DCOND_DATA_TYPE e.g. -DCOND_DATA_TYPE=short
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100155 *
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100156 * @param[in] src_ptr Pointer to the source tensor. Supported data types: S32/F16/F32 and QASYMM8 for operation MEAN
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100157 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
158 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
159 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
160 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptt
161 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
162 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
163 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
164 */
Michalis Spyrou7930db42018-11-22 17:36:28 +0000165__kernel void reduction_operation_non_parallel_x(
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100166 VECTOR_DECLARATION(src),
167 VECTOR_DECLARATION(output))
168{
169 Vector src = CONVERT_TO_VECTOR_STRUCT(src);
170 Vector output = CONVERT_TO_VECTOR_STRUCT(output);
171
Michalis Spyrou7930db42018-11-22 17:36:28 +0000172 DATA_TYPE_PROMOTED res = *((__global DATA_TYPE *)vector_offset(&src, 0));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100173
Michalis Spyrou7930db42018-11-22 17:36:28 +0000174 for(unsigned int x = 1; x < WIDTH; ++x)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100175 {
Michalis Spyrou7930db42018-11-22 17:36:28 +0000176 DATA_TYPE_PROMOTED in = *((__global DATA_TYPE *)vector_offset(&src, x));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100177#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100178 res = select(res, in, CONVERT(ISLESS(in, res), COND_DATA_TYPE));
179#elif defined(MAX)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100180 res = select(res, in, CONVERT(ISGREATER(in, res), COND_DATA_TYPE));
181#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou7930db42018-11-22 17:36:28 +0000182 res += in;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100183#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100184 }
185
Michalis Spyrou7930db42018-11-22 17:36:28 +0000186 // Store result
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100187#if defined(MEAN)
188 res /= WIDTH;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000189#endif // defined(MEAN)
Usama Arif048b0f32019-05-22 16:32:27 +0100190#if defined(MIN) || defined(MAX)
Usama Arifb2890502019-05-21 11:48:37 +0100191 *((__global DATA_TYPE_PROMOTED *)output.ptr) = res;
Usama Arif048b0f32019-05-22 16:32:27 +0100192#else // defined(MIN) || defined(MAX)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100193 *((__global uchar *)output.ptr) = convert_uchar(res);
Usama Arif048b0f32019-05-22 16:32:27 +0100194#endif // defined(MIN) || defined(MAX)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100195}
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100196#endif // defined(WIDTH)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100197
198#if defined(HEIGHT)
199/** This kernel performs reduction on y-axis.
200 *
Michalis Spyrou7930db42018-11-22 17:36:28 +0000201 * @note The input data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100202 * @note The height size must be passed at compile time using -DHEIGHT e.g. -DHEIGHT=128
203 *
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100204 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/S32/F16/F32
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100205 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
206 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
207 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
208 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
209 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
210 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptt
211 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
212 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
213 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
214 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
215 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
216 */
217__kernel void reduction_operation_y(
218 IMAGE_DECLARATION(src),
219 IMAGE_DECLARATION(output))
220{
221 Image src = CONVERT_TO_IMAGE_STRUCT(src);
222 Image output = CONVERT_TO_IMAGE_STRUCT(output);
223
224 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000225 res = CONVERT(vload16(0, (__global DATA_TYPE *)offset(&src, 0, 0)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100226
Michalis Spyrou7930db42018-11-22 17:36:28 +0000227#if defined(SUM_SQUARE)
228 res *= res;
229#endif // defined(SUM_SQUARE)
230
Michalis Spyrou7930db42018-11-22 17:36:28 +0000231 for(unsigned int y = 1; y < HEIGHT; ++y)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100232 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100233 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
234 in = CONVERT(vload16(0, (__global DATA_TYPE *)offset(&src, 0, y)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100235#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100236 res = select(res, in, ISLESS(in, res));
237#elif defined(MAX)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100238 res = select(res, in, ISGREATER(in, res));
239#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100240#if defined(SUM_SQUARE)
241 in *= in;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000242#endif // defined(SUM_SQUARE)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000243#if defined(PROD)
244 res *= in;
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100245#else // !defined(PROD)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100246 res += in;
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100247#endif // defined(PROD)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100248#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100249 }
250
Michalis Spyrou7930db42018-11-22 17:36:28 +0000251 // Store result
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100252#if defined(MEAN)
253 res /= HEIGHT;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000254#endif // defined(MEAN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100255 vstore16(CONVERT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
256}
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100257#endif // defined(HEIGHT)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100258
259#if defined(DEPTH)
260/** This kernel performs reduction on z-axis.
261 *
262 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
263 * @note The depth size must be passed at compile time using -DDEPTH e.g. -DDEPTH=128
264 *
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100265 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/S32/F16/F32
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100266 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
267 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
268 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
269 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
270 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
271 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
272 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
273 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p input_ptt
274 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
275 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
276 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
277 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
278 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
279 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
280 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
281 */
282__kernel void reduction_operation_z(
283 TENSOR3D_DECLARATION(input),
284 TENSOR3D_DECLARATION(output))
285{
286 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
287 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
288
289 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000290 res = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100291
Georgios Pinitas8be91482019-03-26 17:23:28 +0000292#if defined(COMPLEX)
293 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
294 res1 = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 8, 0, 0)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
295#endif // defined(COMPLEX)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000296#if defined(SUM_SQUARE)
297 res *= res;
298#endif // defined(SUM_SQUARE)
299
Michalis Spyrou7930db42018-11-22 17:36:28 +0000300 for(unsigned int z = 1; z < DEPTH; ++z)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100301 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100302 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
303 in = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, z)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7930db42018-11-22 17:36:28 +0000304
Georgios Pinitas8be91482019-03-26 17:23:28 +0000305#if defined(COMPLEX)
306 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
307 in1 = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 8, 0, z)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
308#endif // defined(COMPLEX)
309
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100310#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100311 res = select(res, in, ISLESS(in, res));
312#elif defined(MAX)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100313 res = select(res, in, ISGREATER(in, res));
314#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100315#if defined(SUM_SQUARE)
316 in *= in;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000317#endif // defined(SUM_SQUARE)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000318#if defined(PROD)
319 res *= in;
Georgios Pinitas8be91482019-03-26 17:23:28 +0000320#else //!defined(PROD)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100321 res += in;
Georgios Pinitas8be91482019-03-26 17:23:28 +0000322#if defined(COMPLEX)
323 res1 += in1;
324#endif // defined(COMPLEX)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100325#endif // defined(PROD)
326#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100327 }
328
Michalis Spyrou7930db42018-11-22 17:36:28 +0000329 // Store result
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100330#if defined(MEAN)
331 res /= DEPTH;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000332#endif // defined(MEAN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100333 vstore16(CONVERT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000334#if defined(COMPLEX)
335 vstore16(CONVERT(res1, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)tensor3D_offset(&output, 8, 0, 0));
336#endif // defined(COMPLEX)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100337}
338#endif /* defined(DEPTH) */
339
340#if defined(BATCH) && defined(DEPTH)
341/** This kernel performs reduction on w-axis.
342 *
343 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
344 * @note The batch size must be passed at compile time using -DBATCH e.g. -DBATCH=128
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000345 * @note The depth size must be passed at compile time using -DBATCH e.g. -DDEPTH=128
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100346 *
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100347 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/S32/F16/F32
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100348 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
349 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
350 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
351 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
352 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
353 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
354 * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes)
355 * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes)
356 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
357 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p input_ptt
358 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
359 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
360 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
361 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
362 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
363 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
364 * @param[in] output_stride_w Stride of the output tensor in W dimension (in bytes)
365 * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes)
366 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
367 */
368__kernel void reduction_operation_w(
369 TENSOR4D_DECLARATION(input),
370 TENSOR4D_DECLARATION(output))
371{
372 Tensor4D input = CONVERT_TO_TENSOR4D_STRUCT(input, DEPTH);
373 Tensor4D output = CONVERT_TO_TENSOR4D_STRUCT(output, DEPTH);
374
375 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000376 res = CONVERT(vload16(0, (__global DATA_TYPE *)tensor4D_offset(&input, 0, 0, 0, 0)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100377
Michalis Spyrou7930db42018-11-22 17:36:28 +0000378#if defined(SUM_SQUARE)
379 res *= res;
380#endif // defined(SUM_SQUARE)
381
Michalis Spyrou7930db42018-11-22 17:36:28 +0000382 for(unsigned int w = 1; w < BATCH; ++w)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100383 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100384 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
385 in = CONVERT(vload16(0, (__global DATA_TYPE *)tensor4D_offset(&input, 0, 0, 0, w)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7930db42018-11-22 17:36:28 +0000386
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100387#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100388 res = select(res, in, ISLESS(in, res));
389#elif defined(MAX)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100390 res = select(res, in, ISGREATER(in, res));
391#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100392#if defined(SUM_SQUARE)
393 in *= in;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000394#endif // defined(SUM_SQUARE)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000395#if defined(PROD)
396 res *= in;
397#else //!defined(PROD)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100398 res += in;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000399#endif //defined(PROD)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100400#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100401 }
402
Michalis Spyrou7930db42018-11-22 17:36:28 +0000403 // Store result
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100404#if defined(MEAN)
405 res /= BATCH;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000406#endif // defined(MEAN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100407 vstore16(CONVERT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
408}
Manuel Bottinib412fab2018-12-10 17:40:23 +0000409#endif /* defined(BATCH) && defined(DEPTH) */