blob: 1b2e42df9da98f4f2d28e9731677e5559fbb1b38 [file] [log] [blame]
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -08001// Ceres Solver - A fast non-linear least squares minimizer
Sameer Agarwal5a30cae2023-09-19 15:29:34 -07002// Copyright 2023 Google Inc. All rights reserved.
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -08003// 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 Agarwalc8011922022-07-18 06:43:12 -070035#include <sstream>
Sameer Agarwal5a99e422022-01-26 13:24:58 -080036#include <string>
Sameer Agarwal3e1cc892022-08-08 21:13:30 -070037#include <utility>
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -080038#include <vector>
39
40#include "Eigen/Dense"
Sameer Agarwal47051592022-03-12 15:22:19 -080041#include "ceres/internal/config.h"
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -080042#include "ceres/internal/eigen.h"
Sameer Agarwalcb6ad462022-07-29 15:35:53 -070043#include "ceres/iterative_refiner.h"
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -080044#include "ceres/linear_solver.h"
45#include "glog/logging.h"
46#include "gmock/gmock.h"
47#include "gtest/gtest.h"
48
Sameer Agarwalcaf614a2022-04-21 17:41:10 -070049namespace ceres::internal {
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -080050
Sameer Agarwald9a3dfb2022-07-14 06:56:58 -070051using Param = ::testing::tuple<DenseLinearAlgebraLibraryType, bool>;
Joydeep Biswas88e08cf2022-06-04 20:17:06 -050052constexpr bool kMixedPrecision = true;
53constexpr bool kFullPrecision = false;
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -080054
Sergiu Deitschfdfa5182022-02-10 15:29:40 +010055namespace {
56
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -080057std::string ParamInfoToString(testing::TestParamInfo<Param> info) {
Joydeep Biswas88e08cf2022-06-04 20:17:06 -050058 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 Agarwal6d06e9b2022-01-21 18:33:16 -080063}
Sergiu Deitschfdfa5182022-02-10 15:29:40 +010064} // namespace
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -080065
66class DenseCholeskyTest : public ::testing::TestWithParam<Param> {};
67
68TEST_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 Biswas8e084212022-02-14 20:56:30 -060077 ContextImpl context;
Joydeep Biswasadda97a2022-08-16 00:16:24 -050078#ifndef CERES_NO_CUDA
Joydeep Biswas8e084212022-02-14 20:56:30 -060079 options.context = &context;
Joydeep Biswas82908902022-08-14 19:14:04 -050080 std::string error;
Joydeep Biswas34492962022-08-17 19:19:11 -050081 CHECK(context.InitCuda(&error)) << error;
Joydeep Biswasadda97a2022-08-16 00:16:24 -050082#endif // CERES_NO_CUDA
Joydeep Biswas88e08cf2022-06-04 20:17:06 -050083 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 Agarwalcb6ad462022-07-29 15:35:53 -070089 auto dense_cholesky = DenseCholesky::Create(options);
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -080090
91 const int kNumTrials = 10;
92 const int kMinNumCols = 1;
93 const int kMaxNumCols = 10;
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -080094 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 Agarwalc8493fc2022-05-14 14:14:22 -0700106 EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -0800107 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 Agarwald9a3dfb2022-07-14 06:56:58 -0700116INSTANTIATE_TEST_SUITE_P(EigenCholesky,
117 DenseCholeskyTest,
118 ::testing::Combine(::testing::Values(EIGEN),
Sameer Agarwalcb6ad462022-07-29 15:35:53 -0700119 ::testing::Values(kMixedPrecision,
120 kFullPrecision)),
Sameer Agarwald9a3dfb2022-07-14 06:56:58 -0700121 ParamInfoToString);
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -0800122#ifndef CERES_NO_LAPACK
Sameer Agarwald9a3dfb2022-07-14 06:56:58 -0700123INSTANTIATE_TEST_SUITE_P(LapackCholesky,
124 DenseCholeskyTest,
125 ::testing::Combine(::testing::Values(LAPACK),
Sameer Agarwalcb6ad462022-07-29 15:35:53 -0700126 ::testing::Values(kMixedPrecision,
127 kFullPrecision)),
Sameer Agarwald9a3dfb2022-07-14 06:56:58 -0700128 ParamInfoToString);
Sameer Agarwal6d06e9b2022-01-21 18:33:16 -0800129#endif
Joydeep Biswas36d6d862022-02-03 08:09:10 -0600130#ifndef CERES_NO_CUDA
Sameer Agarwald9a3dfb2022-07-14 06:56:58 -0700131INSTANTIATE_TEST_SUITE_P(CudaCholesky,
132 DenseCholeskyTest,
133 ::testing::Combine(::testing::Values(CUDA),
134 ::testing::Values(kMixedPrecision,
135 kFullPrecision)),
136 ParamInfoToString);
Joydeep Biswas36d6d862022-02-03 08:09:10 -0600137#endif
Joydeep Biswas88e08cf2022-06-04 20:17:06 -0500138
Sameer Agarwalcb6ad462022-07-29 15:35:53 -0700139class 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 Agarwald9a3dfb2022-07-14 06:56:58 -0700150
Sameer Agarwalcb6ad462022-07-29 15:35:53 -0700151class 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 Deitschf0851662022-02-17 00:56:27 +0100161
Sameer Agarwalcb6ad462022-07-29 15:35:53 -0700162using testing::_;
163using testing::Return;
164
165TEST(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
180TEST(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
199TEST(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 Agarwal6d06e9b2022-01-21 18:33:16 -0800220
Sameer Agarwalcaf614a2022-04-21 17:41:10 -0700221} // namespace ceres::internal