blob: 3bd80eb51d1da72ba610c38cad2ccefe77972073 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas45514032020-12-30 00:03:09 +00002 * Copyright (c) 2016-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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"
SiCongLi3b5981c2021-03-12 12:31:17 +000029#include "arm_compute/core/Log.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Utils.h"
SiCongLi3b5981c2021-03-12 12:31:17 +000031#include "arm_compute/core/utils/misc/Utility.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/runtime/CPUUtils.h"
Pablo Tello27251972019-09-19 16:39:04 +010033#include "support/Mutex.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Anthony Barbierd89940e2018-06-28 13:39:35 +010035#include <atomic>
Moritz Pflanzerff06f202017-09-08 13:48:23 +010036#include <condition_variable>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include <iostream>
Georgios Pinitas12833d02019-07-25 13:31:10 +010038#include <list>
Georgios Pinitas40f51a62020-11-21 03:04:18 +000039#include <memory>
Moritz Pflanzerff06f202017-09-08 13:48:23 +010040#include <mutex>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041#include <system_error>
42#include <thread>
SiCongLi3b5981c2021-03-12 12:31:17 +000043#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
Moritz Pflanzerff06f202017-09-08 13:48:23 +010045namespace arm_compute
46{
Anthony Barbier52ecb062018-05-25 13:32:10 +010047namespace
48{
49class ThreadFeeder
50{
51public:
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 Barbierd89940e2018-06-28 13:39:35 +010058 : _atomic_counter(start), _end(end)
Anthony Barbier52ecb062018-05-25 13:32:10 +010059 {
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 Barbierd89940e2018-06-28 13:39:35 +010069 next = atomic_fetch_add_explicit(&_atomic_counter, 1u, std::memory_order_relaxed);
70 return next < _end;
Anthony Barbier52ecb062018-05-25 13:32:10 +010071 }
72
73private:
Anthony Barbierd89940e2018-06-28 13:39:35 +010074 std::atomic_uint _atomic_counter;
Anthony Barbier52ecb062018-05-25 13:32:10 +010075 const unsigned int _end;
Anthony Barbier52ecb062018-05-25 13:32:10 +010076};
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 */
86void 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 Barbier52ecb062018-05-25 13:32:10 +010096
SiCongLi3b5981c2021-03-12 12:31:17 +000097/** 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 Pinitas06e890b2020-07-09 18:38:34 +0100101void set_thread_affinity(int core_id)
Georgios Pinitas12833d02019-07-25 13:31:10 +0100102{
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100103 if(core_id < 0)
Georgios Pinitas12833d02019-07-25 13:31:10 +0100104 {
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100105 return;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100106 }
107
Georgios Pinitas45514032020-12-30 00:03:09 +0000108#if !defined(__APPLE__)
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100109 cpu_set_t set;
110 CPU_ZERO(&set);
111 CPU_SET(core_id, &set);
Georgios Pinitas45514032020-12-30 00:03:09 +0000112 ARM_COMPUTE_EXIT_ON_MSG(sched_setaffinity(0, sizeof(set), &set), "Error setting thread affinity");
113#endif /* !defined(__APPLE__) */
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100114}
Georgios Pinitas12833d02019-07-25 13:31:10 +0100115
SiCongLi3b5981c2021-03-12 12:31:17 +0000116/** 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 Pinitas06e890b2020-07-09 18:38:34 +0100143class Thread final
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144{
145public:
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100146 /** 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 Pflanzerff06f202017-09-08 13:48:23 +0100153
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 Thread(const Thread &) = delete;
155 Thread &operator=(const Thread &) = delete;
156 Thread(Thread &&) = delete;
157 Thread &operator=(Thread &&) = delete;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100158
159 /** Destructor. Make the thread join. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 ~Thread();
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100161
SiCongLi3b5981c2021-03-12 12:31:17 +0000162 /** Set workloads */
163 void set_workload(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info);
164
Anthony Barbier52ecb062018-05-25 13:32:10 +0100165 /** 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 Barbier6ff3b192017-09-04 18:44:23 +0100171 * wait() needs to be called to ensure the execution is complete.
172 */
SiCongLi3b5981c2021-03-12 12:31:17 +0000173 void start();
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100174
175 /** Wait for the current kernel execution to complete. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 void wait();
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100177
178 /** Function ran by the worker thread. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 void worker_thread();
180
SiCongLi3b5981c2021-03-12 12:31:17 +0000181 /** 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 Barbier6ff3b192017-09-04 18:44:23 +0100197private:
Anthony Barbier52ecb062018-05-25 13:32:10 +0100198 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 Pinitas06e890b2020-07-09 18:38:34 +0100207 int _core_pin{ -1 };
SiCongLi3b5981c2021-03-12 12:31:17 +0000208 std::list<Thread> *_thread_pool{ nullptr };
209 unsigned int _wake_beg{ 0 };
210 unsigned int _wake_end{ 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211};
212
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100213Thread::Thread(int core_pin)
214 : _core_pin(core_pin)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 _thread = std::thread(&Thread::worker_thread, this);
217}
218
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100219Thread::~Thread()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100221 // Make sure worker thread has ended
222 if(_thread.joinable())
223 {
Anthony Barbier52ecb062018-05-25 13:32:10 +0100224 ThreadFeeder feeder;
SiCongLi3b5981c2021-03-12 12:31:17 +0000225 set_workload(nullptr, feeder, ThreadInfo());
226 start();
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100227 _thread.join();
228 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229}
230
SiCongLi3b5981c2021-03-12 12:31:17 +0000231void Thread::set_workload(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100232{
Anthony Barbier52ecb062018-05-25 13:32:10 +0100233 _workloads = workloads;
234 _feeder = &feeder;
235 _info = info;
SiCongLi3b5981c2021-03-12 12:31:17 +0000236}
237
238void Thread::start()
239{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100240 {
241 std::lock_guard<std::mutex> lock(_m);
242 _wait_for_work = true;
243 _job_complete = false;
244 }
245 _cv.notify_one();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246}
247
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100248void Thread::wait()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100250 {
251 std::unique_lock<std::mutex> lock(_m);
252 _cv.wait(lock, [&] { return _job_complete; });
253 }
254
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255 if(_current_exception)
256 {
257 std::rethrow_exception(_current_exception);
258 }
259}
260
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100261void Thread::worker_thread()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262{
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100263 set_thread_affinity(_core_pin);
264
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100265 while(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100266 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100267 std::unique_lock<std::mutex> lock(_m);
268 _cv.wait(lock, [&] { return _wait_for_work; });
269 _wait_for_work = false;
270
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100271 _current_exception = nullptr;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100272
SiCongLi3b5981c2021-03-12 12:31:17 +0000273 // Exit if the worker thread has not been fed with workloads
274 if(_workloads == nullptr || _feeder == nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100275 {
276 return;
277 }
278
SiCongLi3b5981c2021-03-12 12:31:17 +0000279 // 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 Spyrou323ce0f2018-11-30 16:30:43 +0000291#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292 try
293 {
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000294#endif /* ARM_COMPUTE_EXCEPTIONS_ENABLED */
Anthony Barbier52ecb062018-05-25 13:32:10 +0100295 process_workloads(*_workloads, *_feeder, _info);
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000296
297#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100298 }
299 catch(...)
300 {
301 _current_exception = std::current_exception();
302 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000303#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
SiCongLi3b5981c2021-03-12 12:31:17 +0000304 _workloads = nullptr;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100305 _job_complete = true;
306 lock.unlock();
307 _cv.notify_one();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100308 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309}
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100310} //namespace
311
312struct CPPScheduler::Impl final
313{
SiCongLi3b5981c2021-03-12 12:31:17 +0000314 constexpr static unsigned int m_default_wake_fanout = 4;
315 enum class Mode
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100316 {
SiCongLi3b5981c2021-03-12 12:31:17 +0000317 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 Pinitas06e890b2020-07-09 18:38:34 +0100342 }
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);
SiCongLi3b5981c2021-03-12 12:31:17 +0000347 auto_switch_mode(_num_threads);
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100348 }
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 }
SiCongLi3b5981c2021-03-12 12:31:17 +0000362 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 Pinitas06e890b2020-07-09 18:38:34 +0100406 }
407 unsigned int num_threads() const
408 {
409 return _num_threads;
410 }
SiCongLi3b5981c2021-03-12 12:31:17 +0000411 unsigned int wake_fanout() const
412 {
413 return _wake_fanout;
414 }
415 Mode mode() const
416 {
417 return _mode;
418 }
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100419
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{};
SiCongLi3b5981c2021-03-12 12:31:17 +0000425 Mode _mode{ Mode::Linear };
426 ModeToggle _forced_mode{ ModeToggle::None };
427 unsigned int _wake_fanout{ 0 };
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100428};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100429
Georgios Pinitas12833d02019-07-25 13:31:10 +0100430/*
Giorgio Arena5b50f422021-02-17 11:43:05 +0000431 * This singleton has been deprecated and will be removed in future releases
Georgios Pinitas12833d02019-07-25 13:31:10 +0100432 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433CPPScheduler &CPPScheduler::get()
434{
435 static CPPScheduler scheduler;
436 return scheduler;
437}
438
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439CPPScheduler::CPPScheduler()
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000440 : _impl(std::make_unique<Impl>(num_threads_hint()))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100441{
442}
443
Georgios Pinitas12833d02019-07-25 13:31:10 +0100444CPPScheduler::~CPPScheduler() = default;
445
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100446void CPPScheduler::set_num_threads(unsigned int num_threads)
447{
Pablo Tello27251972019-09-19 16:39:04 +0100448 // 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 Pinitas12833d02019-07-25 13:31:10 +0100450 _impl->set_num_threads(num_threads, num_threads_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100451}
452
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100453void 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 Pflanzerd929b9c2017-06-28 10:15:48 +0100460unsigned int CPPScheduler::num_threads() const
461{
Georgios Pinitas12833d02019-07-25 13:31:10 +0100462 return _impl->num_threads();
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +0100463}
464
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000465#ifndef DOXYGEN_SKIP_THIS
Anthony Barbier52ecb062018-05-25 13:32:10 +0100466void CPPScheduler::run_workloads(std::vector<IScheduler::Workload> &workloads)
467{
Pablo Tello27251972019-09-19 16:39:04 +0100468 // 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);
SiCongLi3b5981c2021-03-12 12:31:17 +0000473 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 Barbier52ecb062018-05-25 13:32:10 +0100475 {
476 return;
477 }
SiCongLi3b5981c2021-03-12 12:31:17 +0000478 // 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 Barbier52ecb062018-05-25 13:32:10 +0100496 ThreadInfo info;
497 info.cpu_info = &_cpu_info;
SiCongLi3b5981c2021-03-12 12:31:17 +0000498 info.num_threads = num_threads_to_use;
Anthony Barbier52ecb062018-05-25 13:32:10 +0100499 unsigned int t = 0;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100500 auto thread_it = _impl->_threads.begin();
SiCongLi3b5981c2021-03-12 12:31:17 +0000501 // 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 Barbier52ecb062018-05-25 13:32:10 +0100503 {
504 info.thread_id = t;
SiCongLi3b5981c2021-03-12 12:31:17 +0000505 thread_it->set_workload(&workloads, feeder, info);
Anthony Barbier52ecb062018-05-25 13:32:10 +0100506 }
SiCongLi3b5981c2021-03-12 12:31:17 +0000507 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 Spyrou323ce0f2018-11-30 16:30:43 +0000514#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier52ecb062018-05-25 13:32:10 +0100515 try
516 {
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000517#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
SiCongLi3b5981c2021-03-12 12:31:17 +0000518 thread_it = _impl->_threads.begin();
519 for(unsigned int i = 0; i < num_threads_to_use - 1; ++i, ++thread_it)
Anthony Barbier52ecb062018-05-25 13:32:10 +0100520 {
SiCongLi3b5981c2021-03-12 12:31:17 +0000521 thread_it->wait();
Anthony Barbier52ecb062018-05-25 13:32:10 +0100522 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000523#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier52ecb062018-05-25 13:32:10 +0100524 }
525 catch(const std::system_error &e)
526 {
527 std::cerr << "Caught system_error with code " << e.code() << " meaning " << e.what() << '\n';
528 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000529#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier52ecb062018-05-25 13:32:10 +0100530}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000531#endif /* DOXYGEN_SKIP_THIS */
Anthony Barbier52ecb062018-05-25 13:32:10 +0100532
Sang-Hoon Park0094c022021-01-20 18:16:47 +0000533void CPPScheduler::schedule_op(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors)
Michalis Spyroubcd23522020-05-21 15:02:36 +0100534{
Sang-Hoon Park0094c022021-01-20 18:16:47 +0000535 schedule_common(kernel, hints, window, tensors);
Michalis Spyroubcd23522020-05-21 15:02:36 +0100536}
537
538void CPPScheduler::schedule(ICPPKernel *kernel, const Hints &hints)
539{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100540 ITensorPack tensors;
Sang-Hoon Park0094c022021-01-20 18:16:47 +0000541 schedule_common(kernel, hints, kernel->window(), tensors);
Michalis Spyroubcd23522020-05-21 15:02:36 +0100542}
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100543} // namespace arm_compute