Compute Library
 23.11
CLScheduler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-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  */
25 
28 
29 #include "src/core/CL/ICLKernel.h"
30 
31 namespace arm_compute
32 {
33 cl::Context &CLScheduler::context()
34 {
35  ARM_COMPUTE_ERROR_ON(!_is_initialised);
36  _context = CLKernelLibrary::get().context();
37  return _context;
38 }
39 
40 cl::CommandQueue &CLScheduler::queue()
41 {
42  ARM_COMPUTE_ERROR_ON(!_is_initialised);
43  return _queue;
44 }
45 
47 {
48  return _target;
49 }
50 
52 {
53  return _gemm_heuristics;
54 }
55 
56 void CLScheduler::set_queue(cl::CommandQueue queue)
57 {
58  _queue = std::move(queue);
59 }
60 
62 {
63  _target = target;
64 }
65 
67 {
68  _cl_tuner = tuner;
69 }
70 
72 {
73  _queue.finish();
74 }
75 
77 {
78  cl::Event event;
79  _queue.enqueueMarker(&event);
80  return event;
81 }
82 
84 {
85  if (_cl_tuner != nullptr)
86  {
87  _cl_tuner->tune_kernel_static(kernel);
88  }
89 }
90 
92 {
93  return _is_initialised;
94 }
95 
96 std::once_flag CLScheduler::_initialize_symbols;
97 
99  : _context(),
100  _queue(),
101  _target(GPUTarget::MIDGARD),
102  _is_initialised(false),
103  _cl_tuner(nullptr),
104  _gemm_heuristics(nullptr),
105  _backend_type(CLBackendType::Native),
106  _job_chaining_enabled(true),
107  _job_chaining_size(1),
108  _job_chaining_count(0)
109 {
110 }
111 
113 {
114  std::call_once(_initialize_symbols, opencl_is_available);
115  static CLScheduler scheduler;
116  return scheduler;
117 }
118 
120  cl::Context &ctx,
121  ICLTuner *cl_tuner,
122  CLGEMMHeuristicsHandle *gemm_h)
123 {
124  if (!_is_initialised)
125  {
126  const std::string cl_kernels_folder("./cl_kernels/");
127  cl::CommandQueue queue = cl::CommandQueue(ctx, device);
128  CLKernelLibrary::get().init(cl_kernels_folder, ctx, device);
129  init(ctx, queue, device, cl_tuner, gemm_h);
130  _cl_tuner = cl_tuner;
131  }
132 }
133 
135 {
136  if (!_is_initialised)
137  {
138  cl::Context ctx;
139  cl::Device dev;
140  cl_int err;
141  std::tie(ctx, dev, err) = create_opencl_context_and_device(cl_backend_type);
142  ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context");
143  cl::CommandQueue queue = cl::CommandQueue(ctx, dev);
144  CLKernelLibrary::get().init("./cl_kernels/", ctx, dev);
145  init(ctx, queue, dev, cl_tuner, gemm_h);
146  }
147 
148  // Set CL tuner and GEMM heuristics
149  _cl_tuner = cl_tuner;
150  _gemm_heuristics = gemm_h;
151 }
152 
154 {
155  _is_initialised = false;
156 
157  default_init(cl_tuner, gemm_h, cl_backend_type);
158 }
159 
161 {
162  _context = std::move(context);
163  CLKernelLibrary::get().set_context(_context);
164 }
165 
166 void CLScheduler::init(cl::Context context,
167  cl::CommandQueue queue,
168  const cl::Device &device,
169  ICLTuner *cl_tuner,
170  CLGEMMHeuristicsHandle *gemm_h,
171  CLBackendType cl_backend_type)
172 {
173  set_context(std::move(context));
174  _queue = std::move(queue);
175  _target = get_target_from_device(device);
176  _is_initialised = true;
177  _cl_tuner = cl_tuner;
178  _gemm_heuristics = gemm_h;
179  _backend_type = cl_backend_type;
180 }
181 
182 void CLScheduler::enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush)
183 {
185  !_is_initialised, "The CLScheduler is not initialised yet! Please call the CLScheduler::get().default_init(), \
186  or CLScheduler::get()::init() and CLKernelLibrary::get()::init() function before running functions!");
187 
188  const bool inject_memory = !tensors.empty();
189 
190  // Tune the kernel if the CLTuner has been provided
191  if (_cl_tuner != nullptr)
192  {
193  inject_memory ? _cl_tuner->tune_kernel_dynamic(kernel, tensors) : _cl_tuner->tune_kernel_dynamic(kernel);
194  }
195 
196  // Run kernel
197  inject_memory ? kernel.run_op(tensors, kernel.window(), _queue) : kernel.run(kernel.window(), _queue);
198  if (_job_chaining_enabled)
199  {
200  ++_job_chaining_count;
201  }
202 
203  flush_queue(flush);
204 }
205 
206 void CLScheduler::flush_queue(bool flush)
207 {
208  if (_job_chaining_enabled)
209  {
210  if (_job_chaining_count >= _job_chaining_size)
211  {
212  _job_chaining_count = 0;
213  /*
214  Optimisation note: Flush the queue at the first enqueue to start the GPU
215  execution and then incrementally saturate the clFlush calls to minimize
216  the CPU activity for job-scheduling.
217  For eg. job-chain size goes from 1, 2, 4, 8 and 16
218  */
219  if (_job_chaining_size < 16)
220  {
221  _job_chaining_size <<= 1;
222  }
223  _queue.flush();
224  }
225  }
226  else if (flush)
227  {
228  _queue.flush();
229  }
230 }
231 
232 void CLScheduler::enqueue(ICLKernel &kernel, bool flush)
233 {
235  enqueue_common(kernel, pack, flush);
236 }
237 
238 void CLScheduler::enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush)
239 {
240  enqueue_common(kernel, tensors, flush);
241 }
242 
243 void CLScheduler::enable_job_chaining(int job_chaining_size)
244 {
245  _job_chaining_enabled = true;
246  _job_chaining_size = job_chaining_size;
247 }
248 } // namespace arm_compute
arm_compute::CLScheduler::gemm_heuristics
CLGEMMHeuristicsHandle * gemm_heuristics() const
Accessor for the associated CLGEMMHeuristicsHandle.
Definition: CLScheduler.cpp:51
arm_compute::CLKernelLibrary::context
cl::Context & context()
Accessor for the associated CL context.
Definition: CLKernelLibrary.cpp:69
arm_compute::opencl_is_available
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:208
arm_compute::CLScheduler::set_tuner
void set_tuner(ICLTuner *tuner)
Accessor to set the CL tuner to be used by the scheduler.
Definition: CLScheduler.cpp:66
arm_compute::CLScheduler
Provides global access to a CL context and command queue.
Definition: CLScheduler.h:43
arm_compute::create_opencl_context_and_device
std::tuple< cl::Context, cl::Device, cl_int > create_opencl_context_and_device(CLBackendType cl_backend_type)
This function creates an OpenCL context and a device.
Definition: CLHelpers.cpp:121
arm_compute::CLScheduler::enqueue
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: CLScheduler.cpp:232
arm_compute::CLKernelLibrary::init
void init(std::string kernel_path, cl::Context context, cl::Device device)
Initialises the kernel library.
Definition: CLKernelLibrary.cpp:60
arm_compute::CLScheduler::CLScheduler
CLScheduler()
Constructor.
Definition: CLScheduler.cpp:98
arm_compute::CLScheduler::sync
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.cpp:71
arm_compute::ICLTuner::tune_kernel_dynamic
virtual void tune_kernel_dynamic(ICLKernel &kernel)=0
Tune OpenCL kernel dynamically.
arm_compute::CLKernelLibrary::get
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Definition: CLKernelLibrary.cpp:41
CLKernelLibrary.h
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
arm_compute::ITensorPack::empty
bool empty() const
Checks if pack is empty.
Definition: ITensorPack.cpp:79
CLTuner.h
ICLKernel.h
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
arm_compute::ITensorPack
Tensor packing service.
Definition: ITensorPack.h:39
ARM_COMPUTE_ERROR_ON_MSG
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
arm_compute::CLScheduler::init
void init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner=nullptr, CLGEMMHeuristicsHandle *gemm_h=nullptr, CLBackendType cl_backend_type=CLBackendType::Native)
Initialises the context and command queue to be used by the scheduler.
Definition: CLScheduler.cpp:166
CLScheduler.h
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
arm_compute::CLScheduler::set_target
void set_target(GPUTarget target)
Accessor to set target GPU to be used by the scheduler.
Definition: CLScheduler.cpp:61
arm_compute::CLGEMMHeuristicsHandle
Handle for loading and retrieving GEMM heuristics.
Definition: CLGEMMHeuristicsHandle.h:38
arm_compute::CLScheduler::default_reinit
void default_reinit(ICLTuner *cl_tuner=nullptr, CLGEMMHeuristicsHandle *gemm_h=nullptr, CLBackendType cl_backend_type=CLBackendType::Native)
Re-initializes the context and command queue used by the scheduler to default values and sets a defau...
Definition: CLScheduler.cpp:153
arm_compute::CLScheduler::tune_kernel_static
void tune_kernel_static(ICLKernel &kernel)
Tunes OpenCL kernel.
Definition: CLScheduler.cpp:83
arm_compute::test::validation::pack
ITensorPack pack
Definition: Im2Col.cpp:188
arm_compute::get_target_from_device
GPUTarget get_target_from_device(const cl::Device &device)
Helper function to get the GPU target from CL device.
Definition: CLHelpers.cpp:224
arm_compute::test::validation::context
auto context
Definition: DirectConv2d.cpp:156
arm_compute::ICLKernel
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:67
arm_compute::GPUTarget::MIDGARD
@ MIDGARD
arm_compute::CLScheduler::get
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:112
arm_compute::CLBackendType
CLBackendType
List the possible OpenCL backends.
Definition: CLTypes.h:55
arm_compute::CLScheduler::enable_job_chaining
void enable_job_chaining(int job_chaining_size)
Enable job chaining.
Definition: CLScheduler.cpp:243
arm_compute::CLScheduler::set_context
void set_context(cl::Context context)
Accessor to set the CL context to be used by the scheduler.
Definition: CLScheduler.cpp:160
arm_compute::CLScheduler::target
GPUTarget target() const
Get the target GPU.
Definition: CLScheduler.cpp:46
arm_compute::IKernel::window
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
arm_compute::GPUTarget
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
arm_compute::ICLTuner::tune_kernel_static
virtual void tune_kernel_static(ICLKernel &kernel)=0
Tune OpenCL kernel statically.
arm_compute::CLScheduler::default_init_with_context
void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner=nullptr, CLGEMMHeuristicsHandle *gemm_h=nullptr)
Initialises the scheduler with context and device provided by the user.
Definition: CLScheduler.cpp:119
arm_compute::CLBackendType::Native
@ Native
OpenCL native backend.
arm_compute::CLScheduler::default_init
void default_init(ICLTuner *cl_tuner=nullptr, CLGEMMHeuristicsHandle *gemm_h=nullptr, CLBackendType cl_backend_type=CLBackendType::Native)
Initialises the context and command queue used by the scheduler to default values and sets a default ...
Definition: CLScheduler.cpp:134
arm_compute::CLKernelLibrary::set_context
void set_context(cl::Context context)
Sets the CL context used to create programs.
Definition: CLKernelLibrary.cpp:81
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::CLScheduler::is_initialised
bool is_initialised() const
Definition: CLScheduler.cpp:91
arm_compute::ICLKernel::run_op
virtual void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Definition: ICLKernel.h:352
arm_compute::CLScheduler::enqueue_op
void enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: CLScheduler.cpp:238
arm_compute::CLScheduler::queue
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.cpp:40
arm_compute::ICLTuner
Basic interface for tuning the OpenCL kernels.
Definition: ICLTuner.h:34
arm_compute::CLScheduler::set_queue
void set_queue(cl::CommandQueue queue)
Accessor to set the CL command queue to be used by the scheduler.
Definition: CLScheduler.cpp:56
arm_compute::CLScheduler::context
cl::Context & context()
Accessor for the associated CL context.
Definition: CLScheduler.cpp:33
arm_compute::CLScheduler::enqueue_sync_event
cl::Event enqueue_sync_event()
Enqueues a marker into the associated command queue and return the event.
Definition: CLScheduler.cpp:76
arm_compute::ICLKernel::run
virtual void run(const Window &window, cl::CommandQueue &queue)
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
Definition: ICLKernel.h:340