blob: 8d30c0536128eef52f633c6c70962d4cffc68754 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arena232c4522022-03-03 10:09:01 +00002 * Copyright (c) 2016-2022 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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#include "arm_compute/runtime/CL/CLScheduler.h"
25
Pablo Tellodb8485a2019-09-24 11:03:47 +010026#include "arm_compute/core/CL/CLKernelLibrary.h"
Gian Marcode691f02017-09-08 16:13:11 +010027#include "arm_compute/runtime/CL/CLTuner.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010028#include "src/core/CL/ICLKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
Giorgio Arena232c4522022-03-03 10:09:01 +000030#if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
31#include "src/gpu/cl/kernels/experimental/dynamic_fusion/ClCompositeKernel.h"
32#endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
33
Pablo Tellodb8485a2019-09-24 11:03:47 +010034namespace arm_compute
35{
36cl::Context &CLScheduler::context()
37{
38 ARM_COMPUTE_ERROR_ON(!_is_initialised);
39 _context = CLKernelLibrary::get().context();
40 return _context;
41}
42
43cl::CommandQueue &CLScheduler::queue()
44{
45 ARM_COMPUTE_ERROR_ON(!_is_initialised);
46 return _queue;
47}
48
49GPUTarget CLScheduler::target() const
50{
51 return _target;
52}
53
SiCong Lidb4a6c12021-02-05 09:30:57 +000054CLGEMMHeuristicsHandle *CLScheduler::gemm_heuristics() const
55{
56 return _gemm_heuristics;
57}
58
Pablo Tellodb8485a2019-09-24 11:03:47 +010059void CLScheduler::set_queue(cl::CommandQueue queue)
60{
61 _queue = std::move(queue);
62}
63
64void CLScheduler::set_target(GPUTarget target)
65{
66 _target = target;
67}
68
69void CLScheduler::set_tuner(ICLTuner *tuner)
70{
71 _cl_tuner = tuner;
72}
73
74void CLScheduler::sync()
75{
76 _queue.finish();
77}
78
79cl::Event CLScheduler::enqueue_sync_event()
80{
81 cl::Event event;
82 _queue.enqueueMarker(&event);
83 return event;
84}
85
86void CLScheduler::tune_kernel_static(ICLKernel &kernel)
87{
88 if(_cl_tuner != nullptr)
89 {
90 _cl_tuner->tune_kernel_static(kernel);
91 }
92}
93
94bool CLScheduler::is_initialised() const
95{
96 return _is_initialised;
97}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +000099std::once_flag CLScheduler::_initialize_symbols;
100
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101CLScheduler::CLScheduler()
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100102 : _context(), _queue(), _target(GPUTarget::MIDGARD), _is_initialised(false), _cl_tuner(nullptr), _gemm_heuristics(nullptr), _backend_type(CLBackendType::Native), _job_chaining_enabled(false),
103 _job_chaining_size(), _job_chaining_count(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104{
105}
106
107CLScheduler &CLScheduler::get()
108{
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000109 std::call_once(_initialize_symbols, opencl_is_available);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 static CLScheduler scheduler;
111 return scheduler;
112}
113
SiCong Lidb4a6c12021-02-05 09:30:57 +0000114void CLScheduler::default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h)
Pablo Tellob7c308a2019-01-22 12:59:26 +0000115{
116 if(!_is_initialised)
117 {
Pablo Tellodb8485a2019-09-24 11:03:47 +0100118 const std::string cl_kernels_folder("./cl_kernels/");
119 cl::CommandQueue queue = cl::CommandQueue(ctx, device);
120 CLKernelLibrary::get().init(cl_kernels_folder, ctx, device);
SiCong Lidb4a6c12021-02-05 09:30:57 +0000121 init(ctx, queue, device, cl_tuner, gemm_h);
Sheri Zhang1efed922021-03-10 22:43:38 +0000122 _cl_tuner = cl_tuner;
Pablo Tellob7c308a2019-01-22 12:59:26 +0000123 }
124}
125
Michalis Spyrou402740d2021-04-20 11:26:21 +0100126void CLScheduler::default_init(ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h, CLBackendType cl_backend_type)
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100127{
128 if(!_is_initialised)
129 {
Pablo Tellob7c308a2019-01-22 12:59:26 +0000130 cl::Context ctx;
131 cl::Device dev;
132 cl_int err;
Michalis Spyrou402740d2021-04-20 11:26:21 +0100133 std::tie(ctx, dev, err) = create_opencl_context_and_device(cl_backend_type);
Pablo Tellob7c308a2019-01-22 12:59:26 +0000134 ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context");
135 cl::CommandQueue queue = cl::CommandQueue(ctx, dev);
136 CLKernelLibrary::get().init("./cl_kernels/", ctx, dev);
SiCong Lidb4a6c12021-02-05 09:30:57 +0000137 init(ctx, queue, dev, cl_tuner, gemm_h);
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100138 }
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100139
Michalis Spyrou22dd8b92022-07-05 14:07:23 +0100140 // Set CL tuner and GEMM heuristics
141 _cl_tuner = cl_tuner;
142 _gemm_heuristics = gemm_h;
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100143}
144
Gunes Bayir16c56972022-03-28 21:32:33 +0100145void CLScheduler::default_reinit(ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h, CLBackendType cl_backend_type)
146{
147 _is_initialised = false;
148
149 default_init(cl_tuner, gemm_h, cl_backend_type);
150}
151
Pablo Tellob7c308a2019-01-22 12:59:26 +0000152void CLScheduler::set_context(cl::Context context)
153{
154 _context = std::move(context);
155 CLKernelLibrary::get().set_context(_context);
156}
157
Michalis Spyrou402740d2021-04-20 11:26:21 +0100158void CLScheduler::init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h, CLBackendType cl_backend_type)
Pablo Tellob7c308a2019-01-22 12:59:26 +0000159{
160 set_context(std::move(context));
SiCong Lidb4a6c12021-02-05 09:30:57 +0000161 _queue = std::move(queue);
162 _target = get_target_from_device(device);
163 _is_initialised = true;
164 _cl_tuner = cl_tuner;
165 _gemm_heuristics = gemm_h;
Michalis Spyrou402740d2021-04-20 11:26:21 +0100166 _backend_type = cl_backend_type;
Pablo Tellob7c308a2019-01-22 12:59:26 +0000167}
168
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100169void CLScheduler::enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170{
steniu0134702472017-07-11 09:22:58 +0100171 ARM_COMPUTE_ERROR_ON_MSG(!_is_initialised,
172 "The CLScheduler is not initialised yet! Please call the CLScheduler::get().default_init(), \
173 or CLScheduler::get()::init() and CLKernelLibrary::get()::init() function before running functions!");
174
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100175 const bool inject_memory = !tensors.empty();
Georgios Pinitas9c82e012020-07-17 12:47:56 +0100176
Gian Marcode691f02017-09-08 16:13:11 +0100177 // Tune the kernel if the CLTuner has been provided
178 if(_cl_tuner != nullptr)
179 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100180 inject_memory ? _cl_tuner->tune_kernel_dynamic(kernel, tensors) : _cl_tuner->tune_kernel_dynamic(kernel);
Gian Marcode691f02017-09-08 16:13:11 +0100181 }
182
183 // Run kernel
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100184 inject_memory ? kernel.run_op(tensors, kernel.window(), _queue) : kernel.run(kernel.window(), _queue);
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100185 if(_job_chaining_enabled)
186 {
SiCong Li0a486cf2022-04-07 17:41:51 +0100187 ++_job_chaining_count;
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100188 }
SiCong Li0a486cf2022-04-07 17:41:51 +0100189
190 flush_queue(flush);
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000191}
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100192
Giorgio Arena232c4522022-03-03 10:09:01 +0000193#if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
194
SiCong Lib63b1192022-01-28 18:24:39 +0000195void CLScheduler::enqueue_common(ICLKernel &kernel, ITensorPack &tensors, const experimental::dynamic_fusion::ClExecutionDescriptor &exec_desc, bool flush)
Giorgio Arena232c4522022-03-03 10:09:01 +0000196{
197 ARM_COMPUTE_ERROR_ON_MSG(!_is_initialised,
198 "The CLScheduler is not initialised yet! Please call the CLScheduler::get().default_init(), \
199 or CLScheduler::get()::init() and CLKernelLibrary::get()::init() function before running functions!");
200
SiCong Li0a486cf2022-04-07 17:41:51 +0100201 // ClCompositeKernel is stateless thus alway requires memory injection
202
203 // Tune the kernel if the CLTuner has been provided
204 if(_cl_tuner != nullptr)
205 {
206 _cl_tuner->tune_kernel_dynamic(kernel, tensors, exec_desc);
207 }
Giorgio Arena232c4522022-03-03 10:09:01 +0000208
209 // Run kernel
SiCong Li0a486cf2022-04-07 17:41:51 +0100210 kernel.run_composite_op(tensors, kernel.window(), _queue, exec_desc);
Giorgio Arena232c4522022-03-03 10:09:01 +0000211 if(_job_chaining_enabled)
212 {
SiCong Li0a486cf2022-04-07 17:41:51 +0100213 ++_job_chaining_count;
214 }
215
216 flush_queue(flush);
217}
218
219#endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
220
221void CLScheduler::flush_queue(bool flush)
222{
223 if(_job_chaining_enabled)
224 {
225 if(_job_chaining_count >= _job_chaining_size)
Giorgio Arena232c4522022-03-03 10:09:01 +0000226 {
227 _job_chaining_count = 0;
228 _queue.flush();
229 }
230 }
231 else if(flush)
232 {
233 _queue.flush();
234 }
235}
236
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100237void CLScheduler::enqueue(ICLKernel &kernel, bool flush)
238{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100239 ITensorPack pack;
240 enqueue_common(kernel, pack, flush);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100241}
242
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100243void CLScheduler::enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush)
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100244{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100245 enqueue_common(kernel, tensors, flush);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100246}
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100247
Giorgio Arena232c4522022-03-03 10:09:01 +0000248#if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
249
SiCong Lib63b1192022-01-28 18:24:39 +0000250void CLScheduler::enqueue_op(ICLKernel &kernel, ITensorPack &tensors, const experimental::dynamic_fusion::ClExecutionDescriptor &exec_desc, bool flush)
Giorgio Arena232c4522022-03-03 10:09:01 +0000251{
252 enqueue_common(kernel, tensors, exec_desc, flush);
253}
254
255#endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
256
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100257void CLScheduler::enable_job_chaining(int job_chaining_size)
258{
259 _job_chaining_enabled = true;
260 _job_chaining_size = job_chaining_size;
261}
Pablo Tellodb8485a2019-09-24 11:03:47 +0100262} // namespace arm_compute