Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2022 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLSCHEDULER_H |
| 25 | #define ARM_COMPUTE_CLSCHEDULER_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #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 Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/experimental/Types.h" |
SiCong Li | db4a6c1 | 2021-02-05 09:30:57 +0000 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/CLGEMMHeuristicsHandle.h" |
Michalis Spyrou | 402740d | 2021-04-20 11:26:21 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/CL/CLHelpers.h" |
| 35 | #include "arm_compute/runtime/CL/CLTypes.h" |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 36 | #include "arm_compute/runtime/CL/ICLTuner.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | class ICLKernel; |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 41 | class ICLTuner; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | /** Provides global access to a CL context and command queue. */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 43 | class CLScheduler final |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 45 | public: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | /** Constructor */ |
| 47 | CLScheduler(); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 48 | /** 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 Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 52 | /** Default destructor */ |
| 53 | ~CLScheduler() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | /** Access the scheduler singleton. |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 55 | * This method has been deprecated and will be removed in future releases |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | * @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 Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 61 | * |
Michalis Spyrou | 402740d | 2021-04-20 11:26:21 +0100 | [diff] [blame] | 62 | * @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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | */ |
Michalis Spyrou | 402740d | 2021-04-20 11:26:21 +0100 | [diff] [blame] | 66 | void default_init(ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr, CLBackendType cl_backend_type = CLBackendType::Native); |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 67 | /** 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 Li | db4a6c1 | 2021-02-05 09:30:57 +0000 | [diff] [blame] | 72 | * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr) |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 73 | */ |
SiCong Li | db4a6c1 | 2021-02-05 09:30:57 +0000 | [diff] [blame] | 74 | void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr); |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 75 | |
Gunes Bayir | 16c5697 | 2022-03-28 21:32:33 +0100 | [diff] [blame] | 76 | /** Re-initializes the context and command queue used by the scheduler to default values |
| 77 | * and sets a default device and kernel path for the @ref CLKernelLibrary. |
| 78 | * |
| 79 | * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr) |
| 80 | * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr) |
| 81 | * @param[in] cl_backend_type (Optional) Type of backend to use (default = CLBackendType::Native) |
| 82 | */ |
| 83 | void default_reinit(ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr, CLBackendType cl_backend_type = CLBackendType::Native); |
| 84 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | /** Schedule the execution of the passed kernel if possible. |
| 86 | * |
| 87 | * @param[in] kernel Kernel to execute. |
Giorgio Arena | 8afe6c7 | 2021-09-06 14:32:19 +0100 | [diff] [blame] | 88 | * @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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | */ |
| 90 | void enqueue(ICLKernel &kernel, bool flush = true); |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 91 | /** Schedule the execution of the passed kernel if possible. |
| 92 | * |
| 93 | * @param[in] kernel Kernel to execute. |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 94 | * @param[in] tensors Vector containing the tensors to operate on. |
Giorgio Arena | 8afe6c7 | 2021-09-06 14:32:19 +0100 | [diff] [blame] | 95 | * @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 Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 96 | */ |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 97 | void enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush = true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | /** Initialises the context and command queue to be used by the scheduler. |
| 99 | * |
Michalis Spyrou | 402740d | 2021-04-20 11:26:21 +0100 | [diff] [blame] | 100 | * @param[in] context A CL context. |
| 101 | * @param[in] queue A CL command queue. |
| 102 | * @param[in] device A CL device. |
| 103 | * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr) |
| 104 | * Note: It is caller's responsibility to release the allocated memory for CLTuner |
| 105 | * @param[in] gemm_h (Optional) Pointer to CLGEMMHeuristicsHandle (default = nullptr) |
| 106 | * @param[in] cl_backend_type (Optional) Type of backend to use (default = CLBackendType::Native) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | */ |
Michalis Spyrou | 402740d | 2021-04-20 11:26:21 +0100 | [diff] [blame] | 108 | void init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner = nullptr, CLGEMMHeuristicsHandle *gemm_h = nullptr, |
| 109 | CLBackendType cl_backend_type = CLBackendType::Native); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | |
| 111 | /** Accessor for the associated CL context. |
| 112 | * |
| 113 | * @return A CL context. |
| 114 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 115 | cl::Context &context(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | /** Accessor for the associated CL command queue. |
| 118 | * |
| 119 | * @return A CL command queue. |
| 120 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 121 | cl::CommandQueue &queue(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | |
| 123 | /** Get the target GPU. |
| 124 | * |
| 125 | * @return The target GPU. |
| 126 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 127 | GPUTarget target() const; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | |
SiCong Li | db4a6c1 | 2021-02-05 09:30:57 +0000 | [diff] [blame] | 129 | /** Accessor for the associated CLGEMMHeuristicsHandle |
| 130 | * |
| 131 | * @return Pointer to CLGEMMHeuristicsHandle |
| 132 | */ |
| 133 | CLGEMMHeuristicsHandle *gemm_heuristics() const; |
| 134 | |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 135 | /** Accessor to set the CL context to be used by the scheduler. |
| 136 | * |
| 137 | * @param[in] context A CL context. |
| 138 | */ |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 139 | void set_context(cl::Context context); |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 140 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | /** Accessor to set the CL command queue to be used by the scheduler. |
| 142 | * |
| 143 | * @param[in] queue A CL command queue. |
| 144 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 145 | void set_queue(cl::CommandQueue queue); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | |
| 147 | /** Accessor to set target GPU to be used by the scheduler. |
| 148 | * |
| 149 | * @param[in] target The target GPU. |
| 150 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 151 | void set_target(GPUTarget target); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 153 | /** Accessor to set the CL tuner to be used by the scheduler. |
| 154 | * |
| 155 | * @param[in] tuner A CL tuner |
| 156 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 157 | void set_tuner(ICLTuner *tuner); |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 158 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | /** Blocks until all commands in the associated command queue have finished. */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 160 | void sync(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 161 | |
| 162 | /** Enqueues a marker into the associated command queue and return the event. |
| 163 | * |
| 164 | * @return An event that can be waited on to block the executing thread. |
| 165 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 166 | cl::Event enqueue_sync_event(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 168 | /** Tunes OpenCL kernel |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 169 | * |
| 170 | * @param[in] kernel Kernel to tune |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 171 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 172 | void tune_kernel_static(ICLKernel &kernel); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 173 | |
Giorgio Arena | 8afe6c7 | 2021-09-06 14:32:19 +0100 | [diff] [blame] | 174 | /** Enable job chaining. The command queue will only be flushed when @p job_chaining_size kernels have been enqueued. |
| 175 | * |
| 176 | * @param[in] job_chaining_size Kernels to enqueue before flushing |
| 177 | */ |
| 178 | void enable_job_chaining(int job_chaining_size); |
| 179 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 180 | bool is_initialised() const; |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 181 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 182 | private: |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 183 | void enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush); |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 184 | /** If job chain is disabled, then flush the command queue according to @p flush. Otherwise @p flush is ignored and the queue is only flushed when job chain count exceeds allocated job chain size |
| 185 | * |
| 186 | * @param[in] flush Flush the command queue. Ignored when job chain is enabled. |
| 187 | */ |
| 188 | void flush_queue(bool flush); |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 189 | |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 190 | /** Flag to ensure symbols initialisation is happening before Scheduler creation */ |
| 191 | static std::once_flag _initialize_symbols; |
| 192 | |
Sheri Zhang | 1efed92 | 2021-03-10 22:43:38 +0000 | [diff] [blame] | 193 | cl::Context _context; |
| 194 | cl::CommandQueue _queue; |
| 195 | GPUTarget _target; |
| 196 | bool _is_initialised; |
| 197 | ICLTuner *_cl_tuner; |
| 198 | CLGEMMHeuristicsHandle *_gemm_heuristics; |
Michalis Spyrou | 402740d | 2021-04-20 11:26:21 +0100 | [diff] [blame] | 199 | CLBackendType _backend_type; |
Giorgio Arena | 8afe6c7 | 2021-09-06 14:32:19 +0100 | [diff] [blame] | 200 | bool _job_chaining_enabled; |
| 201 | int _job_chaining_size; |
| 202 | int _job_chaining_count; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | }; |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 204 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 205 | #endif /* ARM_COMPUTE_CLSCHEDULER_H */ |