arm_compute v17.10
[platform/upstream/armcl.git] / src / core / CL / ICLKernel.cpp
1 /*
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"
34
35 #include <cstddef>
36
37 using namespace arm_compute;
38
39 void 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     if((window.x().end() - window.x().start()) == 0 || (window.y().end() - window.y().start()) == 0)
47     {
48         return;
49     }
50
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());
54
55     cl::NDRange valid_lws;
56     if(lws_hint[0] * lws_hint[1] * lws_hint[2] > kernel.get_max_workgroup_size())
57     {
58         valid_lws = cl::NullRange;
59     }
60     else
61     {
62         valid_lws = lws_hint;
63     }
64
65     cl::NDRange lws = cl::NullRange;
66
67     if((valid_lws[0] <= gws[0]) && (valid_lws[1] <= gws[1]) && (valid_lws[2] <= gws[2]))
68     {
69         lws = valid_lws;
70     }
71
72     queue.enqueueNDRangeKernel(kernel.kernel(), cl::NullRange, gws, lws);
73 }
74
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)
77 {
78 }
79
80 cl::Kernel &ICLKernel::kernel()
81 {
82     return _kernel;
83 }
84
85 template <unsigned int dimension_size>
86 void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window)
87 {
88     ARM_COMPUTE_ERROR_ON(tensor == nullptr);
89
90     const ITensorInfo *info    = tensor->info();
91     const Strides     &strides = info->strides_in_bytes();
92
93     // Calculate offset to the start of the window
94     unsigned int offset_first_element = info->offset_first_element_in_bytes();
95
96     for(unsigned int n = 0; n < info->num_dimensions(); ++n)
97     {
98         offset_first_element += window[n].start() * strides[n];
99     }
100
101     unsigned int idx_start = idx;
102     _kernel.setArg(idx++, tensor->cl_buffer());
103
104     for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
105     {
106         _kernel.setArg<cl_uint>(idx++, strides[dimension]);
107         _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
108     }
109
110     _kernel.setArg<cl_uint>(idx++, offset_first_element);
111
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);
115 }
116
117 void ICLKernel::add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
118 {
119     add_tensor_argument<1>(idx, tensor, window);
120 }
121
122 void ICLKernel::add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
123 {
124     add_tensor_argument<2>(idx, tensor, window);
125 }
126
127 void ICLKernel::add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
128 {
129     add_tensor_argument<3>(idx, tensor, window);
130 }
131
132 void ICLKernel::add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
133 {
134     add_tensor_argument<4>(idx, tensor, window);
135 }
136
137 unsigned int ICLKernel::num_arguments_per_1D_array() const
138 {
139     return num_arguments_per_array<1>();
140 }
141
142 unsigned int ICLKernel::num_arguments_per_1D_tensor() const
143 {
144     return num_arguments_per_tensor<1>();
145 }
146
147 unsigned int ICLKernel::num_arguments_per_2D_tensor() const
148 {
149     return num_arguments_per_tensor<2>();
150 }
151
152 unsigned int ICLKernel::num_arguments_per_3D_tensor() const
153 {
154     return num_arguments_per_tensor<3>();
155 }
156
157 unsigned int ICLKernel::num_arguments_per_4D_tensor() const
158 {
159     return num_arguments_per_tensor<4>();
160 }
161
162 void ICLKernel::set_target(cl::Device &device)
163 {
164     _target = get_target_from_device(device);
165 }
166
167 void ICLKernel::set_target(GPUTarget target)
168 {
169     _target = target;
170 }
171
172 GPUTarget ICLKernel::get_target() const
173 {
174     return _target;
175 }
176
177 size_t ICLKernel::get_max_workgroup_size()
178 {
179     if(_max_workgroup_size == 0)
180     {
181         _max_workgroup_size = CLKernelLibrary::get().max_local_workgroup_size(_kernel);
182     }
183     return _max_workgroup_size;
184 }