blob: 37d9e2ad050bf08d472c1801d41cc01b08e75202 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCong Lidb4a6c12021-02-05 09:30:57 +00002 * Copyright (c) 2016-2021 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"
SiCong Lidb4a6c12021-02-05 09:30:57 +000033#include "arm_compute/runtime/CL/CLGEMMHeuristicsHandle.h"
Michalis Spyrou402740d2021-04-20 11:26:21 +010034#include "arm_compute/runtime/CL/CLHelpers.h"
35#include "arm_compute/runtime/CL/CLTypes.h"
Georgios Pinitasc0d1c862018-03-23 15:13:15 +000036#include "arm_compute/runtime/CL/ICLTuner.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38namespace arm_compute
39{
40class ICLKernel;
Pablo Tellodb8485a2019-09-24 11:03:47 +010041class ICLTuner;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042/** Provides global access to a CL context and command queue. */
Pablo Tellodb8485a2019-09-24 11:03:47 +010043class CLScheduler final
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044{
Pablo Tellodb8485a2019-09-24 11:03:47 +010045public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046 /** Constructor */
47 CLScheduler();
Gian Marcode691f02017-09-08 16:13:11 +010048 /** Prevent instances of this class from being copied (As this class contains pointers) */
49 CLScheduler(const CLScheduler &) = delete;
50 /** Prevent instances of this class from being copied (As this class contains pointers) */
51 CLScheduler &operator=(const CLScheduler &) = delete;
Pablo Tellodb8485a2019-09-24 11:03:47 +010052 /** Default destructor */
53 ~CLScheduler() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 /** Access the scheduler singleton.
Pablo Tellodb8485a2019-09-24 11:03:47 +010055 * This method has been deprecated and will be removed in future releases
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 * @return The scheduler
57 */
58 static CLScheduler &get();
59 /** Initialises the context and command queue used by the scheduler to default values
60 * and sets a default device and kernel path for the @ref CLKernelLibrary.
Gian Marcode691f02017-09-08 16:13:11 +010061 *
Michalis Spyrou402740d2021-04-20 11:26:21 +010062 * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr)
63 * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr)
64 * @param[in] cl_backend_type (Optional) Type of backend to use (default = CLBackendType::Native)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 */
Michalis Spyrou402740d2021-04-20 11:26:21 +010066 void default_init(ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr, CLBackendType cl_backend_type = CLBackendType::Native);
Pablo Tellob7c308a2019-01-22 12:59:26 +000067 /** Initialises the scheduler with context and device provided by the user
68 *
69 * @param[in] device OpenCL device to be used
70 * @param[in] ctx OpenCL ctx to be used
71 * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr)
SiCong Lidb4a6c12021-02-05 09:30:57 +000072 * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr)
Pablo Tellob7c308a2019-01-22 12:59:26 +000073 */
SiCong Lidb4a6c12021-02-05 09:30:57 +000074 void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr);
Pablo Tellob7c308a2019-01-22 12:59:26 +000075
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 /** Schedule the execution of the passed kernel if possible.
77 *
78 * @param[in] kernel Kernel to execute.
Giorgio Arena8afe6c72021-09-06 14:32:19 +010079 * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel. This will be ignored if job chaining is enabled.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 */
81 void enqueue(ICLKernel &kernel, bool flush = true);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010082 /** Schedule the execution of the passed kernel if possible.
83 *
84 * @param[in] kernel Kernel to execute.
Georgios Pinitas0499dff2020-07-31 22:21:38 +010085 * @param[in] tensors Vector containing the tensors to operate on.
Giorgio Arena8afe6c72021-09-06 14:32:19 +010086 * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel. This will be ignored if job chaining is enabled.
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010087 */
Georgios Pinitas0499dff2020-07-31 22:21:38 +010088 void enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush = true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089
90 /** Initialises the context and command queue to be used by the scheduler.
91 *
Michalis Spyrou402740d2021-04-20 11:26:21 +010092 * @param[in] context A CL context.
93 * @param[in] queue A CL command queue.
94 * @param[in] device A CL device.
95 * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr)
96 * Note: It is caller's responsibility to release the allocated memory for CLTuner
97 * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr)
98 * @param[in] cl_backend_type (Optional) Type of backend to use (default = CLBackendType::Native)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 */
Michalis Spyrou402740d2021-04-20 11:26:21 +0100100 void init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr,
101 CLBackendType cl_backend_type = CLBackendType::Native);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
103 /** Accessor for the associated CL context.
104 *
105 * @return A CL context.
106 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100107 cl::Context &context();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 /** Accessor for the associated CL command queue.
110 *
111 * @return A CL command queue.
112 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100113 cl::CommandQueue &queue();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
115 /** Get the target GPU.
116 *
117 * @return The target GPU.
118 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100119 GPUTarget target() const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
SiCong Lidb4a6c12021-02-05 09:30:57 +0000121 /** Accessor for the associated CLGEMMHeuristicsHandle
122 *
123 * @return Pointer to CLGEMMHeuristicsHandle
124 */
125 CLGEMMHeuristicsHandle *gemm_heuristics() const;
126
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100127 /** Accessor to set the CL context to be used by the scheduler.
128 *
129 * @param[in] context A CL context.
130 */
Pablo Tellob7c308a2019-01-22 12:59:26 +0000131 void set_context(cl::Context context);
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100132
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 /** Accessor to set the CL command queue to be used by the scheduler.
134 *
135 * @param[in] queue A CL command queue.
136 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100137 void set_queue(cl::CommandQueue queue);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138
139 /** Accessor to set target GPU to be used by the scheduler.
140 *
141 * @param[in] target The target GPU.
142 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100143 void set_target(GPUTarget target);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100145 /** Accessor to set the CL tuner to be used by the scheduler.
146 *
147 * @param[in] tuner A CL tuner
148 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100149 void set_tuner(ICLTuner *tuner);
Georgios Pinitas7777b1a2018-07-11 18:16:20 +0100150
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 /** Blocks until all commands in the associated command queue have finished. */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100152 void sync();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
154 /** Enqueues a marker into the associated command queue and return the event.
155 *
156 * @return An event that can be waited on to block the executing thread.
157 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100158 cl::Event enqueue_sync_event();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000160 /** Tunes OpenCL kernel
Gian Marcode691f02017-09-08 16:13:11 +0100161 *
162 * @param[in] kernel Kernel to tune
Gian Marcode691f02017-09-08 16:13:11 +0100163 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100164 void tune_kernel_static(ICLKernel &kernel);
Gian Marcode691f02017-09-08 16:13:11 +0100165
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100166 /** Enable job chaining. The command queue will only be flushed when @p job_chaining_size kernels have been enqueued.
167 *
168 * @param[in] job_chaining_size Kernels to enqueue before flushing
169 */
170 void enable_job_chaining(int job_chaining_size);
171
Pablo Tellodb8485a2019-09-24 11:03:47 +0100172 bool is_initialised() const;
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100173
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000174private:
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100175 void enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush);
Ioan-Cristian Szabo77eb21f2017-12-22 17:32:17 +0000176 /** Flag to ensure symbols initialisation is happening before Scheduler creation */
177 static std::once_flag _initialize_symbols;
178
Sheri Zhang1efed922021-03-10 22:43:38 +0000179 cl::Context _context;
180 cl::CommandQueue _queue;
181 GPUTarget _target;
182 bool _is_initialised;
183 ICLTuner *_cl_tuner;
184 CLGEMMHeuristicsHandle *_gemm_heuristics;
Michalis Spyrou402740d2021-04-20 11:26:21 +0100185 CLBackendType _backend_type;
Giorgio Arena8afe6c72021-09-06 14:32:19 +0100186 bool _job_chaining_enabled;
187 int _job_chaining_size;
188 int _job_chaining_count;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189};
Georgios Pinitas7ae80a92019-10-25 18:25:17 +0100190} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000191#endif /* ARM_COMPUTE_CLSCHEDULER_H */