blob: ef5cb03b32df42a1cc65a8452f577f996f640f71 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCong Lidb4a6c12021-02-05 09:30:57 +00002 * Copyright (c) 2016-2021 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"
Pablo Tellodb8485a2019-09-24 11:03:47 +010027#include "arm_compute/runtime/CL/CLHelpers.h"
Gian Marcode691f02017-09-08 16:13:11 +010028#include "arm_compute/runtime/CL/CLTuner.h"
Georgios Pinitas7777b1a2018-07-11 18:16:20 +010029#include "arm_compute/runtime/CL/tuners/Tuners.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010030#include "src/core/CL/ICLKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
Pablo Tellodb8485a2019-09-24 11:03:47 +010032namespace arm_compute
33{
34cl::Context &CLScheduler::context()
35{
36 ARM_COMPUTE_ERROR_ON(!_is_initialised);
37 _context = CLKernelLibrary::get().context();
38 return _context;
39}
40
41cl::CommandQueue &CLScheduler::queue()
42{
43 ARM_COMPUTE_ERROR_ON(!_is_initialised);
44 return _queue;
45}
46
47GPUTarget CLScheduler::target() const
48{
49 return _target;
50}
51
SiCong Lidb4a6c12021-02-05 09:30:57 +000052CLGEMMHeuristicsHandle *CLScheduler::gemm_heuristics() const
53{
54 return _gemm_heuristics;
55}
56
Pablo Tellodb8485a2019-09-24 11:03:47 +010057void CLScheduler::set_queue(cl::CommandQueue queue)
58{
59 _queue = std::move(queue);
60}
61
62void CLScheduler::set_target(GPUTarget target)
63{
64 _target = target;
65}
66
67void CLScheduler::set_tuner(ICLTuner *tuner)
68{
69 _cl_tuner = tuner;
70}
71
72void CLScheduler::sync()
73{
74 _queue.finish();
75}
76
77cl::Event CLScheduler::enqueue_sync_event()
78{
79 cl::Event event;
80 _queue.enqueueMarker(&event);
81 return event;
82}
83
84void CLScheduler::tune_kernel_static(ICLKernel &kernel)
85{
86 if(_cl_tuner != nullptr)
87 {
88 _cl_tuner->tune_kernel_static(kernel);
89 }
90}
91
92bool CLScheduler::is_initialised() const
93{
94 return _is_initialised;
95}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +000097std::once_flag CLScheduler::_initialize_symbols;
98
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099CLScheduler::CLScheduler()
SiCong Lidb4a6c12021-02-05 09:30:57 +0000100 : _context(), _queue(), _target(GPUTarget::MIDGARD), _is_initialised(false), _cl_tuner(nullptr), _cl_default_static_tuner(nullptr), _gemm_heuristics(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101{
102}
103
104CLScheduler &CLScheduler::get()
105{
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000106 std::call_once(_initialize_symbols, opencl_is_available);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 static CLScheduler scheduler;
108 return scheduler;
109}
110
SiCong Lidb4a6c12021-02-05 09:30:57 +0000111void CLScheduler::default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h)
Pablo Tellob7c308a2019-01-22 12:59:26 +0000112{
113 if(!_is_initialised)
114 {
Pablo Tellodb8485a2019-09-24 11:03:47 +0100115 const std::string cl_kernels_folder("./cl_kernels/");
116 cl::CommandQueue queue = cl::CommandQueue(ctx, device);
117 CLKernelLibrary::get().init(cl_kernels_folder, ctx, device);
SiCong Lidb4a6c12021-02-05 09:30:57 +0000118 init(ctx, queue, device, cl_tuner, gemm_h);
Pablo Tellob7c308a2019-01-22 12:59:26 +0000119 _cl_default_static_tuner = tuners::TunerFactory::create_tuner(_target);
120 _cl_tuner = (cl_tuner == nullptr) ? _cl_default_static_tuner.get() : cl_tuner;
121 }
122}
123
SiCong Lidb4a6c12021-02-05 09:30:57 +0000124void CLScheduler::default_init(ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h)
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100125{
126 if(!_is_initialised)
127 {
Pablo Tellob7c308a2019-01-22 12:59:26 +0000128 cl::Context ctx;
129 cl::Device dev;
130 cl_int err;
131 std::tie(ctx, dev, err) = create_opencl_context_and_device();
132 ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context");
133 cl::CommandQueue queue = cl::CommandQueue(ctx, dev);
134 CLKernelLibrary::get().init("./cl_kernels/", ctx, dev);
SiCong Lidb4a6c12021-02-05 09:30:57 +0000135 init(ctx, queue, dev, cl_tuner, gemm_h);
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100136 // Create a default static tuner and set if none was provided
137 _cl_default_static_tuner = tuners::TunerFactory::create_tuner(_target);
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100138 }
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100139
140 // Set CL tuner
141 _cl_tuner = (cl_tuner == nullptr) ? _cl_default_static_tuner.get() : cl_tuner;
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100142}
143
Pablo Tellob7c308a2019-01-22 12:59:26 +0000144void CLScheduler::set_context(cl::Context context)
145{
146 _context = std::move(context);
147 CLKernelLibrary::get().set_context(_context);
148}
149
SiCong Lidb4a6c12021-02-05 09:30:57 +0000150void CLScheduler::init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h)
Pablo Tellob7c308a2019-01-22 12:59:26 +0000151{
152 set_context(std::move(context));
SiCong Lidb4a6c12021-02-05 09:30:57 +0000153 _queue = std::move(queue);
154 _target = get_target_from_device(device);
155 _is_initialised = true;
156 _cl_tuner = cl_tuner;
157 _gemm_heuristics = gemm_h;
Pablo Tellob7c308a2019-01-22 12:59:26 +0000158}
159
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100160void CLScheduler::enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161{
steniu0134702472017-07-11 09:22:58 +0100162 ARM_COMPUTE_ERROR_ON_MSG(!_is_initialised,
163 "The CLScheduler is not initialised yet! Please call the CLScheduler::get().default_init(), \
164 or CLScheduler::get()::init() and CLKernelLibrary::get()::init() function before running functions!");
165
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100166 const bool inject_memory = !tensors.empty();
Georgios Pinitas9c82e012020-07-17 12:47:56 +0100167
Gian Marcode691f02017-09-08 16:13:11 +0100168 // Tune the kernel if the CLTuner has been provided
169 if(_cl_tuner != nullptr)
170 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100171 inject_memory ? _cl_tuner->tune_kernel_dynamic(kernel, tensors) : _cl_tuner->tune_kernel_dynamic(kernel);
Gian Marcode691f02017-09-08 16:13:11 +0100172 }
173
174 // Run kernel
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100175 inject_memory ? kernel.run_op(tensors, kernel.window(), _queue) : kernel.run(kernel.window(), _queue);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176
177 if(flush)
178 {
179 _queue.flush();
180 }
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000181}
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100182
183void CLScheduler::enqueue(ICLKernel &kernel, bool flush)
184{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100185 ITensorPack pack;
186 enqueue_common(kernel, pack, flush);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100187}
188
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100189void CLScheduler::enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush)
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100190{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100191 enqueue_common(kernel, tensors, flush);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100192}
Pablo Tellodb8485a2019-09-24 11:03:47 +0100193} // namespace arm_compute