parameter_block_ordering.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 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/parameter_block_ordering.h"
  31. #include <map>
  32. #include <memory>
  33. #include <set>
  34. #include <unordered_set>
  35. #include <vector>
  36. #include "ceres/graph.h"
  37. #include "ceres/graph_algorithms.h"
  38. #include "ceres/map_util.h"
  39. #include "ceres/parameter_block.h"
  40. #include "ceres/program.h"
  41. #include "ceres/residual_block.h"
  42. #include "ceres/wall_time.h"
  43. #include "glog/logging.h"
  44. namespace ceres::internal {
  45. int ComputeStableSchurOrdering(const Program& program,
  46. std::vector<ParameterBlock*>* ordering) {
  47. CHECK(ordering != nullptr);
  48. ordering->clear();
  49. EventLogger event_logger("ComputeStableSchurOrdering");
  50. auto graph = CreateHessianGraph(program);
  51. event_logger.AddEvent("CreateHessianGraph");
  52. const std::vector<ParameterBlock*>& parameter_blocks =
  53. program.parameter_blocks();
  54. const std::unordered_set<ParameterBlock*>& vertices = graph->vertices();
  55. for (auto* parameter_block : parameter_blocks) {
  56. if (vertices.count(parameter_block) > 0) {
  57. ordering->push_back(parameter_block);
  58. }
  59. }
  60. event_logger.AddEvent("Preordering");
  61. int independent_set_size = StableIndependentSetOrdering(*graph, ordering);
  62. event_logger.AddEvent("StableIndependentSet");
  63. // Add the excluded blocks to back of the ordering vector.
  64. for (auto* parameter_block : parameter_blocks) {
  65. if (parameter_block->IsConstant()) {
  66. ordering->push_back(parameter_block);
  67. }
  68. }
  69. event_logger.AddEvent("ConstantParameterBlocks");
  70. return independent_set_size;
  71. }
  72. int ComputeSchurOrdering(const Program& program,
  73. std::vector<ParameterBlock*>* ordering) {
  74. CHECK(ordering != nullptr);
  75. ordering->clear();
  76. auto graph = CreateHessianGraph(program);
  77. int independent_set_size = IndependentSetOrdering(*graph, ordering);
  78. const std::vector<ParameterBlock*>& parameter_blocks =
  79. program.parameter_blocks();
  80. // Add the excluded blocks to back of the ordering vector.
  81. for (auto* parameter_block : parameter_blocks) {
  82. if (parameter_block->IsConstant()) {
  83. ordering->push_back(parameter_block);
  84. }
  85. }
  86. return independent_set_size;
  87. }
  88. void ComputeRecursiveIndependentSetOrdering(const Program& program,
  89. ParameterBlockOrdering* ordering) {
  90. CHECK(ordering != nullptr);
  91. ordering->Clear();
  92. const std::vector<ParameterBlock*> parameter_blocks =
  93. program.parameter_blocks();
  94. auto graph = CreateHessianGraph(program);
  95. int num_covered = 0;
  96. int round = 0;
  97. while (num_covered < parameter_blocks.size()) {
  98. std::vector<ParameterBlock*> independent_set_ordering;
  99. const int independent_set_size =
  100. IndependentSetOrdering(*graph, &independent_set_ordering);
  101. for (int i = 0; i < independent_set_size; ++i) {
  102. ParameterBlock* parameter_block = independent_set_ordering[i];
  103. ordering->AddElementToGroup(parameter_block->mutable_user_state(), round);
  104. graph->RemoveVertex(parameter_block);
  105. }
  106. num_covered += independent_set_size;
  107. ++round;
  108. }
  109. }
  110. std::unique_ptr<Graph<ParameterBlock*>> CreateHessianGraph(
  111. const Program& program) {
  112. auto graph = std::make_unique<Graph<ParameterBlock*>>();
  113. CHECK(graph != nullptr);
  114. const std::vector<ParameterBlock*>& parameter_blocks =
  115. program.parameter_blocks();
  116. for (auto* parameter_block : parameter_blocks) {
  117. if (!parameter_block->IsConstant()) {
  118. graph->AddVertex(parameter_block);
  119. }
  120. }
  121. const std::vector<ResidualBlock*>& residual_blocks =
  122. program.residual_blocks();
  123. for (auto* residual_block : residual_blocks) {
  124. const int num_parameter_blocks = residual_block->NumParameterBlocks();
  125. ParameterBlock* const* parameter_blocks =
  126. residual_block->parameter_blocks();
  127. for (int j = 0; j < num_parameter_blocks; ++j) {
  128. if (parameter_blocks[j]->IsConstant()) {
  129. continue;
  130. }
  131. for (int k = j + 1; k < num_parameter_blocks; ++k) {
  132. if (parameter_blocks[k]->IsConstant()) {
  133. continue;
  134. }
  135. graph->AddEdge(parameter_blocks[j], parameter_blocks[k]);
  136. }
  137. }
  138. }
  139. return graph;
  140. }
  141. void OrderingToGroupSizes(const ParameterBlockOrdering* ordering,
  142. std::vector<int>* group_sizes) {
  143. CHECK(group_sizes != nullptr);
  144. group_sizes->clear();
  145. if (ordering == nullptr) {
  146. return;
  147. }
  148. // TODO(sameeragarwal): Investigate if this should be a set or an
  149. // unordered_set.
  150. const std::map<int, std::set<double*>>& group_to_elements =
  151. ordering->group_to_elements();
  152. for (const auto& g_t_e : group_to_elements) {
  153. group_sizes->push_back(g_t_e.second.size());
  154. }
  155. }
  156. } // namespace ceres::internal