sparse_cholesky.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/sparse_cholesky.h"
  31. #include <memory>
  32. #include <utility>
  33. #include "ceres/accelerate_sparse.h"
  34. #include "ceres/eigensparse.h"
  35. #include "ceres/float_suitesparse.h"
  36. #include "ceres/iterative_refiner.h"
  37. #include "ceres/suitesparse.h"
  38. namespace ceres::internal {
  39. std::unique_ptr<SparseCholesky> SparseCholesky::Create(
  40. const LinearSolver::Options& options) {
  41. std::unique_ptr<SparseCholesky> sparse_cholesky;
  42. switch (options.sparse_linear_algebra_library_type) {
  43. case SUITE_SPARSE:
  44. #ifndef CERES_NO_SUITESPARSE
  45. if (options.use_mixed_precision_solves) {
  46. sparse_cholesky =
  47. FloatSuiteSparseCholesky::Create(options.ordering_type);
  48. } else {
  49. sparse_cholesky = SuiteSparseCholesky::Create(options.ordering_type);
  50. }
  51. break;
  52. #else
  53. LOG(FATAL) << "Ceres was compiled without support for SuiteSparse.";
  54. #endif
  55. case EIGEN_SPARSE:
  56. #ifdef CERES_USE_EIGEN_SPARSE
  57. if (options.use_mixed_precision_solves) {
  58. sparse_cholesky =
  59. FloatEigenSparseCholesky::Create(options.ordering_type);
  60. } else {
  61. sparse_cholesky = EigenSparseCholesky::Create(options.ordering_type);
  62. }
  63. break;
  64. #else
  65. LOG(FATAL) << "Ceres was compiled without support for "
  66. << "Eigen's sparse Cholesky factorization routines.";
  67. #endif
  68. case ACCELERATE_SPARSE:
  69. #ifndef CERES_NO_ACCELERATE_SPARSE
  70. if (options.use_mixed_precision_solves) {
  71. sparse_cholesky =
  72. AppleAccelerateCholesky<float>::Create(options.ordering_type);
  73. } else {
  74. sparse_cholesky =
  75. AppleAccelerateCholesky<double>::Create(options.ordering_type);
  76. }
  77. break;
  78. #else
  79. LOG(FATAL) << "Ceres was compiled without support for Apple's Accelerate "
  80. << "framework solvers.";
  81. #endif
  82. default:
  83. LOG(FATAL) << "Unknown sparse linear algebra library type : "
  84. << SparseLinearAlgebraLibraryTypeToString(
  85. options.sparse_linear_algebra_library_type);
  86. }
  87. if (options.max_num_refinement_iterations > 0) {
  88. auto refiner = std::make_unique<SparseIterativeRefiner>(
  89. options.max_num_refinement_iterations);
  90. sparse_cholesky = std::make_unique<RefinedSparseCholesky>(
  91. std::move(sparse_cholesky), std::move(refiner));
  92. }
  93. return sparse_cholesky;
  94. }
  95. SparseCholesky::~SparseCholesky() = default;
  96. LinearSolverTerminationType SparseCholesky::FactorAndSolve(
  97. CompressedRowSparseMatrix* lhs,
  98. const double* rhs,
  99. double* solution,
  100. std::string* message) {
  101. LinearSolverTerminationType termination_type = Factorize(lhs, message);
  102. if (termination_type == LinearSolverTerminationType::SUCCESS) {
  103. termination_type = Solve(rhs, solution, message);
  104. }
  105. return termination_type;
  106. }
  107. RefinedSparseCholesky::RefinedSparseCholesky(
  108. std::unique_ptr<SparseCholesky> sparse_cholesky,
  109. std::unique_ptr<SparseIterativeRefiner> iterative_refiner)
  110. : sparse_cholesky_(std::move(sparse_cholesky)),
  111. iterative_refiner_(std::move(iterative_refiner)) {}
  112. RefinedSparseCholesky::~RefinedSparseCholesky() = default;
  113. CompressedRowSparseMatrix::StorageType RefinedSparseCholesky::StorageType()
  114. const {
  115. return sparse_cholesky_->StorageType();
  116. }
  117. LinearSolverTerminationType RefinedSparseCholesky::Factorize(
  118. CompressedRowSparseMatrix* lhs, std::string* message) {
  119. lhs_ = lhs;
  120. return sparse_cholesky_->Factorize(lhs, message);
  121. }
  122. LinearSolverTerminationType RefinedSparseCholesky::Solve(const double* rhs,
  123. double* solution,
  124. std::string* message) {
  125. CHECK(lhs_ != nullptr);
  126. auto termination_type = sparse_cholesky_->Solve(rhs, solution, message);
  127. if (termination_type != LinearSolverTerminationType::SUCCESS) {
  128. return termination_type;
  129. }
  130. iterative_refiner_->Refine(*lhs_, rhs, sparse_cholesky_.get(), solution);
  131. return LinearSolverTerminationType::SUCCESS;
  132. }
  133. } // namespace ceres::internal