Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 31 | #include "ceres/thread_pool.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 32 | |
| 33 | #include <cmath> |
| 34 | #include <limits> |
| 35 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 36 | #include "ceres/internal/config.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 37 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 38 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 39 | namespace { |
| 40 | |
| 41 | // Constrain the total number of threads to the amount the hardware can support. |
| 42 | int GetNumAllowedThreads(int requested_num_threads) { |
| 43 | return std::min(requested_num_threads, ThreadPool::MaxNumThreadsAvailable()); |
| 44 | } |
| 45 | |
| 46 | } // namespace |
| 47 | |
| 48 | int ThreadPool::MaxNumThreadsAvailable() { |
| 49 | const int num_hardware_threads = std::thread::hardware_concurrency(); |
| 50 | // hardware_concurrency() can return 0 if the value is not well defined or not |
| 51 | // computable. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 52 | return num_hardware_threads == 0 ? std::numeric_limits<int>::max() |
| 53 | : num_hardware_threads; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 56 | ThreadPool::ThreadPool() = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 57 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 58 | ThreadPool::ThreadPool(int num_threads) { Resize(num_threads); } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 59 | |
| 60 | ThreadPool::~ThreadPool() { |
| 61 | std::lock_guard<std::mutex> lock(thread_pool_mutex_); |
| 62 | // Signal the thread workers to stop and wait for them to finish all scheduled |
| 63 | // tasks. |
| 64 | Stop(); |
| 65 | for (std::thread& thread : thread_pool_) { |
| 66 | thread.join(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void ThreadPool::Resize(int num_threads) { |
| 71 | std::lock_guard<std::mutex> lock(thread_pool_mutex_); |
| 72 | |
| 73 | const int num_current_threads = thread_pool_.size(); |
| 74 | if (num_current_threads >= num_threads) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | const int create_num_threads = |
| 79 | GetNumAllowedThreads(num_threads) - num_current_threads; |
| 80 | |
| 81 | for (int i = 0; i < create_num_threads; ++i) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 82 | thread_pool_.emplace_back(&ThreadPool::ThreadMainLoop, this); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | void ThreadPool::AddTask(const std::function<void()>& func) { |
| 87 | task_queue_.Push(func); |
| 88 | } |
| 89 | |
| 90 | int ThreadPool::Size() { |
| 91 | std::lock_guard<std::mutex> lock(thread_pool_mutex_); |
| 92 | return thread_pool_.size(); |
| 93 | } |
| 94 | |
| 95 | void ThreadPool::ThreadMainLoop() { |
| 96 | std::function<void()> task; |
| 97 | while (task_queue_.Wait(&task)) { |
| 98 | task(); |
| 99 | } |
| 100 | } |
| 101 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 102 | void ThreadPool::Stop() { task_queue_.StopWaiters(); } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 103 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 104 | } // namespace ceres::internal |