2 * Copyright (c) 2016, 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/runtime/CPP/CPPScheduler.h"
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"
31 #include <condition_variable>
34 #include <system_error>
42 /** Start a new thread. */
45 Thread(const Thread &) = delete;
46 Thread &operator=(const Thread &) = delete;
47 Thread(Thread &&) = delete;
48 Thread &operator=(Thread &&) = delete;
50 /** Destructor. Make the thread join. */
53 /** 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.
57 void start(ICPPKernel *kernel, const Window &window, const ThreadInfo &info);
59 /** Wait for the current kernel execution to complete. */
62 /** Function ran by the worker thread. */
67 ICPPKernel *_kernel{ nullptr };
71 std::condition_variable _cv;
72 bool _wait_for_work{ false };
73 bool _job_complete{ true };
74 std::exception_ptr _current_exception;
78 : _thread(), _window(), _info(), _m(), _cv(), _current_exception(nullptr)
80 _thread = std::thread(&Thread::worker_thread, this);
85 // Make sure worker thread has ended
86 if(_thread.joinable())
88 start(nullptr, Window(), ThreadInfo());
93 void Thread::start(ICPPKernel *kernel, const Window &window, const ThreadInfo &info)
100 std::lock_guard<std::mutex> lock(_m);
101 _wait_for_work = true;
102 _job_complete = false;
110 std::unique_lock<std::mutex> lock(_m);
111 _cv.wait(lock, [&] { return _job_complete; });
114 if(_current_exception)
116 std::rethrow_exception(_current_exception);
120 void Thread::worker_thread()
124 std::unique_lock<std::mutex> lock(_m);
125 _cv.wait(lock, [&] { return _wait_for_work; });
126 _wait_for_work = false;
128 _current_exception = nullptr;
131 if(_kernel == nullptr)
139 _kernel->run(_window, _info);
143 _current_exception = std::current_exception();
146 _job_complete = true;
152 CPPScheduler &CPPScheduler::get()
154 static CPPScheduler scheduler;
158 CPPScheduler::CPPScheduler()
159 : _num_threads(std::thread::hardware_concurrency()),
160 _threads(_num_threads - 1)
164 void CPPScheduler::set_num_threads(unsigned int num_threads)
166 _num_threads = num_threads == 0 ? std::thread::hardware_concurrency() : num_threads;
167 _threads.resize(_num_threads - 1);
170 unsigned int CPPScheduler::num_threads() const
175 void CPPScheduler::schedule(ICPPKernel *kernel, unsigned int split_dimension)
177 ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
179 /** [Scheduler example] */
181 info.cpu_info = _info;
183 const Window &max_window = kernel->window();
184 const unsigned int num_iterations = max_window.num_iterations(split_dimension);
185 info.num_threads = std::min(num_iterations, _num_threads);
187 if(num_iterations == 0)
192 if(!kernel->is_parallelisable() || info.num_threads == 1)
194 kernel->run(max_window, info);
199 auto thread_it = _threads.begin();
201 for(; t < info.num_threads - 1; ++t, ++thread_it)
203 Window win = max_window.split_window(split_dimension, t, info.num_threads);
205 thread_it->start(kernel, win, info);
208 // Run last part on main thread
209 Window win = max_window.split_window(split_dimension, t, info.num_threads);
211 kernel->run(win, info);
215 for(auto &thread : _threads)
220 catch(const std::system_error &e)
222 std::cerr << "Caught system_error with code " << e.code() << " meaning " << e.what() << '\n';
225 /** [Scheduler example] */
227 } // namespace arm_compute