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 | |
steniu01 | f01f9de | 2017-09-27 17:00:11 +0100 | [diff] [blame] | 35 | #if defined(ARM_COMPUTE_DEBUG_ENABLED) |
| 36 | namespace |
| 37 | { |
| 38 | void printf_callback(const char *buffer, unsigned int len, size_t complete, void *user_data) |
| 39 | { |
| 40 | printf("%.*s", len, buffer); |
| 41 | } |
steniu01 | f01f9de | 2017-09-27 17:00:11 +0100 | [diff] [blame] | 42 | } |
| 43 | #endif /* defined(ARM_COMPUTE_DEBUG_ENABLED) */ |
| 44 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | namespace arm_compute |
| 46 | { |
| 47 | class ICLKernel; |
| 48 | |
| 49 | /** Provides global access to a CL context and command queue. */ |
| 50 | class CLScheduler |
| 51 | { |
| 52 | private: |
| 53 | /** Constructor */ |
| 54 | CLScheduler(); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 55 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 56 | CLScheduler(const CLScheduler &) = delete; |
| 57 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 58 | CLScheduler &operator=(const CLScheduler &) = delete; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | |
| 60 | public: |
| 61 | /** Access the scheduler singleton. |
| 62 | * |
| 63 | * @return The scheduler |
| 64 | */ |
| 65 | static CLScheduler &get(); |
| 66 | /** Initialises the context and command queue used by the scheduler to default values |
| 67 | * and sets a default device and kernel path for the @ref CLKernelLibrary. |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 68 | * |
| 69 | * @param[in] cl_tuner (Optional) Pointer to ICLTuner (default=nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | */ |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 71 | void default_init(ICLTuner *cl_tuner = nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | { |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 73 | if(!_is_initialised) |
Anthony Barbier | a9e1533 | 2017-12-22 16:37:30 +0000 | [diff] [blame] | 74 | { |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 75 | #if defined(ARM_COMPUTE_DEBUG_ENABLED) |
Vidhya Sudhan Loganathan | eb8a399 | 2018-04-10 12:23:22 +0100 | [diff] [blame] | 76 | bool is_cl_arm_printf_supported = false; |
| 77 | |
| 78 | //query devices in the context for cl_arm_printf support |
| 79 | std::vector<cl::Device> def_platform_devices; |
| 80 | cl::Platform::getDefault().getDevices(CL_DEVICE_TYPE_DEFAULT, &def_platform_devices); |
| 81 | is_cl_arm_printf_supported = device_supports_extension(def_platform_devices[0], "cl_arm_printf"); |
| 82 | |
| 83 | if(is_cl_arm_printf_supported) |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 84 | { |
Vidhya Sudhan Loganathan | eb8a399 | 2018-04-10 12:23:22 +0100 | [diff] [blame] | 85 | // Create a cl_context with a printf_callback and user specified buffer size. |
| 86 | cl_context_properties properties[] = |
| 87 | { |
| 88 | // Enable a printf callback function for this context. |
| 89 | CL_PRINTF_CALLBACK_ARM, reinterpret_cast<cl_context_properties>(printf_callback), |
| 90 | // Request a minimum printf buffer size of 4MB for devices in the |
| 91 | // context that support this extension. |
| 92 | CL_PRINTF_BUFFERSIZE_ARM, static_cast<cl_context_properties>(0x100000), |
| 93 | CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(cl::Platform::get()()), |
| 94 | 0 |
| 95 | }; |
| 96 | cl::Context::setDefault(cl::Context(CL_DEVICE_TYPE_DEFAULT, properties)); |
| 97 | } |
steniu01 | f01f9de | 2017-09-27 17:00:11 +0100 | [diff] [blame] | 98 | #endif // defined(ARM_COMPUTE_DEBUG_ENABLED) |
| 99 | |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 100 | CLKernelLibrary::get().init("./cl_kernels/", cl::Context::getDefault(), cl::Device::getDefault()); |
| 101 | init(cl::Context::getDefault(), cl::CommandQueue::getDefault(), cl::Device::getDefault(), cl_tuner); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | _cl_tuner = cl_tuner; |
| 106 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | } |
| 108 | /** Schedule the execution of the passed kernel if possible. |
| 109 | * |
| 110 | * @param[in] kernel Kernel to execute. |
| 111 | * @param[in] flush (Optional) Specifies if the command queue will be flushed after running the kernel. |
| 112 | */ |
| 113 | void enqueue(ICLKernel &kernel, bool flush = true); |
| 114 | |
| 115 | /** Initialises the context and command queue to be used by the scheduler. |
| 116 | * |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 117 | * @param[in] context A CL context. |
| 118 | * @param[in] queue A CL command queue. |
| 119 | * @param[in] device A CL device. |
| 120 | * @param[in] cl_tuner (Optional) Pointer to OpenCL tuner (default=nullptr) |
| 121 | * 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] | 122 | */ |
| 123 | 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] | 124 | cl::Device device = cl::Device::getDefault(), ICLTuner *cl_tuner = nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | { |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 126 | _context = std::move(context); |
| 127 | _queue = std::move(queue); |
| 128 | _target = get_target_from_device(device); |
| 129 | _is_initialised = true; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 130 | _cl_tuner = cl_tuner; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** Accessor for the associated CL context. |
| 134 | * |
| 135 | * @return A CL context. |
| 136 | */ |
| 137 | cl::Context &context() |
| 138 | { |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 139 | ARM_COMPUTE_ERROR_ON(!_is_initialised); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | return _context; |
| 141 | } |
| 142 | |
| 143 | /** Accessor to set the CL context to be used by the scheduler. |
| 144 | * |
| 145 | * @param[in] context A CL context. |
| 146 | */ |
| 147 | void set_context(cl::Context context) |
| 148 | { |
| 149 | _context = std::move(context); |
| 150 | } |
| 151 | |
| 152 | /** Accessor for the associated CL command queue. |
| 153 | * |
| 154 | * @return A CL command queue. |
| 155 | */ |
| 156 | cl::CommandQueue &queue() |
| 157 | { |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 158 | ARM_COMPUTE_ERROR_ON(!_is_initialised); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | return _queue; |
| 160 | } |
| 161 | |
| 162 | /** Get the target GPU. |
| 163 | * |
| 164 | * @return The target GPU. |
| 165 | */ |
| 166 | GPUTarget target() const |
| 167 | { |
| 168 | return _target; |
| 169 | } |
| 170 | |
| 171 | /** Accessor to set the CL command queue to be used by the scheduler. |
| 172 | * |
| 173 | * @param[in] queue A CL command queue. |
| 174 | */ |
| 175 | void set_queue(cl::CommandQueue queue) |
| 176 | { |
| 177 | _queue = std::move(queue); |
| 178 | } |
| 179 | |
| 180 | /** Accessor to set target GPU to be used by the scheduler. |
| 181 | * |
| 182 | * @param[in] target The target GPU. |
| 183 | */ |
| 184 | void set_target(GPUTarget target) |
| 185 | { |
| 186 | _target = target; |
| 187 | } |
| 188 | |
| 189 | /** Blocks until all commands in the associated command queue have finished. */ |
| 190 | void sync() |
| 191 | { |
| 192 | _queue.finish(); |
| 193 | } |
| 194 | |
| 195 | /** Enqueues a marker into the associated command queue and return the event. |
| 196 | * |
| 197 | * @return An event that can be waited on to block the executing thread. |
| 198 | */ |
| 199 | cl::Event enqueue_sync_event() |
| 200 | { |
| 201 | cl::Event event; |
| 202 | _queue.enqueueMarker(&event); |
| 203 | |
| 204 | return event; |
| 205 | } |
| 206 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 207 | /** Tunes OpenCL kernel |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 208 | * |
| 209 | * @param[in] kernel Kernel to tune |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 210 | */ |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 211 | void tune_kernel_static(ICLKernel &kernel) |
| 212 | { |
| 213 | if(_cl_tuner != nullptr) |
| 214 | { |
| 215 | _cl_tuner->tune_kernel_static(kernel); |
| 216 | } |
| 217 | } |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 218 | |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 219 | bool is_initialised() const |
| 220 | { |
| 221 | return _is_initialised; |
| 222 | } |
| 223 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 224 | private: |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 225 | /** Flag to ensure symbols initialisation is happening before Scheduler creation */ |
| 226 | static std::once_flag _initialize_symbols; |
| 227 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 228 | cl::Context _context; |
| 229 | cl::CommandQueue _queue; |
| 230 | GPUTarget _target; |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 231 | bool _is_initialised; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 232 | ICLTuner *_cl_tuner; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 233 | }; |
| 234 | } |
| 235 | #endif /* __ARM_COMPUTE_CLSCHEDULER_H__ */ |