blob: 3985829a49be07a28cdaa4517961b9a1368cf8f1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasfd14f9e2018-01-08 15:55:21 +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"
Gian Marcode691f02017-09-08 16:13:11 +010033#include "arm_compute/runtime/CL/CLTuner.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
steniu01f01f9de2017-09-27 17:00:11 +010035#if defined(ARM_COMPUTE_DEBUG_ENABLED)
36namespace
37{
38void printf_callback(const char *buffer, unsigned int len, size_t complete, void *user_data)
39{
40 printf("%.*s", len, buffer);
41}
steniu01f01f9de2017-09-27 17:00:11 +010042}
43#endif /* defined(ARM_COMPUTE_DEBUG_ENABLED) */
44
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045namespace arm_compute
46{
47class ICLKernel;
48
49/** Provides global access to a CL context and command queue. */
50class CLScheduler
51{
52private:
53 /** Constructor */
54 CLScheduler();
Georgios Pinitasfd14f9e2018-01-08 15:55:21 +000055 /** Destructor */
56 ~CLScheduler();
Gian Marcode691f02017-09-08 16:13:11 +010057 /** Prevent instances of this class from being copied (As this class contains pointers) */
58 CLScheduler(const CLScheduler &) = delete;
59 /** Prevent instances of this class from being copied (As this class contains pointers) */
60 CLScheduler &operator=(const CLScheduler &) = delete;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061
62public:
63 /** Access the scheduler singleton.
64 *
65 * @return The scheduler
66 */
67 static CLScheduler &get();
68 /** Initialises the context and command queue used by the scheduler to default values
69 * and sets a default device and kernel path for the @ref CLKernelLibrary.
Gian Marcode691f02017-09-08 16:13:11 +010070 *
71 * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 */
Gian Marcode691f02017-09-08 16:13:11 +010073 void default_init(ICLTuner *cl_tuner = nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 {
steniu01f01f9de2017-09-27 17:00:11 +010075#if defined(ARM_COMPUTE_DEBUG_ENABLED)
Anthony Barbiera9e15332017-12-22 16:37:30 +000076 // Create a cl_context with a printf_callback and user specified buffer size.
77 cl_context_properties properties[] =
78 {
79 // Enable a printf callback function for this context.
80 CL_PRINTF_CALLBACK_ARM, reinterpret_cast<cl_context_properties>(printf_callback),
81 // Request a minimum printf buffer size of 4MB for devices in the
82 // context that support this extension.
83 CL_PRINTF_BUFFERSIZE_ARM, static_cast<cl_context_properties>(0x100000),
84 CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(cl::Platform::get()()),
85 0
86 };
steniu01f01f9de2017-09-27 17:00:11 +010087 cl::Context::setDefault(cl::Context(CL_DEVICE_TYPE_DEFAULT, properties));
88#endif // defined(ARM_COMPUTE_DEBUG_ENABLED)
89
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 CLKernelLibrary::get().init("./cl_kernels/", cl::Context::getDefault(), cl::Device::getDefault());
Gian Marcode691f02017-09-08 16:13:11 +010091 init(cl::Context::getDefault(), cl::CommandQueue::getDefault(), cl::Device::getDefault(), cl_tuner);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 }
93 /** Schedule the execution of the passed kernel if possible.
94 *
95 * @param[in] kernel Kernel to execute.
96 * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel.
97 */
98 void enqueue(ICLKernel &kernel, bool flush = true);
99
100 /** Initialises the context and command queue to be used by the scheduler.
101 *
Gian Marcode691f02017-09-08 16:13:11 +0100102 * @param[in] context A CL context.
103 * @param[in] queue A CL command queue.
104 * @param[in] device A CL device.
105 * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr)
106 * Note: It is caller's responsibility to release the allocated memory for CLTuner
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 */
108 void init(cl::Context context = cl::Context::getDefault(), cl::CommandQueue queue = cl::CommandQueue::getDefault(),
Gian Marcode691f02017-09-08 16:13:11 +0100109 cl::Device device = cl::Device::getDefault(), ICLTuner *cl_tuner = nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 {
steniu0134702472017-07-11 09:22:58 +0100111 _context = std::move(context);
112 _queue = std::move(queue);
113 _target = get_target_from_device(device);
114 _is_initialised = true;
Gian Marcode691f02017-09-08 16:13:11 +0100115 _cl_tuner = cl_tuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 }
117
118 /** Accessor for the associated CL context.
119 *
120 * @return A CL context.
121 */
122 cl::Context &context()
123 {
steniu0134702472017-07-11 09:22:58 +0100124 ARM_COMPUTE_ERROR_ON(!_is_initialised);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 return _context;
126 }
127
128 /** Accessor to set the CL context to be used by the scheduler.
129 *
130 * @param[in] context A CL context.
131 */
132 void set_context(cl::Context context)
133 {
134 _context = std::move(context);
135 }
136
137 /** Accessor for the associated CL command queue.
138 *
139 * @return A CL command queue.
140 */
141 cl::CommandQueue &queue()
142 {
steniu0134702472017-07-11 09:22:58 +0100143 ARM_COMPUTE_ERROR_ON(!_is_initialised);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144 return _queue;
145 }
146
147 /** Get the target GPU.
148 *
149 * @return The target GPU.
150 */
151 GPUTarget target() const
152 {
153 return _target;
154 }
155
156 /** Accessor to set the CL command queue to be used by the scheduler.
157 *
158 * @param[in] queue A CL command queue.
159 */
160 void set_queue(cl::CommandQueue queue)
161 {
162 _queue = std::move(queue);
163 }
164
165 /** Accessor to set target GPU to be used by the scheduler.
166 *
167 * @param[in] target The target GPU.
168 */
169 void set_target(GPUTarget target)
170 {
171 _target = target;
172 }
173
174 /** Blocks until all commands in the associated command queue have finished. */
175 void sync()
176 {
177 _queue.finish();
178 }
179
180 /** Enqueues a marker into the associated command queue and return the event.
181 *
182 * @return An event that can be waited on to block the executing thread.
183 */
184 cl::Event enqueue_sync_event()
185 {
186 cl::Event event;
187 _queue.enqueueMarker(&event);
188
189 return event;
190 }
191
192private:
Gian Marcode691f02017-09-08 16:13:11 +0100193 /** Tune OpenCL kernel
194 *
195 * @note This method uses a brute force approach to find the optimal LWS
196 *
197 * @param[in] kernel Kernel to tune
198 *
199 * @return The optimal LWS for the specified kernel
200 */
201 cl::NDRange tune_kernel(ICLKernel &kernel);
202
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000203 /** Flag to ensure symbols initialisation is happening before Scheduler creation */
204 static std::once_flag _initialize_symbols;
205
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206 cl::Context _context;
207 cl::CommandQueue _queue;
208 GPUTarget _target;
steniu0134702472017-07-11 09:22:58 +0100209 bool _is_initialised;
Gian Marcode691f02017-09-08 16:13:11 +0100210 ICLTuner *_cl_tuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211};
212}
213#endif /* __ARM_COMPUTE_CLSCHEDULER_H__ */