blob: 7da74381d3af20acf1e43e2ab01fdccc7b91968c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbier967f86d2018-01-24 09:47:44 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44: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/core/CL/ICLKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35#include <cstddef>
36
37using namespace arm_compute;
38
39void arm_compute::enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint)
40{
41 if(kernel.kernel()() == nullptr)
42 {
43 return;
44 }
45
Anthony Barbier967f86d2018-01-24 09:47:44 +000046 // Make sure that dimensions > Z are 1
47 for(unsigned int i = 3; i < Coordinates::num_max_dimensions; ++i)
48 {
49 ARM_COMPUTE_ERROR_ON((window[i].end() - window[i].start()) != 1);
50 }
51
Georgios Pinitas1f378ee2017-10-27 13:37:16 +010052 cl::NDRange gws = ICLKernel::gws_from_window(window);
53
54 // Check for empty NDRange
55 if(gws.dimensions() == 0)
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010056 {
57 return;
58 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010060 cl::NDRange valid_lws;
61 if(lws_hint[0] * lws_hint[1] * lws_hint[2] > kernel.get_max_workgroup_size())
62 {
63 valid_lws = cl::NullRange;
64 }
65 else
66 {
67 valid_lws = lws_hint;
68 }
69
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 cl::NDRange lws = cl::NullRange;
71
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010072 if((valid_lws[0] <= gws[0]) && (valid_lws[1] <= gws[1]) && (valid_lws[2] <= gws[2]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 {
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010074 lws = valid_lws;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 }
76
77 queue.enqueueNDRangeKernel(kernel.kernel(), cl::NullRange, gws, lws);
78}
79
80ICLKernel::ICLKernel()
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010081 : _kernel(nullptr), _lws_hint(CLKernelLibrary::get().default_ndrange()), _target(GPUTarget::MIDGARD), _config_id(arm_compute::default_config_id), _max_workgroup_size(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082{
83}
84
85cl::Kernel &ICLKernel::kernel()
86{
87 return _kernel;
88}
89
90template <unsigned int dimension_size>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window)
92{
93 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
94
95 const ITensorInfo *info = tensor->info();
96 const Strides &strides = info->strides_in_bytes();
97
98 // Calculate offset to the start of the window
99 unsigned int offset_first_element = info->offset_first_element_in_bytes();
100
101 for(unsigned int n = 0; n < info->num_dimensions(); ++n)
102 {
103 offset_first_element += window[n].start() * strides[n];
104 }
105
106 unsigned int idx_start = idx;
107 _kernel.setArg(idx++, tensor->cl_buffer());
108
109 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
110 {
111 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
112 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
113 }
114
115 _kernel.setArg<cl_uint>(idx++, offset_first_element);
116
117 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
118 "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
119 ARM_COMPUTE_UNUSED(idx_start);
120}
121
122void ICLKernel::add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
123{
124 add_tensor_argument<1>(idx, tensor, window);
125}
126
127void ICLKernel::add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
128{
129 add_tensor_argument<2>(idx, tensor, window);
130}
131
132void ICLKernel::add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
133{
134 add_tensor_argument<3>(idx, tensor, window);
135}
136
steniu01868e5412017-07-17 23:16:00 +0100137void ICLKernel::add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
138{
139 add_tensor_argument<4>(idx, tensor, window);
140}
141
SiCong Li3e363692017-07-04 15:02:10 +0100142unsigned int ICLKernel::num_arguments_per_1D_array() const
143{
144 return num_arguments_per_array<1>();
145}
146
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147unsigned int ICLKernel::num_arguments_per_1D_tensor() const
148{
149 return num_arguments_per_tensor<1>();
150}
151
152unsigned int ICLKernel::num_arguments_per_2D_tensor() const
153{
154 return num_arguments_per_tensor<2>();
155}
156
157unsigned int ICLKernel::num_arguments_per_3D_tensor() const
158{
159 return num_arguments_per_tensor<3>();
160}
161
steniu01868e5412017-07-17 23:16:00 +0100162unsigned int ICLKernel::num_arguments_per_4D_tensor() const
163{
164 return num_arguments_per_tensor<4>();
165}
166
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167void ICLKernel::set_target(cl::Device &device)
168{
169 _target = get_target_from_device(device);
170}
171
172void ICLKernel::set_target(GPUTarget target)
173{
174 _target = target;
175}
176
177GPUTarget ICLKernel::get_target() const
178{
179 return _target;
180}
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100181
182size_t ICLKernel::get_max_workgroup_size()
183{
184 if(_max_workgroup_size == 0)
185 {
186 _max_workgroup_size = CLKernelLibrary::get().max_local_workgroup_size(_kernel);
187 }
188 return _max_workgroup_size;
189}
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100190
191cl::NDRange ICLKernel::gws_from_window(const Window &window)
192{
193 if((window.x().end() - window.x().start()) == 0 || (window.y().end() - window.y().start()) == 0)
194 {
195 return cl::NullRange;
196 }
197
198 cl::NDRange gws((window.x().end() - window.x().start()) / window.x().step(),
199 (window.y().end() - window.y().start()) / window.y().step(),
200 (window.z().end() - window.z().start()) / window.z().step());
201
202 return gws;
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000203}