blob: 2d28a496c96b45844e70a526c8d51f9cc15d9ca8 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +00002 * Copyright (c) 2016-2019 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
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +000039void arm_compute::enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint, bool use_dummy_work_items)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
41 if(kernel.kernel()() == nullptr)
42 {
43 return;
44 }
45
Diego Lopez Recas0021d752017-12-18 14:42:56 +000046 for(unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i)
Anthony Barbier967f86d2018-01-24 09:47:44 +000047 {
Diego Lopez Recas0021d752017-12-18 14:42:56 +000048 ARM_COMPUTE_ERROR_ON(window[i].step() == 0);
49 // Make sure that dimensions > Z are 1
50 ARM_COMPUTE_ERROR_ON((i >= 3) && ((window[i].end() - window[i].start()) != 1));
Anthony Barbier967f86d2018-01-24 09:47:44 +000051 }
52
Georgios Pinitas1f378ee2017-10-27 13:37:16 +010053 cl::NDRange gws = ICLKernel::gws_from_window(window);
54
55 // Check for empty NDRange
56 if(gws.dimensions() == 0)
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010057 {
58 return;
59 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +000061 // Use dummy work-items
62 if(use_dummy_work_items)
63 {
64 gws.get()[0] = get_next_power_two(gws[0]);
65 gws.get()[1] = get_next_power_two(gws[1]);
66 }
67
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010068 cl::NDRange valid_lws;
69 if(lws_hint[0] * lws_hint[1] * lws_hint[2] > kernel.get_max_workgroup_size())
70 {
71 valid_lws = cl::NullRange;
72 }
73 else
74 {
75 valid_lws = lws_hint;
76 }
77
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 cl::NDRange lws = cl::NullRange;
79
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010080 if((valid_lws[0] <= gws[0]) && (valid_lws[1] <= gws[1]) && (valid_lws[2] <= gws[2]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 {
Abel Bernabeu5a6e0532017-09-28 09:53:45 +010082 lws = valid_lws;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 }
84
85 queue.enqueueNDRangeKernel(kernel.kernel(), cl::NullRange, gws, lws);
86}
87
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088template <unsigned int dimension_size>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window)
90{
91 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
92
93 const ITensorInfo *info = tensor->info();
94 const Strides &strides = info->strides_in_bytes();
95
96 // Calculate offset to the start of the window
97 unsigned int offset_first_element = info->offset_first_element_in_bytes();
98
99 for(unsigned int n = 0; n < info->num_dimensions(); ++n)
100 {
101 offset_first_element += window[n].start() * strides[n];
102 }
103
104 unsigned int idx_start = idx;
105 _kernel.setArg(idx++, tensor->cl_buffer());
106
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000107 for(unsigned int d = 0; d < dimension_size; ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 {
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000109 _kernel.setArg<cl_uint>(idx++, strides[d]);
110 _kernel.setArg<cl_uint>(idx++, strides[d] * window[d].step());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 }
112
113 _kernel.setArg<cl_uint>(idx++, offset_first_element);
114
115 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
116 "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
117 ARM_COMPUTE_UNUSED(idx_start);
118}
119
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000120#ifndef DOXYGEN_SKIP_THIS
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000121template void ICLKernel::add_tensor_argument<1>(unsigned &idx, const ICLTensor *tensor, const Window &window);
122template void ICLKernel::add_tensor_argument<2>(unsigned &idx, const ICLTensor *tensor, const Window &window);
123template void ICLKernel::add_tensor_argument<3>(unsigned &idx, const ICLTensor *tensor, const Window &window);
124template void ICLKernel::add_tensor_argument<4>(unsigned &idx, const ICLTensor *tensor, const Window &window);
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000125#endif /* DOXYGEN_SKIP_THIS */
steniu01868e5412017-07-17 23:16:00 +0100126
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127void ICLKernel::set_target(cl::Device &device)
128{
129 _target = get_target_from_device(device);
130}
131
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100132size_t ICLKernel::get_max_workgroup_size()
133{
134 if(_max_workgroup_size == 0)
135 {
136 _max_workgroup_size = CLKernelLibrary::get().max_local_workgroup_size(_kernel);
137 }
138 return _max_workgroup_size;
139}
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100140
141cl::NDRange ICLKernel::gws_from_window(const Window &window)
142{
143 if((window.x().end() - window.x().start()) == 0 || (window.y().end() - window.y().start()) == 0)
144 {
145 return cl::NullRange;
146 }
147
148 cl::NDRange gws((window.x().end() - window.x().start()) / window.x().step(),
149 (window.y().end() - window.y().start()) / window.y().step(),
150 (window.z().end() - window.z().start()) / window.z().step());
151
152 return gws;
Anton Lokhmotov3e80c7f2017-11-20 11:02:10 +0000153}