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/CLCompatCommandBuffer.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 | CLCompatCommandBuffer::CLCompatCommandBuffer(cl_command_queue queue) : _queue(queue) |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
| 38 | CLCompatCommandBuffer::~CLCompatCommandBuffer() |
| 39 | { |
| 40 | } |
| 41 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 42 | void CLCompatCommandBuffer::add_kernel(cl_kernel kernel, |
| 43 | const cl::NDRange &offset, |
| 44 | const cl::NDRange &global, |
| 45 | const cl::NDRange &local) |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 46 | { |
| 47 | ARM_COMPUTE_ERROR_ON(state() != State::Created); |
| 48 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | _kernel_cmds.push_back(KernelCommand{kernel, offset, global, local, {}}); |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void CLCompatCommandBuffer::add_mutable_argument_generic(cl_uint arg_idx, const void *value, size_t size) |
| 53 | { |
| 54 | ARM_COMPUTE_ERROR_ON(state() != State::Created); |
| 55 | ARM_COMPUTE_ERROR_ON(_kernel_cmds.empty()); |
| 56 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | _kernel_cmds.back().mutable_args.push_back(cl_mutable_dispatch_arg_khr{arg_idx, size, value}); |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void CLCompatCommandBuffer::finalize() |
| 61 | { |
| 62 | ARM_COMPUTE_ERROR_ON(state() != State::Created); |
| 63 | |
| 64 | _kernel_cmds.shrink_to_fit(); |
| 65 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 66 | for (auto &cmd : _kernel_cmds) |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 67 | { |
| 68 | cmd.mutable_args.shrink_to_fit(); |
| 69 | } |
| 70 | |
| 71 | state(State::Finalized); |
| 72 | } |
| 73 | |
| 74 | void CLCompatCommandBuffer::update() |
| 75 | { |
| 76 | ARM_COMPUTE_ERROR_ON(state() != State::Finalized); |
| 77 | |
| 78 | // Nothing to do here - The kernel arguments will be updated when each command is enqueued. |
| 79 | } |
| 80 | |
| 81 | void CLCompatCommandBuffer::enqueue() |
| 82 | { |
| 83 | ARM_COMPUTE_ERROR_ON(state() != State::Finalized); |
| 84 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | for (const auto &cmd : _kernel_cmds) |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 86 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | for (const auto &arg : cmd.mutable_args) |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 88 | { |
| 89 | const auto error = clSetKernelArg(cmd.kernel, arg.arg_index, arg.arg_size, arg.arg_value); |
| 90 | |
| 91 | handle_cl_error("clSetKernelArg", error); |
| 92 | } |
| 93 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 94 | const auto error = |
| 95 | clEnqueueNDRangeKernel(_queue, cmd.kernel, static_cast<cl_uint>(cmd.global.dimensions()), |
| 96 | cmd.offset.dimensions() != 0 ? cmd.offset.get() : nullptr, cmd.global.get(), |
| 97 | cmd.local.dimensions() != 0 ? cmd.local.get() : nullptr, 0, nullptr, nullptr); |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 98 | |
| 99 | handle_cl_error("clEnqueueNDRangeKernel", error); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | bool CLCompatCommandBuffer::is_finalized() const |
| 104 | { |
| 105 | return state() == State::Finalized; |
| 106 | } |
| 107 | |
| 108 | } // namespace arm_compute |