blob: 11affebc48fe236a2b87b1b96e5dc908837687d4 [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
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 */
Gian Marcode691f02017-09-08 16:13:11 +010061 void default_init(ICLTuner *cl_tuner = nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062 {
63 CLKernelLibrary::get().init("./cl_kernels/", cl::Context::getDefault(), cl::Device::getDefault());
Gian Marcode691f02017-09-08 16:13:11 +010064 init(cl::Context::getDefault(), cl::CommandQueue::getDefault(), cl::Device::getDefault(), cl_tuner);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 }
66 /** Schedule the execution of the passed kernel if possible.
67 *
68 * @param[in] kernel Kernel to execute.
69 * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel.
70 */
71 void enqueue(ICLKernel &kernel, bool flush = true);
72
73 /** Initialises the context and command queue to be used by the scheduler.
74 *
Gian Marcode691f02017-09-08 16:13:11 +010075 * @param[in] context A CL context.
76 * @param[in] queue A CL command queue.
77 * @param[in] device A CL device.
78 * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr)
79 * Note: It is caller's responsibility to release the allocated memory for CLTuner
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 */
81 void init(cl::Context context = cl::Context::getDefault(), cl::CommandQueue queue = cl::CommandQueue::getDefault(),
Gian Marcode691f02017-09-08 16:13:11 +010082 cl::Device device = cl::Device::getDefault(), ICLTuner *cl_tuner = nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 {
steniu0134702472017-07-11 09:22:58 +010084 _context = std::move(context);
85 _queue = std::move(queue);
86 _target = get_target_from_device(device);
87 _is_initialised = true;
Gian Marcode691f02017-09-08 16:13:11 +010088 _cl_tuner = cl_tuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 }
90
91 /** Accessor for the associated CL context.
92 *
93 * @return A CL context.
94 */
95 cl::Context &context()
96 {
steniu0134702472017-07-11 09:22:58 +010097 ARM_COMPUTE_ERROR_ON(!_is_initialised);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 return _context;
99 }
100
101 /** Accessor to set the CL context to be used by the scheduler.
102 *
103 * @param[in] context A CL context.
104 */
105 void set_context(cl::Context context)
106 {
107 _context = std::move(context);
108 }
109
110 /** Accessor for the associated CL command queue.
111 *
112 * @return A CL command queue.
113 */
114 cl::CommandQueue &queue()
115 {
steniu0134702472017-07-11 09:22:58 +0100116 ARM_COMPUTE_ERROR_ON(!_is_initialised);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 return _queue;
118 }
119
120 /** Get the target GPU.
121 *
122 * @return The target GPU.
123 */
124 GPUTarget target() const
125 {
126 return _target;
127 }
128
129 /** Accessor to set the CL command queue to be used by the scheduler.
130 *
131 * @param[in] queue A CL command queue.
132 */
133 void set_queue(cl::CommandQueue queue)
134 {
135 _queue = std::move(queue);
136 }
137
138 /** Accessor to set target GPU to be used by the scheduler.
139 *
140 * @param[in] target The target GPU.
141 */
142 void set_target(GPUTarget target)
143 {
144 _target = target;
145 }
146
147 /** Blocks until all commands in the associated command queue have finished. */
148 void sync()
149 {
150 _queue.finish();
151 }
152
153 /** Enqueues a marker into the associated command queue and return the event.
154 *
155 * @return An event that can be waited on to block the executing thread.
156 */
157 cl::Event enqueue_sync_event()
158 {
159 cl::Event event;
160 _queue.enqueueMarker(&event);
161
162 return event;
163 }
164
165private:
Gian Marcode691f02017-09-08 16:13:11 +0100166 /** Tune OpenCL kernel
167 *
168 * @note This method uses a brute force approach to find the optimal LWS
169 *
170 * @param[in] kernel Kernel to tune
171 *
172 * @return The optimal LWS for the specified kernel
173 */
174 cl::NDRange tune_kernel(ICLKernel &kernel);
175
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 cl::Context _context;
177 cl::CommandQueue _queue;
178 GPUTarget _target;
steniu0134702472017-07-11 09:22:58 +0100179 bool _is_initialised;
Gian Marcode691f02017-09-08 16:13:11 +0100180 ICLTuner *_cl_tuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181};
182}
183#endif /* __ARM_COMPUTE_CLSCHEDULER_H__ */