blob: 3a5133d91f9e66514c2c6323aa1b852fa33a9712 [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Jenkinsb3a371b2018-05-23 11:36:53 +01002 * Copyright (c) 2017-2018 ARM Limited.
Kaizen8938bd32017-09-28 14:38:23 +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 "arm_compute/runtime/CL/functions/CLReductionOperation.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/CL/kernels/CLReductionOperationKernel.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/PixelValue.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/runtime/CL/CLScheduler.h"
33#include "arm_compute/runtime/Tensor.h"
34#include "support/ToolchainSupport.h"
35
36using namespace arm_compute;
37
Jenkinsb3a371b2018-05-23 11:36:53 +010038namespace
39{
40unsigned int calculate_number_of_stages(const ITensorInfo *input)
41{
42 // Calculate number of WGs. 16 elements per thread, 8 threads per WG
43 const unsigned int num_of_wg = ceil(input->dimension(0) / 128.f);
44
45 // Calculate number of stages. First stage performs op and the rest reduction sum
46 // depending on the size of the input. Last stage should have only 1 WG.
47 const unsigned int num_of_stages = num_of_wg / 128 + 2;
48
49 return num_of_stages;
50}
51} // namespace
52
Kaizen8938bd32017-09-28 14:38:23 +010053CLReductionOperation::CLReductionOperation(std::shared_ptr<IMemoryManager> memory_manager)
54 : _memory_group(std::move(memory_manager)), _sums_vector(), _reduction_kernels_vector(), _border_handlers_vector(), _num_of_stages()
55{
56}
57
Jenkinsb3a371b2018-05-23 11:36:53 +010058Status CLReductionOperation::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
59{
60 const unsigned int num_of_stages = calculate_number_of_stages(input);
61
62 // Create temporary tensor infos
63 auto sums_vector = arm_compute::support::cpp14::make_unique<TensorInfo[]>(num_of_stages - 1);
64
65 // Create intermediate tensor info
66 TensorShape shape{ input->tensor_shape() };
67
68 for(unsigned int i = 0; i < num_of_stages - 1; i++)
69 {
70 shape.set(0, ceil(shape.x() / 128.f));
71 sums_vector[i].set_data_type(input->data_type());
72 sums_vector[i].set_tensor_shape(shape);
73 sums_vector[i].set_num_channels(input->num_channels());
74 sums_vector[i].set_fixed_point_position(input->fixed_point_position());
75 }
76
77 // Validate ReductionOperation only on first kernel
78 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(input, sums_vector.get(), axis, op));
79
80 // Validate ReductionOperation on intermediate stages
81 for(unsigned int i = 1; i < num_of_stages - 1; ++i)
82 {
83 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(sums_vector.get() + i - 1, sums_vector.get() + i, axis, op));
84 }
85
86 // Validate ReductionOperation on the last stage
87 const unsigned int last_stage = num_of_stages - 1;
88 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(sums_vector.get() + last_stage - 1, output, axis, op));
89
90 return Status{};
91}
92
Kaizen8938bd32017-09-28 14:38:23 +010093void CLReductionOperation::configure(ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op)
94{
Jenkinsb3a371b2018-05-23 11:36:53 +010095 _num_of_stages = calculate_number_of_stages(input->info());
Kaizen8938bd32017-09-28 14:38:23 +010096
97 // Create temporary tensors
98 _sums_vector = arm_compute::support::cpp14::make_unique<CLTensor[]>(_num_of_stages - 1);
99
100 // Configure reduction operation kernels
101 _reduction_kernels_vector = arm_compute::support::cpp14::make_unique<CLReductionOperationKernel[]>(_num_of_stages);
102 _border_handlers_vector = arm_compute::support::cpp14::make_unique<CLFillBorderKernel[]>(_num_of_stages);
103
104 TensorShape shape{ input->info()->tensor_shape() };
105 for(unsigned int i = 0; i < _num_of_stages - 1; i++)
106 {
107 shape.set(0, ceil(shape.x() / 128.f));
108 _sums_vector[i].allocator()->init(TensorInfo(shape, input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
109 }
110
111 // Apply ReductionOperation only on first kernel
112 _memory_group.manage(_sums_vector.get());
113 _reduction_kernels_vector[0].configure(input, _sums_vector.get(), axis, op);
114 _border_handlers_vector[0].configure(input, _reduction_kernels_vector[0].border_size(), BorderMode::CONSTANT, PixelValue(0));
115
116 // Apply ReductionOperation on intermediate stages
117 for(unsigned int i = 1; i < _num_of_stages - 1; ++i)
118 {
119 _memory_group.manage(_sums_vector.get() + i);
120 _reduction_kernels_vector[i].configure(_sums_vector.get() + i - 1, _sums_vector.get() + i, axis, ReductionOperation::SUM);
121 _border_handlers_vector[i].configure(_sums_vector.get() + i - 1, _reduction_kernels_vector[i].border_size(), BorderMode::CONSTANT, PixelValue(0));
122 _sums_vector[i - 1].allocator()->allocate();
123 }
124
125 // Apply ReductionOperation on the last stage
126 const unsigned int last_stage = _num_of_stages - 1;
127 _reduction_kernels_vector[last_stage].configure(_sums_vector.get() + last_stage - 1, output, axis, ReductionOperation::SUM);
128 _border_handlers_vector[last_stage].configure(_sums_vector.get() + last_stage - 1, _reduction_kernels_vector[last_stage].border_size(), BorderMode::CONSTANT, PixelValue(0));
129 _sums_vector[last_stage - 1].allocator()->allocate();
130}
131
132void CLReductionOperation::run()
133{
134 _memory_group.acquire();
135
136 for(unsigned int i = 0; i < _num_of_stages; ++i)
137 {
138 CLScheduler::get().enqueue(_border_handlers_vector[i], false);
139 CLScheduler::get().enqueue(_reduction_kernels_vector[i], false);
140 }
141
142 _memory_group.release();
Jenkinsb3a371b2018-05-23 11:36:53 +0100143}