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