Mike Vitus | 27789c0 | 2018-01-26 13:51:26 -0800 | [diff] [blame] | 1 | // 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 | #ifndef CERES_INTERNAL_THREAD_POOL_H_ |
| 32 | #define CERES_INTERNAL_THREAD_POOL_H_ |
| 33 | |
Mike Vitus | e809cf0 | 2018-04-03 11:06:52 -0700 | [diff] [blame] | 34 | #include <functional> |
Mike Vitus | 27789c0 | 2018-01-26 13:51:26 -0800 | [diff] [blame] | 35 | #include <mutex> |
| 36 | #include <thread> |
| 37 | #include <vector> |
| 38 | |
| 39 | #include "ceres/concurrent_queue.h" |
Taylor Braun-Jones | 3f6d273 | 2020-01-28 12:09:30 -0500 | [diff] [blame] | 40 | #include "ceres/internal/port.h" |
Mike Vitus | 27789c0 | 2018-01-26 13:51:26 -0800 | [diff] [blame] | 41 | |
| 42 | namespace ceres { |
| 43 | namespace internal { |
| 44 | |
| 45 | // A thread-safe thread pool with an unbounded task queue and a resizable number |
Kuang Fangjun | 0d3a84f | 2018-09-22 11:53:42 +0800 | [diff] [blame] | 46 | // of workers. The size of the thread pool can be increased but never decreased |
Mike Vitus | 27789c0 | 2018-01-26 13:51:26 -0800 | [diff] [blame] | 47 | // in order to support the largest number of threads requested. The ThreadPool |
| 48 | // has three states: |
| 49 | // |
| 50 | // (1) The thread pool size is zero. Tasks may be added to the thread pool via |
| 51 | // AddTask but they will not be executed until the thread pool is resized. |
| 52 | // |
| 53 | // (2) The thread pool size is greater than zero. Tasks may be added to the |
| 54 | // thread pool and will be executed as soon as a worker is available. The |
| 55 | // thread pool may be resized while the thread pool is running. |
| 56 | // |
| 57 | // (3) The thread pool is destructing. The thread pool will signal all the |
| 58 | // workers to stop. The workers will finish all of the tasks that have already |
| 59 | // been added to the thread pool. |
| 60 | // |
Taylor Braun-Jones | 3f6d273 | 2020-01-28 12:09:30 -0500 | [diff] [blame] | 61 | class CERES_EXPORT_INTERNAL ThreadPool { |
Mike Vitus | 27789c0 | 2018-01-26 13:51:26 -0800 | [diff] [blame] | 62 | public: |
Alex Stewart | df6e27e | 2018-09-12 18:30:51 +0100 | [diff] [blame] | 63 | // Returns the maximum number of hardware threads. |
| 64 | static int MaxNumThreadsAvailable(); |
| 65 | |
Mike Vitus | 27789c0 | 2018-01-26 13:51:26 -0800 | [diff] [blame] | 66 | // Default constructor with no active threads. We allow instantiating a |
| 67 | // thread pool with no threads to support the use case of single threaded |
| 68 | // Ceres where everything will be executed on the main thread. For single |
| 69 | // threaded execution this has two benefits: avoid any overhead as threads |
| 70 | // are expensive to create, and no unused threads shown in the debugger. |
| 71 | ThreadPool(); |
| 72 | |
Alex Stewart | df6e27e | 2018-09-12 18:30:51 +0100 | [diff] [blame] | 73 | // Instantiates a thread pool with min(MaxNumThreadsAvailable, num_threads) |
Mike Vitus | 27789c0 | 2018-01-26 13:51:26 -0800 | [diff] [blame] | 74 | // number of threads. |
| 75 | explicit ThreadPool(int num_threads); |
| 76 | |
| 77 | // Signals the workers to stop and waits for them to finish any tasks that |
| 78 | // have been scheduled. |
| 79 | ~ThreadPool(); |
| 80 | |
| 81 | // Resizes the thread pool if it is currently less than the requested number |
Alex Stewart | df6e27e | 2018-09-12 18:30:51 +0100 | [diff] [blame] | 82 | // of threads. The thread pool will be resized to min(MaxNumThreadsAvailable, |
Mike Vitus | 27789c0 | 2018-01-26 13:51:26 -0800 | [diff] [blame] | 83 | // num_threads) number of threads. Resize does not support reducing the |
| 84 | // thread pool size. If a smaller number of threads is requested, the thread |
| 85 | // pool remains the same size. The thread pool is reused within Ceres with |
| 86 | // different number of threads, and we need to ensure we can support the |
| 87 | // largest number of threads requested. It is safe to resize the thread pool |
| 88 | // while the workers are executing tasks, and the resizing is guaranteed to |
| 89 | // complete upon return. |
| 90 | void Resize(int num_threads); |
| 91 | |
| 92 | // Adds a task to the queue and wakes up a blocked thread. If the thread pool |
| 93 | // size is greater than zero, then the task will be executed by a currently |
| 94 | // idle thread or when a thread becomes available. If the thread pool has no |
| 95 | // threads, then the task will never be executed and the user should use |
| 96 | // Resize() to create a non-empty thread pool. |
| 97 | void AddTask(const std::function<void()>& func); |
| 98 | |
| 99 | // Returns the current size of the thread pool. |
| 100 | int Size(); |
| 101 | |
| 102 | private: |
| 103 | // Main loop for the threads which blocks on the task queue until work becomes |
| 104 | // available. It will return if and only if Stop has been called. |
| 105 | void ThreadMainLoop(); |
| 106 | |
| 107 | // Signal all the threads to stop. It does not block until the threads are |
| 108 | // finished. |
| 109 | void Stop(); |
| 110 | |
| 111 | // The queue that stores the units of work available for the thread pool. The |
| 112 | // task queue maintains its own thread safety. |
| 113 | ConcurrentQueue<std::function<void()>> task_queue_; |
| 114 | std::vector<std::thread> thread_pool_; |
| 115 | std::mutex thread_pool_mutex_; |
| 116 | }; |
| 117 | |
| 118 | } // namespace internal |
| 119 | } // namespace ceres |
| 120 | |
| 121 | #endif // CERES_INTERNAL_THREAD_POOL_H_ |