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