blob: aa7403b52ba7ff8d34ac626612aa26e519e0166b [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Michalis Spyrouf6402dd2018-01-26 15:06:19 +00002 * Copyright (c) 2016-2018 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}
63
64/** This kernel performs reduction given an operation.
65 *
66 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
67 * @note The data size must be passed at compile time using -DDATA_SIZE e.g. -DDATA_SIZE=32
68 * @note The operation we want to perform must be passed at compile time using -DOPERATION e.g. -DOPERATION=square_sum
69 *
70 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F32
71 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
72 * @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 +000073 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
74 * @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 +010075 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
76 * @param[in] partial_sum_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptt
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000077 * @param[in] partial_sum_stride_x Stride of the output tensor in X dimension (in bytes)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010078 * @param[in] partial_sum_step_x partial_sum_stride_x * number of elements along X processed per workitem(in bytes)
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000079 * @param[in] partial_sum_stride_y Stride of the output tensor in Y dimension (in bytes)
80 * @param[in] partial_sum_step_y partial_sum_stride_y * number of elements along Y processed per workitem(in bytes)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010081 * @param[in] partial_sum_offset_first_element_in_bytes The offset of the first element in the source tensor
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000082 * @param[in] local_sums Local buffer for storing the partial sum
Michalis Spyrou04f089c2017-08-08 17:42:38 +010083 */
84__kernel void reduction_operation(
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000085 IMAGE_DECLARATION(src),
86 IMAGE_DECLARATION(partial_sum),
Michalis Spyrou04f089c2017-08-08 17:42:38 +010087 __local DATA_TYPE *local_sums)
88{
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000089 Image src = CONVERT_TO_IMAGE_STRUCT(src);
90 Image partial_sum = CONVERT_TO_IMAGE_STRUCT(partial_sum);
Michalis Spyrou04f089c2017-08-08 17:42:38 +010091
92 unsigned int lsize = get_local_size(0);
93 unsigned int lid = get_local_id(0);
94
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000095 for(unsigned int y = 0; y < get_local_size(1); ++y)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010096 {
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000097 local_sums[lid] = OPERATION((__global DATA_TYPE *)offset(&src, 0, y));
Michalis Spyrou04f089c2017-08-08 17:42:38 +010098 barrier(CLK_LOCAL_MEM_FENCE);
Michalis Spyrou04f089c2017-08-08 17:42:38 +010099
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000100 // Perform parallel reduction
101 for(unsigned int i = lsize >> 1; i > 0; i >>= 1)
102 {
103 if(lid < i)
104 {
105 local_sums[lid] += local_sums[lid + i];
106 }
107 barrier(CLK_LOCAL_MEM_FENCE);
108 }
109
110 if(lid == 0)
111 {
112 ((__global DATA_TYPE *)offset(&partial_sum, get_group_id(0), y))[0] = local_sums[0];
113 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100114 }
115}