Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | dba672c | 2023-04-06 16:30:18 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2023 Arm Limited. |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +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/IScheduler.h" |
| 25 | |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/ICPPKernel.h" |
Anthony Barbier | 148b075 | 2018-09-11 14:19:39 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 28 | #include "arm_compute/core/Log.h" |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Window.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | |
Georgios Pinitas | 08302c1 | 2021-06-09 10:08:27 +0100 | [diff] [blame] | 31 | #include "src/common/cpuinfo/CpuInfo.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/runtime/SchedulerUtils.h" |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | IScheduler::IScheduler() |
| 37 | { |
Georgios Pinitas | 53d1227 | 2018-02-01 20:23:25 +0000 | [diff] [blame] | 38 | // Work out the best possible number of execution threads |
Georgios Pinitas | 08302c1 | 2021-06-09 10:08:27 +0100 | [diff] [blame] | 39 | _num_threads_hint = cpuinfo::num_threads_hint(); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 42 | CPUInfo &IScheduler::cpu_info() |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 43 | { |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 44 | return CPUInfo::get(); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 45 | } |
Georgios Pinitas | 53d1227 | 2018-02-01 20:23:25 +0000 | [diff] [blame] | 46 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 47 | void IScheduler::set_num_threads_with_affinity(unsigned int num_threads, BindFunc func) |
| 48 | { |
| 49 | ARM_COMPUTE_UNUSED(num_threads, func); |
| 50 | ARM_COMPUTE_ERROR("Feature for affinity setting is not implemented"); |
| 51 | } |
| 52 | |
Georgios Pinitas | 53d1227 | 2018-02-01 20:23:25 +0000 | [diff] [blame] | 53 | unsigned int IScheduler::num_threads_hint() const |
| 54 | { |
| 55 | return _num_threads_hint; |
| 56 | } |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 57 | |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame] | 58 | void IScheduler::schedule_common(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 59 | { |
| 60 | ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel"); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 61 | #ifndef BARE_METAL |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame] | 62 | const Window &max_window = window; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | if (hints.split_dimension() == IScheduler::split_dimensions_all) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 64 | { |
| 65 | /* |
| 66 | * if the split dim is size_t max then this signals we should parallelise over |
| 67 | * all dimensions |
| 68 | */ |
| 69 | const std::size_t m = max_window.num_iterations(Window::DimX); |
| 70 | const std::size_t n = max_window.num_iterations(Window::DimY); |
| 71 | |
| 72 | //in c++17 this can be swapped for auto [ m_threads, n_threads ] = split_2d(... |
| 73 | unsigned m_threads, n_threads; |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 74 | std::tie(m_threads, n_threads) = scheduler_utils::split_2d(this->num_threads(), m, n); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 75 | |
| 76 | std::vector<IScheduler::Workload> workloads; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 77 | for (unsigned int ni = 0; ni != n_threads; ++ni) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 78 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | for (unsigned int mi = 0; mi != m_threads; ++mi) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 80 | { |
| 81 | workloads.push_back( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | [ni, mi, m_threads, n_threads, &max_window, &kernel](const ThreadInfo &info) |
| 83 | { |
| 84 | //narrow the window to our mi-ni workload |
| 85 | Window win = max_window.split_window(Window::DimX, mi, m_threads) |
| 86 | .split_window(Window::DimY, ni, n_threads); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | win.validate(); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 89 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | Window thread_locator; |
| 91 | thread_locator.set(Window::DimX, Window::Dimension(mi, m_threads)); |
| 92 | thread_locator.set(Window::DimY, Window::Dimension(ni, n_threads)); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 93 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 94 | thread_locator.validate(); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 95 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | kernel->run_nd(win, info, thread_locator); |
| 97 | }); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | run_workloads(workloads); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | const unsigned int num_iterations = max_window.num_iterations(hints.split_dimension()); |
| 105 | const unsigned int num_threads = std::min(num_iterations, this->num_threads()); |
| 106 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | if (num_iterations == 0) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 108 | { |
| 109 | return; |
| 110 | } |
| 111 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 112 | if (!kernel->is_parallelisable() || num_threads == 1) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 113 | { |
| 114 | ThreadInfo info; |
Michalis Spyrou | 20fca52 | 2021-06-07 14:23:57 +0100 | [diff] [blame] | 115 | info.cpu_info = &cpu_info(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 116 | if (tensors.empty()) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 117 | { |
| 118 | kernel->run(max_window, info); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | kernel->run_op(tensors, max_window, info); |
| 123 | } |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | unsigned int num_windows = 0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 128 | switch (hints.strategy()) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 129 | { |
| 130 | case StrategyHint::STATIC: |
| 131 | num_windows = num_threads; |
| 132 | break; |
| 133 | case StrategyHint::DYNAMIC: |
| 134 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 135 | const unsigned int granule_threshold = |
| 136 | (hints.threshold() <= 0) ? num_threads : static_cast<unsigned int>(hints.threshold()); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 137 | // Make sure we don't use some windows which are too small as this might create some contention on the ThreadFeeder |
| 138 | num_windows = num_iterations > granule_threshold ? granule_threshold : num_iterations; |
| 139 | break; |
| 140 | } |
| 141 | default: |
| 142 | ARM_COMPUTE_ERROR("Unknown strategy"); |
| 143 | } |
SiCong Li | dba672c | 2023-04-06 16:30:18 +0100 | [diff] [blame] | 144 | // Make sure the smallest window is larger than minimum workload size |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 145 | num_windows = adjust_num_of_windows(max_window, hints.split_dimension(), num_windows, *kernel, cpu_info()); |
| 146 | |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 147 | std::vector<IScheduler::Workload> workloads(num_windows); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 148 | for (unsigned int t = 0; t < num_windows; ++t) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 149 | { |
| 150 | //Capture 't' by copy, all the other variables by reference: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 151 | workloads[t] = [t, &hints, &max_window, &num_windows, &kernel, &tensors](const ThreadInfo &info) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 152 | { |
| 153 | Window win = max_window.split_window(hints.split_dimension(), t, num_windows); |
| 154 | win.validate(); |
| 155 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 156 | if (tensors.empty()) |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 157 | { |
| 158 | kernel->run(win, info); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | kernel->run_op(tensors, win, info); |
| 163 | } |
| 164 | }; |
| 165 | } |
| 166 | run_workloads(workloads); |
| 167 | } |
| 168 | } |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame] | 169 | #else /* !BARE_METAL */ |
| 170 | ARM_COMPUTE_UNUSED(kernel, hints, window, tensors); |
morgolock | 5111264 | 2020-08-20 14:51:39 +0100 | [diff] [blame] | 171 | #endif /* !BARE_METAL */ |
| 172 | } |
| 173 | |
Anthony Barbier | 148b075 | 2018-09-11 14:19:39 +0100 | [diff] [blame] | 174 | void IScheduler::run_tagged_workloads(std::vector<Workload> &workloads, const char *tag) |
| 175 | { |
| 176 | ARM_COMPUTE_UNUSED(tag); |
| 177 | run_workloads(workloads); |
| 178 | } |
| 179 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 180 | std::size_t IScheduler::adjust_num_of_windows(const Window &window, |
| 181 | std::size_t split_dimension, |
| 182 | std::size_t init_num_windows, |
| 183 | const ICPPKernel &kernel, |
| 184 | const CPUInfo &cpu_info) |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 185 | { |
| 186 | // Mitigation of the narrow split issue, which occurs when the split dimension is too small to split (hence "narrow"). |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 187 | if (window.num_iterations(split_dimension) < init_num_windows) |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 188 | { |
| 189 | auto recommended_split_dim = Window::DimX; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 190 | for (std::size_t dims = Window::DimY; dims <= Window::DimW; ++dims) |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 191 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 192 | if (window.num_iterations(recommended_split_dim) < window.num_iterations(dims)) |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 193 | { |
| 194 | recommended_split_dim = dims; |
| 195 | } |
| 196 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 197 | ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE( |
| 198 | "%zu dimension is not a suitable dimension to split the workload. Recommended: %zu recommended_split_dim", |
| 199 | split_dimension, recommended_split_dim); |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 202 | for (auto t = init_num_windows; t > 0; --t) // Trying the highest number of windows ,init_num_windows, first |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 203 | { |
| 204 | // Try splitting the workload into t, subject to each subworkload size <= mws. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 205 | if ((window.num_iterations(split_dimension) / kernel.get_mws(cpu_info, t)) >= t) |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 206 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 207 | if (t != init_num_windows) |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 208 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 209 | ARM_COMPUTE_LOG_INFO_MSG_CORE( |
| 210 | "The scheduler is using a different thread count than the one assigned by the user."); |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 211 | } |
| 212 | return t; |
| 213 | } |
| 214 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 215 | ARM_COMPUTE_LOG_INFO_MSG_CORE( |
| 216 | "The scheduler is using single thread instead of the thread count assigned by the user."); |
Dana Zlotnik | d7154db | 2021-11-10 11:50:58 +0200 | [diff] [blame] | 217 | return 1; // If the workload is so small that it can't be split, we should run a single thread |
| 218 | } |
| 219 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 220 | } // namespace arm_compute |