Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 53d1227 | 2018-02-01 20:23:25 +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 | #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" |
| 29 | #include "arm_compute/core/Utils.h" |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/CPUUtils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 32 | #include <atomic> |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 33 | #include <condition_variable> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include <iostream> |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 35 | #include <mutex> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | #include <system_error> |
| 37 | #include <thread> |
| 38 | |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 41 | namespace |
| 42 | { |
| 43 | class ThreadFeeder |
| 44 | { |
| 45 | public: |
| 46 | /** Constructor |
| 47 | * |
| 48 | * @param[in] start First value that will be returned by the feeder |
| 49 | * @param[in] end End condition (The last value returned by get_next() will be end - 1) |
| 50 | */ |
| 51 | explicit ThreadFeeder(unsigned int start = 0, unsigned int end = 0) |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 52 | : _atomic_counter(start), _end(end) |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | /** Return the next element in the range if there is one. |
| 56 | * |
| 57 | * @param[out] next Will contain the next element if there is one. |
| 58 | * |
| 59 | * @return False if the end of the range has been reached and next wasn't set. |
| 60 | */ |
| 61 | bool get_next(unsigned int &next) |
| 62 | { |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 63 | next = atomic_fetch_add_explicit(&_atomic_counter, 1u, std::memory_order_relaxed); |
| 64 | return next < _end; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private: |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 68 | std::atomic_uint _atomic_counter; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 69 | const unsigned int _end; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /** Execute workloads[info.thread_id] first, then call the feeder to get the index of the next workload to run. |
| 73 | * |
| 74 | * Will run workloads until the feeder reaches the end of its range. |
| 75 | * |
| 76 | * @param[in] workloads The array of workloads |
| 77 | * @param[in,out] feeder The feeder indicating which workload to execute next. |
| 78 | * @param[in] info Threading and CPU info. |
| 79 | */ |
| 80 | void process_workloads(std::vector<IScheduler::Workload> &workloads, ThreadFeeder &feeder, const ThreadInfo &info) |
| 81 | { |
| 82 | unsigned int workload_index = info.thread_id; |
| 83 | do |
| 84 | { |
| 85 | ARM_COMPUTE_ERROR_ON(workload_index >= workloads.size()); |
| 86 | workloads[workload_index](info); |
| 87 | } |
| 88 | while(feeder.get_next(workload_index)); |
| 89 | } |
| 90 | |
| 91 | } //namespace |
| 92 | |
| 93 | class CPPScheduler::Thread |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | { |
| 95 | public: |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 96 | /** Start a new thread. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | Thread(); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 98 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | Thread(const Thread &) = delete; |
| 100 | Thread &operator=(const Thread &) = delete; |
| 101 | Thread(Thread &&) = delete; |
| 102 | Thread &operator=(Thread &&) = delete; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 103 | |
| 104 | /** Destructor. Make the thread join. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | ~Thread(); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 106 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 107 | /** Request the worker thread to start executing workloads. |
| 108 | * |
| 109 | * The thread will start by executing workloads[info.thread_id] and will then call the feeder to |
| 110 | * get the index of the following workload to run. |
| 111 | * |
| 112 | * @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] | 113 | * wait() needs to be called to ensure the execution is complete. |
| 114 | */ |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 115 | void start(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 116 | |
| 117 | /** Wait for the current kernel execution to complete. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | void wait(); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 119 | |
| 120 | /** Function ran by the worker thread. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | void worker_thread(); |
| 122 | |
| 123 | private: |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 124 | std::thread _thread{}; |
| 125 | ThreadInfo _info{}; |
| 126 | std::vector<IScheduler::Workload> *_workloads{ nullptr }; |
| 127 | ThreadFeeder *_feeder{ nullptr }; |
| 128 | std::mutex _m{}; |
| 129 | std::condition_variable _cv{}; |
| 130 | bool _wait_for_work{ false }; |
| 131 | bool _job_complete{ true }; |
| 132 | std::exception_ptr _current_exception{ nullptr }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 135 | CPPScheduler::Thread::Thread() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | _thread = std::thread(&Thread::worker_thread, this); |
| 138 | } |
| 139 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 140 | CPPScheduler::Thread::~Thread() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 142 | // Make sure worker thread has ended |
| 143 | if(_thread.joinable()) |
| 144 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 145 | ThreadFeeder feeder; |
| 146 | start(nullptr, feeder, ThreadInfo()); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 147 | _thread.join(); |
| 148 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 151 | void CPPScheduler::Thread::start(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 153 | _workloads = workloads; |
| 154 | _feeder = &feeder; |
| 155 | _info = info; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 156 | { |
| 157 | std::lock_guard<std::mutex> lock(_m); |
| 158 | _wait_for_work = true; |
| 159 | _job_complete = false; |
| 160 | } |
| 161 | _cv.notify_one(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 164 | void CPPScheduler::Thread::wait() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 166 | { |
| 167 | std::unique_lock<std::mutex> lock(_m); |
| 168 | _cv.wait(lock, [&] { return _job_complete; }); |
| 169 | } |
| 170 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | if(_current_exception) |
| 172 | { |
| 173 | std::rethrow_exception(_current_exception); |
| 174 | } |
| 175 | } |
| 176 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 177 | void CPPScheduler::Thread::worker_thread() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 178 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 179 | while(true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 181 | std::unique_lock<std::mutex> lock(_m); |
| 182 | _cv.wait(lock, [&] { return _wait_for_work; }); |
| 183 | _wait_for_work = false; |
| 184 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | _current_exception = nullptr; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 186 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | // Time to exit |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 188 | if(_workloads == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | try |
| 194 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 195 | process_workloads(*_workloads, *_feeder, _info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | } |
| 197 | catch(...) |
| 198 | { |
| 199 | _current_exception = std::current_exception(); |
| 200 | } |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 201 | |
| 202 | _job_complete = true; |
| 203 | lock.unlock(); |
| 204 | _cv.notify_one(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 208 | CPPScheduler &CPPScheduler::get() |
| 209 | { |
| 210 | static CPPScheduler scheduler; |
| 211 | return scheduler; |
| 212 | } |
| 213 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | CPPScheduler::CPPScheduler() |
Georgios Pinitas | 53d1227 | 2018-02-01 20:23:25 +0000 | [diff] [blame] | 215 | : _num_threads(num_threads_hint()), |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 216 | _threads(_num_threads - 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 217 | { |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 218 | get_cpu_configuration(_cpu_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void CPPScheduler::set_num_threads(unsigned int num_threads) |
| 222 | { |
Anthony Barbier | ce87612 | 2018-02-22 12:44:15 +0000 | [diff] [blame] | 223 | _num_threads = num_threads == 0 ? num_threads_hint() : num_threads; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 224 | _threads.resize(_num_threads - 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 227 | unsigned int CPPScheduler::num_threads() const |
| 228 | { |
| 229 | return _num_threads; |
| 230 | } |
| 231 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 232 | void CPPScheduler::run_workloads(std::vector<IScheduler::Workload> &workloads) |
| 233 | { |
| 234 | const unsigned int num_threads = std::min(_num_threads, static_cast<unsigned int>(workloads.size())); |
| 235 | if(num_threads < 1) |
| 236 | { |
| 237 | return; |
| 238 | } |
| 239 | ThreadFeeder feeder(num_threads, workloads.size()); |
| 240 | ThreadInfo info; |
| 241 | info.cpu_info = &_cpu_info; |
| 242 | info.num_threads = num_threads; |
| 243 | unsigned int t = 0; |
| 244 | auto thread_it = _threads.begin(); |
| 245 | for(; t < num_threads - 1; ++t, ++thread_it) |
| 246 | { |
| 247 | info.thread_id = t; |
| 248 | thread_it->start(&workloads, feeder, info); |
| 249 | } |
| 250 | |
| 251 | info.thread_id = t; |
| 252 | process_workloads(workloads, feeder, info); |
| 253 | |
| 254 | try |
| 255 | { |
| 256 | for(auto &thread : _threads) |
| 257 | { |
| 258 | thread.wait(); |
| 259 | } |
| 260 | } |
| 261 | catch(const std::system_error &e) |
| 262 | { |
| 263 | std::cerr << "Caught system_error with code " << e.code() << " meaning " << e.what() << '\n'; |
| 264 | } |
| 265 | } |
| 266 | |
Anthony Barbier | 376c85f | 2018-05-25 14:17:21 +0100 | [diff] [blame] | 267 | void CPPScheduler::schedule(ICPPKernel *kernel, const Hints &hints) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 268 | { |
| 269 | ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel"); |
| 270 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 271 | const Window &max_window = kernel->window(); |
Anthony Barbier | 376c85f | 2018-05-25 14:17:21 +0100 | [diff] [blame] | 272 | const unsigned int num_iterations = max_window.num_iterations(hints.split_dimension()); |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 273 | const unsigned int num_threads = std::min(num_iterations, _num_threads); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 274 | |
Moritz Pflanzer | 2fd5d95 | 2017-09-24 12:10:46 +0100 | [diff] [blame] | 275 | if(num_iterations == 0) |
| 276 | { |
| 277 | return; |
| 278 | } |
| 279 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 280 | if(!kernel->is_parallelisable() || num_threads == 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 282 | ThreadInfo info; |
| 283 | info.cpu_info = &_cpu_info; |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 284 | kernel->run(max_window, info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 285 | } |
| 286 | else |
| 287 | { |
Anthony Barbier | 376c85f | 2018-05-25 14:17:21 +0100 | [diff] [blame] | 288 | unsigned int num_windows = 0; |
| 289 | switch(hints.strategy()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 290 | { |
Anthony Barbier | 376c85f | 2018-05-25 14:17:21 +0100 | [diff] [blame] | 291 | case StrategyHint::STATIC: |
| 292 | num_windows = num_threads; |
| 293 | break; |
| 294 | case StrategyHint::DYNAMIC: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 295 | { |
Anthony Barbier | 376c85f | 2018-05-25 14:17:21 +0100 | [diff] [blame] | 296 | // Make sure we don't use some windows which are too small as this might create some contention on the ThreadFeeder |
| 297 | const unsigned int max_iterations = static_cast<unsigned int>(_num_threads) * 3; |
| 298 | num_windows = num_iterations > max_iterations ? max_iterations : num_iterations; |
| 299 | break; |
| 300 | } |
| 301 | default: |
| 302 | ARM_COMPUTE_ERROR("Unknown strategy"); |
| 303 | } |
| 304 | std::vector<IScheduler::Workload> workloads(num_windows); |
| 305 | for(unsigned int t = 0; t < num_windows; t++) |
| 306 | { |
| 307 | //Capture 't' by copy, all the other variables by reference: |
| 308 | workloads[t] = [t, &hints, &max_window, &num_windows, &kernel](const ThreadInfo & info) |
| 309 | { |
| 310 | Window win = max_window.split_window(hints.split_dimension(), t, num_windows); |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 311 | win.validate(); |
| 312 | kernel->run(win, info); |
| 313 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 314 | } |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 315 | run_workloads(workloads); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 316 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 317 | } |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 318 | } // namespace arm_compute |