blob: 2de48657d7cef40106d89fbb3785540036e14d9d [file] [log] [blame]
Mike Vitusdc5ea0e2018-01-24 15:53:19 -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
Mike Vitusf408f892018-02-22 10:28:39 -080034#if defined(CERES_USE_TBB) || defined(CERES_USE_CXX11_THREADS)
Mike Vitusdc5ea0e2018-01-24 15:53:19 -080035
36#include "ceres/parallel_for.h"
37
Sameer Agarwal8c811492018-02-28 19:41:47 -080038#include <cmath>
Mike Vitusf0c3b232018-02-28 13:08:48 -080039#include <condition_variable>
40#include <mutex>
41#include <thread>
Mike Vitusdc5ea0e2018-01-24 15:53:19 -080042#include <vector>
43
Mike Vitusf408f892018-02-22 10:28:39 -080044#include "ceres/context_impl.h"
Mike Vitusf0c3b232018-02-28 13:08:48 -080045#include "glog/logging.h"
Mike Vitusdc5ea0e2018-01-24 15:53:19 -080046#include "gmock/gmock.h"
47#include "gtest/gtest.h"
48
49namespace ceres {
50namespace internal {
51
52using testing::ElementsAreArray;
Mike Vitusf0c3b232018-02-28 13:08:48 -080053using testing::UnorderedElementsAreArray;
Mike Vitusdc5ea0e2018-01-24 15:53:19 -080054
55// Tests the parallel for loop computes the correct result for various number of
56// threads.
57TEST(ParallelFor, NumThreads) {
Mike Vitusf408f892018-02-22 10:28:39 -080058 ContextImpl context;
59 context.EnsureMinimumThreads(/*num_threads=*/2);
60
Mike Vitusdc5ea0e2018-01-24 15:53:19 -080061 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);
Mike Vitusf408f892018-02-22 10:28:39 -080069 ParallelFor(&context, 0, size, num_threads,
Mike Vitusdc5ea0e2018-01-24 15:53:19 -080070 [&values](int i) { values[i] = std::sqrt(i); });
71 EXPECT_THAT(values, ElementsAreArray(expected_results));
72 }
73}
74
Mike Vitusf0c3b232018-02-28 13:08:48 -080075// Tests the parallel for loop with the thread ID interface computes the correct
76// result for various number of threads.
77TEST(ParallelForWithThreadId, NumThreads) {
78 ContextImpl context;
79 context.EnsureMinimumThreads(/*num_threads=*/2);
80
81 const int size = 16;
82 std::vector<int> expected_results(size, 0);
83 for (int i = 0; i < size; ++i) {
84 expected_results[i] = std::sqrt(i);
85 }
86
87 for (int num_threads = 1; num_threads <= 8; ++num_threads) {
88 std::vector<int> values(size, 0);
89 ParallelFor(&context, 0, size, num_threads,
90 [&values](int thread_id, int i) { values[i] = std::sqrt(i); });
91 EXPECT_THAT(values, ElementsAreArray(expected_results));
92 }
93}
94
Mike Vitusdc5ea0e2018-01-24 15:53:19 -080095// Tests nested for loops do not result in a deadlock.
96TEST(ParallelFor, NestedParallelForDeadlock) {
Mike Vitusf408f892018-02-22 10:28:39 -080097 ContextImpl context;
98 context.EnsureMinimumThreads(/*num_threads=*/2);
99
Mike Vitusdc5ea0e2018-01-24 15:53:19 -0800100 // Increment each element in the 2D matrix.
101 std::vector<std::vector<int>> x(3, {1, 2, 3});
Mike Vitusf408f892018-02-22 10:28:39 -0800102 ParallelFor(&context, 0, 3, 2, [&x, &context](int i) {
Mike Vitusdc5ea0e2018-01-24 15:53:19 -0800103 std::vector<int>& y = x.at(i);
Mike Vitusf408f892018-02-22 10:28:39 -0800104 ParallelFor(&context, 0, 3, 2, [&y](int j) { ++y.at(j); });
Mike Vitusdc5ea0e2018-01-24 15:53:19 -0800105 });
106
107 const std::vector<int> results = {2, 3, 4};
108 for (const std::vector<int>& value : x) {
109 EXPECT_THAT(value, ElementsAreArray(results));
110 }
111}
112
Mike Vitusf0c3b232018-02-28 13:08:48 -0800113// Tests nested for loops do not result in a deadlock for the parallel for with
114// thread ID interface.
115TEST(ParallelForWithThreadId, NestedParallelForDeadlock) {
116 ContextImpl context;
117 context.EnsureMinimumThreads(/*num_threads=*/2);
118
119 // Increment each element in the 2D matrix.
120 std::vector<std::vector<int>> x(3, {1, 2, 3});
121 ParallelFor(&context, 0, 3, 2, [&x, &context](int thread_id, int i) {
122 std::vector<int>& y = x.at(i);
123 ParallelFor(&context, 0, 3, 2, [&y](int thread_id, int j) { ++y.at(j); });
124 });
125
126 const std::vector<int> results = {2, 3, 4};
127 for (const std::vector<int>& value : x) {
128 EXPECT_THAT(value, ElementsAreArray(results));
129 }
130}
131
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
Mike Vitusdc5ea0e2018-01-24 15:53:19 -0800161} // namespace internal
162} // namespace ceres
163
Mike Vitusf408f892018-02-22 10:28:39 -0800164#endif // defined(CERES_USE_TBB) || defined(CERES_USE_CXX11_THREADS)