blob: e39f673dc41c3aee456b6b622c170f5521d7707e [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
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080034#ifdef CERES_USE_CXX_THREADS
Austin Schuh70cc9552019-01-21 19:46:48 -080035
36#include <chrono>
37#include <condition_variable>
38#include <mutex>
39#include <thread>
40
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080041#include "ceres/thread_pool.h"
42#include "glog/logging.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080043#include "gmock/gmock.h"
44#include "gtest/gtest.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080045
46namespace ceres {
47namespace internal {
48
49// Adds a number of tasks to the thread pool and ensures they all run.
50TEST(ThreadPool, AddTask) {
51 int value = 0;
52 const int num_tasks = 100;
53 {
54 ThreadPool thread_pool(2);
55
56 std::condition_variable condition;
57 std::mutex mutex;
58
59 for (int i = 0; i < num_tasks; ++i) {
60 thread_pool.AddTask([&]() {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080061 std::lock_guard<std::mutex> lock(mutex);
62 ++value;
63 condition.notify_all();
64 });
Austin Schuh70cc9552019-01-21 19:46:48 -080065 }
66
67 std::unique_lock<std::mutex> lock(mutex);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080068 condition.wait(lock, [&]() { return value == num_tasks; });
Austin Schuh70cc9552019-01-21 19:46:48 -080069 }
70
71 EXPECT_EQ(num_tasks, value);
72}
73
74// Adds a number of tasks to the queue and resizes the thread pool while the
75// threads are executing their work.
76TEST(ThreadPool, ResizingDuringExecution) {
77 int value = 0;
78
79 const int num_tasks = 100;
80
81 // Run this test in a scope to delete the thread pool and all of the threads
82 // are stopped.
83 {
84 ThreadPool thread_pool(/*num_threads=*/2);
85
86 std::condition_variable condition;
87 std::mutex mutex;
88
89 // Acquire a lock on the mutex to prevent the threads from finishing their
90 // execution so we can test resizing the thread pool while the workers are
91 // executing a task.
92 std::unique_lock<std::mutex> lock(mutex);
93
94 // The same task for all of the workers to execute.
95 auto task = [&]() {
96 // This will block until the mutex is released inside the condition
97 // variable.
98 std::lock_guard<std::mutex> lock(mutex);
99 ++value;
100 condition.notify_all();
101 };
102
103 // Add the initial set of tasks to run.
104 for (int i = 0; i < num_tasks / 2; ++i) {
105 thread_pool.AddTask(task);
106 }
107
108 // Resize the thread pool while tasks are executing.
109 thread_pool.Resize(/*num_threads=*/3);
110
111 // Add more tasks to the thread pool to guarantee these are also completed.
112 for (int i = 0; i < num_tasks / 2; ++i) {
113 thread_pool.AddTask(task);
114 }
115
116 // Unlock the mutex to unblock all of the threads and wait until all of the
117 // tasks are completed.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800118 condition.wait(lock, [&]() { return value == num_tasks; });
Austin Schuh70cc9552019-01-21 19:46:48 -0800119 }
120
121 EXPECT_EQ(num_tasks, value);
122}
123
124// Tests the destructor will wait until all running tasks are finished before
125// destructing the thread pool.
126TEST(ThreadPool, Destructor) {
127 // Ensure the hardware supports more than 1 thread to ensure the test will
128 // pass.
129 const int num_hardware_threads = std::thread::hardware_concurrency();
130 if (num_hardware_threads <= 1) {
131 LOG(ERROR)
132 << "Test not supported, the hardware does not support threading.";
133 return;
134 }
135
136 std::condition_variable condition;
137 std::mutex mutex;
138 // Lock the mutex to ensure the tasks are blocked.
139 std::unique_lock<std::mutex> master_lock(mutex);
140 int value = 0;
141
142 // Create a thread that will instantiate and delete the thread pool. This is
143 // required because we need to block on the thread pool being deleted and
144 // signal the tasks to finish.
145 std::thread thread([&]() {
146 ThreadPool thread_pool(/*num_threads=*/2);
147
148 for (int i = 0; i < 100; ++i) {
149 thread_pool.AddTask([&]() {
150 // This will block until the mutex is released inside the condition
151 // variable.
152 std::lock_guard<std::mutex> lock(mutex);
153 ++value;
154 condition.notify_all();
155 });
156 }
157 // The thread pool should be deleted.
158 });
159
160 // Give the thread pool time to start, add all the tasks, and then delete
161 // itself.
162 std::this_thread::sleep_for(std::chrono::milliseconds(500));
163
164 // Unlock the tasks.
165 master_lock.unlock();
166
167 // Wait for the thread to complete.
168 thread.join();
169
170 EXPECT_EQ(100, value);
171}
172
173TEST(ThreadPool, Resize) {
174 // Ensure the hardware supports more than 1 thread to ensure the test will
175 // pass.
176 const int num_hardware_threads = std::thread::hardware_concurrency();
177 if (num_hardware_threads <= 1) {
178 LOG(ERROR)
179 << "Test not supported, the hardware does not support threading.";
180 return;
181 }
182
183 ThreadPool thread_pool(1);
184
185 EXPECT_EQ(1, thread_pool.Size());
186
187 thread_pool.Resize(2);
188
189 EXPECT_EQ(2, thread_pool.Size());
190
191 // Try reducing the thread pool size and verify it stays the same size.
192 thread_pool.Resize(1);
193 EXPECT_EQ(2, thread_pool.Size());
194}
195
196} // namespace internal
197} // namespace ceres
198
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800199#endif // CERES_USE_CXX_THREADS