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