blob: 991da3091c35c8dbb10e55b7e3091d0f3528da4a [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2018 Google Inc. All rights reserved.
3// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: vitus@google.com (Michael Vitus)
30
31// This include must come before any #ifndef check on Ceres compile options.
32#include "ceres/internal/port.h"
33
34#ifdef CERES_USE_CXX11_THREADS
35
36#include "ceres/thread_pool.h"
37
38#include <cmath>
39#include <limits>
40
41namespace ceres {
42namespace internal {
43namespace {
44
45// Constrain the total number of threads to the amount the hardware can support.
46int GetNumAllowedThreads(int requested_num_threads) {
47 return std::min(requested_num_threads, ThreadPool::MaxNumThreadsAvailable());
48}
49
50} // namespace
51
52int ThreadPool::MaxNumThreadsAvailable() {
53 const int num_hardware_threads = std::thread::hardware_concurrency();
54 // hardware_concurrency() can return 0 if the value is not well defined or not
55 // computable.
56 return num_hardware_threads == 0
57 ? std::numeric_limits<int>::max()
58 : num_hardware_threads;
59}
60
61ThreadPool::ThreadPool() { }
62
63ThreadPool::ThreadPool(int num_threads) {
64 Resize(num_threads);
65}
66
67ThreadPool::~ThreadPool() {
68 std::lock_guard<std::mutex> lock(thread_pool_mutex_);
69 // Signal the thread workers to stop and wait for them to finish all scheduled
70 // tasks.
71 Stop();
72 for (std::thread& thread : thread_pool_) {
73 thread.join();
74 }
75}
76
77void ThreadPool::Resize(int num_threads) {
78 std::lock_guard<std::mutex> lock(thread_pool_mutex_);
79
80 const int num_current_threads = thread_pool_.size();
81 if (num_current_threads >= num_threads) {
82 return;
83 }
84
85 const int create_num_threads =
86 GetNumAllowedThreads(num_threads) - num_current_threads;
87
88 for (int i = 0; i < create_num_threads; ++i) {
89 thread_pool_.push_back(std::thread(&ThreadPool::ThreadMainLoop, this));
90 }
91}
92
93void ThreadPool::AddTask(const std::function<void()>& func) {
94 task_queue_.Push(func);
95}
96
97int ThreadPool::Size() {
98 std::lock_guard<std::mutex> lock(thread_pool_mutex_);
99 return thread_pool_.size();
100}
101
102void ThreadPool::ThreadMainLoop() {
103 std::function<void()> task;
104 while (task_queue_.Wait(&task)) {
105 task();
106 }
107}
108
109void ThreadPool::Stop() {
110 task_queue_.StopWaiters();
111}
112
113} // namespace internal
114} // namespace ceres
115
116#endif // CERES_USE_CXX11_THREADS