blob: 2a171c396900c94e3f79dfc8aeb74478b60ad0dd [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
John Richardson62385bc2018-04-20 13:11:36 +01002 * Copyright (c) 2017-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 "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
John Richardson62385bc2018-04-20 13:11:36 +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
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +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()
Michalis Spyrou04f089c2017-08-08 17:42:38 +010055{
56}
57
John Richardson62385bc2018-04-20 13:11:36 +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());
John Richardson62385bc2018-04-20 13:11:36 +010074 }
75
76 // Validate ReductionOperation only on first kernel
77 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(input, sums_vector.get(), axis, op));
78
79 // Validate ReductionOperation on intermediate stages
80 for(unsigned int i = 1; i < num_of_stages - 1; ++i)
81 {
82 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(sums_vector.get() + i - 1, sums_vector.get() + i, axis, op));
83 }
84
85 // Validate ReductionOperation on the last stage
86 const unsigned int last_stage = num_of_stages - 1;
87 ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(sums_vector.get() + last_stage - 1, output, axis, op));
88
89 return Status{};
90}
91
Michalis Spyrou04f089c2017-08-08 17:42:38 +010092void CLReductionOperation::configure(ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op)
93{
John Richardson62385bc2018-04-20 13:11:36 +010094 _num_of_stages = calculate_number_of_stages(input->info());
Michalis Spyrou04f089c2017-08-08 17:42:38 +010095
Georgios Pinitasaec513c2017-09-15 19:36:30 +010096 // Create temporary tensors
97 _sums_vector = arm_compute::support::cpp14::make_unique<CLTensor[]>(_num_of_stages - 1);
98
Michalis Spyrou04f089c2017-08-08 17:42:38 +010099 // Configure reduction operation kernels
100 _reduction_kernels_vector = arm_compute::support::cpp14::make_unique<CLReductionOperationKernel[]>(_num_of_stages);
101 _border_handlers_vector = arm_compute::support::cpp14::make_unique<CLFillBorderKernel[]>(_num_of_stages);
102
103 TensorShape shape{ input->info()->tensor_shape() };
104 for(unsigned int i = 0; i < _num_of_stages - 1; i++)
105 {
106 shape.set(0, ceil(shape.x() / 128.f));
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100107 _sums_vector[i].allocator()->init(TensorInfo(shape, input->info()->num_channels(), input->info()->data_type()));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100108 }
109
110 // Apply ReductionOperation only on first kernel
Georgios Pinitasaec513c2017-09-15 19:36:30 +0100111 _memory_group.manage(_sums_vector.get());
112 _reduction_kernels_vector[0].configure(input, _sums_vector.get(), axis, op);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100113 _border_handlers_vector[0].configure(input, _reduction_kernels_vector[0].border_size(), BorderMode::CONSTANT, PixelValue(0));
Georgios Pinitasaec513c2017-09-15 19:36:30 +0100114
115 // Apply ReductionOperation on intermediate stages
116 for(unsigned int i = 1; i < _num_of_stages - 1; ++i)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100117 {
Georgios Pinitasaec513c2017-09-15 19:36:30 +0100118 _memory_group.manage(_sums_vector.get() + i);
119 _reduction_kernels_vector[i].configure(_sums_vector.get() + i - 1, _sums_vector.get() + i, axis, ReductionOperation::SUM);
120 _border_handlers_vector[i].configure(_sums_vector.get() + i - 1, _reduction_kernels_vector[i].border_size(), BorderMode::CONSTANT, PixelValue(0));
121 _sums_vector[i - 1].allocator()->allocate();
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100122 }
Georgios Pinitasaec513c2017-09-15 19:36:30 +0100123
124 // Apply ReductionOperation on the last stage
125 const unsigned int last_stage = _num_of_stages - 1;
126 _reduction_kernels_vector[last_stage].configure(_sums_vector.get() + last_stage - 1, output, axis, ReductionOperation::SUM);
127 _border_handlers_vector[last_stage].configure(_sums_vector.get() + last_stage - 1, _reduction_kernels_vector[last_stage].border_size(), BorderMode::CONSTANT, PixelValue(0));
128 _sums_vector[last_stage - 1].allocator()->allocate();
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100129}
130
131void CLReductionOperation::run()
132{
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100133 _memory_group.acquire();
134
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100135 for(unsigned int i = 0; i < _num_of_stages; ++i)
136 {
137 CLScheduler::get().enqueue(_border_handlers_vector[i], false);
138 CLScheduler::get().enqueue(_reduction_kernels_vector[i], false);
139 }
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100140
141 _memory_group.release();
John Richardson62385bc2018-04-20 13:11:36 +0100142}