Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 84f3ae8 | 2018-01-15 11:15:26 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 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 | */ |
| 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" |
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; |
| 38 | |
| 39 | /** Provides global access to a CL context and command queue. */ |
| 40 | class CLScheduler |
| 41 | { |
| 42 | private: |
| 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; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | |
| 50 | public: |
| 51 | /** Access the scheduler singleton. |
| 52 | * |
| 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); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | /** Schedule the execution of the passed kernel if possible. |
| 63 | * |
| 64 | * @param[in] kernel Kernel to execute. |
| 65 | * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel. |
| 66 | */ |
| 67 | void enqueue(ICLKernel &kernel, bool flush = true); |
| 68 | |
| 69 | /** Initialises the context and command queue to be used by the scheduler. |
| 70 | * |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 71 | * @param[in] context A CL context. |
| 72 | * @param[in] queue A CL command queue. |
| 73 | * @param[in] device A CL device. |
| 74 | * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr) |
| 75 | * 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] | 76 | */ |
| 77 | void init(cl::Context context = cl::Context::getDefault(), cl::CommandQueue queue = cl::CommandQueue::getDefault(), |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 78 | cl::Device device = cl::Device::getDefault(), ICLTuner *cl_tuner = nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | { |
Anthony Barbier | 4dcb583 | 2018-05-08 11:29:05 +0100 | [diff] [blame] | 80 | set_context(context); |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 81 | _queue = std::move(queue); |
| 82 | _target = get_target_from_device(device); |
| 83 | _is_initialised = true; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 84 | _cl_tuner = cl_tuner; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** Accessor for the associated CL context. |
| 88 | * |
| 89 | * @return A CL context. |
| 90 | */ |
| 91 | cl::Context &context() |
| 92 | { |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 93 | ARM_COMPUTE_ERROR_ON(!_is_initialised); |
Anthony Barbier | 4dcb583 | 2018-05-08 11:29:05 +0100 | [diff] [blame] | 94 | return CLKernelLibrary::get().context(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** Accessor to set the CL context to be used by the scheduler. |
| 98 | * |
| 99 | * @param[in] context A CL context. |
| 100 | */ |
| 101 | void set_context(cl::Context context) |
| 102 | { |
Anthony Barbier | 4dcb583 | 2018-05-08 11:29:05 +0100 | [diff] [blame] | 103 | CLKernelLibrary::get().set_context(context); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** Accessor for the associated CL command queue. |
| 107 | * |
| 108 | * @return A CL command queue. |
| 109 | */ |
| 110 | cl::CommandQueue &queue() |
| 111 | { |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 112 | ARM_COMPUTE_ERROR_ON(!_is_initialised); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | return _queue; |
| 114 | } |
| 115 | |
| 116 | /** Get the target GPU. |
| 117 | * |
| 118 | * @return The target GPU. |
| 119 | */ |
| 120 | GPUTarget target() const |
| 121 | { |
| 122 | return _target; |
| 123 | } |
| 124 | |
| 125 | /** Accessor to set the CL command queue to be used by the scheduler. |
| 126 | * |
| 127 | * @param[in] queue A CL command queue. |
| 128 | */ |
| 129 | void set_queue(cl::CommandQueue queue) |
| 130 | { |
| 131 | _queue = std::move(queue); |
| 132 | } |
| 133 | |
| 134 | /** Accessor to set target GPU to be used by the scheduler. |
| 135 | * |
| 136 | * @param[in] target The target GPU. |
| 137 | */ |
| 138 | void set_target(GPUTarget target) |
| 139 | { |
| 140 | _target = target; |
| 141 | } |
| 142 | |
| 143 | /** Blocks until all commands in the associated command queue have finished. */ |
| 144 | void sync() |
| 145 | { |
| 146 | _queue.finish(); |
| 147 | } |
| 148 | |
| 149 | /** Enqueues a marker into the associated command queue and return the event. |
| 150 | * |
| 151 | * @return An event that can be waited on to block the executing thread. |
| 152 | */ |
| 153 | cl::Event enqueue_sync_event() |
| 154 | { |
| 155 | cl::Event event; |
| 156 | _queue.enqueueMarker(&event); |
| 157 | |
| 158 | return event; |
| 159 | } |
| 160 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 161 | /** Tunes OpenCL kernel |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 162 | * |
| 163 | * @param[in] kernel Kernel to tune |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 164 | */ |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 165 | void tune_kernel_static(ICLKernel &kernel) |
| 166 | { |
| 167 | if(_cl_tuner != nullptr) |
| 168 | { |
| 169 | _cl_tuner->tune_kernel_static(kernel); |
| 170 | } |
| 171 | } |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 172 | |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 173 | bool is_initialised() const |
| 174 | { |
| 175 | return _is_initialised; |
| 176 | } |
| 177 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 178 | private: |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 179 | /** Flag to ensure symbols initialisation is happening before Scheduler creation */ |
| 180 | static std::once_flag _initialize_symbols; |
| 181 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | cl::CommandQueue _queue; |
| 183 | GPUTarget _target; |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 184 | bool _is_initialised; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 185 | ICLTuner *_cl_tuner; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 186 | }; |
| 187 | } |
| 188 | #endif /* __ARM_COMPUTE_CLSCHEDULER_H__ */ |