Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Sameer Agarwal | 5a30cae | 2023-09-19 15:29:34 -0700 | [diff] [blame] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #include "ceres/dense_cholesky.h" |
| 32 | |
| 33 | #include <memory> |
| 34 | #include <numeric> |
Sameer Agarwal | c801192 | 2022-07-18 06:43:12 -0700 | [diff] [blame] | 35 | #include <sstream> |
Sameer Agarwal | 5a99e42 | 2022-01-26 13:24:58 -0800 | [diff] [blame] | 36 | #include <string> |
Sameer Agarwal | 3e1cc89 | 2022-08-08 21:13:30 -0700 | [diff] [blame] | 37 | #include <utility> |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
| 40 | #include "Eigen/Dense" |
Sameer Agarwal | 4705159 | 2022-03-12 15:22:19 -0800 | [diff] [blame] | 41 | #include "ceres/internal/config.h" |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 42 | #include "ceres/internal/eigen.h" |
Sameer Agarwal | cb6ad46 | 2022-07-29 15:35:53 -0700 | [diff] [blame] | 43 | #include "ceres/iterative_refiner.h" |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 44 | #include "ceres/linear_solver.h" |
| 45 | #include "glog/logging.h" |
| 46 | #include "gmock/gmock.h" |
| 47 | #include "gtest/gtest.h" |
| 48 | |
Sameer Agarwal | caf614a | 2022-04-21 17:41:10 -0700 | [diff] [blame] | 49 | namespace ceres::internal { |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 50 | |
Sameer Agarwal | d9a3dfb | 2022-07-14 06:56:58 -0700 | [diff] [blame] | 51 | using Param = ::testing::tuple<DenseLinearAlgebraLibraryType, bool>; |
Joydeep Biswas | 88e08cf | 2022-06-04 20:17:06 -0500 | [diff] [blame] | 52 | constexpr bool kMixedPrecision = true; |
| 53 | constexpr bool kFullPrecision = false; |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 54 | |
Sergiu Deitsch | fdfa518 | 2022-02-10 15:29:40 +0100 | [diff] [blame] | 55 | namespace { |
| 56 | |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 57 | std::string ParamInfoToString(testing::TestParamInfo<Param> info) { |
Joydeep Biswas | 88e08cf | 2022-06-04 20:17:06 -0500 | [diff] [blame] | 58 | Param param = info.param; |
| 59 | std::stringstream ss; |
| 60 | ss << DenseLinearAlgebraLibraryTypeToString(::testing::get<0>(param)) << "_" |
| 61 | << (::testing::get<1>(param) ? "MixedPrecision" : "FullPrecision"); |
| 62 | return ss.str(); |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 63 | } |
Sergiu Deitsch | fdfa518 | 2022-02-10 15:29:40 +0100 | [diff] [blame] | 64 | } // namespace |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 65 | |
| 66 | class DenseCholeskyTest : public ::testing::TestWithParam<Param> {}; |
| 67 | |
| 68 | TEST_P(DenseCholeskyTest, FactorAndSolve) { |
| 69 | // TODO(sameeragarwal): Convert these tests into type parameterized tests so |
| 70 | // that we can test the single and double precision solvers. |
| 71 | |
| 72 | using Scalar = double; |
| 73 | using MatrixType = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>; |
| 74 | using VectorType = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>; |
| 75 | |
| 76 | LinearSolver::Options options; |
Joydeep Biswas | 8e08421 | 2022-02-14 20:56:30 -0600 | [diff] [blame] | 77 | ContextImpl context; |
Joydeep Biswas | adda97a | 2022-08-16 00:16:24 -0500 | [diff] [blame] | 78 | #ifndef CERES_NO_CUDA |
Joydeep Biswas | 8e08421 | 2022-02-14 20:56:30 -0600 | [diff] [blame] | 79 | options.context = &context; |
Joydeep Biswas | 8290890 | 2022-08-14 19:14:04 -0500 | [diff] [blame] | 80 | std::string error; |
Joydeep Biswas | 3449296 | 2022-08-17 19:19:11 -0500 | [diff] [blame] | 81 | CHECK(context.InitCuda(&error)) << error; |
Joydeep Biswas | adda97a | 2022-08-16 00:16:24 -0500 | [diff] [blame] | 82 | #endif // CERES_NO_CUDA |
Joydeep Biswas | 88e08cf | 2022-06-04 20:17:06 -0500 | [diff] [blame] | 83 | options.dense_linear_algebra_library_type = ::testing::get<0>(GetParam()); |
| 84 | options.use_mixed_precision_solves = ::testing::get<1>(GetParam()); |
| 85 | const int kNumRefinementSteps = 4; |
| 86 | if (options.use_mixed_precision_solves) { |
| 87 | options.max_num_refinement_iterations = kNumRefinementSteps; |
| 88 | } |
Sameer Agarwal | cb6ad46 | 2022-07-29 15:35:53 -0700 | [diff] [blame] | 89 | auto dense_cholesky = DenseCholesky::Create(options); |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 90 | |
| 91 | const int kNumTrials = 10; |
| 92 | const int kMinNumCols = 1; |
| 93 | const int kMaxNumCols = 10; |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 94 | for (int num_cols = kMinNumCols; num_cols < kMaxNumCols; ++num_cols) { |
| 95 | for (int trial = 0; trial < kNumTrials; ++trial) { |
| 96 | const MatrixType a = MatrixType::Random(num_cols, num_cols); |
| 97 | MatrixType lhs = a.transpose() * a; |
| 98 | lhs += VectorType::Ones(num_cols).asDiagonal(); |
| 99 | Vector x = VectorType::Random(num_cols); |
| 100 | Vector rhs = lhs * x; |
| 101 | Vector actual = Vector::Random(num_cols); |
| 102 | |
| 103 | LinearSolver::Summary summary; |
| 104 | summary.termination_type = dense_cholesky->FactorAndSolve( |
| 105 | num_cols, lhs.data(), rhs.data(), actual.data(), &summary.message); |
Sameer Agarwal | c8493fc | 2022-05-14 14:14:22 -0700 | [diff] [blame] | 106 | EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS); |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 107 | EXPECT_NEAR((x - actual).norm() / x.norm(), |
| 108 | 0.0, |
| 109 | std::numeric_limits<double>::epsilon() * 10) |
| 110 | << "\nexpected: " << x.transpose() |
| 111 | << "\nactual : " << actual.transpose(); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Sameer Agarwal | d9a3dfb | 2022-07-14 06:56:58 -0700 | [diff] [blame] | 116 | INSTANTIATE_TEST_SUITE_P(EigenCholesky, |
| 117 | DenseCholeskyTest, |
| 118 | ::testing::Combine(::testing::Values(EIGEN), |
Sameer Agarwal | cb6ad46 | 2022-07-29 15:35:53 -0700 | [diff] [blame] | 119 | ::testing::Values(kMixedPrecision, |
| 120 | kFullPrecision)), |
Sameer Agarwal | d9a3dfb | 2022-07-14 06:56:58 -0700 | [diff] [blame] | 121 | ParamInfoToString); |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 122 | #ifndef CERES_NO_LAPACK |
Sameer Agarwal | d9a3dfb | 2022-07-14 06:56:58 -0700 | [diff] [blame] | 123 | INSTANTIATE_TEST_SUITE_P(LapackCholesky, |
| 124 | DenseCholeskyTest, |
| 125 | ::testing::Combine(::testing::Values(LAPACK), |
Sameer Agarwal | cb6ad46 | 2022-07-29 15:35:53 -0700 | [diff] [blame] | 126 | ::testing::Values(kMixedPrecision, |
| 127 | kFullPrecision)), |
Sameer Agarwal | d9a3dfb | 2022-07-14 06:56:58 -0700 | [diff] [blame] | 128 | ParamInfoToString); |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 129 | #endif |
Joydeep Biswas | 36d6d86 | 2022-02-03 08:09:10 -0600 | [diff] [blame] | 130 | #ifndef CERES_NO_CUDA |
Sameer Agarwal | d9a3dfb | 2022-07-14 06:56:58 -0700 | [diff] [blame] | 131 | INSTANTIATE_TEST_SUITE_P(CudaCholesky, |
| 132 | DenseCholeskyTest, |
| 133 | ::testing::Combine(::testing::Values(CUDA), |
| 134 | ::testing::Values(kMixedPrecision, |
| 135 | kFullPrecision)), |
| 136 | ParamInfoToString); |
Joydeep Biswas | 36d6d86 | 2022-02-03 08:09:10 -0600 | [diff] [blame] | 137 | #endif |
Joydeep Biswas | 88e08cf | 2022-06-04 20:17:06 -0500 | [diff] [blame] | 138 | |
Sameer Agarwal | cb6ad46 | 2022-07-29 15:35:53 -0700 | [diff] [blame] | 139 | class MockDenseCholesky : public DenseCholesky { |
| 140 | public: |
| 141 | MOCK_METHOD3(Factorize, |
| 142 | LinearSolverTerminationType(int num_cols, |
| 143 | double* lhs, |
| 144 | std::string* message)); |
| 145 | MOCK_METHOD3(Solve, |
| 146 | LinearSolverTerminationType(const double* rhs, |
| 147 | double* solution, |
| 148 | std::string* message)); |
| 149 | }; |
Sameer Agarwal | d9a3dfb | 2022-07-14 06:56:58 -0700 | [diff] [blame] | 150 | |
Sameer Agarwal | cb6ad46 | 2022-07-29 15:35:53 -0700 | [diff] [blame] | 151 | class MockDenseIterativeRefiner : public DenseIterativeRefiner { |
| 152 | public: |
| 153 | MockDenseIterativeRefiner() : DenseIterativeRefiner(1) {} |
| 154 | MOCK_METHOD5(Refine, |
| 155 | void(int num_cols, |
| 156 | const double* lhs, |
| 157 | const double* rhs, |
| 158 | DenseCholesky* dense_cholesky, |
| 159 | double* solution)); |
| 160 | }; |
Sergiu Deitsch | f085166 | 2022-02-17 00:56:27 +0100 | [diff] [blame] | 161 | |
Sameer Agarwal | cb6ad46 | 2022-07-29 15:35:53 -0700 | [diff] [blame] | 162 | using testing::_; |
| 163 | using testing::Return; |
| 164 | |
| 165 | TEST(RefinedDenseCholesky, Factorize) { |
| 166 | auto dense_cholesky = std::make_unique<MockDenseCholesky>(); |
| 167 | auto iterative_refiner = std::make_unique<MockDenseIterativeRefiner>(); |
| 168 | EXPECT_CALL(*dense_cholesky, Factorize(_, _, _)) |
| 169 | .Times(1) |
| 170 | .WillRepeatedly(Return(LinearSolverTerminationType::SUCCESS)); |
| 171 | EXPECT_CALL(*iterative_refiner, Refine(_, _, _, _, _)).Times(0); |
| 172 | RefinedDenseCholesky refined_dense_cholesky(std::move(dense_cholesky), |
| 173 | std::move(iterative_refiner)); |
| 174 | double lhs; |
| 175 | std::string message; |
| 176 | EXPECT_EQ(refined_dense_cholesky.Factorize(1, &lhs, &message), |
| 177 | LinearSolverTerminationType::SUCCESS); |
| 178 | }; |
| 179 | |
| 180 | TEST(RefinedDenseCholesky, FactorAndSolveWithUnsuccessfulFactorization) { |
| 181 | auto dense_cholesky = std::make_unique<MockDenseCholesky>(); |
| 182 | auto iterative_refiner = std::make_unique<MockDenseIterativeRefiner>(); |
| 183 | EXPECT_CALL(*dense_cholesky, Factorize(_, _, _)) |
| 184 | .Times(1) |
| 185 | .WillRepeatedly(Return(LinearSolverTerminationType::FAILURE)); |
| 186 | EXPECT_CALL(*dense_cholesky, Solve(_, _, _)).Times(0); |
| 187 | EXPECT_CALL(*iterative_refiner, Refine(_, _, _, _, _)).Times(0); |
| 188 | RefinedDenseCholesky refined_dense_cholesky(std::move(dense_cholesky), |
| 189 | std::move(iterative_refiner)); |
| 190 | double lhs; |
| 191 | std::string message; |
| 192 | double rhs; |
| 193 | double solution; |
| 194 | EXPECT_EQ( |
| 195 | refined_dense_cholesky.FactorAndSolve(1, &lhs, &rhs, &solution, &message), |
| 196 | LinearSolverTerminationType::FAILURE); |
| 197 | }; |
| 198 | |
| 199 | TEST(RefinedDenseCholesky, FactorAndSolveWithSuccess) { |
| 200 | auto dense_cholesky = std::make_unique<MockDenseCholesky>(); |
| 201 | auto iterative_refiner = std::make_unique<MockDenseIterativeRefiner>(); |
| 202 | EXPECT_CALL(*dense_cholesky, Factorize(_, _, _)) |
| 203 | .Times(1) |
| 204 | .WillRepeatedly(Return(LinearSolverTerminationType::SUCCESS)); |
| 205 | EXPECT_CALL(*dense_cholesky, Solve(_, _, _)) |
| 206 | .Times(1) |
| 207 | .WillRepeatedly(Return(LinearSolverTerminationType::SUCCESS)); |
| 208 | EXPECT_CALL(*iterative_refiner, Refine(_, _, _, _, _)).Times(1); |
| 209 | |
| 210 | RefinedDenseCholesky refined_dense_cholesky(std::move(dense_cholesky), |
| 211 | std::move(iterative_refiner)); |
| 212 | double lhs; |
| 213 | std::string message; |
| 214 | double rhs; |
| 215 | double solution; |
| 216 | EXPECT_EQ( |
| 217 | refined_dense_cholesky.FactorAndSolve(1, &lhs, &rhs, &solution, &message), |
| 218 | LinearSolverTerminationType::SUCCESS); |
| 219 | }; |
Sameer Agarwal | 6d06e9b | 2022-01-21 18:33:16 -0800 | [diff] [blame] | 220 | |
Sameer Agarwal | caf614a | 2022-04-21 17:41:10 -0700 | [diff] [blame] | 221 | } // namespace ceres::internal |