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 | */ |
| 24 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 25 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLKernel.h" |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLHelpers.h" |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLTuner.h" |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/tuners/Tuners.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 32 | namespace arm_compute |
| 33 | { |
| 34 | cl::Context &CLScheduler::context() |
| 35 | { |
| 36 | ARM_COMPUTE_ERROR_ON(!_is_initialised); |
| 37 | _context = CLKernelLibrary::get().context(); |
| 38 | return _context; |
| 39 | } |
| 40 | |
| 41 | cl::CommandQueue &CLScheduler::queue() |
| 42 | { |
| 43 | ARM_COMPUTE_ERROR_ON(!_is_initialised); |
| 44 | return _queue; |
| 45 | } |
| 46 | |
| 47 | GPUTarget CLScheduler::target() const |
| 48 | { |
| 49 | return _target; |
| 50 | } |
| 51 | |
| 52 | void CLScheduler::set_queue(cl::CommandQueue queue) |
| 53 | { |
| 54 | _queue = std::move(queue); |
| 55 | } |
| 56 | |
| 57 | void CLScheduler::set_target(GPUTarget target) |
| 58 | { |
| 59 | _target = target; |
| 60 | } |
| 61 | |
| 62 | void CLScheduler::set_tuner(ICLTuner *tuner) |
| 63 | { |
| 64 | _cl_tuner = tuner; |
| 65 | } |
| 66 | |
| 67 | void CLScheduler::sync() |
| 68 | { |
| 69 | _queue.finish(); |
| 70 | } |
| 71 | |
| 72 | cl::Event CLScheduler::enqueue_sync_event() |
| 73 | { |
| 74 | cl::Event event; |
| 75 | _queue.enqueueMarker(&event); |
| 76 | return event; |
| 77 | } |
| 78 | |
| 79 | void CLScheduler::tune_kernel_static(ICLKernel &kernel) |
| 80 | { |
| 81 | if(_cl_tuner != nullptr) |
| 82 | { |
| 83 | _cl_tuner->tune_kernel_static(kernel); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | bool CLScheduler::is_initialised() const |
| 88 | { |
| 89 | return _is_initialised; |
| 90 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 92 | std::once_flag CLScheduler::_initialize_symbols; |
| 93 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | CLScheduler::CLScheduler() |
Georgios Pinitas | 72ccc44 | 2018-08-14 12:52:53 +0100 | [diff] [blame] | 95 | : _context(), _queue(), _target(GPUTarget::MIDGARD), _is_initialised(false), _cl_tuner(nullptr), _cl_default_static_tuner(nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | { |
| 97 | } |
| 98 | |
| 99 | CLScheduler &CLScheduler::get() |
| 100 | { |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 101 | std::call_once(_initialize_symbols, opencl_is_available); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | static CLScheduler scheduler; |
| 103 | return scheduler; |
| 104 | } |
| 105 | |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 106 | void CLScheduler::default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner) |
| 107 | { |
| 108 | if(!_is_initialised) |
| 109 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 110 | const std::string cl_kernels_folder("./cl_kernels/"); |
| 111 | cl::CommandQueue queue = cl::CommandQueue(ctx, device); |
| 112 | CLKernelLibrary::get().init(cl_kernels_folder, ctx, device); |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 113 | init(ctx, queue, device, cl_tuner); |
| 114 | _cl_default_static_tuner = tuners::TunerFactory::create_tuner(_target); |
| 115 | _cl_tuner = (cl_tuner == nullptr) ? _cl_default_static_tuner.get() : cl_tuner; |
| 116 | } |
| 117 | } |
| 118 | |
Georgios Pinitas | df473ea | 2018-05-31 18:53:52 +0100 | [diff] [blame] | 119 | void CLScheduler::default_init(ICLTuner *cl_tuner) |
| 120 | { |
| 121 | if(!_is_initialised) |
| 122 | { |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 123 | cl::Context ctx; |
| 124 | cl::Device dev; |
| 125 | cl_int err; |
| 126 | std::tie(ctx, dev, err) = create_opencl_context_and_device(); |
| 127 | ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Failed to create OpenCL context"); |
| 128 | cl::CommandQueue queue = cl::CommandQueue(ctx, dev); |
| 129 | CLKernelLibrary::get().init("./cl_kernels/", ctx, dev); |
| 130 | init(ctx, queue, dev, cl_tuner); |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 131 | // Create a default static tuner and set if none was provided |
| 132 | _cl_default_static_tuner = tuners::TunerFactory::create_tuner(_target); |
Georgios Pinitas | df473ea | 2018-05-31 18:53:52 +0100 | [diff] [blame] | 133 | } |
Georgios Pinitas | 7777b1a | 2018-07-11 18:16:20 +0100 | [diff] [blame] | 134 | |
| 135 | // Set CL tuner |
| 136 | _cl_tuner = (cl_tuner == nullptr) ? _cl_default_static_tuner.get() : cl_tuner; |
Georgios Pinitas | df473ea | 2018-05-31 18:53:52 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Pablo Tello | b7c308a | 2019-01-22 12:59:26 +0000 | [diff] [blame] | 139 | void CLScheduler::set_context(cl::Context context) |
| 140 | { |
| 141 | _context = std::move(context); |
| 142 | CLKernelLibrary::get().set_context(_context); |
| 143 | } |
| 144 | |
| 145 | void CLScheduler::init(cl::Context context, cl::CommandQueue queue, const cl::Device &device, ICLTuner *cl_tuner) |
| 146 | { |
| 147 | set_context(std::move(context)); |
| 148 | _queue = std::move(queue); |
| 149 | _target = get_target_from_device(device); |
| 150 | _is_initialised = true; |
| 151 | _cl_tuner = cl_tuner; |
| 152 | } |
| 153 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 154 | void CLScheduler::enqueue_common(ICLKernel &kernel, ITensorPack &tensors, bool flush) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | { |
steniu01 | 3470247 | 2017-07-11 09:22:58 +0100 | [diff] [blame] | 156 | ARM_COMPUTE_ERROR_ON_MSG(!_is_initialised, |
| 157 | "The CLScheduler is not initialised yet! Please call the CLScheduler::get().default_init(), \ |
| 158 | or CLScheduler::get()::init() and CLKernelLibrary::get()::init() function before running functions!"); |
| 159 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 160 | const bool inject_memory = !tensors.empty(); |
Georgios Pinitas | 9c82e01 | 2020-07-17 12:47:56 +0100 | [diff] [blame] | 161 | |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 162 | // Tune the kernel if the CLTuner has been provided |
| 163 | if(_cl_tuner != nullptr) |
| 164 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 165 | inject_memory ? _cl_tuner->tune_kernel_dynamic(kernel, tensors) : _cl_tuner->tune_kernel_dynamic(kernel); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Run kernel |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 169 | inject_memory ? kernel.run_op(tensors, kernel.window(), _queue) : kernel.run(kernel.window(), _queue); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | |
| 171 | if(flush) |
| 172 | { |
| 173 | _queue.flush(); |
| 174 | } |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 175 | } |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 176 | |
| 177 | void CLScheduler::enqueue(ICLKernel &kernel, bool flush) |
| 178 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 179 | ITensorPack pack; |
| 180 | enqueue_common(kernel, pack, flush); |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 183 | void CLScheduler::enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush) |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 184 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 185 | enqueue_common(kernel, tensors, flush); |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 186 | } |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 187 | } // namespace arm_compute |