Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 | |
| 25 | #ifndef ACL_SRC_CORE_CL_CLCOMMANDBUFFER_H |
| 26 | #define ACL_SRC_CORE_CL_CLCOMMANDBUFFER_H |
| 27 | |
| 28 | #include "arm_compute/core/CL/OpenCL.h" |
| 29 | |
| 30 | #include <cstdint> |
| 31 | #include <memory> |
| 32 | #include <type_traits> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | |
| 37 | /** Command buffer contains a list of commands that is constructed once and later enqueued multiple times. |
| 38 | * |
| 39 | * To prepare a command buffer: |
| 40 | * - Construct a new command buffer targeting a command queue using @ref CLCommandBuffer::create. |
| 41 | * - Add kernel enqueue command to the buffer using @ref CLCommandBuffer::add_kernel. |
| 42 | * The kernel must be ready to be enqueued with all the arguments set. |
| 43 | * - Specify which kernel argument is mutable after the command buffer has been finalized. |
| 44 | * - When all the kernel enqueue commands have been added, call @ref CLCommandBuffer::finalize. |
| 45 | * After this point the command buffer is ready to be executed. |
| 46 | * |
| 47 | * To execute the command buffer: |
| 48 | * - Make any changes in the value which the mutable arguments are pointing to. |
| 49 | * - Call @ref CLCommandBuffer::update to apply the argument value changes. |
| 50 | * - Call @ref CLCommandBuffer::enqueue to enqueue the command buffer to execute. |
| 51 | */ |
| 52 | class CLCommandBuffer |
| 53 | { |
| 54 | public: |
| 55 | /** Create a new command buffer targeting the specified command queue. |
| 56 | * |
| 57 | * @param[in] queue The command queue to execute the command buffer. |
| 58 | * |
| 59 | * @return A unique pointer to the newly created command buffer. |
| 60 | */ |
| 61 | static std::unique_ptr<CLCommandBuffer> create(cl_command_queue queue); |
| 62 | |
| 63 | /** Constructor. */ |
| 64 | CLCommandBuffer(); |
| 65 | |
| 66 | /** Destructor. */ |
| 67 | virtual ~CLCommandBuffer(); |
| 68 | |
| 69 | /** Disallow copy constructor. */ |
| 70 | CLCommandBuffer(const CLCommandBuffer &) = delete; |
| 71 | |
| 72 | /** Disallow copy assignment. */ |
| 73 | CLCommandBuffer &operator=(const CLCommandBuffer &) = delete; |
| 74 | |
| 75 | /** Disallow move constructor. */ |
| 76 | CLCommandBuffer(CLCommandBuffer &&other) = delete; |
| 77 | |
| 78 | /** Disallow move assignment. */ |
| 79 | CLCommandBuffer &operator=(CLCommandBuffer &&other) = delete; |
| 80 | |
| 81 | /** Add a kernel enqueue command to the command queue. |
| 82 | * |
| 83 | * This function must be called before the command buffer has been finalized. |
| 84 | * |
| 85 | * @param[in] kernel The CL kernel. |
| 86 | * @param[in] offset The global work offset. |
| 87 | * @param[in] global The global work size. |
| 88 | * @param[in] local The local work size. |
| 89 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 90 | virtual void |
| 91 | add_kernel(cl_kernel kernel, const cl::NDRange &offset, const cl::NDRange &global, const cl::NDRange &local) = 0; |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 92 | |
| 93 | /** Add the mutable argument to the current kernel enqueue command. |
| 94 | * |
| 95 | * This function must be called after @ref CLCommandBuffer::add_kernel but before the command buffer |
| 96 | * has been finalized. |
| 97 | * |
| 98 | * The pointer must be valid and it must point to the correct value at the time |
| 99 | * @ref CLCommandBuffer::update is called so that the value of the argument |
| 100 | * can be applied successfully to the kernel enqueue command. |
| 101 | * |
| 102 | * @param[in] arg_idx The index of the argument in the current kernel program. |
| 103 | * @param[in] value The pointer to the value of the argument. |
| 104 | */ |
| 105 | template <typename T, typename = std::enable_if_t<std::is_arithmetic<T>::value || std::is_pointer<T>::value>> |
| 106 | void add_mutable_argument(cl_uint arg_idx, const T *value) |
| 107 | { |
| 108 | add_mutable_argument_generic(arg_idx, value, sizeof(T)); |
| 109 | } |
| 110 | |
| 111 | /** Finalize the command buffer. */ |
| 112 | virtual void finalize() = 0; |
| 113 | |
| 114 | /** Update the command buffer with new kernel argument values. |
| 115 | * |
| 116 | * This function must be called after the command buffer has been finalized. |
| 117 | * |
| 118 | * All the value pointed by the mutable argument will be applied to the command buffer. |
| 119 | */ |
| 120 | virtual void update() = 0; |
| 121 | |
| 122 | /** Enqueue the command buffer. |
| 123 | * |
| 124 | * This function must be called after the command buffer has been finalized. |
| 125 | */ |
| 126 | virtual void enqueue() = 0; |
| 127 | |
| 128 | /** Check if the command buffer has been finalized. |
| 129 | * |
| 130 | * @return true if the command buffer has been finalized. |
| 131 | */ |
| 132 | virtual bool is_finalized() const = 0; |
| 133 | |
| 134 | protected: |
| 135 | /** Add the mutable argument to the current kernel enqueue command. |
| 136 | * |
| 137 | * @see CLCommandBuffer::add_mutable_argument for more information. |
| 138 | */ |
| 139 | virtual void add_mutable_argument_generic(cl_uint arg_idx, const void *value, size_t size) = 0; |
| 140 | |
| 141 | /** The state of the command buffer. */ |
| 142 | enum class State : int32_t |
| 143 | { |
| 144 | /** The command buffer has been created and is being specified. */ |
| 145 | Created, |
| 146 | |
| 147 | /** The command buffer has been finalized and is ready to be executed. */ |
| 148 | Finalized, |
| 149 | }; |
| 150 | |
| 151 | /** Get the state of the command buffer. */ |
| 152 | State state() const; |
| 153 | |
| 154 | /** Set the state of the command buffer. */ |
| 155 | CLCommandBuffer &state(State state); |
| 156 | |
| 157 | private: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 158 | State _state{State::Created}; |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | } // namespace arm_compute |
| 162 | |
| 163 | #endif // ACL_SRC_CORE_CL_CLCOMMANDBUFFER_H |