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 | #ifndef __ARM_COMPUTE_ICLKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_ICLKERNEL_H__ |
| 26 | |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLTypes.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | #include "arm_compute/core/IKernel.h" |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | class ICLTensor; |
| 35 | class Window; |
| 36 | |
| 37 | /** Common interface for all the OpenCL kernels */ |
| 38 | class ICLKernel : public IKernel |
| 39 | { |
| 40 | public: |
| 41 | /** Constructor */ |
| 42 | ICLKernel(); |
| 43 | /** Returns a reference to the OpenCL kernel of this object. |
| 44 | * |
| 45 | * @return A reference to the OpenCL kernel of this object. |
| 46 | */ |
| 47 | cl::Kernel &kernel(); |
| 48 | /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx. |
| 49 | * |
| 50 | * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set. |
| 51 | * @param[in] tensor Tensor to set as an argument of the object's kernel. |
| 52 | * @param[in] window Window the kernel will be executed on. |
| 53 | */ |
| 54 | void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window); |
| 55 | /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx. |
| 56 | * |
| 57 | * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set. |
| 58 | * @param[in] tensor Tensor to set as an argument of the object's kernel. |
| 59 | * @param[in] window Window the kernel will be executed on. |
| 60 | */ |
| 61 | void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window); |
| 62 | /** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx. |
| 63 | * |
| 64 | * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set. |
| 65 | * @param[in] tensor Tensor to set as an argument of the object's kernel. |
| 66 | * @param[in] window Window the kernel will be executed on. |
| 67 | */ |
| 68 | void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window); |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 69 | /** Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx. |
| 70 | * |
| 71 | * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set. |
| 72 | * @param[in] tensor Tensor to set as an argument of the object's kernel. |
| 73 | * @param[in] window Window the kernel will be executed on. |
| 74 | */ |
| 75 | void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | /** Returns the number of arguments enqueued per 1D tensor object. |
| 77 | * |
| 78 | * @return The number of arguments enqueues per 1D tensor object. |
| 79 | */ |
| 80 | unsigned int num_arguments_per_1D_tensor() const; |
| 81 | /** Returns the number of arguments enqueued per 2D tensor object. |
| 82 | * |
| 83 | * @return The number of arguments enqueues per 2D tensor object. |
| 84 | */ |
| 85 | unsigned int num_arguments_per_2D_tensor() const; |
| 86 | /** Returns the number of arguments enqueued per 3D tensor object. |
| 87 | * |
| 88 | * @return The number of arguments enqueues per 3D tensor object. |
| 89 | */ |
| 90 | unsigned int num_arguments_per_3D_tensor() const; |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 91 | /** Returns the number of arguments enqueued per 4D tensor object. |
| 92 | * |
| 93 | * @return The number of arguments enqueues per 4D tensor object. |
| 94 | */ |
| 95 | unsigned int num_arguments_per_4D_tensor() const; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. |
| 97 | * |
| 98 | * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns. |
| 99 | * |
| 100 | * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()). |
| 101 | * @param[in,out] queue Command queue on which to enqueue the kernel. |
| 102 | */ |
| 103 | virtual void run(const Window &window, cl::CommandQueue &queue) = 0; |
| 104 | /** Add the passed parameters to the object's kernel's arguments starting from the index idx. |
| 105 | * |
| 106 | * @param[in,out] idx Index at which to start adding the arguments. Will be incremented by the number of kernel arguments set. |
| 107 | * @param[in] value Value to set as an argument of the object's kernel. |
| 108 | */ |
| 109 | template <typename T> |
| 110 | void add_argument(unsigned int &idx, T value) |
| 111 | { |
| 112 | _kernel.setArg(idx++, value); |
| 113 | } |
| 114 | |
Gian Marco Iodice | 9331aeb | 2017-08-10 17:11:08 +0100 | [diff] [blame] | 115 | /** Set the Local-Workgroup-Size hint |
| 116 | * |
| 117 | * @note This method should be called after the configuration of the kernel |
| 118 | * |
| 119 | * @param[in] lws_hint Local-Workgroup-Size to use |
| 120 | */ |
| 121 | void set_lws_hint(cl::NDRange &lws_hint) |
| 122 | { |
| 123 | _lws_hint = lws_hint; |
| 124 | } |
| 125 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | /** Set the targeted GPU architecture |
| 127 | * |
| 128 | * @param[in] target The targeted GPU architecture |
| 129 | */ |
| 130 | void set_target(GPUTarget target); |
| 131 | |
| 132 | /** Set the targeted GPU architecture according to the CL device |
| 133 | * |
| 134 | * @param[in] device A CL device |
| 135 | */ |
| 136 | void set_target(cl::Device &device); |
| 137 | |
| 138 | /** Get the targeted GPU architecture |
| 139 | * |
| 140 | * @return The targeted GPU architecture. |
| 141 | */ |
| 142 | GPUTarget get_target() const; |
| 143 | |
| 144 | private: |
| 145 | /** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx. |
| 146 | * |
| 147 | * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set. |
| 148 | * @param[in] tensor Tensor to set as an argument of the object's kernel. |
| 149 | * @param[in] window Window the kernel will be executed on. |
| 150 | */ |
| 151 | template <unsigned int dimension_size> |
| 152 | void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window); |
| 153 | /** Returns the number of arguments enqueued per tensor object. |
| 154 | * |
| 155 | * @return The number of arguments enqueued per tensor object. |
| 156 | */ |
| 157 | template <unsigned int dimension_size> |
| 158 | unsigned int num_arguments_per_tensor() const; |
| 159 | |
| 160 | protected: |
| 161 | cl::Kernel _kernel; /**< OpenCL kernel to run */ |
| 162 | cl::NDRange _lws_hint; /**< Local workgroup size hint for the OpenCL kernel */ |
| 163 | GPUTarget _target; /**< The targeted GPU */ |
| 164 | }; |
| 165 | |
| 166 | /** Add the kernel to the command queue with the given window. |
| 167 | * |
| 168 | * @note Depending on the size of the window, this might translate into several jobs being enqueued. |
| 169 | * |
| 170 | * @note If kernel->kernel() is empty then the function will return without adding anything to the queue. |
| 171 | * |
| 172 | * @param[in,out] queue OpenCL command queue. |
| 173 | * @param[in] kernel Kernel to enqueue |
| 174 | * @param[in] window Window the kernel has to process. |
| 175 | * @param[in] lws_hint Local workgroup size requested, by default (128,1) |
| 176 | * |
| 177 | * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed. |
| 178 | */ |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 179 | void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint = CLKernelLibrary::get().default_ndrange()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | } |
| 181 | #endif /*__ARM_COMPUTE_ICLKERNEL_H__ */ |