Compute Library
 19.08
CLScheduler.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019 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  */
24 #ifndef __ARM_COMPUTE_CLSCHEDULER_H__
25 #define __ARM_COMPUTE_CLSCHEDULER_H__
26 
31 #include "arm_compute/core/Error.h"
32 #include "arm_compute/core/Types.h"
34 
35 namespace arm_compute
36 {
37 class ICLKernel;
38 
41 {
42 private:
44  CLScheduler();
46  CLScheduler(const CLScheduler &) = delete;
48  CLScheduler &operator=(const CLScheduler &) = delete;
49 
50 public:
55  static CLScheduler &get();
61  void default_init(ICLTuner *cl_tuner = nullptr);
68  void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner = nullptr);
69 
75  void enqueue(ICLKernel &kernel, bool flush = true);
76 
85  void init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner = nullptr);
86 
91  cl::Context &context()
92  {
93  ARM_COMPUTE_ERROR_ON(!_is_initialised);
94  _context = CLKernelLibrary::get().context();
95  return _context;
96  }
97 
102  cl::CommandQueue &queue()
103  {
104  ARM_COMPUTE_ERROR_ON(!_is_initialised);
105  return _queue;
106  }
107 
113  {
114  return _target;
115  }
116 
121  void set_context(cl::Context context);
122 
127  void set_queue(cl::CommandQueue queue)
128  {
129  _queue = std::move(queue);
130  }
131 
137  {
138  _target = target;
139  }
140 
145  void set_tuner(ICLTuner *tuner)
146  {
147  _cl_tuner = tuner;
148  }
149 
151  void sync()
152  {
153  _queue.finish();
154  }
155 
160  cl::Event enqueue_sync_event()
161  {
162  cl::Event event;
163  _queue.enqueueMarker(&event);
164 
165  return event;
166  }
167 
173  {
174  if(_cl_tuner != nullptr)
175  {
176  _cl_tuner->tune_kernel_static(kernel);
177  }
178  }
179 
180  bool is_initialised() const
181  {
182  return _is_initialised;
183  }
184 
185 private:
187  static std::once_flag _initialize_symbols;
188 
189  cl::Context _context;
190  cl::CommandQueue _queue;
191  GPUTarget _target;
192  bool _is_initialised;
193  ICLTuner *_cl_tuner;
194  std::unique_ptr<ICLTuner> _cl_default_static_tuner;
195 };
196 }
197 #endif /* __ARM_COMPUTE_CLSCHEDULER_H__ */
void set_target(GPUTarget target)
Accessor to set target GPU to be used by the scheduler.
Definition: CLScheduler.h:136
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
GPUTarget target() const
Get the target GPU.
Definition: CLScheduler.h:112
void set_context(cl::Context context)
Accessor to set the CL context to be used by the scheduler.
Definition: CLScheduler.cpp:80
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
bool is_initialised() const
Definition: CLScheduler.h:180
void init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner=nullptr)
Initialises the context and command queue to be used by the scheduler.
Definition: CLScheduler.cpp:86
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:43
cl::Context & context()
Accessor for the associated CL context.
Copyright (c) 2017-2018 ARM Limited.
cl::Event enqueue_sync_event()
Enqueues a marker into the associated command queue and return the event.
Definition: CLScheduler.h:160
void set_tuner(ICLTuner *tuner)
Accessor to set the CL tuner to be used by the scheduler.
Definition: CLScheduler.h:145
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: CLScheduler.cpp:95
void set_queue(cl::CommandQueue queue)
Accessor to set the CL command queue to be used by the scheduler.
Definition: CLScheduler.h:127
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.h:102
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.h:151
void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner=nullptr)
Initialises the scheduler with context and device provided by the user.
Definition: CLScheduler.cpp:48
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:40
Basic interface for tuning the OpenCL kernels.
Definition: ICLTuner.h:32
void tune_kernel_static(ICLKernel &kernel)
Tunes OpenCL kernel.
Definition: CLScheduler.h:172
void default_init(ICLTuner *cl_tuner=nullptr)
Initialises the context and command queue used by the scheduler to default values and sets a default ...
Definition: CLScheduler.cpp:60
cl::Context & context()
Accessor for the associated CL context.
Definition: CLScheduler.h:91