schur_jacobi_preconditioner.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/schur_jacobi_preconditioner.h"
  31. #include <memory>
  32. #include <utility>
  33. #include <vector>
  34. #include "ceres/block_random_access_diagonal_matrix.h"
  35. #include "ceres/block_sparse_matrix.h"
  36. #include "ceres/linear_solver.h"
  37. #include "ceres/schur_eliminator.h"
  38. #include "glog/logging.h"
  39. namespace ceres::internal {
  40. SchurJacobiPreconditioner::SchurJacobiPreconditioner(
  41. const CompressedRowBlockStructure& bs, Preconditioner::Options options)
  42. : options_(std::move(options)) {
  43. CHECK_GT(options_.elimination_groups.size(), 1);
  44. CHECK_GT(options_.elimination_groups[0], 0);
  45. const int num_blocks = bs.cols.size() - options_.elimination_groups[0];
  46. CHECK_GT(num_blocks, 0) << "Jacobian should have at least 1 f_block for "
  47. << "SCHUR_JACOBI preconditioner.";
  48. CHECK(options_.context != nullptr);
  49. std::vector<Block> blocks(num_blocks);
  50. int position = 0;
  51. for (int i = 0; i < num_blocks; ++i) {
  52. blocks[i] =
  53. Block(bs.cols[i + options_.elimination_groups[0]].size, position);
  54. position += blocks[i].size;
  55. }
  56. m_ = std::make_unique<BlockRandomAccessDiagonalMatrix>(
  57. blocks, options_.context, options_.num_threads);
  58. InitEliminator(bs);
  59. }
  60. SchurJacobiPreconditioner::~SchurJacobiPreconditioner() = default;
  61. // Initialize the SchurEliminator.
  62. void SchurJacobiPreconditioner::InitEliminator(
  63. const CompressedRowBlockStructure& bs) {
  64. LinearSolver::Options eliminator_options;
  65. eliminator_options.elimination_groups = options_.elimination_groups;
  66. eliminator_options.num_threads = options_.num_threads;
  67. eliminator_options.e_block_size = options_.e_block_size;
  68. eliminator_options.f_block_size = options_.f_block_size;
  69. eliminator_options.row_block_size = options_.row_block_size;
  70. eliminator_options.context = options_.context;
  71. eliminator_ = SchurEliminatorBase::Create(eliminator_options);
  72. const bool kFullRankETE = true;
  73. eliminator_->Init(
  74. eliminator_options.elimination_groups[0], kFullRankETE, &bs);
  75. }
  76. // Update the values of the preconditioner matrix and factorize it.
  77. bool SchurJacobiPreconditioner::UpdateImpl(const BlockSparseMatrix& A,
  78. const double* D) {
  79. const int num_rows = m_->num_rows();
  80. CHECK_GT(num_rows, 0);
  81. // Compute a subset of the entries of the Schur complement.
  82. eliminator_->Eliminate(
  83. BlockSparseMatrixData(A), nullptr, D, m_.get(), nullptr);
  84. m_->Invert();
  85. return true;
  86. }
  87. void SchurJacobiPreconditioner::RightMultiplyAndAccumulate(const double* x,
  88. double* y) const {
  89. m_->RightMultiplyAndAccumulate(x, y);
  90. }
  91. int SchurJacobiPreconditioner::num_rows() const { return m_->num_rows(); }
  92. } // namespace ceres::internal