Compute Library
 22.08
CLScheduler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2022 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 #include "src/core/CL/ICLKernel.h"
29 
30 #if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
32 #endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
33 
34 namespace arm_compute
35 {
36 cl::Context &CLScheduler::context()
37 {
38  ARM_COMPUTE_ERROR_ON(!_is_initialised);
39  _context = CLKernelLibrary::get().context();
40  return _context;
41 }
42 
43 cl::CommandQueue &CLScheduler::queue()
44 {
45  ARM_COMPUTE_ERROR_ON(!_is_initialised);
46  return _queue;
47 }
48 
50 {
51  return _target;
52 }
53 
55 {
56  return _gemm_heuristics;
57 }
58 
59 void CLScheduler::set_queue(cl::CommandQueue queue)
60 {
61  _queue = std::move(queue);
62 }
63 
65 {
66  _target = target;
67 }
68 
70 {
71  _cl_tuner = tuner;
72 }
73 
75 {
76  _queue.finish();
77 }
78 
80 {
81  cl::Event event;
82  _queue.enqueueMarker(&event);
83  return event;
84 }
85 
87 {
88  if(_cl_tuner != nullptr)
89  {
90  _cl_tuner->tune_kernel_static(kernel);
91  }
92 }
93 
95 {
96  return _is_initialised;
97 }
98 
99 std::once_flag CLScheduler::_initialize_symbols;
100 
102  : _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)
104 {
105 }
106 
108 {
109  std::call_once(_initialize_symbols, opencl_is_available);
110  static CLScheduler scheduler;
111  return scheduler;
112 }
113 
114 void CLScheduler::default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h)
115 {
116  if(!_is_initialised)
117  {
118  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);
121  init(ctx, queue, device, cl_tuner, gemm_h);
122  _cl_tuner = cl_tuner;
123  }
124 }
125 
127 {
128  if(!_is_initialised)
129  {
130  cl::Context ctx;
131  cl::Device dev;
132  cl_int err;
133  std::tie(ctx, dev, err) = create_opencl_context_and_device(cl_backend_type);
134  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);
137  init(ctx, queue, dev, cl_tuner, gemm_h);
138  }
139 
140  // Set CL tuner and GEMM heuristics
141  _cl_tuner = cl_tuner;
142  _gemm_heuristics = gemm_h;
143 }
144 
146 {
147  _is_initialised = false;
148 
149  default_init(cl_tuner, gemm_h, cl_backend_type);
150 }
151 
153 {
154  _context = std::move(context);
155  CLKernelLibrary::get().set_context(_context);
156 }
157 
158 void CLScheduler::init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h, CLBackendType cl_backend_type)
159 {
160  set_context(std::move(context));
161  _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;
166  _backend_type = cl_backend_type;
167 }
168 
169 void CLScheduler::enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush)
170 {
171  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 
175  const bool inject_memory = !tensors.empty();
176 
177  // Tune the kernel if the CLTuner has been provided
178  if(_cl_tuner != nullptr)
179  {
180  inject_memory ? _cl_tuner->tune_kernel_dynamic(kernel, tensors) : _cl_tuner->tune_kernel_dynamic(kernel);
181  }
182 
183  // Run kernel
184  inject_memory ? kernel.run_op(tensors, kernel.window(), _queue) : kernel.run(kernel.window(), _queue);
185  if(_job_chaining_enabled)
186  {
187  ++_job_chaining_count;
188  }
189 
190  flush_queue(flush);
191 }
192 
193 #if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
194 
195 void CLScheduler::enqueue_common(ICLKernel &kernel, ITensorPack &tensors, const experimental::dynamic_fusion::ClExecutionDescriptor &exec_desc, bool flush)
196 {
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 
201  // 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  }
208 
209  // Run kernel
210  kernel.run_composite_op(tensors, kernel.window(), _queue, exec_desc);
211  if(_job_chaining_enabled)
212  {
213  ++_job_chaining_count;
214  }
215 
216  flush_queue(flush);
217 }
218 
219 #endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
220 
221 void CLScheduler::flush_queue(bool flush)
222 {
223  if(_job_chaining_enabled)
224  {
225  if(_job_chaining_count >= _job_chaining_size)
226  {
227  _job_chaining_count = 0;
228  _queue.flush();
229  }
230  }
231  else if(flush)
232  {
233  _queue.flush();
234  }
235 }
236 
237 void CLScheduler::enqueue(ICLKernel &kernel, bool flush)
238 {
240  enqueue_common(kernel, pack, flush);
241 }
242 
243 void CLScheduler::enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush)
244 {
245  enqueue_common(kernel, tensors, flush);
246 }
247 
248 #if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
249 
251 {
252  enqueue_common(kernel, tensors, exec_desc, flush);
253 }
254 
255 #endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
256 
257 void CLScheduler::enable_job_chaining(int job_chaining_size)
258 {
259  _job_chaining_enabled = true;
260  _job_chaining_size = job_chaining_size;
261 }
262 } // namespace arm_compute
void set_target(GPUTarget target)
Accessor to set target GPU to be used by the scheduler.
Definition: CLScheduler.cpp:64
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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...
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:333
static CLScheduler & get()
Access the scheduler singleton.
void enable_job_chaining(int job_chaining_size)
Enable job chaining.
bool empty() const
Checks if pack is empty.
Definition: ITensorPack.cpp:80
GPUTarget target() const
Get the target GPU.
Definition: CLScheduler.cpp:49
void set_context(cl::Context context)
Accessor to set the CL context to be used by the scheduler.
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.
virtual void run_composite_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue, const experimental::dynamic_fusion::ClExecutionDescriptor &exec_desc)
The execution is carried out through run_op method. But the run_op method needs to be extended to inc...
Definition: ICLKernel.h:352
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 ...
OpenCL native backend.
#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
virtual void tune_kernel_dynamic(ICLKernel &kernel)=0
Tune OpenCL kernel dynamically.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
bool is_initialised() const
Definition: CLScheduler.cpp:94
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
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:126
void set_context(cl::Context context)
Sets the CL context used to create programs.
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:81
CLGEMMHeuristicsHandle * gemm_heuristics() const
Accessor for the associated CLGEMMHeuristicsHandle.
Definition: CLScheduler.cpp:54
Copyright (c) 2017-2022 Arm Limited.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
Descriptor containing information required to run a single ClWorkload.
Definition: ClWorkload.h:91
cl::Context & context()
Accessor for the associated CL context.
Definition: CLScheduler.cpp:36
cl::Event enqueue_sync_event()
Enqueues a marker into the associated command queue and return the event.
Definition: CLScheduler.cpp:79
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush=true)
Schedule the execution of the passed kernel if possible.
GPUTarget get_target_from_device(const cl::Device &device)
Helper function to get the GPU target from CL device.
Definition: CLHelpers.cpp:223
void init(std::string kernel_path, cl::Context context, cl::Device device)
Initialises the kernel library.
void set_tuner(ICLTuner *tuner)
Accessor to set the CL tuner to be used by the scheduler.
Definition: CLScheduler.cpp:69
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.cpp:43
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
void set_queue(cl::CommandQueue queue)
Accessor to set the CL command queue to be used by the scheduler.
Definition: CLScheduler.cpp:59
CLScheduler()
Constructor.
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.cpp:74
Handle for loading and retrieving GEMM heuristics.
CLBackendType
List the possible OpenCL backends.
Definition: CLTypes.h:55
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
virtual void tune_kernel_static(ICLKernel &kernel)=0
Tune OpenCL kernel statically.
Provides global access to a CL context and command queue.
Definition: CLScheduler.h:56
Tensor packing service.
Definition: ITensorPack.h:39
Basic interface for tuning the OpenCL kernels.
Definition: ICLTuner.h:43
void tune_kernel_static(ICLKernel &kernel)
Tunes OpenCL kernel.
Definition: CLScheduler.cpp:86
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:345
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.
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:188
cl::Context & context()
Accessor for the associated CL context.