dense_cholesky_test.cc 8.8 KB

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