blob: 242fd7719cb18de600f50ef4555bdaff217c7dac [file] [log] [blame]
Viet-Hoa Do500e10b2023-09-12 17:49:38 +01001/*
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
31namespace arm_compute
32{
33
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034CLCompatCommandBuffer::CLCompatCommandBuffer(cl_command_queue queue) : _queue(queue)
Viet-Hoa Do500e10b2023-09-12 17:49:38 +010035{
36}
37
38CLCompatCommandBuffer::~CLCompatCommandBuffer()
39{
40}
41
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042void CLCompatCommandBuffer::add_kernel(cl_kernel kernel,
43 const cl::NDRange &offset,
44 const cl::NDRange &global,
45 const cl::NDRange &local)
Viet-Hoa Do500e10b2023-09-12 17:49:38 +010046{
47 ARM_COMPUTE_ERROR_ON(state() != State::Created);
48
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010049 _kernel_cmds.push_back(KernelCommand{kernel, offset, global, local, {}});
Viet-Hoa Do500e10b2023-09-12 17:49:38 +010050}
51
52void 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 _kernel_cmds.back().mutable_args.push_back(cl_mutable_dispatch_arg_khr{arg_idx, size, value});
Viet-Hoa Do500e10b2023-09-12 17:49:38 +010058}
59
60void CLCompatCommandBuffer::finalize()
61{
62 ARM_COMPUTE_ERROR_ON(state() != State::Created);
63
64 _kernel_cmds.shrink_to_fit();
65
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010066 for (auto &cmd : _kernel_cmds)
Viet-Hoa Do500e10b2023-09-12 17:49:38 +010067 {
68 cmd.mutable_args.shrink_to_fit();
69 }
70
71 state(State::Finalized);
72}
73
74void 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
81void CLCompatCommandBuffer::enqueue()
82{
83 ARM_COMPUTE_ERROR_ON(state() != State::Finalized);
84
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 for (const auto &cmd : _kernel_cmds)
Viet-Hoa Do500e10b2023-09-12 17:49:38 +010086 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 for (const auto &arg : cmd.mutable_args)
Viet-Hoa Do500e10b2023-09-12 17:49:38 +010088 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 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 Do500e10b2023-09-12 17:49:38 +010098
99 handle_cl_error("clEnqueueNDRangeKernel", error);
100 }
101}
102
103bool CLCompatCommandBuffer::is_finalized() const
104{
105 return state() == State::Finalized;
106}
107
108} // namespace arm_compute