schur_jacobi_preconditioner.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 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. //
  31. // Detailed descriptions of these preconditions beyond what is
  32. // documented here can be found in
  33. //
  34. // Bundle Adjustment in the Large
  35. // S. Agarwal, N. Snavely, S. Seitz & R. Szeliski, ECCV 2010
  36. // http://www.cs.washington.edu/homes/sagarwal/bal.pdf
  37. #ifndef CERES_INTERNAL_SCHUR_JACOBI_PRECONDITIONER_H_
  38. #define CERES_INTERNAL_SCHUR_JACOBI_PRECONDITIONER_H_
  39. #include <memory>
  40. #include <set>
  41. #include <utility>
  42. #include <vector>
  43. #include "ceres/internal/disable_warnings.h"
  44. #include "ceres/internal/export.h"
  45. #include "ceres/preconditioner.h"
  46. namespace ceres::internal {
  47. class BlockRandomAccessDiagonalMatrix;
  48. class BlockSparseMatrix;
  49. struct CompressedRowBlockStructure;
  50. class SchurEliminatorBase;
  51. // This class implements the SCHUR_JACOBI preconditioner for Structure
  52. // from Motion/Bundle Adjustment problems. Full mathematical details
  53. // can be found in
  54. //
  55. // Bundle Adjustment in the Large
  56. // S. Agarwal, N. Snavely, S. Seitz & R. Szeliski, ECCV 2010
  57. // http://www.cs.washington.edu/homes/sagarwal/bal.pdf
  58. //
  59. // Example usage:
  60. //
  61. // Preconditioner::Options options;
  62. // options.preconditioner_type = SCHUR_JACOBI;
  63. // options.elimination_groups.push_back(num_points);
  64. // options.elimination_groups.push_back(num_cameras);
  65. // SchurJacobiPreconditioner preconditioner(
  66. // *A.block_structure(), options);
  67. // preconditioner.Update(A, nullptr);
  68. // preconditioner.RightMultiplyAndAccumulate(x, y);
  69. //
  70. // TODO(https://github.com/ceres-solver/ceres-solver/issues/935):
  71. // SchurJacobiPreconditioner::RightMultiply will benefit from multithreading
  72. class CERES_NO_EXPORT SchurJacobiPreconditioner
  73. : public BlockSparseMatrixPreconditioner {
  74. public:
  75. // Initialize the symbolic structure of the preconditioner. bs is
  76. // the block structure of the linear system to be solved. It is used
  77. // to determine the sparsity structure of the preconditioner matrix.
  78. //
  79. // It has the same structural requirement as other Schur complement
  80. // based solvers. Please see schur_eliminator.h for more details.
  81. SchurJacobiPreconditioner(const CompressedRowBlockStructure& bs,
  82. Preconditioner::Options options);
  83. SchurJacobiPreconditioner(const SchurJacobiPreconditioner&) = delete;
  84. void operator=(const SchurJacobiPreconditioner&) = delete;
  85. ~SchurJacobiPreconditioner() override;
  86. // Preconditioner interface.
  87. void RightMultiplyAndAccumulate(const double* x, double* y) const final;
  88. int num_rows() const final;
  89. private:
  90. void InitEliminator(const CompressedRowBlockStructure& bs);
  91. bool UpdateImpl(const BlockSparseMatrix& A, const double* D) final;
  92. Preconditioner::Options options_;
  93. std::unique_ptr<SchurEliminatorBase> eliminator_;
  94. // Preconditioner matrix.
  95. std::unique_ptr<BlockRandomAccessDiagonalMatrix> m_;
  96. };
  97. } // namespace ceres::internal
  98. #include "ceres/internal/reenable_warnings.h"
  99. #endif // CERES_INTERNAL_SCHUR_JACOBI_PRECONDITIONER_H_