blob: 77aa0441446c9a20c25ae4fd21c307b097e311bb [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
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"
30
Moritz Pflanzerff06f202017-09-08 13:48:23 +010031#include <condition_variable>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include <iostream>
Moritz Pflanzerff06f202017-09-08 13:48:23 +010033#include <mutex>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include <system_error>
35#include <thread>
36
Moritz Pflanzerff06f202017-09-08 13:48:23 +010037namespace arm_compute
38{
39class Thread
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
41public:
Moritz Pflanzerff06f202017-09-08 13:48:23 +010042 /** Start a new thread. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 Thread();
Moritz Pflanzerff06f202017-09-08 13:48:23 +010044
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 Thread(const Thread &) = delete;
46 Thread &operator=(const Thread &) = delete;
47 Thread(Thread &&) = delete;
48 Thread &operator=(Thread &&) = delete;
Moritz Pflanzerff06f202017-09-08 13:48:23 +010049
50 /** Destructor. Make the thread join. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 ~Thread();
Moritz Pflanzerff06f202017-09-08 13:48:23 +010052
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 /** Request the worker thread to start executing the given kernel
54 * This function will return as soon as the kernel has been sent to the worker thread.
55 * wait() needs to be called to ensure the execution is complete.
56 */
Moritz Pflanzerc186b572017-09-07 09:48:04 +010057 void start(ICPPKernel *kernel, const Window &window, const ThreadInfo &info);
Moritz Pflanzerff06f202017-09-08 13:48:23 +010058
59 /** Wait for the current kernel execution to complete. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060 void wait();
Moritz Pflanzerff06f202017-09-08 13:48:23 +010061
62 /** Function ran by the worker thread. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 void worker_thread();
64
65private:
Moritz Pflanzerff06f202017-09-08 13:48:23 +010066 std::thread _thread;
67 ICPPKernel *_kernel{ nullptr };
68 Window _window;
69 ThreadInfo _info;
70 std::mutex _m;
71 std::condition_variable _cv;
72 bool _wait_for_work{ false };
73 bool _job_complete{ true };
74 std::exception_ptr _current_exception;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075};
76
77Thread::Thread()
Moritz Pflanzerff06f202017-09-08 13:48:23 +010078 : _thread(), _window(), _info(), _m(), _cv(), _current_exception(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 _thread = std::thread(&Thread::worker_thread, this);
81}
82
83Thread::~Thread()
84{
Moritz Pflanzerff06f202017-09-08 13:48:23 +010085 // Make sure worker thread has ended
86 if(_thread.joinable())
87 {
88 start(nullptr, Window(), ThreadInfo());
89 _thread.join();
90 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091}
92
Moritz Pflanzerc186b572017-09-07 09:48:04 +010093void Thread::start(ICPPKernel *kernel, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094{
95 _kernel = kernel;
96 _window = window;
Moritz Pflanzerc186b572017-09-07 09:48:04 +010097 _info = info;
Moritz Pflanzerff06f202017-09-08 13:48:23 +010098
99 {
100 std::lock_guard<std::mutex> lock(_m);
101 _wait_for_work = true;
102 _job_complete = false;
103 }
104 _cv.notify_one();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105}
106
107void Thread::wait()
108{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100109 {
110 std::unique_lock<std::mutex> lock(_m);
111 _cv.wait(lock, [&] { return _job_complete; });
112 }
113
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 if(_current_exception)
115 {
116 std::rethrow_exception(_current_exception);
117 }
118}
119
120void Thread::worker_thread()
121{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100122 while(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100124 std::unique_lock<std::mutex> lock(_m);
125 _cv.wait(lock, [&] { return _wait_for_work; });
126 _wait_for_work = false;
127
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 _current_exception = nullptr;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100129
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 // Time to exit
131 if(_kernel == nullptr)
132 {
133 return;
134 }
135
136 try
137 {
138 _window.validate();
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100139 _kernel->run(_window, _info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 }
141 catch(...)
142 {
143 _current_exception = std::current_exception();
144 }
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100145
146 _job_complete = true;
147 lock.unlock();
148 _cv.notify_one();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150}
151
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152CPPScheduler &CPPScheduler::get()
153{
154 static CPPScheduler scheduler;
155 return scheduler;
156}
157
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158CPPScheduler::CPPScheduler()
159 : _num_threads(std::thread::hardware_concurrency()),
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100160 _threads(_num_threads - 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161{
162}
163
164void CPPScheduler::set_num_threads(unsigned int num_threads)
165{
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100166 _num_threads = num_threads == 0 ? std::thread::hardware_concurrency() : num_threads;
167 _threads.resize(_num_threads - 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168}
169
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +0100170unsigned int CPPScheduler::num_threads() const
171{
172 return _num_threads;
173}
174
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175void CPPScheduler::schedule(ICPPKernel *kernel, unsigned int split_dimension)
176{
177 ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
178
179 /** [Scheduler example] */
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100180 ThreadInfo info;
181 info.cpu = _target;
182
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 const Window &max_window = kernel->window();
184 const unsigned int num_iterations = max_window.num_iterations(split_dimension);
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100185 info.num_threads = std::min(num_iterations, _num_threads);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
Moritz Pflanzer2fd5d952017-09-24 12:10:46 +0100187 if(num_iterations == 0)
188 {
189 return;
190 }
191
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100192 if(!kernel->is_parallelisable() || info.num_threads == 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 {
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100194 kernel->run(max_window, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 }
196 else
197 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100198 int t = 0;
199 auto thread_it = _threads.begin();
200
201 for(; t < info.num_threads - 1; ++t, ++thread_it)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 {
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100203 Window win = max_window.split_window(split_dimension, t, info.num_threads);
204 info.thread_id = t;
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100205 thread_it->start(kernel, win, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206 }
207
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100208 // Run last part on main thread
209 Window win = max_window.split_window(split_dimension, t, info.num_threads);
210 info.thread_id = t;
211 kernel->run(win, info);
212
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 try
214 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100215 for(auto &thread : _threads)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100217 thread.wait();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218 }
219 }
220 catch(const std::system_error &e)
221 {
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100222 std::cerr << "Caught system_error with code " << e.code() << " meaning " << e.what() << '\n';
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223 }
224 }
225 /** [Scheduler example] */
226}
Moritz Pflanzerff06f202017-09-08 13:48:23 +0100227} // namespace arm_compute