Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 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" |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/ICLTuner.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | class ICLKernel; |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 38 | class ICLTuner; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | /** Provides global access to a CL context and command queue. */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 40 | class CLScheduler final |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 42 | public: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | /** Constructor */ |
| 44 | CLScheduler(); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 45 | /** 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 Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 49 | /** Default destructor */ |
| 50 | ~CLScheduler() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | /** Access the scheduler singleton. |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 52 | * This method has been deprecated and will be removed in future releases |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 58 | * |
| 59 | * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | */ |
Georgios Pinitas | df473ea | 2018-05-31 18:53:52 +0100 | [diff] [blame] | 61 | void default_init(ICLTuner *cl_tuner = nullptr); |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 62 | /** 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | /** 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 Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 76 | /** Schedule the execution of the passed kernel if possible. |
| 77 | * |
| 78 | * @param[in] kernel Kernel to execute. |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 79 | * @param[in] tensors Vector containing the tensors to operate on. |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 80 | * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel. |
| 81 | */ |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 82 | void enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush = true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | |
| 84 | /** Initialises the context and command queue to be used by the scheduler. |
| 85 | * |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 86 | * @param[in] context A CL context. |
| 87 | * @param[in] queue A CL command queue. |
| 88 | * @param[in] device A CL device. |
| 89 | * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr) |
| 90 | * Note: It is caller's responsibility to release the allocated memory for CLTuner |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | */ |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 92 | void init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
| 94 | /** Accessor for the associated CL context. |
| 95 | * |
| 96 | * @return A CL context. |
| 97 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 98 | cl::Context &context(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | /** Accessor for the associated CL command queue. |
| 101 | * |
| 102 | * @return A CL command queue. |
| 103 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 104 | cl::CommandQueue &queue(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | |
| 106 | /** Get the target GPU. |
| 107 | * |
| 108 | * @return The target GPU. |
| 109 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 110 | GPUTarget target() const; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 112 | /** Accessor to set the CL context to be used by the scheduler. |
| 113 | * |
| 114 | * @param[in] context A CL context. |
| 115 | */ |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 116 | void set_context(cl::Context context); |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 117 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | /** Accessor to set the CL command queue to be used by the scheduler. |
| 119 | * |
| 120 | * @param[in] queue A CL command queue. |
| 121 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 122 | void set_queue(cl::CommandQueue queue); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | |
| 124 | /** Accessor to set target GPU to be used by the scheduler. |
| 125 | * |
| 126 | * @param[in] target The target GPU. |
| 127 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 128 | void set_target(GPUTarget target); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 130 | /** Accessor to set the CL tuner to be used by the scheduler. |
| 131 | * |
| 132 | * @param[in] tuner A CL tuner |
| 133 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 134 | void set_tuner(ICLTuner *tuner); |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 135 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | /** Blocks until all commands in the associated command queue have finished. */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 137 | void sync(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | |
| 139 | /** Enqueues a marker into the associated command queue and return the event. |
| 140 | * |
| 141 | * @return An event that can be waited on to block the executing thread. |
| 142 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 143 | cl::Event enqueue_sync_event(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 145 | /** Tunes OpenCL kernel |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 146 | * |
| 147 | * @param[in] kernel Kernel to tune |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 148 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 149 | void tune_kernel_static(ICLKernel &kernel); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 150 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 151 | bool is_initialised() const; |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 152 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 153 | private: |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 154 | void enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush); |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 155 | /** Flag to ensure symbols initialisation is happening before Scheduler creation */ |
| 156 | static std::once_flag _initialize_symbols; |
| 157 | |
Georgios Pinitas | 72ccc44 | 2018-08-14 12:52:53 +0100 | [diff] [blame] | 158 | cl::Context _context; |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 159 | cl::CommandQueue _queue; |
| 160 | GPUTarget _target; |
| 161 | bool _is_initialised; |
| 162 | ICLTuner *_cl_tuner; |
| 163 | std::unique_ptr<ICLTuner> _cl_default_static_tuner; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 164 | }; |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 165 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 166 | #endif /* ARM_COMPUTE_CLSCHEDULER_H */ |