dynamic_sparse_normal_cholesky_solver.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/dynamic_sparse_normal_cholesky_solver.h"
  31. #include <algorithm>
  32. #include <cstring>
  33. #include <ctime>
  34. #include <memory>
  35. #include <sstream>
  36. #include <utility>
  37. #include "Eigen/SparseCore"
  38. #include "ceres/compressed_row_sparse_matrix.h"
  39. #include "ceres/internal/config.h"
  40. #include "ceres/internal/eigen.h"
  41. #include "ceres/linear_solver.h"
  42. #include "ceres/suitesparse.h"
  43. #include "ceres/triplet_sparse_matrix.h"
  44. #include "ceres/types.h"
  45. #include "ceres/wall_time.h"
  46. #ifdef CERES_USE_EIGEN_SPARSE
  47. #include "Eigen/SparseCholesky"
  48. #endif
  49. namespace ceres::internal {
  50. DynamicSparseNormalCholeskySolver::DynamicSparseNormalCholeskySolver(
  51. LinearSolver::Options options)
  52. : options_(std::move(options)) {}
  53. LinearSolver::Summary DynamicSparseNormalCholeskySolver::SolveImpl(
  54. CompressedRowSparseMatrix* A,
  55. const double* b,
  56. const LinearSolver::PerSolveOptions& per_solve_options,
  57. double* x) {
  58. const int num_cols = A->num_cols();
  59. VectorRef(x, num_cols).setZero();
  60. A->LeftMultiplyAndAccumulate(b, x);
  61. if (per_solve_options.D != nullptr) {
  62. // Temporarily append a diagonal block to the A matrix, but undo
  63. // it before returning the matrix to the user.
  64. std::unique_ptr<CompressedRowSparseMatrix> regularizer;
  65. if (!A->col_blocks().empty()) {
  66. regularizer = CompressedRowSparseMatrix::CreateBlockDiagonalMatrix(
  67. per_solve_options.D, A->col_blocks());
  68. } else {
  69. regularizer = std::make_unique<CompressedRowSparseMatrix>(
  70. per_solve_options.D, num_cols);
  71. }
  72. A->AppendRows(*regularizer);
  73. }
  74. LinearSolver::Summary summary;
  75. switch (options_.sparse_linear_algebra_library_type) {
  76. case SUITE_SPARSE:
  77. summary = SolveImplUsingSuiteSparse(A, x);
  78. break;
  79. case EIGEN_SPARSE:
  80. summary = SolveImplUsingEigen(A, x);
  81. break;
  82. default:
  83. LOG(FATAL) << "Unsupported sparse linear algebra library for "
  84. << "dynamic sparsity: "
  85. << SparseLinearAlgebraLibraryTypeToString(
  86. options_.sparse_linear_algebra_library_type);
  87. }
  88. if (per_solve_options.D != nullptr) {
  89. A->DeleteRows(num_cols);
  90. }
  91. return summary;
  92. }
  93. LinearSolver::Summary DynamicSparseNormalCholeskySolver::SolveImplUsingEigen(
  94. CompressedRowSparseMatrix* A, double* rhs_and_solution) {
  95. #ifndef CERES_USE_EIGEN_SPARSE
  96. LinearSolver::Summary summary;
  97. summary.num_iterations = 0;
  98. summary.termination_type = LinearSolverTerminationType::FATAL_ERROR;
  99. summary.message =
  100. "SPARSE_NORMAL_CHOLESKY cannot be used with EIGEN_SPARSE "
  101. "because Ceres was not built with support for "
  102. "Eigen's SimplicialLDLT decomposition. "
  103. "This requires enabling building with -DEIGENSPARSE=ON.";
  104. return summary;
  105. #else
  106. EventLogger event_logger("DynamicSparseNormalCholeskySolver::Eigen::Solve");
  107. Eigen::Map<Eigen::SparseMatrix<double, Eigen::RowMajor>> a(
  108. A->num_rows(),
  109. A->num_cols(),
  110. A->num_nonzeros(),
  111. A->mutable_rows(),
  112. A->mutable_cols(),
  113. A->mutable_values());
  114. Eigen::SparseMatrix<double> lhs = a.transpose() * a;
  115. Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> solver;
  116. LinearSolver::Summary summary;
  117. summary.num_iterations = 1;
  118. summary.termination_type = LinearSolverTerminationType::SUCCESS;
  119. summary.message = "Success.";
  120. solver.analyzePattern(lhs);
  121. if (VLOG_IS_ON(2)) {
  122. std::stringstream ss;
  123. solver.dumpMemory(ss);
  124. VLOG(2) << "Symbolic Analysis\n" << ss.str();
  125. }
  126. event_logger.AddEvent("Analyze");
  127. if (solver.info() != Eigen::Success) {
  128. summary.termination_type = LinearSolverTerminationType::FATAL_ERROR;
  129. summary.message = "Eigen failure. Unable to find symbolic factorization.";
  130. return summary;
  131. }
  132. solver.factorize(lhs);
  133. event_logger.AddEvent("Factorize");
  134. if (solver.info() != Eigen::Success) {
  135. summary.termination_type = LinearSolverTerminationType::FAILURE;
  136. summary.message = "Eigen failure. Unable to find numeric factorization.";
  137. return summary;
  138. }
  139. const Vector rhs = VectorRef(rhs_and_solution, lhs.cols());
  140. VectorRef(rhs_and_solution, lhs.cols()) = solver.solve(rhs);
  141. event_logger.AddEvent("Solve");
  142. if (solver.info() != Eigen::Success) {
  143. summary.termination_type = LinearSolverTerminationType::FAILURE;
  144. summary.message = "Eigen failure. Unable to do triangular solve.";
  145. return summary;
  146. }
  147. return summary;
  148. #endif // CERES_USE_EIGEN_SPARSE
  149. }
  150. LinearSolver::Summary
  151. DynamicSparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
  152. CompressedRowSparseMatrix* A, double* rhs_and_solution) {
  153. #ifdef CERES_NO_SUITESPARSE
  154. (void)A;
  155. (void)rhs_and_solution;
  156. LinearSolver::Summary summary;
  157. summary.num_iterations = 0;
  158. summary.termination_type = LinearSolverTerminationType::FATAL_ERROR;
  159. summary.message =
  160. "SPARSE_NORMAL_CHOLESKY cannot be used with SUITE_SPARSE "
  161. "because Ceres was not built with support for SuiteSparse. "
  162. "This requires enabling building with -DSUITESPARSE=ON.";
  163. return summary;
  164. #else
  165. EventLogger event_logger(
  166. "DynamicSparseNormalCholeskySolver::SuiteSparse::Solve");
  167. LinearSolver::Summary summary;
  168. summary.termination_type = LinearSolverTerminationType::SUCCESS;
  169. summary.num_iterations = 1;
  170. summary.message = "Success.";
  171. SuiteSparse ss;
  172. const int num_cols = A->num_cols();
  173. cholmod_sparse lhs = ss.CreateSparseMatrixTransposeView(A);
  174. event_logger.AddEvent("Setup");
  175. cholmod_factor* factor =
  176. ss.AnalyzeCholesky(&lhs, options_.ordering_type, &summary.message);
  177. event_logger.AddEvent("Analysis");
  178. if (factor == nullptr) {
  179. summary.termination_type = LinearSolverTerminationType::FATAL_ERROR;
  180. return summary;
  181. }
  182. summary.termination_type = ss.Cholesky(&lhs, factor, &summary.message);
  183. if (summary.termination_type == LinearSolverTerminationType::SUCCESS) {
  184. cholmod_dense cholmod_rhs =
  185. ss.CreateDenseVectorView(rhs_and_solution, num_cols);
  186. cholmod_dense* solution = ss.Solve(factor, &cholmod_rhs, &summary.message);
  187. event_logger.AddEvent("Solve");
  188. if (solution != nullptr) {
  189. memcpy(
  190. rhs_and_solution, solution->x, num_cols * sizeof(*rhs_and_solution));
  191. ss.Free(solution);
  192. } else {
  193. summary.termination_type = LinearSolverTerminationType::FAILURE;
  194. }
  195. }
  196. ss.Free(factor);
  197. event_logger.AddEvent("Teardown");
  198. return summary;
  199. #endif
  200. }
  201. } // namespace ceres::internal