partitioned_matrix_view.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. //
  31. // For generalized bi-partite Jacobian matrices that arise in
  32. // Structure from Motion related problems, it is sometimes useful to
  33. // have access to the two parts of the matrix as linear operators
  34. // themselves. This class provides that functionality.
  35. #ifndef CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_
  36. #define CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_
  37. #include <algorithm>
  38. #include <cstring>
  39. #include <memory>
  40. #include <vector>
  41. #include "ceres/block_structure.h"
  42. #include "ceres/internal/config.h"
  43. #include "ceres/internal/disable_warnings.h"
  44. #include "ceres/internal/eigen.h"
  45. #include "ceres/internal/export.h"
  46. #include "ceres/linear_solver.h"
  47. #include "ceres/small_blas.h"
  48. #include "glog/logging.h"
  49. namespace ceres::internal {
  50. class ContextImpl;
  51. // Given generalized bi-partite matrix A = [E F], with the same block
  52. // structure as required by the Schur complement based solver, found
  53. // in schur_complement_solver.h, provide access to the
  54. // matrices E and F and their outer products E'E and F'F with
  55. // themselves.
  56. //
  57. // Lack of BlockStructure object will result in a crash and if the
  58. // block structure of the matrix does not satisfy the requirements of
  59. // the Schur complement solver it will result in unpredictable and
  60. // wrong output.
  61. class CERES_NO_EXPORT PartitionedMatrixViewBase {
  62. public:
  63. virtual ~PartitionedMatrixViewBase();
  64. // y += E'x
  65. virtual void LeftMultiplyAndAccumulateE(const double* x, double* y) const = 0;
  66. virtual void LeftMultiplyAndAccumulateESingleThreaded(const double* x,
  67. double* y) const = 0;
  68. virtual void LeftMultiplyAndAccumulateEMultiThreaded(const double* x,
  69. double* y) const = 0;
  70. // y += F'x
  71. virtual void LeftMultiplyAndAccumulateF(const double* x, double* y) const = 0;
  72. virtual void LeftMultiplyAndAccumulateFSingleThreaded(const double* x,
  73. double* y) const = 0;
  74. virtual void LeftMultiplyAndAccumulateFMultiThreaded(const double* x,
  75. double* y) const = 0;
  76. // y += Ex
  77. virtual void RightMultiplyAndAccumulateE(const double* x,
  78. double* y) const = 0;
  79. // y += Fx
  80. virtual void RightMultiplyAndAccumulateF(const double* x,
  81. double* y) const = 0;
  82. // Create and return the block diagonal of the matrix E'E.
  83. virtual std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalEtE() const = 0;
  84. // Create and return the block diagonal of the matrix F'F. Caller
  85. // owns the result.
  86. virtual std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalFtF() const = 0;
  87. // Compute the block diagonal of the matrix E'E and store it in
  88. // block_diagonal. The matrix block_diagonal is expected to have a
  89. // BlockStructure (preferably created using
  90. // CreateBlockDiagonalMatrixEtE) which is has the same structure as
  91. // the block diagonal of E'E.
  92. virtual void UpdateBlockDiagonalEtE(
  93. BlockSparseMatrix* block_diagonal) const = 0;
  94. // Compute the block diagonal of the matrix F'F and store it in
  95. // block_diagonal. The matrix block_diagonal is expected to have a
  96. // BlockStructure (preferably created using
  97. // CreateBlockDiagonalMatrixFtF) which is has the same structure as
  98. // the block diagonal of F'F.
  99. virtual void UpdateBlockDiagonalFtF(
  100. BlockSparseMatrix* block_diagonal) const = 0;
  101. // clang-format off
  102. virtual int num_col_blocks_e() const = 0;
  103. virtual int num_col_blocks_f() const = 0;
  104. virtual int num_cols_e() const = 0;
  105. virtual int num_cols_f() const = 0;
  106. virtual int num_rows() const = 0;
  107. virtual int num_cols() const = 0;
  108. virtual const std::vector<int>& e_cols_partition() const = 0;
  109. virtual const std::vector<int>& f_cols_partition() const = 0;
  110. // clang-format on
  111. static std::unique_ptr<PartitionedMatrixViewBase> Create(
  112. const LinearSolver::Options& options, const BlockSparseMatrix& matrix);
  113. };
  114. template <int kRowBlockSize = Eigen::Dynamic,
  115. int kEBlockSize = Eigen::Dynamic,
  116. int kFBlockSize = Eigen::Dynamic>
  117. class CERES_NO_EXPORT PartitionedMatrixView final
  118. : public PartitionedMatrixViewBase {
  119. public:
  120. // matrix = [E F], where the matrix E contains the first
  121. // options.elimination_groups[0] column blocks.
  122. PartitionedMatrixView(const LinearSolver::Options& options,
  123. const BlockSparseMatrix& matrix);
  124. // y += E'x
  125. virtual void LeftMultiplyAndAccumulateE(const double* x,
  126. double* y) const final;
  127. virtual void LeftMultiplyAndAccumulateESingleThreaded(const double* x,
  128. double* y) const final;
  129. virtual void LeftMultiplyAndAccumulateEMultiThreaded(const double* x,
  130. double* y) const final;
  131. // y += F'x
  132. virtual void LeftMultiplyAndAccumulateF(const double* x,
  133. double* y) const final;
  134. virtual void LeftMultiplyAndAccumulateFSingleThreaded(const double* x,
  135. double* y) const final;
  136. virtual void LeftMultiplyAndAccumulateFMultiThreaded(const double* x,
  137. double* y) const final;
  138. // y += Ex
  139. virtual void RightMultiplyAndAccumulateE(const double* x,
  140. double* y) const final;
  141. // y += Fx
  142. virtual void RightMultiplyAndAccumulateF(const double* x,
  143. double* y) const final;
  144. std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalEtE() const final;
  145. std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalFtF() const final;
  146. void UpdateBlockDiagonalEtE(BlockSparseMatrix* block_diagonal) const final;
  147. void UpdateBlockDiagonalEtESingleThreaded(
  148. BlockSparseMatrix* block_diagonal) const;
  149. void UpdateBlockDiagonalEtEMultiThreaded(
  150. BlockSparseMatrix* block_diagonal) const;
  151. void UpdateBlockDiagonalFtF(BlockSparseMatrix* block_diagonal) const final;
  152. void UpdateBlockDiagonalFtFSingleThreaded(
  153. BlockSparseMatrix* block_diagonal) const;
  154. void UpdateBlockDiagonalFtFMultiThreaded(
  155. BlockSparseMatrix* block_diagonal) const;
  156. // clang-format off
  157. int num_col_blocks_e() const final { return num_col_blocks_e_; }
  158. int num_col_blocks_f() const final { return num_col_blocks_f_; }
  159. int num_cols_e() const final { return num_cols_e_; }
  160. int num_cols_f() const final { return num_cols_f_; }
  161. int num_rows() const final { return matrix_.num_rows(); }
  162. int num_cols() const final { return matrix_.num_cols(); }
  163. // clang-format on
  164. const std::vector<int>& e_cols_partition() const final {
  165. return e_cols_partition_;
  166. }
  167. const std::vector<int>& f_cols_partition() const final {
  168. return f_cols_partition_;
  169. }
  170. private:
  171. std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalMatrixLayout(
  172. int start_col_block, int end_col_block) const;
  173. const LinearSolver::Options options_;
  174. const BlockSparseMatrix& matrix_;
  175. int num_row_blocks_e_;
  176. int num_col_blocks_e_;
  177. int num_col_blocks_f_;
  178. int num_cols_e_;
  179. int num_cols_f_;
  180. std::vector<int> e_cols_partition_;
  181. std::vector<int> f_cols_partition_;
  182. };
  183. } // namespace ceres::internal
  184. #include "ceres/internal/reenable_warnings.h"
  185. #endif // CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_