Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 4551403 | 2020-12-30 00:03:09 +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/CPP/CPPScheduler.h" |
| 25 | |
| 26 | #include "arm_compute/core/CPP/ICPPKernel.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 29 | #include "arm_compute/core/Log.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Utils.h" |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 31 | #include "arm_compute/core/utils/misc/Utility.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/runtime/CPUUtils.h" |
Pablo Tello | 2725197 | 2019-09-19 16:39:04 +0100 | [diff] [blame] | 33 | #include "support/Mutex.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 35 | #include <atomic> |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 36 | #include <condition_variable> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include <iostream> |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 38 | #include <list> |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 39 | #include <memory> |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 40 | #include <mutex> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | #include <system_error> |
| 42 | #include <thread> |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 43 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 45 | namespace arm_compute |
| 46 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 47 | namespace |
| 48 | { |
| 49 | class ThreadFeeder |
| 50 | { |
| 51 | public: |
| 52 | /** Constructor |
| 53 | * |
| 54 | * @param[in] start First value that will be returned by the feeder |
| 55 | * @param[in] end End condition (The last value returned by get_next() will be end - 1) |
| 56 | */ |
| 57 | explicit ThreadFeeder(unsigned int start = 0, unsigned int end = 0) |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 58 | : _atomic_counter(start), _end(end) |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | /** Return the next element in the range if there is one. |
| 62 | * |
| 63 | * @param[out] next Will contain the next element if there is one. |
| 64 | * |
| 65 | * @return False if the end of the range has been reached and next wasn't set. |
| 66 | */ |
| 67 | bool get_next(unsigned int &next) |
| 68 | { |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 69 | next = atomic_fetch_add_explicit(&_atomic_counter, 1u, std::memory_order_relaxed); |
| 70 | return next < _end; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private: |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 74 | std::atomic_uint _atomic_counter; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 75 | const unsigned int _end; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /** Execute workloads[info.thread_id] first, then call the feeder to get the index of the next workload to run. |
| 79 | * |
| 80 | * Will run workloads until the feeder reaches the end of its range. |
| 81 | * |
| 82 | * @param[in] workloads The array of workloads |
| 83 | * @param[in,out] feeder The feeder indicating which workload to execute next. |
| 84 | * @param[in] info Threading and CPU info. |
| 85 | */ |
| 86 | void process_workloads(std::vector<IScheduler::Workload> &workloads, ThreadFeeder &feeder, const ThreadInfo &info) |
| 87 | { |
| 88 | unsigned int workload_index = info.thread_id; |
| 89 | do |
| 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON(workload_index >= workloads.size()); |
| 92 | workloads[workload_index](info); |
| 93 | } |
| 94 | while(feeder.get_next(workload_index)); |
| 95 | } |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 96 | |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 97 | /** Set thread affinity. Pin current thread to a particular core |
| 98 | * |
| 99 | * @param[in] core_id ID of the core to which the current thread is pinned |
| 100 | */ |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 101 | void set_thread_affinity(int core_id) |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 102 | { |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 103 | if(core_id < 0) |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 104 | { |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 105 | return; |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Georgios Pinitas | 4551403 | 2020-12-30 00:03:09 +0000 | [diff] [blame] | 108 | #if !defined(__APPLE__) |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 109 | cpu_set_t set; |
| 110 | CPU_ZERO(&set); |
| 111 | CPU_SET(core_id, &set); |
Georgios Pinitas | 4551403 | 2020-12-30 00:03:09 +0000 | [diff] [blame] | 112 | ARM_COMPUTE_EXIT_ON_MSG(sched_setaffinity(0, sizeof(set), &set), "Error setting thread affinity"); |
| 113 | #endif /* !defined(__APPLE__) */ |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 114 | } |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 115 | |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 116 | /** There are currently 2 scheduling modes supported by CPPScheduler |
| 117 | * |
| 118 | * Linear: |
| 119 | * The default mode where all the scheduling is carried out by the main thread linearly (in a loop). |
| 120 | * E.G. If there are 8 threads in total, there will be 1 main thread + 7 threads in the thread pool, and it is main |
| 121 | * thread's responsibility to start all the other threads in the thread pool. |
| 122 | * |
| 123 | * Fanout: |
| 124 | * In fanout mode, the scheduling (starting other threads) task is distributed across many threads instead of just |
| 125 | * the main thread. |
| 126 | * |
| 127 | * The scheduler has a fixed parameter: wake_fanout, and the scheduling sequence goes like this: |
| 128 | * 1. Main thread wakes the first wake_fanout - 1 number of FanoutThreads from the thread pool |
| 129 | * From thread: 0 |
| 130 | * To thread (non-inclusive): Wake_fanout - 1 |
| 131 | * 2. Each FanoutThread then wakes wake_fanout number of FanoutThreads from the thread pool: |
| 132 | * From thread: (i + 1) * wake_fanout - 1 |
| 133 | * To thread (non-inclusive): (i + 2) * wake_fanout - 1 |
| 134 | * where i is the current thread's thread id |
| 135 | * The end is clamped at the size of the thread pool / the number of threads in use - 1 |
| 136 | * |
| 137 | * E.G. for a total number of 8 threads (1 main thread, 7 FanoutThreads in thread pool) with a fanout of 3 |
| 138 | * 1. Main thread wakes FanoutThread 0, 1 |
| 139 | * 2. FanoutThread 0 wakes FanoutThread 2, 3, 4 |
| 140 | * 3. FanoutThread 1 wakes FanoutThread 5, 6 |
| 141 | */ |
| 142 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 143 | class Thread final |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | { |
| 145 | public: |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 146 | /** Start a new thread |
| 147 | * |
| 148 | * Thread will be pinned to a given core id if value is non-negative |
| 149 | * |
| 150 | * @param[in] core_pin Core id to pin the thread on. If negative no thread pinning will take place |
| 151 | */ |
| 152 | explicit Thread(int core_pin = -1); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 153 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | Thread(const Thread &) = delete; |
| 155 | Thread &operator=(const Thread &) = delete; |
| 156 | Thread(Thread &&) = delete; |
| 157 | Thread &operator=(Thread &&) = delete; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 158 | |
| 159 | /** Destructor. Make the thread join. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | ~Thread(); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 161 | |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 162 | /** Set workloads */ |
| 163 | void set_workload(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info); |
| 164 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 165 | /** Request the worker thread to start executing workloads. |
| 166 | * |
| 167 | * The thread will start by executing workloads[info.thread_id] and will then call the feeder to |
| 168 | * get the index of the following workload to run. |
| 169 | * |
| 170 | * @note This function will return as soon as the workloads have been sent to the worker thread. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | * wait() needs to be called to ensure the execution is complete. |
| 172 | */ |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 173 | void start(); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 174 | |
| 175 | /** Wait for the current kernel execution to complete. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | void wait(); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 177 | |
| 178 | /** Function ran by the worker thread. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 179 | void worker_thread(); |
| 180 | |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 181 | /** Set the scheduling strategy to be linear */ |
| 182 | void set_linear_mode() |
| 183 | { |
| 184 | _thread_pool = nullptr; |
| 185 | _wake_beg = 0; |
| 186 | _wake_end = 0; |
| 187 | } |
| 188 | |
| 189 | /** Set the scheduling strategy to be fanout */ |
| 190 | void set_fanout_mode(std::list<Thread> *thread_pool, unsigned int wake_beg, unsigned int wake_end) |
| 191 | { |
| 192 | _thread_pool = thread_pool; |
| 193 | _wake_beg = wake_beg; |
| 194 | _wake_end = wake_end; |
| 195 | } |
| 196 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 197 | private: |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 198 | std::thread _thread{}; |
| 199 | ThreadInfo _info{}; |
| 200 | std::vector<IScheduler::Workload> *_workloads{ nullptr }; |
| 201 | ThreadFeeder *_feeder{ nullptr }; |
| 202 | std::mutex _m{}; |
| 203 | std::condition_variable _cv{}; |
| 204 | bool _wait_for_work{ false }; |
| 205 | bool _job_complete{ true }; |
| 206 | std::exception_ptr _current_exception{ nullptr }; |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 207 | int _core_pin{ -1 }; |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 208 | std::list<Thread> *_thread_pool{ nullptr }; |
| 209 | unsigned int _wake_beg{ 0 }; |
| 210 | unsigned int _wake_end{ 0 }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 211 | }; |
| 212 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 213 | Thread::Thread(int core_pin) |
| 214 | : _core_pin(core_pin) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 215 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | _thread = std::thread(&Thread::worker_thread, this); |
| 217 | } |
| 218 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 219 | Thread::~Thread() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 220 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 221 | // Make sure worker thread has ended |
| 222 | if(_thread.joinable()) |
| 223 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 224 | ThreadFeeder feeder; |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 225 | set_workload(nullptr, feeder, ThreadInfo()); |
| 226 | start(); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 227 | _thread.join(); |
| 228 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 229 | } |
| 230 | |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 231 | void Thread::set_workload(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 232 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 233 | _workloads = workloads; |
| 234 | _feeder = &feeder; |
| 235 | _info = info; |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 236 | } |
| 237 | |
| 238 | void Thread::start() |
| 239 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 240 | { |
| 241 | std::lock_guard<std::mutex> lock(_m); |
| 242 | _wait_for_work = true; |
| 243 | _job_complete = false; |
| 244 | } |
| 245 | _cv.notify_one(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 248 | void Thread::wait() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 249 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 250 | { |
| 251 | std::unique_lock<std::mutex> lock(_m); |
| 252 | _cv.wait(lock, [&] { return _job_complete; }); |
| 253 | } |
| 254 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 255 | if(_current_exception) |
| 256 | { |
| 257 | std::rethrow_exception(_current_exception); |
| 258 | } |
| 259 | } |
| 260 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 261 | void Thread::worker_thread() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 262 | { |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 263 | set_thread_affinity(_core_pin); |
| 264 | |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 265 | while(true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 266 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 267 | std::unique_lock<std::mutex> lock(_m); |
| 268 | _cv.wait(lock, [&] { return _wait_for_work; }); |
| 269 | _wait_for_work = false; |
| 270 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 271 | _current_exception = nullptr; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 272 | |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 273 | // Exit if the worker thread has not been fed with workloads |
| 274 | if(_workloads == nullptr || _feeder == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 275 | { |
| 276 | return; |
| 277 | } |
| 278 | |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 279 | // Wake up more peer threads from thread pool if this job has been delegated to the current thread |
| 280 | if(_thread_pool != nullptr) |
| 281 | { |
| 282 | auto thread_it = _thread_pool->begin(); |
| 283 | std::advance(thread_it, std::min(static_cast<unsigned int>(_thread_pool->size()), _wake_beg)); |
| 284 | auto wake_end = std::min(_wake_end, static_cast<unsigned int>(_info.num_threads - 1)); |
| 285 | for(unsigned int t = _wake_beg; t < wake_end; ++t, ++thread_it) |
| 286 | { |
| 287 | thread_it->start(); |
| 288 | } |
| 289 | } |
| 290 | |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 291 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 292 | try |
| 293 | { |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 294 | #endif /* ARM_COMPUTE_EXCEPTIONS_ENABLED */ |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 295 | process_workloads(*_workloads, *_feeder, _info); |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 296 | |
| 297 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 298 | } |
| 299 | catch(...) |
| 300 | { |
| 301 | _current_exception = std::current_exception(); |
| 302 | } |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 303 | #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 304 | _workloads = nullptr; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 305 | _job_complete = true; |
| 306 | lock.unlock(); |
| 307 | _cv.notify_one(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 308 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 309 | } |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 310 | } //namespace |
| 311 | |
| 312 | struct CPPScheduler::Impl final |
| 313 | { |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 314 | constexpr static unsigned int m_default_wake_fanout = 4; |
| 315 | enum class Mode |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 316 | { |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 317 | Linear, |
| 318 | Fanout |
| 319 | }; |
| 320 | enum class ModeToggle |
| 321 | { |
| 322 | None, |
| 323 | Linear, |
| 324 | Fanout |
| 325 | }; |
| 326 | explicit Impl(unsigned int thread_hint) |
| 327 | : _num_threads(thread_hint), _threads(_num_threads - 1), _mode(Mode::Linear), _wake_fanout(0U) |
| 328 | { |
| 329 | const auto mode_env_v = utility::tolower(utility::getenv("ARM_COMPUTE_CPP_SCHEDULER_MODE")); |
| 330 | if(mode_env_v == "linear") |
| 331 | { |
| 332 | _forced_mode = ModeToggle::Linear; |
| 333 | } |
| 334 | else if(mode_env_v == "fanout") |
| 335 | { |
| 336 | _forced_mode = ModeToggle::Fanout; |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | _forced_mode = ModeToggle::None; |
| 341 | } |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 342 | } |
| 343 | void set_num_threads(unsigned int num_threads, unsigned int thread_hint) |
| 344 | { |
| 345 | _num_threads = num_threads == 0 ? thread_hint : num_threads; |
| 346 | _threads.resize(_num_threads - 1); |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 347 | auto_switch_mode(_num_threads); |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 348 | } |
| 349 | void set_num_threads_with_affinity(unsigned int num_threads, unsigned int thread_hint, BindFunc func) |
| 350 | { |
| 351 | _num_threads = num_threads == 0 ? thread_hint : num_threads; |
| 352 | |
| 353 | // Set affinity on main thread |
| 354 | set_thread_affinity(func(0, thread_hint)); |
| 355 | |
| 356 | // Set affinity on worked threads |
| 357 | _threads.clear(); |
| 358 | for(auto i = 1U; i < _num_threads; ++i) |
| 359 | { |
| 360 | _threads.emplace_back(func(i, thread_hint)); |
| 361 | } |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 362 | auto_switch_mode(_num_threads); |
| 363 | } |
| 364 | void auto_switch_mode(unsigned int num_threads_to_use) |
| 365 | { |
| 366 | // If the environment variable is set to any of the modes, it overwrites the mode selected over num_threads_to_use |
| 367 | if(_forced_mode == ModeToggle::Fanout || (_forced_mode == ModeToggle::None && num_threads_to_use > 8)) |
| 368 | { |
| 369 | set_fanout_mode(m_default_wake_fanout, num_threads_to_use); |
| 370 | ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("Set CPPScheduler to Fanout mode, with wake up fanout : %d and %d threads to use\n", this->wake_fanout(), num_threads_to_use); |
| 371 | } |
| 372 | else // Equivalent to (_forced_mode == ModeToggle::Linear || (_forced_mode == ModeToggle::None && num_threads_to_use <= 8)) |
| 373 | { |
| 374 | set_linear_mode(); |
| 375 | ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("Set CPPScheduler to Linear mode, with %d threads to use\n", num_threads_to_use); |
| 376 | } |
| 377 | } |
| 378 | void set_linear_mode() |
| 379 | { |
| 380 | for(auto &thread : _threads) |
| 381 | { |
| 382 | thread.set_linear_mode(); |
| 383 | } |
| 384 | _mode = Mode::Linear; |
| 385 | _wake_fanout = 0U; |
| 386 | } |
| 387 | void set_fanout_mode(unsigned int wake_fanout, unsigned int num_threads_to_use) |
| 388 | { |
| 389 | ARM_COMPUTE_ERROR_ON(num_threads_to_use > _threads.size() + 1); |
| 390 | const auto actual_wake_fanout = std::max(2U, std::min(wake_fanout, num_threads_to_use - 1)); |
| 391 | auto thread_it = _threads.begin(); |
| 392 | for(auto i = 1U; i < num_threads_to_use; ++i, ++thread_it) |
| 393 | { |
| 394 | const auto wake_begin = i * actual_wake_fanout - 1; |
| 395 | const auto wake_end = std::min((i + 1) * actual_wake_fanout - 1, num_threads_to_use - 1); |
| 396 | thread_it->set_fanout_mode(&_threads, wake_begin, wake_end); |
| 397 | } |
| 398 | // Reset the remaining threads's wake up schedule |
| 399 | while(thread_it != _threads.end()) |
| 400 | { |
| 401 | thread_it->set_fanout_mode(&_threads, 0U, 0U); |
| 402 | ++thread_it; |
| 403 | } |
| 404 | _mode = Mode::Fanout; |
| 405 | _wake_fanout = actual_wake_fanout; |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 406 | } |
| 407 | unsigned int num_threads() const |
| 408 | { |
| 409 | return _num_threads; |
| 410 | } |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 411 | unsigned int wake_fanout() const |
| 412 | { |
| 413 | return _wake_fanout; |
| 414 | } |
| 415 | Mode mode() const |
| 416 | { |
| 417 | return _mode; |
| 418 | } |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 419 | |
| 420 | void run_workloads(std::vector<IScheduler::Workload> &workloads); |
| 421 | |
| 422 | unsigned int _num_threads; |
| 423 | std::list<Thread> _threads; |
| 424 | arm_compute::Mutex _run_workloads_mutex{}; |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 425 | Mode _mode{ Mode::Linear }; |
| 426 | ModeToggle _forced_mode{ ModeToggle::None }; |
| 427 | unsigned int _wake_fanout{ 0 }; |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 428 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 429 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 430 | /* |
Giorgio Arena | 5b50f42 | 2021-02-17 11:43:05 +0000 | [diff] [blame] | 431 | * This singleton has been deprecated and will be removed in future releases |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 432 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 433 | CPPScheduler &CPPScheduler::get() |
| 434 | { |
| 435 | static CPPScheduler scheduler; |
| 436 | return scheduler; |
| 437 | } |
| 438 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 439 | CPPScheduler::CPPScheduler() |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 440 | : _impl(std::make_unique<Impl>(num_threads_hint())) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 441 | { |
| 442 | } |
| 443 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 444 | CPPScheduler::~CPPScheduler() = default; |
| 445 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 446 | void CPPScheduler::set_num_threads(unsigned int num_threads) |
| 447 | { |
Pablo Tello | 2725197 | 2019-09-19 16:39:04 +0100 | [diff] [blame] | 448 | // No changes in the number of threads while current workloads are running |
| 449 | arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex); |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 450 | _impl->set_num_threads(num_threads, num_threads_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 453 | void CPPScheduler::set_num_threads_with_affinity(unsigned int num_threads, BindFunc func) |
| 454 | { |
| 455 | // No changes in the number of threads while current workloads are running |
| 456 | arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex); |
| 457 | _impl->set_num_threads_with_affinity(num_threads, num_threads_hint(), func); |
| 458 | } |
| 459 | |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 460 | unsigned int CPPScheduler::num_threads() const |
| 461 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 462 | return _impl->num_threads(); |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 465 | #ifndef DOXYGEN_SKIP_THIS |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 466 | void CPPScheduler::run_workloads(std::vector<IScheduler::Workload> &workloads) |
| 467 | { |
Pablo Tello | 2725197 | 2019-09-19 16:39:04 +0100 | [diff] [blame] | 468 | // Mutex to ensure other threads won't interfere with the setup of the current thread's workloads |
| 469 | // Other thread's workloads will be scheduled after the current thread's workloads have finished |
| 470 | // This is not great because different threads workloads won't run in parallel but at least they |
| 471 | // won't interfere each other and deadlock. |
| 472 | arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex); |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 473 | const unsigned int num_threads_to_use = std::min(_impl->num_threads(), static_cast<unsigned int>(workloads.size())); |
| 474 | if(num_threads_to_use < 1) |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 475 | { |
| 476 | return; |
| 477 | } |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 478 | // Re-adjust the mode if the actual number of threads to use is different from the number of threads created |
| 479 | _impl->auto_switch_mode(num_threads_to_use); |
| 480 | int num_threads_to_start = 0; |
| 481 | switch(_impl->mode()) |
| 482 | { |
| 483 | case CPPScheduler::Impl::Mode::Fanout: |
| 484 | { |
| 485 | num_threads_to_start = static_cast<int>(_impl->wake_fanout()) - 1; |
| 486 | break; |
| 487 | } |
| 488 | case CPPScheduler::Impl::Mode::Linear: |
| 489 | default: |
| 490 | { |
| 491 | num_threads_to_start = static_cast<int>(num_threads_to_use) - 1; |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | ThreadFeeder feeder(num_threads_to_use, workloads.size()); |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 496 | ThreadInfo info; |
| 497 | info.cpu_info = &_cpu_info; |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 498 | info.num_threads = num_threads_to_use; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 499 | unsigned int t = 0; |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 500 | auto thread_it = _impl->_threads.begin(); |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 501 | // Set num_threads_to_use - 1 workloads to the threads as the remaining 1 is left to the main thread |
| 502 | for(; t < num_threads_to_use - 1; ++t, ++thread_it) |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 503 | { |
| 504 | info.thread_id = t; |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 505 | thread_it->set_workload(&workloads, feeder, info); |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 506 | } |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 507 | thread_it = _impl->_threads.begin(); |
| 508 | for(int i = 0; i < num_threads_to_start; ++i, ++thread_it) |
| 509 | { |
| 510 | thread_it->start(); |
| 511 | } |
| 512 | info.thread_id = t; // Set main thread's thread_id |
| 513 | process_workloads(workloads, feeder, info); // Main thread processes workloads |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 514 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 515 | try |
| 516 | { |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 517 | #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 518 | thread_it = _impl->_threads.begin(); |
| 519 | for(unsigned int i = 0; i < num_threads_to_use - 1; ++i, ++thread_it) |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 520 | { |
SiCongLi | 3b5981c | 2021-03-12 12:31:17 +0000 | [diff] [blame^] | 521 | thread_it->wait(); |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 522 | } |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 523 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 524 | } |
| 525 | catch(const std::system_error &e) |
| 526 | { |
| 527 | std::cerr << "Caught system_error with code " << e.code() << " meaning " << e.what() << '\n'; |
| 528 | } |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 529 | #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 530 | } |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 531 | #endif /* DOXYGEN_SKIP_THIS */ |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 532 | |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame] | 533 | void CPPScheduler::schedule_op(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors) |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 534 | { |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame] | 535 | schedule_common(kernel, hints, window, tensors); |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void CPPScheduler::schedule(ICPPKernel *kernel, const Hints &hints) |
| 539 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 540 | ITensorPack tensors; |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame] | 541 | schedule_common(kernel, hints, kernel->window(), tensors); |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 542 | } |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 543 | } // namespace arm_compute |