blob: 8e80259b59e2f794ca258694e6f7ca0dc347cf52 [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"
33
34namespace arm_compute
35{
36class ICLKernel;
37
38/** Provides global access to a CL context and command queue. */
39class CLScheduler
40{
41private:
42 /** Constructor */
43 CLScheduler();
44
45public:
46 /** Access the scheduler singleton.
47 *
48 * @return The scheduler
49 */
50 static CLScheduler &get();
51 /** Initialises the context and command queue used by the scheduler to default values
52 * and sets a default device and kernel path for the @ref CLKernelLibrary.
53 */
54 void default_init()
55 {
56 CLKernelLibrary::get().init("./cl_kernels/", cl::Context::getDefault(), cl::Device::getDefault());
57 init(cl::Context::getDefault(), cl::CommandQueue::getDefault(), cl::Device::getDefault());
58 }
59 /** Schedule the execution of the passed kernel if possible.
60 *
61 * @param[in] kernel Kernel to execute.
62 * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel.
63 */
64 void enqueue(ICLKernel &kernel, bool flush = true);
65
66 /** Initialises the context and command queue to be used by the scheduler.
67 *
68 * @param[in] context A CL context.
69 * @param[in] queue A CL command queue.
70 * @param[in] device A CL device.
71 */
72 void init(cl::Context context = cl::Context::getDefault(), cl::CommandQueue queue = cl::CommandQueue::getDefault(),
73 cl::Device device = cl::Device::getDefault())
74 {
75 _context = std::move(context);
76 _queue = std::move(queue);
77 _target = get_target_from_device(device);
78 }
79
80 /** Accessor for the associated CL context.
81 *
82 * @return A CL context.
83 */
84 cl::Context &context()
85 {
86 return _context;
87 }
88
89 /** Accessor to set the CL context to be used by the scheduler.
90 *
91 * @param[in] context A CL context.
92 */
93 void set_context(cl::Context context)
94 {
95 _context = std::move(context);
96 }
97
98 /** Accessor for the associated CL command queue.
99 *
100 * @return A CL command queue.
101 */
102 cl::CommandQueue &queue()
103 {
104 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
116 /** Accessor to set the CL command queue to be used by the scheduler.
117 *
118 * @param[in] queue A CL command queue.
119 */
120 void set_queue(cl::CommandQueue queue)
121 {
122 _queue = std::move(queue);
123 }
124
125 /** Accessor to set target GPU to be used by the scheduler.
126 *
127 * @param[in] target The target GPU.
128 */
129 void set_target(GPUTarget target)
130 {
131 _target = target;
132 }
133
134 /** Blocks until all commands in the associated command queue have finished. */
135 void sync()
136 {
137 _queue.finish();
138 }
139
140 /** Enqueues a marker into the associated command queue and return the event.
141 *
142 * @return An event that can be waited on to block the executing thread.
143 */
144 cl::Event enqueue_sync_event()
145 {
146 cl::Event event;
147 _queue.enqueueMarker(&event);
148
149 return event;
150 }
151
152private:
153 cl::Context _context;
154 cl::CommandQueue _queue;
155 GPUTarget _target;
156};
157}
158#endif /* __ARM_COMPUTE_CLSCHEDULER_H__ */