Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -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 | // This include must come before any #ifndef check on Ceres compile options. |
| 32 | #include "ceres/internal/port.h" |
| 33 | |
Mike Vitus | f408f89 | 2018-02-22 10:28:39 -0800 | [diff] [blame] | 34 | #if defined(CERES_USE_TBB) || defined(CERES_USE_CXX11_THREADS) |
Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 35 | |
| 36 | #include "ceres/parallel_for.h" |
| 37 | |
Sameer Agarwal | 8c81149 | 2018-02-28 19:41:47 -0800 | [diff] [blame] | 38 | #include <cmath> |
Mike Vitus | f0c3b23 | 2018-02-28 13:08:48 -0800 | [diff] [blame] | 39 | #include <condition_variable> |
| 40 | #include <mutex> |
| 41 | #include <thread> |
Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 42 | #include <vector> |
| 43 | |
Mike Vitus | f408f89 | 2018-02-22 10:28:39 -0800 | [diff] [blame] | 44 | #include "ceres/context_impl.h" |
Mike Vitus | f0c3b23 | 2018-02-28 13:08:48 -0800 | [diff] [blame] | 45 | #include "glog/logging.h" |
Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 46 | #include "gmock/gmock.h" |
| 47 | #include "gtest/gtest.h" |
| 48 | |
| 49 | namespace ceres { |
| 50 | namespace internal { |
| 51 | |
| 52 | using testing::ElementsAreArray; |
Mike Vitus | f0c3b23 | 2018-02-28 13:08:48 -0800 | [diff] [blame] | 53 | using testing::UnorderedElementsAreArray; |
Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 54 | |
| 55 | // Tests the parallel for loop computes the correct result for various number of |
| 56 | // threads. |
| 57 | TEST(ParallelFor, NumThreads) { |
Mike Vitus | f408f89 | 2018-02-22 10:28:39 -0800 | [diff] [blame] | 58 | ContextImpl context; |
| 59 | context.EnsureMinimumThreads(/*num_threads=*/2); |
| 60 | |
Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 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); |
Mike Vitus | f408f89 | 2018-02-22 10:28:39 -0800 | [diff] [blame] | 69 | ParallelFor(&context, 0, size, num_threads, |
Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 70 | [&values](int i) { values[i] = std::sqrt(i); }); |
| 71 | EXPECT_THAT(values, ElementsAreArray(expected_results)); |
| 72 | } |
| 73 | } |
| 74 | |
Mike Vitus | f0c3b23 | 2018-02-28 13:08:48 -0800 | [diff] [blame] | 75 | // Tests the parallel for loop with the thread ID interface computes the correct |
| 76 | // result for various number of threads. |
| 77 | TEST(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 Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 95 | // Tests nested for loops do not result in a deadlock. |
| 96 | TEST(ParallelFor, NestedParallelForDeadlock) { |
Mike Vitus | f408f89 | 2018-02-22 10:28:39 -0800 | [diff] [blame] | 97 | ContextImpl context; |
| 98 | context.EnsureMinimumThreads(/*num_threads=*/2); |
| 99 | |
Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 100 | // Increment each element in the 2D matrix. |
| 101 | std::vector<std::vector<int>> x(3, {1, 2, 3}); |
Mike Vitus | f408f89 | 2018-02-22 10:28:39 -0800 | [diff] [blame] | 102 | ParallelFor(&context, 0, 3, 2, [&x, &context](int i) { |
Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 103 | std::vector<int>& y = x.at(i); |
Mike Vitus | f408f89 | 2018-02-22 10:28:39 -0800 | [diff] [blame] | 104 | ParallelFor(&context, 0, 3, 2, [&y](int j) { ++y.at(j); }); |
Mike Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 105 | }); |
| 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 Vitus | f0c3b23 | 2018-02-28 13:08:48 -0800 | [diff] [blame] | 113 | // Tests nested for loops do not result in a deadlock for the parallel for with |
| 114 | // thread ID interface. |
| 115 | TEST(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 | |
| 132 | TEST(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 Vitus | dc5ea0e | 2018-01-24 15:53:19 -0800 | [diff] [blame] | 161 | } // namespace internal |
| 162 | } // namespace ceres |
| 163 | |
Mike Vitus | f408f89 | 2018-02-22 10:28:39 -0800 | [diff] [blame] | 164 | #endif // defined(CERES_USE_TBB) || defined(CERES_USE_CXX11_THREADS) |