blob: 93595c65c729edefb2ca72104b1f8283234f72ea [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLSCHEDULER_H
25#define ARM_COMPUTE_CLSCHEDULER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/CL/CLHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/CLTypes.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Types.h"
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010032#include "arm_compute/core/experimental/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;
Pablo Tellodb8485a2019-09-24 11:03:47 +010038class ICLTuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039/** Provides global access to a CL context and command queue. */
Pablo Tellodb8485a2019-09-24 11:03:47 +010040class CLScheduler final
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
Pablo Tellodb8485a2019-09-24 11:03:47 +010042public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 /** 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;
Pablo Tellodb8485a2019-09-24 11:03:47 +010049 /** Default destructor */
50 ~CLScheduler() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 /** Access the scheduler singleton.
Pablo Tellodb8485a2019-09-24 11:03:47 +010052 * This method has been deprecated and will be removed in future releases
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 * @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);
Pablo Tellob7c308a2019-01-22 12:59:26 +000062 /** Initialises the scheduler with context and device provided by the user
63 *
64 * @param[in] device OpenCL device to be used
65 * @param[in] ctx OpenCL ctx to be used
66 * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr)
67 */
68 void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner = nullptr);
69
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 /** Schedule the execution of the passed kernel if possible.
71 *
72 * @param[in] kernel Kernel to execute.
73 * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel.
74 */
75 void enqueue(ICLKernel &kernel, bool flush = true);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010076 /** Schedule the execution of the passed kernel if possible.
77 *
78 * @param[in] kernel Kernel to execute.
79 * @param[in] inputs Vector containing the input tensors.
80 * @param[in] outputs Vector containing the output tensors.
81 * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel.
82 */
83 void enqueue_op(ICLKernel &kernel, const InputTensorMap &inputs, const OutputTensorMap &outputs, bool flush = true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084
85 /** Initialises the context and command queue to be used by the scheduler.
86 *
Gian Marcode691f02017-09-08 16:13:11 +010087 * @param[in] context A CL context.
88 * @param[in] queue A CL command queue.
89 * @param[in] device A CL device.
90 * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr)
91 * Note: It is caller's responsibility to release the allocated memory for CLTuner
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 */
Pablo Tellob7c308a2019-01-22 12:59:26 +000093 void init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
95 /** Accessor for the associated CL context.
96 *
97 * @return A CL context.
98 */
Pablo Tellodb8485a2019-09-24 11:03:47 +010099 cl::Context &context();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 /** Accessor for the associated CL command queue.
102 *
103 * @return A CL command queue.
104 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100105 cl::CommandQueue &queue();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106
107 /** Get the target GPU.
108 *
109 * @return The target GPU.
110 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100111 GPUTarget target() const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100113 /** Accessor to set the CL context to be used by the scheduler.
114 *
115 * @param[in] context A CL context.
116 */
Pablo Tellob7c308a2019-01-22 12:59:26 +0000117 void set_context(cl::Context context);
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100118
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 /** Accessor to set the CL command queue to be used by the scheduler.
120 *
121 * @param[in] queue A CL command queue.
122 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100123 void set_queue(cl::CommandQueue queue);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
125 /** Accessor to set target GPU to be used by the scheduler.
126 *
127 * @param[in] target The target GPU.
128 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100129 void set_target(GPUTarget target);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100131 /** Accessor to set the CL tuner to be used by the scheduler.
132 *
133 * @param[in] tuner A CL tuner
134 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100135 void set_tuner(ICLTuner *tuner);
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100136
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 /** Blocks until all commands in the associated command queue have finished. */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100138 void sync();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139
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 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100144 cl::Event enqueue_sync_event();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000146 /** Tunes OpenCL kernel
Gian Marcode691f02017-09-08 16:13:11 +0100147 *
148 * @param[in] kernel Kernel to tune
Gian Marcode691f02017-09-08 16:13:11 +0100149 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100150 void tune_kernel_static(ICLKernel &kernel);
Gian Marcode691f02017-09-08 16:13:11 +0100151
Pablo Tellodb8485a2019-09-24 11:03:47 +0100152 bool is_initialised() const;
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100153
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000154private:
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100155 void enqueue_common(ICLKernel &kernel, const InputTensorMap &inputs, const OutputTensorMap &outputs, bool flush);
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000156 /** Flag to ensure symbols initialisation is happening before Scheduler creation */
157 static std::once_flag _initialize_symbols;
158
Georgios Pinitas72ccc442018-08-14 12:52:53 +0100159 cl::Context _context;
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100160 cl::CommandQueue _queue;
161 GPUTarget _target;
162 bool _is_initialised;
163 ICLTuner *_cl_tuner;
164 std::unique_ptr<ICLTuner> _cl_default_static_tuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165};
Georgios Pinitas7ae80a92019-10-25 18:25:17 +0100166} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000167#endif /* ARM_COMPUTE_CLSCHEDULER_H */