blob: 663f78a56cb799393a1c2438bff8b3f2d108805c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou84f3ae82018-01-15 11:15:26 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/CLTypes.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Types.h"
Georgios Pinitasc0d1c862018-03-23 15:13:15 +000033#include "arm_compute/runtime/CL/ICLTuner.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35namespace arm_compute
36{
37class ICLKernel;
38
39/** Provides global access to a CL context and command queue. */
40class CLScheduler
41{
42private:
43 /** Constructor */
44 CLScheduler();
Gian Marcode691f02017-09-08 16:13:11 +010045 /** Prevent instances of this class from being copied (As this class contains pointers) */
46 CLScheduler(const CLScheduler &) = delete;
47 /** Prevent instances of this class from being copied (As this class contains pointers) */
48 CLScheduler &operator=(const CLScheduler &) = delete;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049
50public:
51 /** Access the scheduler singleton.
52 *
53 * @return The scheduler
54 */
55 static CLScheduler &get();
56 /** Initialises the context and command queue used by the scheduler to default values
57 * and sets a default device and kernel path for the @ref CLKernelLibrary.
Gian Marcode691f02017-09-08 16:13:11 +010058 *
59 * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060 */
Georgios Pinitasdf473ea2018-05-31 18:53:52 +010061 void default_init(ICLTuner *cl_tuner = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062 /** Schedule the execution of the passed kernel if possible.
63 *
64 * @param[in] kernel Kernel to execute.
65 * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel.
66 */
67 void enqueue(ICLKernel &kernel, bool flush = true);
68
69 /** Initialises the context and command queue to be used by the scheduler.
70 *
Gian Marcode691f02017-09-08 16:13:11 +010071 * @param[in] context A CL context.
72 * @param[in] queue A CL command queue.
73 * @param[in] device A CL device.
74 * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr)
75 * Note: It is caller's responsibility to release the allocated memory for CLTuner
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 */
Georgios Pinitas72ccc442018-08-14 12:52:53 +010077 void init(cl::Context context, cl::CommandQueue queue, cl::Device device, ICLTuner *cl_tuner = nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 {
Anthony Barbier4dcb5832018-05-08 11:29:05 +010079 set_context(context);
steniu0134702472017-07-11 09:22:58 +010080 _queue = std::move(queue);
81 _target = get_target_from_device(device);
82 _is_initialised = true;
Gian Marcode691f02017-09-08 16:13:11 +010083 _cl_tuner = cl_tuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 }
85
86 /** Accessor for the associated CL context.
87 *
88 * @return A CL context.
89 */
90 cl::Context &context()
91 {
steniu0134702472017-07-11 09:22:58 +010092 ARM_COMPUTE_ERROR_ON(!_is_initialised);
Georgios Pinitas72ccc442018-08-14 12:52:53 +010093 _context = CLKernelLibrary::get().context();
94 return _context;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 }
96
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 /** Accessor for the associated CL command queue.
98 *
99 * @return A CL command queue.
100 */
101 cl::CommandQueue &queue()
102 {
steniu0134702472017-07-11 09:22:58 +0100103 ARM_COMPUTE_ERROR_ON(!_is_initialised);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 return _queue;
105 }
106
107 /** Get the target GPU.
108 *
109 * @return The target GPU.
110 */
111 GPUTarget target() const
112 {
113 return _target;
114 }
115
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100116 /** Accessor to set the CL context to be used by the scheduler.
117 *
118 * @param[in] context A CL context.
119 */
120 void set_context(cl::Context context)
121 {
Georgios Pinitas72ccc442018-08-14 12:52:53 +0100122 _context = std::move(context);
123 CLKernelLibrary::get().set_context(_context);
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100124 }
125
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 /** Accessor to set the CL command queue to be used by the scheduler.
127 *
128 * @param[in] queue A CL command queue.
129 */
130 void set_queue(cl::CommandQueue queue)
131 {
132 _queue = std::move(queue);
133 }
134
135 /** Accessor to set target GPU to be used by the scheduler.
136 *
137 * @param[in] target The target GPU.
138 */
139 void set_target(GPUTarget target)
140 {
141 _target = target;
142 }
143
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100144 /** Accessor to set the CL tuner to be used by the scheduler.
145 *
146 * @param[in] tuner A CL tuner
147 */
148 void set_tuner(ICLTuner *tuner)
149 {
150 _cl_tuner = tuner;
151 }
152
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 /** Blocks until all commands in the associated command queue have finished. */
154 void sync()
155 {
156 _queue.finish();
157 }
158
159 /** Enqueues a marker into the associated command queue and return the event.
160 *
161 * @return An event that can be waited on to block the executing thread.
162 */
163 cl::Event enqueue_sync_event()
164 {
165 cl::Event event;
166 _queue.enqueueMarker(&event);
167
168 return event;
169 }
170
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000171 /** Tunes OpenCL kernel
Gian Marcode691f02017-09-08 16:13:11 +0100172 *
173 * @param[in] kernel Kernel to tune
Gian Marcode691f02017-09-08 16:13:11 +0100174 */
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000175 void tune_kernel_static(ICLKernel &kernel)
176 {
177 if(_cl_tuner != nullptr)
178 {
179 _cl_tuner->tune_kernel_static(kernel);
180 }
181 }
Gian Marcode691f02017-09-08 16:13:11 +0100182
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100183 bool is_initialised() const
184 {
185 return _is_initialised;
186 }
187
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000188private:
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000189 /** Flag to ensure symbols initialisation is happening before Scheduler creation */
190 static std::once_flag _initialize_symbols;
191
Georgios Pinitas72ccc442018-08-14 12:52:53 +0100192 cl::Context _context;
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100193 cl::CommandQueue _queue;
194 GPUTarget _target;
195 bool _is_initialised;
196 ICLTuner *_cl_tuner;
197 std::unique_ptr<ICLTuner> _cl_default_static_tuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198};
199}
200#endif /* __ARM_COMPUTE_CLSCHEDULER_H__ */