blob: 04e5783a0044088ccf2e4b10f7d2c0659fee04ee [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#include "ceres/parallel_for.h"
35
36#include <cmath>
37#include <condition_variable>
38#include <mutex>
39#include <thread>
40#include <vector>
41
42#include "ceres/context_impl.h"
43#include "glog/logging.h"
44#include "gmock/gmock.h"
45#include "gtest/gtest.h"
46
47namespace ceres {
48namespace internal {
49
50using testing::ElementsAreArray;
51using testing::UnorderedElementsAreArray;
52
53// Tests the parallel for loop computes the correct result for various number of
54// threads.
55TEST(ParallelFor, NumThreads) {
56 ContextImpl context;
57 context.EnsureMinimumThreads(/*num_threads=*/2);
58
59 const int size = 16;
60 std::vector<int> expected_results(size, 0);
61 for (int i = 0; i < size; ++i) {
62 expected_results[i] = std::sqrt(i);
63 }
64
65 for (int num_threads = 1; num_threads <= 8; ++num_threads) {
66 std::vector<int> values(size, 0);
67 ParallelFor(&context, 0, size, num_threads,
68 [&values](int i) { values[i] = std::sqrt(i); });
69 EXPECT_THAT(values, ElementsAreArray(expected_results));
70 }
71}
72
73// Tests the parallel for loop with the thread ID interface computes the correct
74// result for various number of threads.
75TEST(ParallelForWithThreadId, NumThreads) {
76 ContextImpl context;
77 context.EnsureMinimumThreads(/*num_threads=*/2);
78
79 const int size = 16;
80 std::vector<int> expected_results(size, 0);
81 for (int i = 0; i < size; ++i) {
82 expected_results[i] = std::sqrt(i);
83 }
84
85 for (int num_threads = 1; num_threads <= 8; ++num_threads) {
86 std::vector<int> values(size, 0);
87 ParallelFor(&context, 0, size, num_threads,
88 [&values](int thread_id, int i) { values[i] = std::sqrt(i); });
89 EXPECT_THAT(values, ElementsAreArray(expected_results));
90 }
91}
92
93// Tests nested for loops do not result in a deadlock.
94TEST(ParallelFor, NestedParallelForDeadlock) {
95 ContextImpl context;
96 context.EnsureMinimumThreads(/*num_threads=*/2);
97
98 // Increment each element in the 2D matrix.
99 std::vector<std::vector<int>> x(3, {1, 2, 3});
100 ParallelFor(&context, 0, 3, 2, [&x, &context](int i) {
101 std::vector<int>& y = x.at(i);
102 ParallelFor(&context, 0, 3, 2, [&y](int j) { ++y.at(j); });
103 });
104
105 const std::vector<int> results = {2, 3, 4};
106 for (const std::vector<int>& value : x) {
107 EXPECT_THAT(value, ElementsAreArray(results));
108 }
109}
110
111// Tests nested for loops do not result in a deadlock for the parallel for with
112// thread ID interface.
113TEST(ParallelForWithThreadId, NestedParallelForDeadlock) {
114 ContextImpl context;
115 context.EnsureMinimumThreads(/*num_threads=*/2);
116
117 // Increment each element in the 2D matrix.
118 std::vector<std::vector<int>> x(3, {1, 2, 3});
119 ParallelFor(&context, 0, 3, 2, [&x, &context](int thread_id, int i) {
120 std::vector<int>& y = x.at(i);
121 ParallelFor(&context, 0, 3, 2, [&y](int thread_id, int j) { ++y.at(j); });
122 });
123
124 const std::vector<int> results = {2, 3, 4};
125 for (const std::vector<int>& value : x) {
126 EXPECT_THAT(value, ElementsAreArray(results));
127 }
128}
129
130// This test is only valid when multithreading support is enabled.
131#ifndef CERES_NO_THREADS
132TEST(ParallelForWithThreadId, UniqueThreadIds) {
133 // Ensure the hardware supports more than 1 thread to ensure the test will
134 // pass.
135 const int num_hardware_threads = std::thread::hardware_concurrency();
136 if (num_hardware_threads <= 1) {
137 LOG(ERROR)
138 << "Test not supported, the hardware does not support threading.";
139 return;
140 }
141
142 ContextImpl context;
143 context.EnsureMinimumThreads(/*num_threads=*/2);
144 // Increment each element in the 2D matrix.
145 std::vector<int> x(2, -1);
146 std::mutex mutex;
147 std::condition_variable condition;
148 int count = 0;
149 ParallelFor(&context, 0, 2, 2,
150 [&x, &mutex, &condition, &count](int thread_id, int i) {
151 std::unique_lock<std::mutex> lock(mutex);
152 x[i] = thread_id;
153 ++count;
154 condition.notify_all();
155 condition.wait(lock, [&]() { return count == 2; });
156 });
157
158 EXPECT_THAT(x, UnorderedElementsAreArray({0,1}));
159}
160#endif // CERES_NO_THREADS
161
162} // namespace internal
163} // namespace ceres