blob: 1a7befc046f3a3c77377cc2cd6e1e51aee135f41 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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
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}
42
43// Create a cl_context with a printf_callback and user specified buffer size.
44cl_context_properties properties[] =
45{
46 // Enable a printf callback function for this context.
47 CL_PRINTF_CALLBACK_ARM, reinterpret_cast<cl_context_properties>(printf_callback),
48 // Request a minimum printf buffer size of 4MB for devices in the
49 // context that support this extension.
50 CL_PRINTF_BUFFERSIZE_ARM, static_cast<cl_context_properties>(0x100000),
51 CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(cl::Platform::get()()),
52 0
53};
54}
55#endif /* defined(ARM_COMPUTE_DEBUG_ENABLED) */
56
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057namespace arm_compute
58{
59class ICLKernel;
60
61/** Provides global access to a CL context and command queue. */
62class CLScheduler
63{
64private:
65 /** Constructor */
66 CLScheduler();
Gian Marcode691f02017-09-08 16:13:11 +010067 /** Prevent instances of this class from being copied (As this class contains pointers) */
68 CLScheduler(const CLScheduler &) = delete;
69 /** Prevent instances of this class from being copied (As this class contains pointers) */
70 CLScheduler &operator=(const CLScheduler &) = delete;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
72public:
73 /** Access the scheduler singleton.
74 *
75 * @return The scheduler
76 */
77 static CLScheduler &get();
78 /** Initialises the context and command queue used by the scheduler to default values
79 * and sets a default device and kernel path for the @ref CLKernelLibrary.
Gian Marcode691f02017-09-08 16:13:11 +010080 *
81 * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 */
Gian Marcode691f02017-09-08 16:13:11 +010083 void default_init(ICLTuner *cl_tuner = nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 {
steniu01f01f9de2017-09-27 17:00:11 +010085#if defined(ARM_COMPUTE_DEBUG_ENABLED)
86 cl::Context::setDefault(cl::Context(CL_DEVICE_TYPE_DEFAULT, properties));
87#endif // defined(ARM_COMPUTE_DEBUG_ENABLED)
88
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 CLKernelLibrary::get().init("./cl_kernels/", cl::Context::getDefault(), cl::Device::getDefault());
Gian Marcode691f02017-09-08 16:13:11 +010090 init(cl::Context::getDefault(), cl::CommandQueue::getDefault(), cl::Device::getDefault(), cl_tuner);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 }
92 /** Schedule the execution of the passed kernel if possible.
93 *
94 * @param[in] kernel Kernel to execute.
95 * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel.
96 */
97 void enqueue(ICLKernel &kernel, bool flush = true);
98
99 /** Initialises the context and command queue to be used by the scheduler.
100 *
Gian Marcode691f02017-09-08 16:13:11 +0100101 * @param[in] context A CL context.
102 * @param[in] queue A CL command queue.
103 * @param[in] device A CL device.
104 * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr)
105 * Note: It is caller's responsibility to release the allocated memory for CLTuner
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 */
107 void init(cl::Context context = cl::Context::getDefault(), cl::CommandQueue queue = cl::CommandQueue::getDefault(),
Gian Marcode691f02017-09-08 16:13:11 +0100108 cl::Device device = cl::Device::getDefault(), ICLTuner *cl_tuner = nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 {
steniu0134702472017-07-11 09:22:58 +0100110 _context = std::move(context);
111 _queue = std::move(queue);
112 _target = get_target_from_device(device);
113 _is_initialised = true;
Gian Marcode691f02017-09-08 16:13:11 +0100114 _cl_tuner = cl_tuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 }
116
117 /** Accessor for the associated CL context.
118 *
119 * @return A CL context.
120 */
121 cl::Context &context()
122 {
steniu0134702472017-07-11 09:22:58 +0100123 ARM_COMPUTE_ERROR_ON(!_is_initialised);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 return _context;
125 }
126
127 /** Accessor to set the CL context to be used by the scheduler.
128 *
129 * @param[in] context A CL context.
130 */
131 void set_context(cl::Context context)
132 {
133 _context = std::move(context);
134 }
135
136 /** Accessor for the associated CL command queue.
137 *
138 * @return A CL command queue.
139 */
140 cl::CommandQueue &queue()
141 {
steniu0134702472017-07-11 09:22:58 +0100142 ARM_COMPUTE_ERROR_ON(!_is_initialised);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 return _queue;
144 }
145
146 /** Get the target GPU.
147 *
148 * @return The target GPU.
149 */
150 GPUTarget target() const
151 {
152 return _target;
153 }
154
155 /** Accessor to set the CL command queue to be used by the scheduler.
156 *
157 * @param[in] queue A CL command queue.
158 */
159 void set_queue(cl::CommandQueue queue)
160 {
161 _queue = std::move(queue);
162 }
163
164 /** Accessor to set target GPU to be used by the scheduler.
165 *
166 * @param[in] target The target GPU.
167 */
168 void set_target(GPUTarget target)
169 {
170 _target = target;
171 }
172
173 /** Blocks until all commands in the associated command queue have finished. */
174 void sync()
175 {
176 _queue.finish();
177 }
178
179 /** Enqueues a marker into the associated command queue and return the event.
180 *
181 * @return An event that can be waited on to block the executing thread.
182 */
183 cl::Event enqueue_sync_event()
184 {
185 cl::Event event;
186 _queue.enqueueMarker(&event);
187
188 return event;
189 }
190
191private:
Gian Marcode691f02017-09-08 16:13:11 +0100192 /** Tune OpenCL kernel
193 *
194 * @note This method uses a brute force approach to find the optimal LWS
195 *
196 * @param[in] kernel Kernel to tune
197 *
198 * @return The optimal LWS for the specified kernel
199 */
200 cl::NDRange tune_kernel(ICLKernel &kernel);
201
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 cl::Context _context;
203 cl::CommandQueue _queue;
204 GPUTarget _target;
steniu0134702472017-07-11 09:22:58 +0100205 bool _is_initialised;
Gian Marcode691f02017-09-08 16:13:11 +0100206 ICLTuner *_cl_tuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207};
208}
209#endif /* __ARM_COMPUTE_CLSCHEDULER_H__ */