blob: 92dce34c71da256636cc4f6f7597244572733acb [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas53d12272018-02-01 20:23:25 +00002 * Copyright (c) 2016-2018 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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
Moritz Pflanzerff06f202017-09-08 13:48:23 +010032#include <condition_variable>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include <iostream>
Moritz Pflanzerff06f202017-09-08 13:48:23 +010034#include <mutex>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include <system_error>
36#include <thread>
37
Moritz Pflanzerff06f202017-09-08 13:48:23 +010038namespace arm_compute
39{
40class Thread
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
42public:
Moritz Pflanzerff06f202017-09-08 13:48:23 +010043 /** Start a new thread. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 Thread();
Moritz Pflanzerff06f202017-09-08 13:48:23 +010045
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046 Thread(const Thread &) = delete;
47 Thread &operator=(const Thread &) = delete;
48 Thread(Thread &&) = delete;
49 Thread &operator=(Thread &&) = delete;
Moritz Pflanzerff06f202017-09-08 13:48:23 +010050
51 /** Destructor. Make the thread join. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 ~Thread();
Moritz Pflanzerff06f202017-09-08 13:48:23 +010053
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 /** 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 */
Moritz Pflanzerc186b572017-09-07 09:48:04 +010058 void start(ICPPKernel *kernel, const Window &window, const ThreadInfo &info);
Moritz Pflanzerff06f202017-09-08 13:48:23 +010059
60 /** Wait for the current kernel execution to complete. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 void wait();
Moritz Pflanzerff06f202017-09-08 13:48:23 +010062
63 /** Function ran by the worker thread. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 void worker_thread();
65
66private:
Moritz Pflanzerff06f202017-09-08 13:48: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 Barbier6ff3b192017-09-04 18:44:23 +010076};
77
78Thread::Thread()
Moritz Pflanzerff06f202017-09-08 13:48:23 +010079 : _thread(), _window(), _info(), _m(), _cv(), _current_exception(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 _thread = std::thread(&Thread::worker_thread, this);
82}
83
84Thread::~Thread()
85{
Moritz Pflanzerff06f202017-09-08 13:48:23 +010086 // Make sure worker thread has ended
87 if(_thread.joinable())
88 {
89 start(nullptr, Window(), ThreadInfo());
90 _thread.join();
91 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092}
93
Moritz Pflanzerc186b572017-09-07 09:48:04 +010094void Thread::start(ICPPKernel *kernel, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095{
96 _kernel = kernel;
97 _window = window;
Moritz Pflanzerc186b572017-09-07 09:48:04 +010098 _info = info;
Moritz Pflanzerff06f202017-09-08 13:48:23 +010099
100 {
101 std::lock_guard<std::mutex> lock(_m);
102 _wait_for_work = true;
103 _job_complete = false;
104 }
105 _cv.notify_one();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106}
107
108void Thread::wait()
109{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100110 {
111 std::unique_lock<std::mutex> lock(_m);
112 _cv.wait(lock, [&] { return _job_complete; });
113 }
114
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 if(_current_exception)
116 {
117 std::rethrow_exception(_current_exception);
118 }
119}
120
121void Thread::worker_thread()
122{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100123 while(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100125 std::unique_lock<std::mutex> lock(_m);
126 _cv.wait(lock, [&] { return _wait_for_work; });
127 _wait_for_work = false;
128
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 _current_exception = nullptr;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100130
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 // Time to exit
132 if(_kernel == nullptr)
133 {
134 return;
135 }
136
137 try
138 {
139 _window.validate();
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100140 _kernel->run(_window, _info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 }
142 catch(...)
143 {
144 _current_exception = std::current_exception();
145 }
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100146
147 _job_complete = true;
148 lock.unlock();
149 _cv.notify_one();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151}
152
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153CPPScheduler &CPPScheduler::get()
154{
155 static CPPScheduler scheduler;
156 return scheduler;
157}
158
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159CPPScheduler::CPPScheduler()
Georgios Pinitas53d12272018-02-01 20:23:25 +0000160 : _num_threads(num_threads_hint()),
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100161 _threads(_num_threads - 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162{
Pablo Tello7fad9b12018-03-14 17:55:27 +0000163 get_cpu_configuration(_cpu_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164}
165
166void CPPScheduler::set_num_threads(unsigned int num_threads)
167{
Anthony Barbierce876122018-02-22 12:44:15 +0000168 _num_threads = num_threads == 0 ? num_threads_hint() : num_threads;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100169 _threads.resize(_num_threads - 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170}
171
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +0100172unsigned int CPPScheduler::num_threads() const
173{
174 return _num_threads;
175}
176
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177void CPPScheduler::schedule(ICPPKernel *kernel, unsigned int split_dimension)
178{
179 ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
180
181 /** [Scheduler example] */
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100182 ThreadInfo info;
Pablo Tello7fad9b12018-03-14 17:55:27 +0000183 info.cpu_info = &_cpu_info;
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100184
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 const Window &max_window = kernel->window();
186 const unsigned int num_iterations = max_window.num_iterations(split_dimension);
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100187 info.num_threads = std::min(num_iterations, _num_threads);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188
Moritz Pflanzer2fd5d952017-09-24 12:10:46 +0100189 if(num_iterations == 0)
190 {
191 return;
192 }
193
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100194 if(!kernel->is_parallelisable() || info.num_threads == 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 {
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100196 kernel->run(max_window, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197 }
198 else
199 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100200 int t = 0;
201 auto thread_it = _threads.begin();
202
203 for(; t < info.num_threads - 1; ++t, ++thread_it)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 {
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100205 Window win = max_window.split_window(split_dimension, t, info.num_threads);
206 info.thread_id = t;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100207 thread_it->start(kernel, win, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208 }
209
Moritz Pflanzerff06f202017-09-08 13:48: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 Barbier6ff3b192017-09-04 18:44:23 +0100215 try
216 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100217 for(auto &thread : _threads)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100219 thread.wait();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220 }
221 }
222 catch(const std::system_error &e)
223 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100224 std::cerr << "Caught system_error with code " << e.code() << " meaning " << e.what() << '\n';
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225 }
226 }
227 /** [Scheduler example] */
228}
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100229} // namespace arm_compute