blob: 49fb724cdb576f5b2eb1de154fca4f9de7db9505 [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
Pablo Tellodb8485a2019-09-24 11:03:47 +010030namespace arm_compute
31{
32cl::Context &CLScheduler::context()
33{
34 ARM_COMPUTE_ERROR_ON(!_is_initialised);
35 _context = CLKernelLibrary::get().context();
36 return _context;
37}
38
39cl::CommandQueue &CLScheduler::queue()
40{
41 ARM_COMPUTE_ERROR_ON(!_is_initialised);
42 return _queue;
43}
44
45GPUTarget CLScheduler::target() const
46{
47 return _target;
48}
49
SiCong Lidb4a6c12021-02-05 09:30:57 +000050CLGEMMHeuristicsHandle *CLScheduler::gemm_heuristics() const
51{
52 return _gemm_heuristics;
53}
54
Pablo Tellodb8485a2019-09-24 11:03:47 +010055void CLScheduler::set_queue(cl::CommandQueue queue)
56{
57 _queue = std::move(queue);
58}
59
60void CLScheduler::set_target(GPUTarget target)
61{
62 _target = target;
63}
64
65void CLScheduler::set_tuner(ICLTuner *tuner)
66{
67 _cl_tuner = tuner;
68}
69
70void CLScheduler::sync()
71{
72 _queue.finish();
73}
74
75cl::Event CLScheduler::enqueue_sync_event()
76{
77 cl::Event event;
78 _queue.enqueueMarker(&event);
79 return event;
80}
81
82void CLScheduler::tune_kernel_static(ICLKernel &kernel)
83{
84 if(_cl_tuner != nullptr)
85 {
86 _cl_tuner->tune_kernel_static(kernel);
87 }
88}
89
90bool CLScheduler::is_initialised() const
91{
92 return _is_initialised;
93}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +000095std::once_flag CLScheduler::_initialize_symbols;
96
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097CLScheduler::CLScheduler()
Giorgio Arena8afe6c72021-09-06 14:32:19 +010098 : _context(), _queue(), _target(GPUTarget::MIDGARD), _is_initialised(false), _cl_tuner(nullptr), _gemm_heuristics(nullptr), _backend_type(CLBackendType::Native), _job_chaining_enabled(false),
99 _job_chaining_size(), _job_chaining_count(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100{
101}
102
103CLScheduler &CLScheduler::get()
104{
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000105 std::call_once(_initialize_symbols, opencl_is_available);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 static CLScheduler scheduler;
107 return scheduler;
108}
109
SiCong Lidb4a6c12021-02-05 09:30:57 +0000110void CLScheduler::default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h)
Pablo Tellob7c308a2019-01-22 12:59:26 +0000111{
112 if(!_is_initialised)
113 {
Pablo Tellodb8485a2019-09-24 11:03:47 +0100114 const std::string cl_kernels_folder("./cl_kernels/");
115 cl::CommandQueue queue = cl::CommandQueue(ctx, device);
116 CLKernelLibrary::get().init(cl_kernels_folder, ctx, device);
SiCong Lidb4a6c12021-02-05 09:30:57 +0000117 init(ctx, queue, device, cl_tuner, gemm_h);
Sheri Zhang1efed922021-03-10 22:43:38 +0000118 _cl_tuner = cl_tuner;
Pablo Tellob7c308a2019-01-22 12:59:26 +0000119 }
120}
121
Michalis Spyrou402740d2021-04-20 11:26:21 +0100122void CLScheduler::default_init(ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h, CLBackendType cl_backend_type)
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100123{
124 if(!_is_initialised)
125 {
Pablo Tellob7c308a2019-01-22 12:59:26 +0000126 cl::Context ctx;
127 cl::Device dev;
128 cl_int err;
Michalis Spyrou402740d2021-04-20 11:26:21 +0100129 std::tie(ctx, dev, err) = create_opencl_context_and_device(cl_backend_type);
Pablo Tellob7c308a2019-01-22 12:59:26 +0000130 ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context");
131 cl::CommandQueue queue = cl::CommandQueue(ctx, dev);
132 CLKernelLibrary::get().init("./cl_kernels/", ctx, dev);
SiCong Lidb4a6c12021-02-05 09:30:57 +0000133 init(ctx, queue, dev, cl_tuner, gemm_h);
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100134 }
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100135
Michalis Spyrou22dd8b92022-07-05 14:07:23 +0100136 // Set CL tuner and GEMM heuristics
137 _cl_tuner = cl_tuner;
138 _gemm_heuristics = gemm_h;
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100139}
140
Gunes Bayir16c56972022-03-28 21:32:33 +0100141void CLScheduler::default_reinit(ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h, CLBackendType cl_backend_type)
142{
143 _is_initialised = false;
144
145 default_init(cl_tuner, gemm_h, cl_backend_type);
146}
147
Pablo Tellob7c308a2019-01-22 12:59:26 +0000148void CLScheduler::set_context(cl::Context context)
149{
150 _context = std::move(context);
151 CLKernelLibrary::get().set_context(_context);
152}
153
Michalis Spyrou402740d2021-04-20 11:26:21 +0100154void 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 +0000155{
156 set_context(std::move(context));
SiCong Lidb4a6c12021-02-05 09:30:57 +0000157 _queue = std::move(queue);
158 _target = get_target_from_device(device);
159 _is_initialised = true;
160 _cl_tuner = cl_tuner;
161 _gemm_heuristics = gemm_h;
Michalis Spyrou402740d2021-04-20 11:26:21 +0100162 _backend_type = cl_backend_type;
Pablo Tellob7c308a2019-01-22 12:59:26 +0000163}
164
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100165void CLScheduler::enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166{
steniu0134702472017-07-11 09:22:58 +0100167 ARM_COMPUTE_ERROR_ON_MSG(!_is_initialised,
168 "The CLScheduler is not initialised yet! Please call the CLScheduler::get().default_init(), \
169 or CLScheduler::get()::init() and CLKernelLibrary::get()::init() function before running functions!");
170
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100171 const bool inject_memory = !tensors.empty();
Georgios Pinitas9c82e012020-07-17 12:47:56 +0100172
Gian Marcode691f02017-09-08 16:13:11 +0100173 // Tune the kernel if the CLTuner has been provided
174 if(_cl_tuner != nullptr)
175 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100176 inject_memory ? _cl_tuner->tune_kernel_dynamic(kernel, tensors) : _cl_tuner->tune_kernel_dynamic(kernel);
Gian Marcode691f02017-09-08 16:13:11 +0100177 }
178
179 // Run kernel
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100180 inject_memory ? kernel.run_op(tensors, kernel.window(), _queue) : kernel.run(kernel.window(), _queue);
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100181 if(_job_chaining_enabled)
182 {
SiCong Li0a486cf2022-04-07 17:41:51 +0100183 ++_job_chaining_count;
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100184 }
SiCong Li0a486cf2022-04-07 17:41:51 +0100185
186 flush_queue(flush);
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000187}
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100188
SiCong Li0a486cf2022-04-07 17:41:51 +0100189void CLScheduler::flush_queue(bool flush)
190{
191 if(_job_chaining_enabled)
192 {
193 if(_job_chaining_count >= _job_chaining_size)
Giorgio Arena232c4522022-03-03 10:09:01 +0000194 {
195 _job_chaining_count = 0;
196 _queue.flush();
197 }
198 }
199 else if(flush)
200 {
201 _queue.flush();
202 }
203}
204
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100205void CLScheduler::enqueue(ICLKernel &kernel, bool flush)
206{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100207 ITensorPack pack;
208 enqueue_common(kernel, pack, flush);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100209}
210
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100211void CLScheduler::enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush)
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100212{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100213 enqueue_common(kernel, tensors, flush);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100214}
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100215
216void CLScheduler::enable_job_chaining(int job_chaining_size)
217{
218 _job_chaining_enabled = true;
219 _job_chaining_size = job_chaining_size;
220}
Pablo Tellodb8485a2019-09-24 11:03:47 +0100221} // namespace arm_compute