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" |
| 29 | #include "arm_compute/core/Utils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/runtime/CPUUtils.h" |
Pablo Tello | 2725197 | 2019-09-19 16:39:04 +0100 | [diff] [blame] | 31 | #include "support/Mutex.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 33 | #include <atomic> |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 34 | #include <condition_variable> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include <iostream> |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 36 | #include <list> |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 37 | #include <memory> |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 38 | #include <mutex> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | #include <system_error> |
| 40 | #include <thread> |
| 41 | |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 42 | namespace arm_compute |
| 43 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 44 | namespace |
| 45 | { |
| 46 | class ThreadFeeder |
| 47 | { |
| 48 | public: |
| 49 | /** Constructor |
| 50 | * |
| 51 | * @param[in] start First value that will be returned by the feeder |
| 52 | * @param[in] end End condition (The last value returned by get_next() will be end - 1) |
| 53 | */ |
| 54 | explicit ThreadFeeder(unsigned int start = 0, unsigned int end = 0) |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 55 | : _atomic_counter(start), _end(end) |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | /** Return the next element in the range if there is one. |
| 59 | * |
| 60 | * @param[out] next Will contain the next element if there is one. |
| 61 | * |
| 62 | * @return False if the end of the range has been reached and next wasn't set. |
| 63 | */ |
| 64 | bool get_next(unsigned int &next) |
| 65 | { |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 66 | next = atomic_fetch_add_explicit(&_atomic_counter, 1u, std::memory_order_relaxed); |
| 67 | return next < _end; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | private: |
Anthony Barbier | d89940e | 2018-06-28 13:39:35 +0100 | [diff] [blame] | 71 | std::atomic_uint _atomic_counter; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 72 | const unsigned int _end; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /** Execute workloads[info.thread_id] first, then call the feeder to get the index of the next workload to run. |
| 76 | * |
| 77 | * Will run workloads until the feeder reaches the end of its range. |
| 78 | * |
| 79 | * @param[in] workloads The array of workloads |
| 80 | * @param[in,out] feeder The feeder indicating which workload to execute next. |
| 81 | * @param[in] info Threading and CPU info. |
| 82 | */ |
| 83 | void process_workloads(std::vector<IScheduler::Workload> &workloads, ThreadFeeder &feeder, const ThreadInfo &info) |
| 84 | { |
| 85 | unsigned int workload_index = info.thread_id; |
| 86 | do |
| 87 | { |
| 88 | ARM_COMPUTE_ERROR_ON(workload_index >= workloads.size()); |
| 89 | workloads[workload_index](info); |
| 90 | } |
| 91 | while(feeder.get_next(workload_index)); |
| 92 | } |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 93 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 94 | void set_thread_affinity(int core_id) |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 95 | { |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 96 | if(core_id < 0) |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 97 | { |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 98 | return; |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Georgios Pinitas | 4551403 | 2020-12-30 00:03:09 +0000 | [diff] [blame] | 101 | #if !defined(__APPLE__) |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 102 | cpu_set_t set; |
| 103 | CPU_ZERO(&set); |
| 104 | CPU_SET(core_id, &set); |
Georgios Pinitas | 4551403 | 2020-12-30 00:03:09 +0000 | [diff] [blame] | 105 | ARM_COMPUTE_EXIT_ON_MSG(sched_setaffinity(0, sizeof(set), &set), "Error setting thread affinity"); |
| 106 | #endif /* !defined(__APPLE__) */ |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 107 | } |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 108 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 109 | class Thread final |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | { |
| 111 | public: |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 112 | /** Start a new thread |
| 113 | * |
| 114 | * Thread will be pinned to a given core id if value is non-negative |
| 115 | * |
| 116 | * @param[in] core_pin Core id to pin the thread on. If negative no thread pinning will take place |
| 117 | */ |
| 118 | explicit Thread(int core_pin = -1); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 119 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | Thread(const Thread &) = delete; |
| 121 | Thread &operator=(const Thread &) = delete; |
| 122 | Thread(Thread &&) = delete; |
| 123 | Thread &operator=(Thread &&) = delete; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 124 | |
| 125 | /** Destructor. Make the thread join. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | ~Thread(); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 127 | |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 128 | /** Request the worker thread to start executing workloads. |
| 129 | * |
| 130 | * The thread will start by executing workloads[info.thread_id] and will then call the feeder to |
| 131 | * get the index of the following workload to run. |
| 132 | * |
| 133 | * @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] | 134 | * wait() needs to be called to ensure the execution is complete. |
| 135 | */ |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 136 | void start(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 137 | |
| 138 | /** Wait for the current kernel execution to complete. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | void wait(); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 140 | |
| 141 | /** Function ran by the worker thread. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | void worker_thread(); |
| 143 | |
| 144 | private: |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 145 | std::thread _thread{}; |
| 146 | ThreadInfo _info{}; |
| 147 | std::vector<IScheduler::Workload> *_workloads{ nullptr }; |
| 148 | ThreadFeeder *_feeder{ nullptr }; |
| 149 | std::mutex _m{}; |
| 150 | std::condition_variable _cv{}; |
| 151 | bool _wait_for_work{ false }; |
| 152 | bool _job_complete{ true }; |
| 153 | std::exception_ptr _current_exception{ nullptr }; |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 154 | int _core_pin{ -1 }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | }; |
| 156 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 157 | Thread::Thread(int core_pin) |
| 158 | : _core_pin(core_pin) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | _thread = std::thread(&Thread::worker_thread, this); |
| 161 | } |
| 162 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 163 | Thread::~Thread() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 164 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 165 | // Make sure worker thread has ended |
| 166 | if(_thread.joinable()) |
| 167 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 168 | ThreadFeeder feeder; |
| 169 | start(nullptr, feeder, ThreadInfo()); |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 170 | _thread.join(); |
| 171 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 174 | void Thread::start(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 175 | { |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 176 | _workloads = workloads; |
| 177 | _feeder = &feeder; |
| 178 | _info = info; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 179 | { |
| 180 | std::lock_guard<std::mutex> lock(_m); |
| 181 | _wait_for_work = true; |
| 182 | _job_complete = false; |
| 183 | } |
| 184 | _cv.notify_one(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 187 | void Thread::wait() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 189 | { |
| 190 | std::unique_lock<std::mutex> lock(_m); |
| 191 | _cv.wait(lock, [&] { return _job_complete; }); |
| 192 | } |
| 193 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 194 | if(_current_exception) |
| 195 | { |
| 196 | std::rethrow_exception(_current_exception); |
| 197 | } |
| 198 | } |
| 199 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 200 | void Thread::worker_thread() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | { |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 202 | set_thread_affinity(_core_pin); |
| 203 | |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 204 | while(true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | { |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 206 | std::unique_lock<std::mutex> lock(_m); |
| 207 | _cv.wait(lock, [&] { return _wait_for_work; }); |
| 208 | _wait_for_work = false; |
| 209 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | _current_exception = nullptr; |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 211 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 212 | // Time to exit |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 213 | if(_workloads == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | { |
| 215 | return; |
| 216 | } |
| 217 | |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 218 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | try |
| 220 | { |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 221 | #endif /* ARM_COMPUTE_EXCEPTIONS_ENABLED */ |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 222 | process_workloads(*_workloads, *_feeder, _info); |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 223 | |
| 224 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | } |
| 226 | catch(...) |
| 227 | { |
| 228 | _current_exception = std::current_exception(); |
| 229 | } |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 230 | #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 231 | _job_complete = true; |
| 232 | lock.unlock(); |
| 233 | _cv.notify_one(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 234 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 235 | } |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 236 | } //namespace |
| 237 | |
| 238 | struct CPPScheduler::Impl final |
| 239 | { |
| 240 | explicit Impl(unsigned int thread_hint) |
| 241 | : _num_threads(thread_hint), _threads(_num_threads - 1) |
| 242 | { |
| 243 | } |
| 244 | void set_num_threads(unsigned int num_threads, unsigned int thread_hint) |
| 245 | { |
| 246 | _num_threads = num_threads == 0 ? thread_hint : num_threads; |
| 247 | _threads.resize(_num_threads - 1); |
| 248 | } |
| 249 | void set_num_threads_with_affinity(unsigned int num_threads, unsigned int thread_hint, BindFunc func) |
| 250 | { |
| 251 | _num_threads = num_threads == 0 ? thread_hint : num_threads; |
| 252 | |
| 253 | // Set affinity on main thread |
| 254 | set_thread_affinity(func(0, thread_hint)); |
| 255 | |
| 256 | // Set affinity on worked threads |
| 257 | _threads.clear(); |
| 258 | for(auto i = 1U; i < _num_threads; ++i) |
| 259 | { |
| 260 | _threads.emplace_back(func(i, thread_hint)); |
| 261 | } |
| 262 | } |
| 263 | unsigned int num_threads() const |
| 264 | { |
| 265 | return _num_threads; |
| 266 | } |
| 267 | |
| 268 | void run_workloads(std::vector<IScheduler::Workload> &workloads); |
| 269 | |
| 270 | unsigned int _num_threads; |
| 271 | std::list<Thread> _threads; |
| 272 | arm_compute::Mutex _run_workloads_mutex{}; |
| 273 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 274 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 275 | /* |
| 276 | * This singleton has been deprecated and will be removed in the next release |
| 277 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 278 | CPPScheduler &CPPScheduler::get() |
| 279 | { |
| 280 | static CPPScheduler scheduler; |
| 281 | return scheduler; |
| 282 | } |
| 283 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 284 | CPPScheduler::CPPScheduler() |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 285 | : _impl(std::make_unique<Impl>(num_threads_hint())) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 286 | { |
| 287 | } |
| 288 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 289 | CPPScheduler::~CPPScheduler() = default; |
| 290 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 291 | void CPPScheduler::set_num_threads(unsigned int num_threads) |
| 292 | { |
Pablo Tello | 2725197 | 2019-09-19 16:39:04 +0100 | [diff] [blame] | 293 | // No changes in the number of threads while current workloads are running |
| 294 | arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex); |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 295 | _impl->set_num_threads(num_threads, num_threads_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Georgios Pinitas | 06e890b | 2020-07-09 18:38:34 +0100 | [diff] [blame] | 298 | void CPPScheduler::set_num_threads_with_affinity(unsigned int num_threads, BindFunc func) |
| 299 | { |
| 300 | // No changes in the number of threads while current workloads are running |
| 301 | arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex); |
| 302 | _impl->set_num_threads_with_affinity(num_threads, num_threads_hint(), func); |
| 303 | } |
| 304 | |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 305 | unsigned int CPPScheduler::num_threads() const |
| 306 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 307 | return _impl->num_threads(); |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 310 | #ifndef DOXYGEN_SKIP_THIS |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 311 | void CPPScheduler::run_workloads(std::vector<IScheduler::Workload> &workloads) |
| 312 | { |
Pablo Tello | 2725197 | 2019-09-19 16:39:04 +0100 | [diff] [blame] | 313 | // Mutex to ensure other threads won't interfere with the setup of the current thread's workloads |
| 314 | // Other thread's workloads will be scheduled after the current thread's workloads have finished |
| 315 | // This is not great because different threads workloads won't run in parallel but at least they |
| 316 | // won't interfere each other and deadlock. |
| 317 | arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex); |
| 318 | const unsigned int num_threads = std::min(_impl->num_threads(), static_cast<unsigned int>(workloads.size())); |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 319 | if(num_threads < 1) |
| 320 | { |
| 321 | return; |
| 322 | } |
| 323 | ThreadFeeder feeder(num_threads, workloads.size()); |
| 324 | ThreadInfo info; |
| 325 | info.cpu_info = &_cpu_info; |
| 326 | info.num_threads = num_threads; |
| 327 | unsigned int t = 0; |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 328 | auto thread_it = _impl->_threads.begin(); |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 329 | for(; t < num_threads - 1; ++t, ++thread_it) |
| 330 | { |
| 331 | info.thread_id = t; |
| 332 | thread_it->start(&workloads, feeder, info); |
| 333 | } |
| 334 | |
| 335 | info.thread_id = t; |
| 336 | process_workloads(workloads, feeder, info); |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 337 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 338 | try |
| 339 | { |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 340 | #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 341 | for(auto &thread : _impl->_threads) |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 342 | { |
| 343 | thread.wait(); |
| 344 | } |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 345 | #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 346 | } |
| 347 | catch(const std::system_error &e) |
| 348 | { |
| 349 | std::cerr << "Caught system_error with code " << e.code() << " meaning " << e.what() << '\n'; |
| 350 | } |
Michalis Spyrou | 323ce0f | 2018-11-30 16:30:43 +0000 | [diff] [blame] | 351 | #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 352 | } |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 353 | #endif /* DOXYGEN_SKIP_THIS */ |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 354 | |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame^] | 355 | 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] | 356 | { |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame^] | 357 | schedule_common(kernel, hints, window, tensors); |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void CPPScheduler::schedule(ICPPKernel *kernel, const Hints &hints) |
| 361 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 362 | ITensorPack tensors; |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame^] | 363 | schedule_common(kernel, hints, kernel->window(), tensors); |
Michalis Spyrou | bcd2352 | 2020-05-21 15:02:36 +0100 | [diff] [blame] | 364 | } |
Moritz Pflanzer | ff06f20 | 2017-09-08 13:48:23 +0100 | [diff] [blame] | 365 | } // namespace arm_compute |