subset_preconditioner_test.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/subset_preconditioner.h"
  31. #include <memory>
  32. #include <random>
  33. #include "Eigen/Dense"
  34. #include "Eigen/SparseCore"
  35. #include "ceres/block_sparse_matrix.h"
  36. #include "ceres/compressed_row_sparse_matrix.h"
  37. #include "ceres/inner_product_computer.h"
  38. #include "ceres/internal/config.h"
  39. #include "ceres/internal/eigen.h"
  40. #include "glog/logging.h"
  41. #include "gtest/gtest.h"
  42. namespace ceres::internal {
  43. namespace {
  44. // TODO(sameeragarwal): Refactor the following two functions out of
  45. // here and sparse_cholesky_test.cc into a more suitable place.
  46. template <int UpLoType>
  47. bool SolveLinearSystemUsingEigen(const Matrix& lhs,
  48. const Vector rhs,
  49. Vector* solution) {
  50. Eigen::LLT<Matrix, UpLoType> llt = lhs.selfadjointView<UpLoType>().llt();
  51. if (llt.info() != Eigen::Success) {
  52. return false;
  53. }
  54. *solution = llt.solve(rhs);
  55. return (llt.info() == Eigen::Success);
  56. }
  57. // Use Eigen's Dense Cholesky solver to compute the solution to a
  58. // sparse linear system.
  59. bool ComputeExpectedSolution(const CompressedRowSparseMatrix& lhs,
  60. const Vector& rhs,
  61. Vector* solution) {
  62. Matrix dense_triangular_lhs;
  63. lhs.ToDenseMatrix(&dense_triangular_lhs);
  64. if (lhs.storage_type() ==
  65. CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR) {
  66. Matrix full_lhs = dense_triangular_lhs.selfadjointView<Eigen::Upper>();
  67. return SolveLinearSystemUsingEigen<Eigen::Upper>(full_lhs, rhs, solution);
  68. }
  69. return SolveLinearSystemUsingEigen<Eigen::Lower>(
  70. dense_triangular_lhs, rhs, solution);
  71. }
  72. using Param = ::testing::tuple<SparseLinearAlgebraLibraryType, bool>;
  73. std::string ParamInfoToString(testing::TestParamInfo<Param> info) {
  74. Param param = info.param;
  75. std::stringstream ss;
  76. ss << SparseLinearAlgebraLibraryTypeToString(::testing::get<0>(param)) << "_"
  77. << (::testing::get<1>(param) ? "Diagonal" : "NoDiagonal");
  78. return ss.str();
  79. }
  80. } // namespace
  81. class SubsetPreconditionerTest : public ::testing::TestWithParam<Param> {
  82. protected:
  83. void SetUp() final {
  84. BlockSparseMatrix::RandomMatrixOptions options;
  85. options.num_col_blocks = 4;
  86. options.min_col_block_size = 1;
  87. options.max_col_block_size = 4;
  88. options.num_row_blocks = 8;
  89. options.min_row_block_size = 1;
  90. options.max_row_block_size = 4;
  91. options.block_density = 0.9;
  92. m_ = BlockSparseMatrix::CreateRandomMatrix(options, prng_);
  93. start_row_block_ = m_->block_structure()->rows.size();
  94. // Ensure that the bottom part of the matrix has the same column
  95. // block structure.
  96. options.col_blocks = m_->block_structure()->cols;
  97. b_ = BlockSparseMatrix::CreateRandomMatrix(options, prng_);
  98. m_->AppendRows(*b_);
  99. // Create a Identity block diagonal matrix with the same column
  100. // block structure.
  101. diagonal_ = Vector::Ones(m_->num_cols());
  102. block_diagonal_ = BlockSparseMatrix::CreateDiagonalMatrix(
  103. diagonal_.data(), b_->block_structure()->cols);
  104. // Unconditionally add the block diagonal to the matrix b_,
  105. // because either it is either part of b_ to make it full rank, or
  106. // we pass the same diagonal matrix later as the parameter D. In
  107. // either case the preconditioner matrix is b_' b + D'D.
  108. b_->AppendRows(*block_diagonal_);
  109. inner_product_computer_ = InnerProductComputer::Create(
  110. *b_, CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR);
  111. inner_product_computer_->Compute();
  112. }
  113. std::unique_ptr<BlockSparseMatrix> m_;
  114. std::unique_ptr<BlockSparseMatrix> b_;
  115. std::unique_ptr<BlockSparseMatrix> block_diagonal_;
  116. std::unique_ptr<InnerProductComputer> inner_product_computer_;
  117. std::unique_ptr<Preconditioner> preconditioner_;
  118. Vector diagonal_;
  119. int start_row_block_;
  120. std::mt19937 prng_;
  121. };
  122. TEST_P(SubsetPreconditionerTest, foo) {
  123. Param param = GetParam();
  124. Preconditioner::Options options;
  125. options.subset_preconditioner_start_row_block = start_row_block_;
  126. options.sparse_linear_algebra_library_type = ::testing::get<0>(param);
  127. preconditioner_ = std::make_unique<SubsetPreconditioner>(options, *m_);
  128. const bool with_diagonal = ::testing::get<1>(param);
  129. if (!with_diagonal) {
  130. m_->AppendRows(*block_diagonal_);
  131. }
  132. EXPECT_TRUE(
  133. preconditioner_->Update(*m_, with_diagonal ? diagonal_.data() : nullptr));
  134. // Repeatedly apply the preconditioner to random vectors and check
  135. // that the preconditioned value is the same as one obtained by
  136. // solving the linear system directly.
  137. for (int i = 0; i < 5; ++i) {
  138. CompressedRowSparseMatrix* lhs = inner_product_computer_->mutable_result();
  139. Vector rhs = Vector::Random(lhs->num_rows());
  140. Vector expected(lhs->num_rows());
  141. EXPECT_TRUE(ComputeExpectedSolution(*lhs, rhs, &expected));
  142. Vector actual(lhs->num_rows());
  143. preconditioner_->RightMultiplyAndAccumulate(rhs.data(), actual.data());
  144. Matrix eigen_lhs;
  145. lhs->ToDenseMatrix(&eigen_lhs);
  146. EXPECT_NEAR((actual - expected).norm() / actual.norm(),
  147. 0.0,
  148. std::numeric_limits<double>::epsilon() * 10)
  149. << "\n"
  150. << eigen_lhs << "\n"
  151. << expected.transpose() << "\n"
  152. << actual.transpose();
  153. }
  154. }
  155. #ifndef CERES_NO_SUITESPARSE
  156. INSTANTIATE_TEST_SUITE_P(SubsetPreconditionerWithSuiteSparse,
  157. SubsetPreconditionerTest,
  158. ::testing::Combine(::testing::Values(SUITE_SPARSE),
  159. ::testing::Values(true, false)),
  160. ParamInfoToString);
  161. #endif
  162. #ifndef CERES_NO_ACCELERATE_SPARSE
  163. INSTANTIATE_TEST_SUITE_P(
  164. SubsetPreconditionerWithAccelerateSparse,
  165. SubsetPreconditionerTest,
  166. ::testing::Combine(::testing::Values(ACCELERATE_SPARSE),
  167. ::testing::Values(true, false)),
  168. ParamInfoToString);
  169. #endif
  170. #ifdef CERES_USE_EIGEN_SPARSE
  171. INSTANTIATE_TEST_SUITE_P(SubsetPreconditionerWithEigenSparse,
  172. SubsetPreconditionerTest,
  173. ::testing::Combine(::testing::Values(EIGEN_SPARSE),
  174. ::testing::Values(true, false)),
  175. ParamInfoToString);
  176. #endif
  177. } // namespace ceres::internal