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