blob: 92dce34c71da256636cc4f6f7597244572733acb [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier871448e2017-03-24 14:54:29 +00003 *
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"
Jenkinsb3a371b2018-05-23 11:36:53 +010030#include "arm_compute/runtime/CPUUtils.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000031
Kaizen8938bd32017-09-28 14:38:23 +010032#include <condition_variable>
Anthony Barbier871448e2017-03-24 14:54:29 +000033#include <iostream>
Kaizen8938bd32017-09-28 14:38:23 +010034#include <mutex>
Anthony Barbier871448e2017-03-24 14:54:29 +000035#include <system_error>
36#include <thread>
37
Kaizen8938bd32017-09-28 14:38:23 +010038namespace arm_compute
39{
40class Thread
Anthony Barbier871448e2017-03-24 14:54:29 +000041{
42public:
Kaizen8938bd32017-09-28 14:38:23 +010043 /** Start a new thread. */
Anthony Barbier871448e2017-03-24 14:54:29 +000044 Thread();
Kaizen8938bd32017-09-28 14:38:23 +010045
Anthony Barbier871448e2017-03-24 14:54:29 +000046 Thread(const Thread &) = delete;
47 Thread &operator=(const Thread &) = delete;
48 Thread(Thread &&) = delete;
49 Thread &operator=(Thread &&) = delete;
Kaizen8938bd32017-09-28 14:38:23 +010050
51 /** Destructor. Make the thread join. */
Anthony Barbier871448e2017-03-24 14:54:29 +000052 ~Thread();
Kaizen8938bd32017-09-28 14:38:23 +010053
Anthony Barbier871448e2017-03-24 14:54:29 +000054 /** Request the worker thread to start executing the given kernel
55 * This function will return as soon as the kernel has been sent to the worker thread.
56 * wait() needs to be called to ensure the execution is complete.
57 */
Kaizen8938bd32017-09-28 14:38:23 +010058 void start(ICPPKernel *kernel, const Window &window, const ThreadInfo &info);
59
60 /** Wait for the current kernel execution to complete. */
Anthony Barbier871448e2017-03-24 14:54:29 +000061 void wait();
Kaizen8938bd32017-09-28 14:38:23 +010062
63 /** Function ran by the worker thread. */
Anthony Barbier871448e2017-03-24 14:54:29 +000064 void worker_thread();
65
66private:
Kaizen8938bd32017-09-28 14:38:23 +010067 std::thread _thread;
68 ICPPKernel *_kernel{ nullptr };
69 Window _window;
70 ThreadInfo _info;
71 std::mutex _m;
72 std::condition_variable _cv;
73 bool _wait_for_work{ false };
74 bool _job_complete{ true };
75 std::exception_ptr _current_exception;
Anthony Barbier871448e2017-03-24 14:54:29 +000076};
77
78Thread::Thread()
Kaizen8938bd32017-09-28 14:38:23 +010079 : _thread(), _window(), _info(), _m(), _cv(), _current_exception(nullptr)
Anthony Barbier871448e2017-03-24 14:54:29 +000080{
Anthony Barbier871448e2017-03-24 14:54:29 +000081 _thread = std::thread(&Thread::worker_thread, this);
82}
83
84Thread::~Thread()
85{
Kaizen8938bd32017-09-28 14:38:23 +010086 // Make sure worker thread has ended
87 if(_thread.joinable())
88 {
89 start(nullptr, Window(), ThreadInfo());
90 _thread.join();
91 }
Anthony Barbier871448e2017-03-24 14:54:29 +000092}
93
Kaizen8938bd32017-09-28 14:38:23 +010094void Thread::start(ICPPKernel *kernel, const Window &window, const ThreadInfo &info)
Anthony Barbier871448e2017-03-24 14:54:29 +000095{
96 _kernel = kernel;
97 _window = window;
Kaizen8938bd32017-09-28 14:38:23 +010098 _info = info;
99
100 {
101 std::lock_guard<std::mutex> lock(_m);
102 _wait_for_work = true;
103 _job_complete = false;
104 }
105 _cv.notify_one();
Anthony Barbier871448e2017-03-24 14:54:29 +0000106}
107
108void Thread::wait()
109{
Kaizen8938bd32017-09-28 14:38:23 +0100110 {
111 std::unique_lock<std::mutex> lock(_m);
112 _cv.wait(lock, [&] { return _job_complete; });
113 }
114
Anthony Barbier871448e2017-03-24 14:54:29 +0000115 if(_current_exception)
116 {
117 std::rethrow_exception(_current_exception);
118 }
119}
120
121void Thread::worker_thread()
122{
Kaizen8938bd32017-09-28 14:38:23 +0100123 while(true)
Anthony Barbier871448e2017-03-24 14:54:29 +0000124 {
Kaizen8938bd32017-09-28 14:38:23 +0100125 std::unique_lock<std::mutex> lock(_m);
126 _cv.wait(lock, [&] { return _wait_for_work; });
127 _wait_for_work = false;
128
Anthony Barbier871448e2017-03-24 14:54:29 +0000129 _current_exception = nullptr;
Kaizen8938bd32017-09-28 14:38:23 +0100130
Anthony Barbier871448e2017-03-24 14:54:29 +0000131 // Time to exit
132 if(_kernel == nullptr)
133 {
134 return;
135 }
136
137 try
138 {
139 _window.validate();
Kaizen8938bd32017-09-28 14:38:23 +0100140 _kernel->run(_window, _info);
Anthony Barbier871448e2017-03-24 14:54:29 +0000141 }
142 catch(...)
143 {
144 _current_exception = std::current_exception();
145 }
Kaizen8938bd32017-09-28 14:38:23 +0100146
147 _job_complete = true;
148 lock.unlock();
149 _cv.notify_one();
Anthony Barbier871448e2017-03-24 14:54:29 +0000150 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000151}
152
Anthony Barbier871448e2017-03-24 14:54:29 +0000153CPPScheduler &CPPScheduler::get()
154{
155 static CPPScheduler scheduler;
156 return scheduler;
157}
158
159CPPScheduler::CPPScheduler()
Anthony Barbier06ea0482018-02-22 15:45:35 +0000160 : _num_threads(num_threads_hint()),
Kaizen8938bd32017-09-28 14:38:23 +0100161 _threads(_num_threads - 1)
Anthony Barbier871448e2017-03-24 14:54:29 +0000162{
Jenkinsb3a371b2018-05-23 11:36:53 +0100163 get_cpu_configuration(_cpu_info);
Anthony Barbier871448e2017-03-24 14:54:29 +0000164}
165
Anthony Barbierdbdab852017-06-23 15:42:00 +0100166void CPPScheduler::set_num_threads(unsigned int num_threads)
Anthony Barbier871448e2017-03-24 14:54:29 +0000167{
Jenkinsc3f34a42018-03-02 12:38:09 +0000168 _num_threads = num_threads == 0 ? num_threads_hint() : num_threads;
Kaizen8938bd32017-09-28 14:38:23 +0100169 _threads.resize(_num_threads - 1);
170}
171
172unsigned int CPPScheduler::num_threads() const
173{
174 return _num_threads;
Anthony Barbier871448e2017-03-24 14:54:29 +0000175}
176
Anthony Barbierdbdab852017-06-23 15:42:00 +0100177void CPPScheduler::schedule(ICPPKernel *kernel, unsigned int split_dimension)
Anthony Barbier871448e2017-03-24 14:54:29 +0000178{
179 ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
180
181 /** [Scheduler example] */
Kaizen8938bd32017-09-28 14:38:23 +0100182 ThreadInfo info;
Jenkinsb3a371b2018-05-23 11:36:53 +0100183 info.cpu_info = &_cpu_info;
Kaizen8938bd32017-09-28 14:38:23 +0100184
Anthony Barbierdbdab852017-06-23 15:42:00 +0100185 const Window &max_window = kernel->window();
186 const unsigned int num_iterations = max_window.num_iterations(split_dimension);
Kaizen8938bd32017-09-28 14:38:23 +0100187 info.num_threads = std::min(num_iterations, _num_threads);
Anthony Barbier871448e2017-03-24 14:54:29 +0000188
Kaizen8938bd32017-09-28 14:38:23 +0100189 if(num_iterations == 0)
Anthony Barbier871448e2017-03-24 14:54:29 +0000190 {
Kaizen8938bd32017-09-28 14:38:23 +0100191 return;
192 }
193
194 if(!kernel->is_parallelisable() || info.num_threads == 1)
195 {
196 kernel->run(max_window, info);
Anthony Barbier871448e2017-03-24 14:54:29 +0000197 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000198 else
199 {
Kaizen8938bd32017-09-28 14:38:23 +0100200 int t = 0;
201 auto thread_it = _threads.begin();
Anthony Barbier871448e2017-03-24 14:54:29 +0000202
Kaizen8938bd32017-09-28 14:38:23 +0100203 for(; t < info.num_threads - 1; ++t, ++thread_it)
204 {
205 Window win = max_window.split_window(split_dimension, t, info.num_threads);
206 info.thread_id = t;
207 thread_it->start(kernel, win, info);
Anthony Barbier871448e2017-03-24 14:54:29 +0000208 }
209
Kaizen8938bd32017-09-28 14:38:23 +0100210 // Run last part on main thread
211 Window win = max_window.split_window(split_dimension, t, info.num_threads);
212 info.thread_id = t;
213 kernel->run(win, info);
214
Anthony Barbier871448e2017-03-24 14:54:29 +0000215 try
216 {
Kaizen8938bd32017-09-28 14:38:23 +0100217 for(auto &thread : _threads)
Anthony Barbier871448e2017-03-24 14:54:29 +0000218 {
Kaizen8938bd32017-09-28 14:38:23 +0100219 thread.wait();
Anthony Barbier871448e2017-03-24 14:54:29 +0000220 }
221 }
222 catch(const std::system_error &e)
223 {
Kaizen8938bd32017-09-28 14:38:23 +0100224 std::cerr << "Caught system_error with code " << e.code() << " meaning " << e.what() << '\n';
Anthony Barbier871448e2017-03-24 14:54:29 +0000225 }
226 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000227 /** [Scheduler example] */
228}
Kaizen8938bd32017-09-28 14:38:23 +0100229} // namespace arm_compute