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 | #include "src/core/CL/CLMutableCommandBuffer.h" |
| 26 | |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | |
| 29 | #include "src/core/CL/CLUtils.h" |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | CLMutableCommandBuffer::CLMutableCommandBuffer(cl_command_queue queue) : CLCommandBuffer() |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 35 | { |
| 36 | cl_int status = CL_SUCCESS; |
| 37 | |
| 38 | cl_command_buffer_properties_khr properties[] = { |
| 39 | CL_COMMAND_BUFFER_FLAGS_KHR, |
| 40 | CL_COMMAND_BUFFER_MUTABLE_KHR, |
| 41 | 0, |
| 42 | }; |
| 43 | |
| 44 | _cb = clCreateCommandBufferKHR(1, &queue, properties, &status); |
| 45 | handle_cl_error("clCreateCommandBufferKHR", status); |
| 46 | } |
| 47 | |
| 48 | CLMutableCommandBuffer::~CLMutableCommandBuffer() |
| 49 | { |
| 50 | const auto status = clReleaseCommandBufferKHR(_cb); |
Anitha Raj | 69766d6 | 2023-11-21 11:19:50 +0000 | [diff] [blame] | 51 | handle_cl_error("clReleaseCommandBufferKHR", status); |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | void CLMutableCommandBuffer::add_kernel(cl_kernel kernel, |
| 55 | const cl::NDRange &offset, |
| 56 | const cl::NDRange &global, |
| 57 | const cl::NDRange &local) |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 58 | { |
| 59 | ARM_COMPUTE_ERROR_ON(state() != State::Created); |
| 60 | |
| 61 | cl_mutable_command_khr mutable_handle = nullptr; |
| 62 | |
| 63 | cl_ndrange_kernel_command_properties_khr properties[] = { |
| 64 | CL_MUTABLE_DISPATCH_UPDATABLE_FIELDS_KHR, |
| 65 | CL_MUTABLE_DISPATCH_ARGUMENTS_KHR, |
| 66 | 0, |
| 67 | }; |
| 68 | |
| 69 | const auto error = clCommandNDRangeKernelKHR( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 70 | _cb, nullptr, properties, kernel, global.dimensions(), offset.dimensions() != 0 ? offset.get() : nullptr, |
| 71 | global.get(), local.dimensions() != 0 ? local.get() : nullptr, 0, nullptr, nullptr, &mutable_handle); |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 72 | |
| 73 | handle_cl_error("clCommandNDRangeKernelKHR", error); |
| 74 | |
| 75 | cl_mutable_dispatch_config_khr mut_dispatch_cfg{}; |
| 76 | mut_dispatch_cfg.type = CL_STRUCTURE_TYPE_MUTABLE_DISPATCH_CONFIG_KHR; |
| 77 | mut_dispatch_cfg.command = mutable_handle; |
| 78 | |
| 79 | _mut_dispatch_cfgs.emplace_back(mut_dispatch_cfg); |
| 80 | } |
| 81 | |
| 82 | void CLMutableCommandBuffer::add_mutable_argument_generic(cl_uint arg_idx, const void *value, size_t size) |
| 83 | { |
| 84 | ARM_COMPUTE_ERROR_ON(state() != State::Created); |
| 85 | |
| 86 | cl_mutable_dispatch_arg_khr cfg{}; |
| 87 | cfg.arg_index = arg_idx; |
| 88 | cfg.arg_size = size; |
| 89 | cfg.arg_value = value; |
| 90 | |
| 91 | _mut_arg_cfgs.emplace_back(cfg); |
| 92 | ++_mut_dispatch_cfgs.back().num_args; |
| 93 | } |
| 94 | |
| 95 | void CLMutableCommandBuffer::finalize() |
| 96 | { |
| 97 | ARM_COMPUTE_ERROR_ON(state() != State::Created); |
| 98 | |
| 99 | const auto error = clFinalizeCommandBufferKHR(_cb); |
| 100 | handle_cl_error("clFinalizeCommandBufferKHR", error); |
| 101 | |
| 102 | state(State::Finalized); |
| 103 | |
| 104 | _mut_dispatch_cfgs.shrink_to_fit(); |
| 105 | _mut_arg_cfgs.shrink_to_fit(); |
| 106 | |
| 107 | size_t arg_no = 0; |
| 108 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 109 | for (auto &mut_dispatch_cfg : _mut_dispatch_cfgs) |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 110 | { |
| 111 | ARM_COMPUTE_ERROR_ON(arg_no >= _mut_arg_cfgs.size()); |
| 112 | mut_dispatch_cfg.arg_list = &_mut_arg_cfgs[arg_no]; |
| 113 | |
| 114 | arg_no += mut_dispatch_cfg.num_args; |
| 115 | } |
| 116 | |
| 117 | _mut_cfg.type = CL_STRUCTURE_TYPE_MUTABLE_BASE_CONFIG_KHR; |
| 118 | _mut_cfg.next = nullptr; |
| 119 | _mut_cfg.num_mutable_dispatch = _mut_dispatch_cfgs.size(); |
| 120 | _mut_cfg.mutable_dispatch_list = &_mut_dispatch_cfgs[0]; |
| 121 | } |
| 122 | |
| 123 | void CLMutableCommandBuffer::update() |
| 124 | { |
| 125 | ARM_COMPUTE_ERROR_ON(state() != State::Finalized); |
| 126 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | const auto error = clUpdateMutableCommandsKHR(_cb, &_mut_cfg); |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 128 | |
| 129 | handle_cl_error("clUpdateMutableCommandsKHR", error); |
| 130 | } |
| 131 | |
| 132 | void CLMutableCommandBuffer::enqueue() |
| 133 | { |
| 134 | ARM_COMPUTE_ERROR_ON(state() != State::Finalized); |
| 135 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | const auto error = clEnqueueCommandBufferKHR(0, nullptr, _cb, 0, nullptr, nullptr); |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 137 | |
| 138 | handle_cl_error("clEnqueueCommandBufferKHR", error); |
| 139 | } |
| 140 | |
| 141 | bool CLMutableCommandBuffer::is_finalized() const |
| 142 | { |
| 143 | return state() == State::Finalized; |
| 144 | } |
| 145 | |
| 146 | } // namespace arm_compute |