Compute Library
 20.02.1
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 
30 #include "arm_compute/core/Error.h"
31 #include "arm_compute/core/Types.h"
33 
34 namespace arm_compute
35 {
36 class ICLKernel;
37 class ICLTuner;
38 /** Provides global access to a CL context and command queue. */
39 class CLScheduler final
40 {
41 public:
42  /** Constructor */
43  CLScheduler();
44  /** Prevent instances of this class from being copied (As this class contains pointers) */
45  CLScheduler(const CLScheduler &) = delete;
46  /** Prevent instances of this class from being copied (As this class contains pointers) */
47  CLScheduler &operator=(const CLScheduler &) = delete;
48  /** Default destructor */
49  ~CLScheduler() = default;
50  /** Access the scheduler singleton.
51  * This method has been deprecated and will be removed in future releases
52  * @return The scheduler
53  */
54  static CLScheduler &get();
55  /** Initialises the context and command queue used by the scheduler to default values
56  * and sets a default device and kernel path for the @ref CLKernelLibrary.
57  *
58  * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr)
59  */
60  void default_init(ICLTuner *cl_tuner = nullptr);
61  /** Initialises the scheduler with context and device provided by the user
62  *
63  * @param[in] device OpenCL device to be used
64  * @param[in] ctx OpenCL ctx to be used
65  * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr)
66  */
67  void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner = nullptr);
68 
69  /** Schedule the execution of the passed kernel if possible.
70  *
71  * @param[in] kernel Kernel to execute.
72  * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel.
73  */
74  void enqueue(ICLKernel &kernel, bool flush = true);
75 
76  /** Initialises the context and command queue to be used by the scheduler.
77  *
78  * @param[in] context A CL context.
79  * @param[in] queue A CL command queue.
80  * @param[in] device A CL device.
81  * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr)
82  * Note: It is caller's responsibility to release the allocated memory for CLTuner
83  */
84  void init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner = nullptr);
85 
86  /** Accessor for the associated CL context.
87  *
88  * @return A CL context.
89  */
90  cl::Context &context();
91 
92  /** Accessor for the associated CL command queue.
93  *
94  * @return A CL command queue.
95  */
96  cl::CommandQueue &queue();
97 
98  /** Get the target GPU.
99  *
100  * @return The target GPU.
101  */
102  GPUTarget target() const;
103 
104  /** Accessor to set the CL context to be used by the scheduler.
105  *
106  * @param[in] context A CL context.
107  */
108  void set_context(cl::Context context);
109 
110  /** Accessor to set the CL command queue to be used by the scheduler.
111  *
112  * @param[in] queue A CL command queue.
113  */
114  void set_queue(cl::CommandQueue queue);
115 
116  /** Accessor to set target GPU to be used by the scheduler.
117  *
118  * @param[in] target The target GPU.
119  */
121 
122  /** Accessor to set the CL tuner to be used by the scheduler.
123  *
124  * @param[in] tuner A CL tuner
125  */
126  void set_tuner(ICLTuner *tuner);
127 
128  /** Blocks until all commands in the associated command queue have finished. */
129  void sync();
130 
131  /** Enqueues a marker into the associated command queue and return the event.
132  *
133  * @return An event that can be waited on to block the executing thread.
134  */
135  cl::Event enqueue_sync_event();
136 
137  /** Tunes OpenCL kernel
138  *
139  * @param[in] kernel Kernel to tune
140  */
141  void tune_kernel_static(ICLKernel &kernel);
142 
143  bool is_initialised() const;
144 
145 private:
146  /** Flag to ensure symbols initialisation is happening before Scheduler creation */
147  static std::once_flag _initialize_symbols;
148 
149  cl::Context _context;
150  cl::CommandQueue _queue;
151  GPUTarget _target;
152  bool _is_initialised;
153  ICLTuner *_cl_tuner;
154  std::unique_ptr<ICLTuner> _cl_default_static_tuner;
155 };
156 } // namespace arm_compute
157 #endif /* ARM_COMPUTE_CLSCHEDULER_H */
void set_target(GPUTarget target)
Accessor to set target GPU to be used by the scheduler.
Definition: CLScheduler.cpp:57
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:99
GPUTarget target() const
Get the target GPU.
Definition: CLScheduler.cpp:47
void set_context(cl::Context context)
Accessor to set the CL context to be used by the scheduler.
CLScheduler & operator=(const CLScheduler &)=delete
Prevent instances of this class from being copied (As this class contains pointers)
~CLScheduler()=default
Default destructor.
bool is_initialised() const
Definition: CLScheduler.cpp:87
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.
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:43
Copyright (c) 2017-2020 ARM Limited.
cl::Context & context()
Accessor for the associated CL context.
Definition: CLScheduler.cpp:34
cl::Event enqueue_sync_event()
Enqueues a marker into the associated command queue and return the event.
Definition: CLScheduler.cpp:72
void set_tuner(ICLTuner *tuner)
Accessor to set the CL tuner to be used by the scheduler.
Definition: CLScheduler.cpp:62
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.cpp:41
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:52
CLScheduler()
Constructor.
Definition: CLScheduler.cpp:94
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.cpp:67
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.
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
Provides global access to a CL context and command queue.
Definition: CLScheduler.h:39
Basic interface for tuning the OpenCL kernels.
Definition: ICLTuner.h:32
void tune_kernel_static(ICLKernel &kernel)
Tunes OpenCL kernel.
Definition: CLScheduler.cpp:79
void default_init(ICLTuner *cl_tuner=nullptr)
Initialises the context and command queue used by the scheduler to default values and sets a default ...