Compute Library
 21.11
CLScheduler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021 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 namespace arm_compute
31 {
32 cl::Context &CLScheduler::context()
33 {
34  ARM_COMPUTE_ERROR_ON(!_is_initialised);
35  _context = CLKernelLibrary::get().context();
36  return _context;
37 }
38 
39 cl::CommandQueue &CLScheduler::queue()
40 {
41  ARM_COMPUTE_ERROR_ON(!_is_initialised);
42  return _queue;
43 }
44 
46 {
47  return _target;
48 }
49 
51 {
52  return _gemm_heuristics;
53 }
54 
55 void CLScheduler::set_queue(cl::CommandQueue queue)
56 {
57  _queue = std::move(queue);
58 }
59 
61 {
62  _target = target;
63 }
64 
66 {
67  _cl_tuner = tuner;
68 }
69 
71 {
72  _queue.finish();
73 }
74 
76 {
77  cl::Event event;
78  _queue.enqueueMarker(&event);
79  return event;
80 }
81 
83 {
84  if(_cl_tuner != nullptr)
85  {
86  _cl_tuner->tune_kernel_static(kernel);
87  }
88 }
89 
91 {
92  return _is_initialised;
93 }
94 
95 std::once_flag CLScheduler::_initialize_symbols;
96 
98  : _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)
100 {
101 }
102 
104 {
105  std::call_once(_initialize_symbols, opencl_is_available);
106  static CLScheduler scheduler;
107  return scheduler;
108 }
109 
110 void CLScheduler::default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h)
111 {
112  if(!_is_initialised)
113  {
114  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);
117  init(ctx, queue, device, cl_tuner, gemm_h);
118  _cl_tuner = cl_tuner;
119  }
120 }
121 
123 {
124  if(!_is_initialised)
125  {
126  cl::Context ctx;
127  cl::Device dev;
128  cl_int err;
129  std::tie(ctx, dev, err) = create_opencl_context_and_device(cl_backend_type);
130  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);
133  init(ctx, queue, dev, cl_tuner, gemm_h);
134  }
135 
136  // Set CL tuner
137  _cl_tuner = cl_tuner;
138 }
139 
141 {
142  _context = std::move(context);
143  CLKernelLibrary::get().set_context(_context);
144 }
145 
146 void CLScheduler::init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner, CLGEMMHeuristicsHandle *gemm_h, CLBackendType cl_backend_type)
147 {
148  set_context(std::move(context));
149  _queue = std::move(queue);
150  _target = get_target_from_device(device);
151  _is_initialised = true;
152  _cl_tuner = cl_tuner;
153  _gemm_heuristics = gemm_h;
154  _backend_type = cl_backend_type;
155 }
156 
157 void CLScheduler::enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush)
158 {
159  ARM_COMPUTE_ERROR_ON_MSG(!_is_initialised,
160  "The CLScheduler is not initialised yet! Please call the CLScheduler::get().default_init(), \
161  or CLScheduler::get()::init() and CLKernelLibrary::get()::init() function before running functions!");
162 
163  const bool inject_memory = !tensors.empty();
164 
165  // Tune the kernel if the CLTuner has been provided
166  if(_cl_tuner != nullptr)
167  {
168  inject_memory ? _cl_tuner->tune_kernel_dynamic(kernel, tensors) : _cl_tuner->tune_kernel_dynamic(kernel);
169  }
170 
171  // Run kernel
172  inject_memory ? kernel.run_op(tensors, kernel.window(), _queue) : kernel.run(kernel.window(), _queue);
173 
174  if(_job_chaining_enabled)
175  {
176  if(++_job_chaining_count >= _job_chaining_size)
177  {
178  _job_chaining_count = 0;
179  _queue.flush();
180  }
181  }
182  else if(flush)
183  {
184  _queue.flush();
185  }
186 }
187 
188 void CLScheduler::enqueue(ICLKernel &kernel, bool flush)
189 {
191  enqueue_common(kernel, pack, flush);
192 }
193 
194 void CLScheduler::enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush)
195 {
196  enqueue_common(kernel, tensors, flush);
197 }
198 
199 void CLScheduler::enable_job_chaining(int job_chaining_size)
200 {
201  _job_chaining_enabled = true;
202  _job_chaining_size = job_chaining_size;
203 }
204 } // namespace arm_compute
void set_target(GPUTarget target)
Accessor to set target GPU to be used by the scheduler.
Definition: CLScheduler.cpp:60
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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:275
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:45
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.
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:90
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:68
CLGEMMHeuristicsHandle * gemm_heuristics() const
Accessor for the associated CLGEMMHeuristicsHandle.
Definition: CLScheduler.cpp:50
Copyright (c) 2017-2021 Arm Limited.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
cl::Context & context()
Accessor for the associated CL context.
Definition: CLScheduler.cpp:32
cl::Event enqueue_sync_event()
Enqueues a marker into the associated command queue and return the event.
Definition: CLScheduler.cpp:75
#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:65
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.cpp:39
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:55
CLScheduler()
Constructor.
Definition: CLScheduler.cpp:97
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.cpp:70
Handle for loading and retrieving GEMM heuristics.
CLBackendType
List the possible OpenCL backends.
Definition: CLTypes.h:53
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:43
Tensor packing service.
Definition: ITensorPack.h:39
Basic interface for tuning the OpenCL kernels.
Definition: ICLTuner.h:34
void tune_kernel_static(ICLKernel &kernel)
Tunes OpenCL kernel.
Definition: CLScheduler.cpp:82
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:287
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:154
cl::Context & context()
Accessor for the associated CL context.