reorder_program.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef CERES_INTERNAL_REORDER_PROGRAM_H_
  31. #define CERES_INTERNAL_REORDER_PROGRAM_H_
  32. #include <string>
  33. #include "ceres/internal/disable_warnings.h"
  34. #include "ceres/internal/export.h"
  35. #include "ceres/linear_solver.h"
  36. #include "ceres/parameter_block_ordering.h"
  37. #include "ceres/problem_impl.h"
  38. #include "ceres/types.h"
  39. namespace ceres::internal {
  40. class Program;
  41. // Reorder the parameter blocks in program using the ordering
  42. CERES_NO_EXPORT bool ApplyOrdering(
  43. const ProblemImpl::ParameterMap& parameter_map,
  44. const ParameterBlockOrdering& ordering,
  45. Program* program,
  46. std::string* error);
  47. // Reorder the residuals for program, if necessary, so that the residuals
  48. // involving each E block occur together. This is a necessary condition for the
  49. // Schur eliminator, which works on these "row blocks" in the jacobian.
  50. CERES_NO_EXPORT bool LexicographicallyOrderResidualBlocks(
  51. int size_of_first_elimination_group, Program* program, std::string* error);
  52. // Schur type solvers require that all parameter blocks eliminated
  53. // by the Schur eliminator occur before others and the residuals be
  54. // sorted in lexicographic order of their parameter blocks.
  55. //
  56. // If the parameter_block_ordering only contains one elimination
  57. // group then a maximal independent set is computed and used as the
  58. // first elimination group, otherwise the user's ordering is used.
  59. //
  60. // If the linear solver type is SPARSE_SCHUR and support for
  61. // constrained fill-reducing ordering is available in the sparse
  62. // linear algebra library (SuiteSparse version >= 4.2.0) then
  63. // columns of the schur complement matrix are ordered to reduce the
  64. // fill-in the Cholesky factorization.
  65. //
  66. // Upon return, ordering contains the parameter block ordering that
  67. // was used to order the program.
  68. CERES_NO_EXPORT bool ReorderProgramForSchurTypeLinearSolver(
  69. LinearSolverType linear_solver_type,
  70. SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type,
  71. LinearSolverOrderingType linear_solver_ordering_type,
  72. const ProblemImpl::ParameterMap& parameter_map,
  73. ParameterBlockOrdering* parameter_block_ordering,
  74. Program* program,
  75. std::string* error);
  76. // Sparse cholesky factorization routines when doing the sparse
  77. // cholesky factorization of the Jacobian matrix, reorders its
  78. // columns to reduce the fill-in. Compute this permutation and
  79. // re-order the parameter blocks.
  80. //
  81. // When using SuiteSparse, if the parameter_block_ordering contains
  82. // more than one elimination group and support for constrained
  83. // fill-reducing ordering is available in the sparse linear algebra
  84. // library (SuiteSparse version >= 4.2.0) then the fill reducing
  85. // ordering will take it into account, otherwise it will be ignored.
  86. CERES_NO_EXPORT bool ReorderProgramForSparseCholesky(
  87. SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type,
  88. LinearSolverOrderingType linear_solver_ordering_type,
  89. const ParameterBlockOrdering& parameter_block_ordering,
  90. int start_row_block,
  91. Program* program,
  92. std::string* error);
  93. // Reorder the residual blocks in the program so that all the residual
  94. // blocks in bottom_residual_blocks are at the bottom. The return
  95. // value is the number of residual blocks in the program in "top" part
  96. // of the Program, i.e., the ones not included in
  97. // bottom_residual_blocks.
  98. //
  99. // This number can be different from program->NumResidualBlocks() -
  100. // bottom_residual_blocks.size() because we allow
  101. // bottom_residual_blocks to contain residual blocks not present in
  102. // the Program.
  103. CERES_NO_EXPORT int ReorderResidualBlocksByPartition(
  104. const std::unordered_set<ResidualBlockId>& bottom_residual_blocks,
  105. Program* program);
  106. // The return value of this function indicates whether the columns of
  107. // the Jacobian can be reordered using a fill reducing ordering.
  108. CERES_NO_EXPORT bool AreJacobianColumnsOrdered(
  109. LinearSolverType linear_solver_type,
  110. PreconditionerType preconditioner_type,
  111. SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type,
  112. LinearSolverOrderingType linear_solver_ordering_type);
  113. } // namespace ceres::internal
  114. #include "ceres/internal/reenable_warnings.h"
  115. #endif // CERES_INTERNAL_REORDER_PROGRAM_H_