2 * Copyright (c) 2016, 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/core/CL/ICLKernel.h"
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"
37 using namespace arm_compute;
39 void arm_compute::enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint)
41 if(kernel.kernel()() == nullptr)
46 if((window.x().end() - window.x().start()) == 0 || (window.y().end() - window.y().start()) == 0)
51 cl::NDRange gws((window.x().end() - window.x().start()) / window.x().step(),
52 (window.y().end() - window.y().start()) / window.y().step(),
53 (window.z().end() - window.z().start()) / window.z().step());
55 cl::NDRange valid_lws;
56 if(lws_hint[0] * lws_hint[1] * lws_hint[2] > kernel.get_max_workgroup_size())
58 valid_lws = cl::NullRange;
65 cl::NDRange lws = cl::NullRange;
67 if((valid_lws[0] <= gws[0]) && (valid_lws[1] <= gws[1]) && (valid_lws[2] <= gws[2]))
72 queue.enqueueNDRangeKernel(kernel.kernel(), cl::NullRange, gws, lws);
75 ICLKernel::ICLKernel()
76 : _kernel(nullptr), _lws_hint(CLKernelLibrary::get().default_ndrange()), _target(GPUTarget::MIDGARD), _config_id(arm_compute::default_config_id), _max_workgroup_size(0)
80 cl::Kernel &ICLKernel::kernel()
85 template <unsigned int dimension_size>
86 void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window)
88 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
90 const ITensorInfo *info = tensor->info();
91 const Strides &strides = info->strides_in_bytes();
93 // Calculate offset to the start of the window
94 unsigned int offset_first_element = info->offset_first_element_in_bytes();
96 for(unsigned int n = 0; n < info->num_dimensions(); ++n)
98 offset_first_element += window[n].start() * strides[n];
101 unsigned int idx_start = idx;
102 _kernel.setArg(idx++, tensor->cl_buffer());
104 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
106 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
107 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
110 _kernel.setArg<cl_uint>(idx++, offset_first_element);
112 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
113 "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
114 ARM_COMPUTE_UNUSED(idx_start);
117 void ICLKernel::add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
119 add_tensor_argument<1>(idx, tensor, window);
122 void ICLKernel::add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
124 add_tensor_argument<2>(idx, tensor, window);
127 void ICLKernel::add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
129 add_tensor_argument<3>(idx, tensor, window);
132 void ICLKernel::add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
134 add_tensor_argument<4>(idx, tensor, window);
137 unsigned int ICLKernel::num_arguments_per_1D_array() const
139 return num_arguments_per_array<1>();
142 unsigned int ICLKernel::num_arguments_per_1D_tensor() const
144 return num_arguments_per_tensor<1>();
147 unsigned int ICLKernel::num_arguments_per_2D_tensor() const
149 return num_arguments_per_tensor<2>();
152 unsigned int ICLKernel::num_arguments_per_3D_tensor() const
154 return num_arguments_per_tensor<3>();
157 unsigned int ICLKernel::num_arguments_per_4D_tensor() const
159 return num_arguments_per_tensor<4>();
162 void ICLKernel::set_target(cl::Device &device)
164 _target = get_target_from_device(device);
167 void ICLKernel::set_target(GPUTarget target)
172 GPUTarget ICLKernel::get_target() const
177 size_t ICLKernel::get_max_workgroup_size()
179 if(_max_workgroup_size == 0)
181 _max_workgroup_size = CLKernelLibrary::get().max_local_workgroup_size(_kernel);
183 return _max_workgroup_size;