blob: 7a95374bbf1ea0cd596e6b5f87a8d56097e4d615 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
3 *
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
46 ARM_COMPUTE_ERROR_ON((0 == (window.x().end() - window.x().start())) || (0 == (window.y().end() - window.y().start())));
47
48 cl::NDRange gws((window.x().end() - window.x().start()) / window.x().step(),
49 (window.y().end() - window.y().start()) / window.y().step(),
50 (window.z().end() - window.z().start()) / window.z().step());
51
52 cl::NDRange lws = cl::NullRange;
53
54 if((lws_hint[0] <= gws[0]) && (lws_hint[1] <= gws[1]) && (lws_hint[2] <= gws[2]))
55 {
56 lws = lws_hint;
57 }
58
59 queue.enqueueNDRangeKernel(kernel.kernel(), cl::NullRange, gws, lws);
60}
61
62ICLKernel::ICLKernel()
steniu015f910722017-08-23 10:15:22 +010063 : _kernel(nullptr), _lws_hint(CLKernelLibrary::get().default_ndrange()), _target(GPUTarget::MIDGARD)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064{
65}
66
67cl::Kernel &ICLKernel::kernel()
68{
69 return _kernel;
70}
71
72template <unsigned int dimension_size>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window)
74{
75 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
76
77 const ITensorInfo *info = tensor->info();
78 const Strides &strides = info->strides_in_bytes();
79
80 // Calculate offset to the start of the window
81 unsigned int offset_first_element = info->offset_first_element_in_bytes();
82
83 for(unsigned int n = 0; n < info->num_dimensions(); ++n)
84 {
85 offset_first_element += window[n].start() * strides[n];
86 }
87
88 unsigned int idx_start = idx;
89 _kernel.setArg(idx++, tensor->cl_buffer());
90
91 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
92 {
93 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
94 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
95 }
96
97 _kernel.setArg<cl_uint>(idx++, offset_first_element);
98
99 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
100 "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
101 ARM_COMPUTE_UNUSED(idx_start);
102}
103
104void ICLKernel::add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
105{
106 add_tensor_argument<1>(idx, tensor, window);
107}
108
109void ICLKernel::add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
110{
111 add_tensor_argument<2>(idx, tensor, window);
112}
113
114void ICLKernel::add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
115{
116 add_tensor_argument<3>(idx, tensor, window);
117}
118
steniu01868e5412017-07-17 23:16:00 +0100119void ICLKernel::add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
120{
121 add_tensor_argument<4>(idx, tensor, window);
122}
123
SiCong Li3e363692017-07-04 15:02:10 +0100124unsigned int ICLKernel::num_arguments_per_1D_array() const
125{
126 return num_arguments_per_array<1>();
127}
128
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129unsigned int ICLKernel::num_arguments_per_1D_tensor() const
130{
131 return num_arguments_per_tensor<1>();
132}
133
134unsigned int ICLKernel::num_arguments_per_2D_tensor() const
135{
136 return num_arguments_per_tensor<2>();
137}
138
139unsigned int ICLKernel::num_arguments_per_3D_tensor() const
140{
141 return num_arguments_per_tensor<3>();
142}
143
steniu01868e5412017-07-17 23:16:00 +0100144unsigned int ICLKernel::num_arguments_per_4D_tensor() const
145{
146 return num_arguments_per_tensor<4>();
147}
148
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149void ICLKernel::set_target(cl::Device &device)
150{
151 _target = get_target_from_device(device);
152}
153
154void ICLKernel::set_target(GPUTarget target)
155{
156 _target = target;
157}
158
159GPUTarget ICLKernel::get_target() const
160{
161 return _target;
162}