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