Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | 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 | |
| 62 | ICLKernel::ICLKernel() |
Moritz Pflanzer | d9afd9c | 2017-06-27 15:25:44 +0100 | [diff] [blame] | 63 | : _kernel(nullptr), _lws_hint(cl::Range_128_1), _target(GPUTarget::MIDGARD) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | { |
| 65 | } |
| 66 | |
| 67 | cl::Kernel &ICLKernel::kernel() |
| 68 | { |
| 69 | return _kernel; |
| 70 | } |
| 71 | |
| 72 | template <unsigned int dimension_size> |
| 73 | unsigned int ICLKernel::num_arguments_per_tensor() const |
| 74 | { |
| 75 | return 2 + 2 * dimension_size; |
| 76 | } |
| 77 | |
| 78 | template <unsigned int dimension_size> |
| 79 | void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window) |
| 80 | { |
| 81 | ARM_COMPUTE_ERROR_ON(tensor == nullptr); |
| 82 | |
| 83 | const ITensorInfo *info = tensor->info(); |
| 84 | const Strides &strides = info->strides_in_bytes(); |
| 85 | |
| 86 | // Calculate offset to the start of the window |
| 87 | unsigned int offset_first_element = info->offset_first_element_in_bytes(); |
| 88 | |
| 89 | for(unsigned int n = 0; n < info->num_dimensions(); ++n) |
| 90 | { |
| 91 | offset_first_element += window[n].start() * strides[n]; |
| 92 | } |
| 93 | |
| 94 | unsigned int idx_start = idx; |
| 95 | _kernel.setArg(idx++, tensor->cl_buffer()); |
| 96 | |
| 97 | for(unsigned int dimension = 0; dimension < dimension_size; dimension++) |
| 98 | { |
| 99 | _kernel.setArg<cl_uint>(idx++, strides[dimension]); |
| 100 | _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step()); |
| 101 | } |
| 102 | |
| 103 | _kernel.setArg<cl_uint>(idx++, offset_first_element); |
| 104 | |
| 105 | ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_tensor<dimension_size>() != idx, |
| 106 | "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>()); |
| 107 | ARM_COMPUTE_UNUSED(idx_start); |
| 108 | } |
| 109 | |
| 110 | void ICLKernel::add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window) |
| 111 | { |
| 112 | add_tensor_argument<1>(idx, tensor, window); |
| 113 | } |
| 114 | |
| 115 | void ICLKernel::add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window) |
| 116 | { |
| 117 | add_tensor_argument<2>(idx, tensor, window); |
| 118 | } |
| 119 | |
| 120 | void ICLKernel::add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window) |
| 121 | { |
| 122 | add_tensor_argument<3>(idx, tensor, window); |
| 123 | } |
| 124 | |
| 125 | unsigned int ICLKernel::num_arguments_per_1D_tensor() const |
| 126 | { |
| 127 | return num_arguments_per_tensor<1>(); |
| 128 | } |
| 129 | |
| 130 | unsigned int ICLKernel::num_arguments_per_2D_tensor() const |
| 131 | { |
| 132 | return num_arguments_per_tensor<2>(); |
| 133 | } |
| 134 | |
| 135 | unsigned int ICLKernel::num_arguments_per_3D_tensor() const |
| 136 | { |
| 137 | return num_arguments_per_tensor<3>(); |
| 138 | } |
| 139 | |
| 140 | void ICLKernel::set_target(cl::Device &device) |
| 141 | { |
| 142 | _target = get_target_from_device(device); |
| 143 | } |
| 144 | |
| 145 | void ICLKernel::set_target(GPUTarget target) |
| 146 | { |
| 147 | _target = target; |
| 148 | } |
| 149 | |
| 150 | GPUTarget ICLKernel::get_target() const |
| 151 | { |
| 152 | return _target; |
| 153 | } |