blob: 7ac0fe3bbb8d6f52b2c3c0e82ad7440af376ecb5 [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"
34#include "arm_compute/runtime/CL/CLScheduler.h"
35
36#include <cstddef>
37
38using namespace arm_compute;
39
40void arm_compute::enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint)
41{
42 if(kernel.kernel()() == nullptr)
43 {
44 return;
45 }
46
47 ARM_COMPUTE_ERROR_ON((0 == (window.x().end() - window.x().start())) || (0 == (window.y().end() - window.y().start())));
48
49 cl::NDRange gws((window.x().end() - window.x().start()) / window.x().step(),
50 (window.y().end() - window.y().start()) / window.y().step(),
51 (window.z().end() - window.z().start()) / window.z().step());
52
53 cl::NDRange lws = cl::NullRange;
54
55 if((lws_hint[0] <= gws[0]) && (lws_hint[1] <= gws[1]) && (lws_hint[2] <= gws[2]))
56 {
57 lws = lws_hint;
58 }
59
60 queue.enqueueNDRangeKernel(kernel.kernel(), cl::NullRange, gws, lws);
61}
62
63ICLKernel::ICLKernel()
64 : _kernel(nullptr), _lws_hint(cl::Range_128_1), _target(CLScheduler::get().target())
65{
66}
67
68cl::Kernel &ICLKernel::kernel()
69{
70 return _kernel;
71}
72
73template <unsigned int dimension_size>
74unsigned int ICLKernel::num_arguments_per_tensor() const
75{
76 return 2 + 2 * dimension_size;
77}
78
79template <unsigned int dimension_size>
80void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window)
81{
82 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
83
84 const ITensorInfo *info = tensor->info();
85 const Strides &strides = info->strides_in_bytes();
86
87 // Calculate offset to the start of the window
88 unsigned int offset_first_element = info->offset_first_element_in_bytes();
89
90 for(unsigned int n = 0; n < info->num_dimensions(); ++n)
91 {
92 offset_first_element += window[n].start() * strides[n];
93 }
94
95 unsigned int idx_start = idx;
96 _kernel.setArg(idx++, tensor->cl_buffer());
97
98 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
99 {
100 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
101 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
102 }
103
104 _kernel.setArg<cl_uint>(idx++, offset_first_element);
105
106 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
107 "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
108 ARM_COMPUTE_UNUSED(idx_start);
109}
110
111void ICLKernel::add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
112{
113 add_tensor_argument<1>(idx, tensor, window);
114}
115
116void ICLKernel::add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
117{
118 add_tensor_argument<2>(idx, tensor, window);
119}
120
121void ICLKernel::add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
122{
123 add_tensor_argument<3>(idx, tensor, window);
124}
125
126unsigned int ICLKernel::num_arguments_per_1D_tensor() const
127{
128 return num_arguments_per_tensor<1>();
129}
130
131unsigned int ICLKernel::num_arguments_per_2D_tensor() const
132{
133 return num_arguments_per_tensor<2>();
134}
135
136unsigned int ICLKernel::num_arguments_per_3D_tensor() const
137{
138 return num_arguments_per_tensor<3>();
139}
140
141void ICLKernel::set_target(cl::Device &device)
142{
143 _target = get_target_from_device(device);
144}
145
146void ICLKernel::set_target(GPUTarget target)
147{
148 _target = target;
149}
150
151GPUTarget ICLKernel::get_target() const
152{
153 return _target;
154}