blob: ac53e7f1d2bd119c8cf702755bf96bf0b8981d07 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCong Li47f177e2023-02-22 17:24:09 +00002 * Copyright (c) 2016-2023 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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/ICLKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Helpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/helpers/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31#include <cstddef>
32
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033void arm_compute::enqueue(cl::CommandQueue &queue,
34 ICLKernel &kernel,
35 const Window &window,
36 const cl::NDRange &lws_hint,
37 bool use_dummy_work_items)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 if (kernel.kernel()() == nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 {
41 return;
42 }
43
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044 for (unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i)
Anthony Barbier967f86d2018-01-24 09:47:44 +000045 {
Diego Lopez Recas0021d752017-12-18 14:42:56 +000046 ARM_COMPUTE_ERROR_ON(window[i].step() == 0);
47 // Make sure that dimensions > Z are 1
48 ARM_COMPUTE_ERROR_ON((i >= 3) && ((window[i].end() - window[i].start()) != 1));
Anthony Barbier967f86d2018-01-24 09:47:44 +000049 }
50
SiCong Li47f177e2023-02-22 17:24:09 +000051 cl::NDRange gws = ICLKernel::gws_from_window(window, use_dummy_work_items);
Georgios Pinitas1f378ee2017-10-27 13:37:16 +010052
53 // Check for empty NDRange
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 if (gws.dimensions() == 0)
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010055 {
56 return;
57 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058
SiCong Li47f177e2023-02-22 17:24:09 +000059 kernel.cache_gws(gws);
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +000060
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010061 cl::NDRange valid_lws;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 if (lws_hint[0] * lws_hint[1] * lws_hint[2] > kernel.get_max_workgroup_size())
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010063 {
64 valid_lws = cl::NullRange;
65 }
66 else
67 {
68 valid_lws = lws_hint;
69 }
70
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 cl::NDRange lws = cl::NullRange;
72
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 if ((valid_lws[0] <= gws[0]) && (valid_lws[1] <= gws[1]) && (valid_lws[2] <= gws[2]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 {
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010075 lws = valid_lws;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 }
77
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (CLKernelLibrary::get().is_wbsm_supported())
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000079 {
80 set_wbsm(kernel.kernel(), kernel.wbsm_hint());
81 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 queue.enqueueNDRangeKernel(kernel.kernel(), cl::NullRange, gws, lws);
83}
84
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000085namespace arm_compute
86{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087template <unsigned int dimension_size>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window)
89{
90 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
91
92 const ITensorInfo *info = tensor->info();
93 const Strides &strides = info->strides_in_bytes();
94
95 // Calculate offset to the start of the window
96 unsigned int offset_first_element = info->offset_first_element_in_bytes();
97
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 for (unsigned int n = 0; n < info->num_dimensions(); ++n)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 {
Sang-Hoon Parkcecb0a72019-09-17 08:59:09 +0100100 offset_first_element += (window.is_broadcasted(n) ? 0 : window[n].start()) * strides[n];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 }
102
103 unsigned int idx_start = idx;
104 _kernel.setArg(idx++, tensor->cl_buffer());
105
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 for (unsigned int d = 0; d < dimension_size; ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 {
Giorgio Arenaada6cbc2021-04-16 17:03:39 +0100108 _kernel.setArg<cl_uint>(idx++, window.is_broadcasted(d) ? 0 : strides[d]);
109 _kernel.setArg<cl_uint>(idx++, window.is_broadcasted(d) ? 0 : (strides[d] * window[d].step()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 }
111
112 _kernel.setArg<cl_uint>(idx++, offset_first_element);
113
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100114 ARM_COMPUTE_ERROR_ON_MSG_VAR(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100115 "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel",
116 dimension_size, num_arguments_per_tensor<dimension_size>());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 ARM_COMPUTE_UNUSED(idx_start);
118}
119
Gian Marco Iodice4fb56702021-11-10 11:18:50 +0000120void ICLKernel::add_3d_tensor_nhw_argument(unsigned int &idx, const ICLTensor *tensor)
121{
122 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
123
124 const ITensorInfo *info = tensor->info();
125 ARM_COMPUTE_ERROR_ON(info == nullptr);
126 const Strides &strides = info->strides_in_bytes();
127
128 // Tensor poniter
129 _kernel.setArg(idx++, tensor->cl_buffer());
130
131 // Add stride_y, stride_z
132 _kernel.setArg<cl_uint>(idx++, strides[1]);
133 _kernel.setArg<cl_uint>(idx++, strides[2]);
134
135 // Tensor dimensions
136 _kernel.setArg<cl_uint>(idx++, info->dimension(0));
137 _kernel.setArg<cl_uint>(idx++, info->dimension(1));
138 _kernel.setArg<cl_uint>(idx++, info->dimension(2));
139
140 // Offset of first element
141 unsigned int offset_first_element = info->offset_first_element_in_bytes();
142 _kernel.setArg<cl_uint>(idx++, offset_first_element);
143}
144
Adnan AlSinan17975a62021-11-08 17:46:39 +0000145void ICLKernel::add_4d_tensor_nhwc_argument(unsigned int &idx, const ICLTensor *tensor)
146{
147 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
148
149 const ITensorInfo *info = tensor->info();
150 ARM_COMPUTE_ERROR_ON(info == nullptr);
151 const Strides &strides = info->strides_in_bytes();
152
153 // Tensor poniter
154 _kernel.setArg(idx++, tensor->cl_buffer());
155
156 // Add stride_y, stride_z and stride_w
157 _kernel.setArg<cl_uint>(idx++, strides[1]);
158 _kernel.setArg<cl_uint>(idx++, strides[2]);
159 _kernel.setArg<cl_uint>(idx++, strides[3]);
160
161 // Tensor dimensions
162 _kernel.setArg<cl_uint>(idx++, info->dimension(0));
163 _kernel.setArg<cl_uint>(idx++, info->dimension(1));
164 _kernel.setArg<cl_uint>(idx++, info->dimension(2));
165 _kernel.setArg<cl_uint>(idx++, info->dimension(3));
166
167 // Offset of first element
168 unsigned int offset_first_element = info->offset_first_element_in_bytes();
169 _kernel.setArg<cl_uint>(idx++, offset_first_element);
170}
171
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000172#ifndef DOXYGEN_SKIP_THIS
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000173template void ICLKernel::add_tensor_argument<1>(unsigned &idx, const ICLTensor *tensor, const Window &window);
174template void ICLKernel::add_tensor_argument<2>(unsigned &idx, const ICLTensor *tensor, const Window &window);
175template void ICLKernel::add_tensor_argument<3>(unsigned &idx, const ICLTensor *tensor, const Window &window);
176template void ICLKernel::add_tensor_argument<4>(unsigned &idx, const ICLTensor *tensor, const Window &window);
ramelg0137515692022-02-26 22:06:20 +0000177template void ICLKernel::add_tensor_argument<5>(unsigned &idx, const ICLTensor *tensor, const Window &window);
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000178#endif /* DOXYGEN_SKIP_THIS */
steniu01868e5412017-07-17 23:16:00 +0100179
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180void ICLKernel::set_target(cl::Device &device)
181{
182 _target = get_target_from_device(device);
183}
184
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100185size_t ICLKernel::get_max_workgroup_size()
186{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 if (_max_workgroup_size == 0)
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100188 {
189 _max_workgroup_size = CLKernelLibrary::get().max_local_workgroup_size(_kernel);
190 }
191 return _max_workgroup_size;
192}
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100193
SiCong Li47f177e2023-02-22 17:24:09 +0000194cl::NDRange ICLKernel::gws_from_window(const Window &window, bool use_dummy_work_items)
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100195{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100196 if ((window.x().end() - window.x().start()) == 0 || (window.y().end() - window.y().start()) == 0)
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100197 {
198 return cl::NullRange;
199 }
200
201 cl::NDRange gws((window.x().end() - window.x().start()) / window.x().step(),
202 (window.y().end() - window.y().start()) / window.y().step(),
203 (window.z().end() - window.z().start()) / window.z().step());
204
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100205 if (use_dummy_work_items)
SiCong Li47f177e2023-02-22 17:24:09 +0000206 {
207 gws.get()[0] = get_next_power_two(gws[0]);
208 gws.get()[1] = get_next_power_two(gws[1]);
209 }
210
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100211 return gws;
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000212}
SiCong Li47f177e2023-02-22 17:24:09 +0000213
214cl::NDRange ICLKernel::get_cached_gws() const
215{
216 return _cached_gws;
217}
218
219void ICLKernel::cache_gws(const cl::NDRange &gws)
220{
221 _cached_gws = gws;
222}
223} // namespace arm_compute