blob: b07aa8ce1858c00e16f6662966ffe01bce2d3d88 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 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"
29#include "arm_compute/core/Utils.h"
Pablo Tello7fad9b12018-03-14 17:55:27 +000030#include "arm_compute/runtime/CPUUtils.h"
Pablo Tello27251972019-09-19 16:39:04 +010031#include "support/Mutex.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
Anthony Barbierd89940e2018-06-28 13:39:35 +010033#include <atomic>
Moritz Pflanzerff06f202017-09-08 13:48:23 +010034#include <condition_variable>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include <iostream>
Georgios Pinitas12833d02019-07-25 13:31:10 +010036#include <list>
Moritz Pflanzerff06f202017-09-08 13:48:23 +010037#include <mutex>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038#include <system_error>
39#include <thread>
40
Moritz Pflanzerff06f202017-09-08 13:48:23 +010041namespace arm_compute
42{
Anthony Barbier52ecb062018-05-25 13:32:10 +010043namespace
44{
45class ThreadFeeder
46{
47public:
48 /** Constructor
49 *
50 * @param[in] start First value that will be returned by the feeder
51 * @param[in] end End condition (The last value returned by get_next() will be end - 1)
52 */
53 explicit ThreadFeeder(unsigned int start = 0, unsigned int end = 0)
Anthony Barbierd89940e2018-06-28 13:39:35 +010054 : _atomic_counter(start), _end(end)
Anthony Barbier52ecb062018-05-25 13:32:10 +010055 {
56 }
57 /** Return the next element in the range if there is one.
58 *
59 * @param[out] next Will contain the next element if there is one.
60 *
61 * @return False if the end of the range has been reached and next wasn't set.
62 */
63 bool get_next(unsigned int &next)
64 {
Anthony Barbierd89940e2018-06-28 13:39:35 +010065 next = atomic_fetch_add_explicit(&_atomic_counter, 1u, std::memory_order_relaxed);
66 return next < _end;
Anthony Barbier52ecb062018-05-25 13:32:10 +010067 }
68
69private:
Anthony Barbierd89940e2018-06-28 13:39:35 +010070 std::atomic_uint _atomic_counter;
Anthony Barbier52ecb062018-05-25 13:32:10 +010071 const unsigned int _end;
Anthony Barbier52ecb062018-05-25 13:32:10 +010072};
73
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000074/** Given two dimensions and a maxium number of threads to utilise, calcualte the best
75 * combination of threads that fit in (mutliplied together) max_threads.
76 *
77 * This algorithm assumes that work in either of the dimensions is equally difficult
78 * to compute
79 *
80 * @returns [m_nthreads, n_nthreads] A pair of the threads that should be used in each dimension
81 */
82std::pair<unsigned, unsigned> split_2d(unsigned max_threads, std::size_t m, std::size_t n)
83{
84 /*
85 * We want the same ratio of threads in M & N to the ratio of m and n problem size
86 *
87 * Therefore: mt/nt == m/n where mt*nt == max_threads
88 *
89 * max_threads/nt = mt & (max_threads/nt) * (m/n) = nt
90 * nt^2 = max_threads * (m/n)
91 * nt = sqrt( max_threads * (m/n) )
92 */
93 //ratio of m to n in problem dimensions
94 double ratio = m / static_cast<double>(n);
95
96 // nt = sqrt(max_threads * (m / n) )
97 const unsigned adjusted = std::round(
Michalis Spyroubcd23522020-05-21 15:02:36 +010098 std::sqrt(max_threads * ratio));
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000099
100 //find the nearest factor of max_threads
Michalis Spyroubcd23522020-05-21 15:02:36 +0100101 for(unsigned i = 0; i != adjusted; ++i)
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000102 {
103 //try down
104 const unsigned adj_down = adjusted - i;
105 if(max_threads % adj_down == 0)
106 {
107 return { adj_down, max_threads / adj_down };
108 }
109
110 //try up
111 const unsigned adj_up = adjusted + i;
112 if(max_threads % adj_up == 0)
113 {
114 return { adj_up, max_threads / adj_up };
115 }
116 }
117
118 //we didn't find anything so lets bail out with maxes biased to the largest dimension
119 if(m > n)
120 {
Michalis Spyroubcd23522020-05-21 15:02:36 +0100121 return { std::min<unsigned>(m, max_threads), 1 };
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000122 }
123 else
124 {
Michalis Spyroubcd23522020-05-21 15:02:36 +0100125 return { 1, std::min<unsigned>(n, max_threads) };
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000126 }
127}
128
Anthony Barbier52ecb062018-05-25 13:32:10 +0100129/** Execute workloads[info.thread_id] first, then call the feeder to get the index of the next workload to run.
130 *
131 * Will run workloads until the feeder reaches the end of its range.
132 *
133 * @param[in] workloads The array of workloads
134 * @param[in,out] feeder The feeder indicating which workload to execute next.
135 * @param[in] info Threading and CPU info.
136 */
137void process_workloads(std::vector<IScheduler::Workload> &workloads, ThreadFeeder &feeder, const ThreadInfo &info)
138{
139 unsigned int workload_index = info.thread_id;
140 do
141 {
142 ARM_COMPUTE_ERROR_ON(workload_index >= workloads.size());
143 workloads[workload_index](info);
144 }
145 while(feeder.get_next(workload_index));
146}
Anthony Barbier52ecb062018-05-25 13:32:10 +0100147
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100148void set_thread_affinity(int core_id)
Georgios Pinitas12833d02019-07-25 13:31:10 +0100149{
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100150 if(core_id < 0)
Georgios Pinitas12833d02019-07-25 13:31:10 +0100151 {
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100152 return;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100153 }
154
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100155 cpu_set_t set;
156 CPU_ZERO(&set);
157 CPU_SET(core_id, &set);
158 ARM_COMPUTE_EXIT_ON_MSG(sched_setaffinity(0, sizeof(set), &set),
159 "Error setting thread affinity");
160}
Georgios Pinitas12833d02019-07-25 13:31:10 +0100161
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100162class Thread final
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163{
164public:
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100165 /** Start a new thread
166 *
167 * Thread will be pinned to a given core id if value is non-negative
168 *
169 * @param[in] core_pin Core id to pin the thread on. If negative no thread pinning will take place
170 */
171 explicit Thread(int core_pin = -1);
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100172
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 Thread(const Thread &) = delete;
174 Thread &operator=(const Thread &) = delete;
175 Thread(Thread &&) = delete;
176 Thread &operator=(Thread &&) = delete;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100177
178 /** Destructor. Make the thread join. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 ~Thread();
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100180
Anthony Barbier52ecb062018-05-25 13:32:10 +0100181 /** Request the worker thread to start executing workloads.
182 *
183 * The thread will start by executing workloads[info.thread_id] and will then call the feeder to
184 * get the index of the following workload to run.
185 *
186 * @note This function will return as soon as the workloads have been sent to the worker thread.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 * wait() needs to be called to ensure the execution is complete.
188 */
Anthony Barbier52ecb062018-05-25 13:32:10 +0100189 void start(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info);
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100190
191 /** Wait for the current kernel execution to complete. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192 void wait();
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100193
194 /** Function ran by the worker thread. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 void worker_thread();
196
197private:
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 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208};
209
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100210Thread::Thread(int core_pin)
211 : _core_pin(core_pin)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 _thread = std::thread(&Thread::worker_thread, this);
214}
215
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100216Thread::~Thread()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100218 // Make sure worker thread has ended
219 if(_thread.joinable())
220 {
Anthony Barbier52ecb062018-05-25 13:32:10 +0100221 ThreadFeeder feeder;
222 start(nullptr, feeder, ThreadInfo());
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100223 _thread.join();
224 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225}
226
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100227void Thread::start(std::vector<IScheduler::Workload> *workloads, ThreadFeeder &feeder, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228{
Anthony Barbier52ecb062018-05-25 13:32:10 +0100229 _workloads = workloads;
230 _feeder = &feeder;
231 _info = info;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100232 {
233 std::lock_guard<std::mutex> lock(_m);
234 _wait_for_work = true;
235 _job_complete = false;
236 }
237 _cv.notify_one();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238}
239
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100240void Thread::wait()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100242 {
243 std::unique_lock<std::mutex> lock(_m);
244 _cv.wait(lock, [&] { return _job_complete; });
245 }
246
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247 if(_current_exception)
248 {
249 std::rethrow_exception(_current_exception);
250 }
251}
252
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100253void Thread::worker_thread()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100254{
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100255 set_thread_affinity(_core_pin);
256
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100257 while(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100259 std::unique_lock<std::mutex> lock(_m);
260 _cv.wait(lock, [&] { return _wait_for_work; });
261 _wait_for_work = false;
262
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100263 _current_exception = nullptr;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100264
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100265 // Time to exit
Anthony Barbier52ecb062018-05-25 13:32:10 +0100266 if(_workloads == nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267 {
268 return;
269 }
270
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000271#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 try
273 {
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000274#endif /* ARM_COMPUTE_EXCEPTIONS_ENABLED */
Anthony Barbier52ecb062018-05-25 13:32:10 +0100275 process_workloads(*_workloads, *_feeder, _info);
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000276
277#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100278 }
279 catch(...)
280 {
281 _current_exception = std::current_exception();
282 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000283#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100284 _job_complete = true;
285 lock.unlock();
286 _cv.notify_one();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288}
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100289} //namespace
290
291struct CPPScheduler::Impl final
292{
293 explicit Impl(unsigned int thread_hint)
294 : _num_threads(thread_hint), _threads(_num_threads - 1)
295 {
296 }
297 void set_num_threads(unsigned int num_threads, unsigned int thread_hint)
298 {
299 _num_threads = num_threads == 0 ? thread_hint : num_threads;
300 _threads.resize(_num_threads - 1);
301 }
302 void set_num_threads_with_affinity(unsigned int num_threads, unsigned int thread_hint, BindFunc func)
303 {
304 _num_threads = num_threads == 0 ? thread_hint : num_threads;
305
306 // Set affinity on main thread
307 set_thread_affinity(func(0, thread_hint));
308
309 // Set affinity on worked threads
310 _threads.clear();
311 for(auto i = 1U; i < _num_threads; ++i)
312 {
313 _threads.emplace_back(func(i, thread_hint));
314 }
315 }
316 unsigned int num_threads() const
317 {
318 return _num_threads;
319 }
320
321 void run_workloads(std::vector<IScheduler::Workload> &workloads);
322
323 unsigned int _num_threads;
324 std::list<Thread> _threads;
325 arm_compute::Mutex _run_workloads_mutex{};
326};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100327
Georgios Pinitas12833d02019-07-25 13:31:10 +0100328/*
329 * This singleton has been deprecated and will be removed in the next release
330 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331CPPScheduler &CPPScheduler::get()
332{
333 static CPPScheduler scheduler;
334 return scheduler;
335}
336
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100337CPPScheduler::CPPScheduler()
Georgios Pinitas12833d02019-07-25 13:31:10 +0100338 : _impl(support::cpp14::make_unique<Impl>(num_threads_hint()))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100339{
340}
341
Georgios Pinitas12833d02019-07-25 13:31:10 +0100342CPPScheduler::~CPPScheduler() = default;
343
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344void CPPScheduler::set_num_threads(unsigned int num_threads)
345{
Pablo Tello27251972019-09-19 16:39:04 +0100346 // No changes in the number of threads while current workloads are running
347 arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex);
Georgios Pinitas12833d02019-07-25 13:31:10 +0100348 _impl->set_num_threads(num_threads, num_threads_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100349}
350
Georgios Pinitas06e890b2020-07-09 18:38:34 +0100351void CPPScheduler::set_num_threads_with_affinity(unsigned int num_threads, BindFunc func)
352{
353 // No changes in the number of threads while current workloads are running
354 arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex);
355 _impl->set_num_threads_with_affinity(num_threads, num_threads_hint(), func);
356}
357
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +0100358unsigned int CPPScheduler::num_threads() const
359{
Georgios Pinitas12833d02019-07-25 13:31:10 +0100360 return _impl->num_threads();
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +0100361}
362
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000363#ifndef DOXYGEN_SKIP_THIS
Anthony Barbier52ecb062018-05-25 13:32:10 +0100364void CPPScheduler::run_workloads(std::vector<IScheduler::Workload> &workloads)
365{
Pablo Tello27251972019-09-19 16:39:04 +0100366 // Mutex to ensure other threads won't interfere with the setup of the current thread's workloads
367 // Other thread's workloads will be scheduled after the current thread's workloads have finished
368 // This is not great because different threads workloads won't run in parallel but at least they
369 // won't interfere each other and deadlock.
370 arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex);
371 const unsigned int num_threads = std::min(_impl->num_threads(), static_cast<unsigned int>(workloads.size()));
Anthony Barbier52ecb062018-05-25 13:32:10 +0100372 if(num_threads < 1)
373 {
374 return;
375 }
376 ThreadFeeder feeder(num_threads, workloads.size());
377 ThreadInfo info;
378 info.cpu_info = &_cpu_info;
379 info.num_threads = num_threads;
380 unsigned int t = 0;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100381 auto thread_it = _impl->_threads.begin();
Anthony Barbier52ecb062018-05-25 13:32:10 +0100382 for(; t < num_threads - 1; ++t, ++thread_it)
383 {
384 info.thread_id = t;
385 thread_it->start(&workloads, feeder, info);
386 }
387
388 info.thread_id = t;
389 process_workloads(workloads, feeder, info);
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000390#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier52ecb062018-05-25 13:32:10 +0100391 try
392 {
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000393#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Georgios Pinitas12833d02019-07-25 13:31:10 +0100394 for(auto &thread : _impl->_threads)
Anthony Barbier52ecb062018-05-25 13:32:10 +0100395 {
396 thread.wait();
397 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000398#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
Anthony Barbier52ecb062018-05-25 13:32:10 +0100399 }
400 catch(const std::system_error &e)
401 {
402 std::cerr << "Caught system_error with code " << e.code() << " meaning " << e.what() << '\n';
403 }
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000404#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
Anthony Barbier52ecb062018-05-25 13:32:10 +0100405}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000406#endif /* DOXYGEN_SKIP_THIS */
Anthony Barbier52ecb062018-05-25 13:32:10 +0100407
Michalis Spyrouce0c6752020-06-18 10:14:57 +0100408void CPPScheduler::schedule_common(ICPPKernel *kernel, const Hints &hints, const InputTensorMap &inputs, const OutputTensorMap &outputs)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100409{
410 ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
411
Michalis Spyroubcd23522020-05-21 15:02:36 +0100412 const Window &max_window = kernel->window();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000414 if(hints.split_dimension() == IScheduler::split_dimensions_all)
Moritz Pflanzer2fd5d952017-09-24 12:10:46 +0100415 {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000416 /*
417 * if the split dim is size_t max then this signals we should parallelise over
418 * all dimensions
419 */
420 const std::size_t m = max_window.num_iterations(Window::DimX);
421 const std::size_t n = max_window.num_iterations(Window::DimY);
Moritz Pflanzer2fd5d952017-09-24 12:10:46 +0100422
Michalis Spyroubcd23522020-05-21 15:02:36 +0100423 //in c++17 this can be swapped for auto [ m_threads, n_threads ] = split_2d(...
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000424 unsigned m_threads, n_threads;
425 std::tie(m_threads, n_threads) = split_2d(_impl->_num_threads, m, n);
426
427 std::vector<IScheduler::Workload> workloads;
Michalis Spyroubcd23522020-05-21 15:02:36 +0100428 for(unsigned int ni = 0; ni != n_threads; ++ni)
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000429 {
Michalis Spyroubcd23522020-05-21 15:02:36 +0100430 for(unsigned int mi = 0; mi != m_threads; ++mi)
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000431 {
432 workloads.push_back(
Michalis Spyroubcd23522020-05-21 15:02:36 +0100433 [ni, mi, m_threads, n_threads, &max_window, &kernel](const ThreadInfo & info)
434 {
435 //narrow the window to our mi-ni workload
436 Window win = max_window.split_window(Window::DimX, mi, m_threads)
437 .split_window(Window::DimY, ni, n_threads);
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000438
Michalis Spyroubcd23522020-05-21 15:02:36 +0100439 win.validate();
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000440
Michalis Spyroubcd23522020-05-21 15:02:36 +0100441 Window thread_locator;
442 thread_locator.set(Window::DimX, Window::Dimension(mi, m_threads));
443 thread_locator.set(Window::DimY, Window::Dimension(ni, n_threads));
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000444
Michalis Spyroubcd23522020-05-21 15:02:36 +0100445 thread_locator.validate();
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000446
Michalis Spyroubcd23522020-05-21 15:02:36 +0100447 kernel->run_nd(win, info, thread_locator);
448 });
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000449 }
450 }
451 run_workloads(workloads);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100452 }
453 else
454 {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000455 const unsigned int num_iterations = max_window.num_iterations(hints.split_dimension());
456 const unsigned int num_threads = std::min(num_iterations, _impl->_num_threads);
457
458 if(num_iterations == 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100459 {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000460 return;
461 }
462
463 if(!kernel->is_parallelisable() || num_threads == 1)
464 {
465 ThreadInfo info;
466 info.cpu_info = &_cpu_info;
Michalis Spyroubcd23522020-05-21 15:02:36 +0100467 if(inputs.empty())
468 {
469 kernel->run(max_window, info);
470 }
471 else
472 {
473 kernel->run_op(inputs, outputs, max_window, info);
474 }
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000475 }
476 else
477 {
478 unsigned int num_windows = 0;
479 switch(hints.strategy())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100480 {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000481 case StrategyHint::STATIC:
482 num_windows = num_threads;
483 break;
484 case StrategyHint::DYNAMIC:
485 {
486 const unsigned int granule_threshold = (hints.threshold() <= 0) ? num_threads : static_cast<unsigned int>(hints.threshold());
487 // Make sure we don't use some windows which are too small as this might create some contention on the ThreadFeeder
488 num_windows = num_iterations > granule_threshold ? granule_threshold : num_iterations;
489 break;
490 }
491 default:
492 ARM_COMPUTE_ERROR("Unknown strategy");
Anthony Barbier376c85f2018-05-25 14:17:21 +0100493 }
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000494 std::vector<IScheduler::Workload> workloads(num_windows);
495 for(unsigned int t = 0; t < num_windows; t++)
Anthony Barbier376c85f2018-05-25 14:17:21 +0100496 {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000497 //Capture 't' by copy, all the other variables by reference:
Michalis Spyroubcd23522020-05-21 15:02:36 +0100498 workloads[t] = [t, &hints, &max_window, &num_windows, &kernel, &inputs, &outputs](const ThreadInfo & info)
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000499 {
500 Window win = max_window.split_window(hints.split_dimension(), t, num_windows);
501 win.validate();
Michalis Spyroubcd23522020-05-21 15:02:36 +0100502
503 if(inputs.empty())
504 {
505 kernel->run(win, info);
506 }
507 else
508 {
509 kernel->run_op(inputs, outputs, win, info);
510 }
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000511 };
512 }
513 run_workloads(workloads);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100514 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100515 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100516}
Michalis Spyroubcd23522020-05-21 15:02:36 +0100517
Michalis Spyrouce0c6752020-06-18 10:14:57 +0100518void CPPScheduler::schedule_op(ICPPKernel *kernel, const Hints &hints, const InputTensorMap &inputs, const OutputTensorMap &outputs)
Michalis Spyroubcd23522020-05-21 15:02:36 +0100519{
520 schedule_common(kernel, hints, inputs, outputs);
521}
522
523void CPPScheduler::schedule(ICPPKernel *kernel, const Hints &hints)
524{
Michalis Spyrouce0c6752020-06-18 10:14:57 +0100525 const InputTensorMap inputs;
526 OutputTensorMap outputs;
Michalis Spyroubcd23522020-05-21 15:02:36 +0100527 schedule_common(kernel, hints, inputs, outputs);
528}
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100529} // namespace arm_compute