schur_eliminator_benchmark.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // Authors: sameeragarwal@google.com (Sameer Agarwal)
  30. #include <algorithm>
  31. #include <memory>
  32. #include <random>
  33. #include <vector>
  34. #include "Eigen/Dense"
  35. #include "benchmark/benchmark.h"
  36. #include "ceres/block_random_access_dense_matrix.h"
  37. #include "ceres/block_sparse_matrix.h"
  38. #include "ceres/block_structure.h"
  39. #include "ceres/schur_eliminator.h"
  40. namespace ceres::internal {
  41. constexpr int kRowBlockSize = 2;
  42. constexpr int kEBlockSize = 3;
  43. constexpr int kFBlockSize = 6;
  44. class BenchmarkData {
  45. public:
  46. explicit BenchmarkData(const int num_e_blocks) {
  47. auto* bs = new CompressedRowBlockStructure;
  48. bs->cols.resize(num_e_blocks + 1);
  49. int col_pos = 0;
  50. for (int i = 0; i < num_e_blocks; ++i) {
  51. bs->cols[i].position = col_pos;
  52. bs->cols[i].size = kEBlockSize;
  53. col_pos += kEBlockSize;
  54. }
  55. bs->cols.back().position = col_pos;
  56. bs->cols.back().size = kFBlockSize;
  57. bs->rows.resize(2 * num_e_blocks);
  58. int row_pos = 0;
  59. int cell_pos = 0;
  60. for (int i = 0; i < num_e_blocks; ++i) {
  61. {
  62. auto& row = bs->rows[2 * i];
  63. row.block.position = row_pos;
  64. row.block.size = kRowBlockSize;
  65. row_pos += kRowBlockSize;
  66. auto& cells = row.cells;
  67. cells.resize(2);
  68. cells[0].block_id = i;
  69. cells[0].position = cell_pos;
  70. cell_pos += kRowBlockSize * kEBlockSize;
  71. cells[1].block_id = num_e_blocks;
  72. cells[1].position = cell_pos;
  73. cell_pos += kRowBlockSize * kFBlockSize;
  74. }
  75. {
  76. auto& row = bs->rows[2 * i + 1];
  77. row.block.position = row_pos;
  78. row.block.size = kRowBlockSize;
  79. row_pos += kRowBlockSize;
  80. auto& cells = row.cells;
  81. cells.resize(1);
  82. cells[0].block_id = i;
  83. cells[0].position = cell_pos;
  84. cell_pos += kRowBlockSize * kEBlockSize;
  85. }
  86. }
  87. matrix_ = std::make_unique<BlockSparseMatrix>(bs);
  88. double* values = matrix_->mutable_values();
  89. std::generate_n(values, matrix_->num_nonzeros(), [this] {
  90. return standard_normal_(prng_);
  91. });
  92. b_.resize(matrix_->num_rows());
  93. b_.setRandom();
  94. std::vector<Block> blocks;
  95. blocks.emplace_back(kFBlockSize, 0);
  96. lhs_ = std::make_unique<BlockRandomAccessDenseMatrix>(blocks, &context_, 1);
  97. diagonal_.resize(matrix_->num_cols());
  98. diagonal_.setOnes();
  99. rhs_.resize(kFBlockSize);
  100. y_.resize(num_e_blocks * kEBlockSize);
  101. y_.setZero();
  102. z_.resize(kFBlockSize);
  103. z_.setOnes();
  104. }
  105. const BlockSparseMatrix& matrix() const { return *matrix_; }
  106. const Vector& b() const { return b_; }
  107. const Vector& diagonal() const { return diagonal_; }
  108. BlockRandomAccessDenseMatrix* mutable_lhs() { return lhs_.get(); }
  109. Vector* mutable_rhs() { return &rhs_; }
  110. Vector* mutable_y() { return &y_; }
  111. Vector* mutable_z() { return &z_; }
  112. ContextImpl* context() { return &context_; }
  113. private:
  114. ContextImpl context_;
  115. std::unique_ptr<BlockSparseMatrix> matrix_;
  116. Vector b_;
  117. std::unique_ptr<BlockRandomAccessDenseMatrix> lhs_;
  118. Vector rhs_;
  119. Vector diagonal_;
  120. Vector z_;
  121. Vector y_;
  122. std::mt19937 prng_;
  123. std::normal_distribution<> standard_normal_;
  124. };
  125. static void BM_SchurEliminatorEliminate(benchmark::State& state) {
  126. const int num_e_blocks = state.range(0);
  127. BenchmarkData data(num_e_blocks);
  128. LinearSolver::Options linear_solver_options;
  129. linear_solver_options.e_block_size = kEBlockSize;
  130. linear_solver_options.row_block_size = kRowBlockSize;
  131. linear_solver_options.f_block_size = kFBlockSize;
  132. linear_solver_options.context = data.context();
  133. std::unique_ptr<SchurEliminatorBase> eliminator(
  134. SchurEliminatorBase::Create(linear_solver_options));
  135. eliminator->Init(num_e_blocks, true, data.matrix().block_structure());
  136. for (auto _ : state) {
  137. eliminator->Eliminate(BlockSparseMatrixData(data.matrix()),
  138. data.b().data(),
  139. data.diagonal().data(),
  140. data.mutable_lhs(),
  141. data.mutable_rhs()->data());
  142. }
  143. }
  144. static void BM_SchurEliminatorBackSubstitute(benchmark::State& state) {
  145. const int num_e_blocks = state.range(0);
  146. BenchmarkData data(num_e_blocks);
  147. LinearSolver::Options linear_solver_options;
  148. linear_solver_options.e_block_size = kEBlockSize;
  149. linear_solver_options.row_block_size = kRowBlockSize;
  150. linear_solver_options.f_block_size = kFBlockSize;
  151. linear_solver_options.context = data.context();
  152. std::unique_ptr<SchurEliminatorBase> eliminator(
  153. SchurEliminatorBase::Create(linear_solver_options));
  154. eliminator->Init(num_e_blocks, true, data.matrix().block_structure());
  155. eliminator->Eliminate(BlockSparseMatrixData(data.matrix()),
  156. data.b().data(),
  157. data.diagonal().data(),
  158. data.mutable_lhs(),
  159. data.mutable_rhs()->data());
  160. for (auto _ : state) {
  161. eliminator->BackSubstitute(BlockSparseMatrixData(data.matrix()),
  162. data.b().data(),
  163. data.diagonal().data(),
  164. data.mutable_z()->data(),
  165. data.mutable_y()->data());
  166. }
  167. }
  168. static void BM_SchurEliminatorForOneFBlockEliminate(benchmark::State& state) {
  169. const int num_e_blocks = state.range(0);
  170. BenchmarkData data(num_e_blocks);
  171. SchurEliminatorForOneFBlock<2, 3, 6> eliminator;
  172. eliminator.Init(num_e_blocks, true, data.matrix().block_structure());
  173. for (auto _ : state) {
  174. eliminator.Eliminate(BlockSparseMatrixData(data.matrix()),
  175. data.b().data(),
  176. data.diagonal().data(),
  177. data.mutable_lhs(),
  178. data.mutable_rhs()->data());
  179. }
  180. }
  181. static void BM_SchurEliminatorForOneFBlockBackSubstitute(
  182. benchmark::State& state) {
  183. const int num_e_blocks = state.range(0);
  184. BenchmarkData data(num_e_blocks);
  185. SchurEliminatorForOneFBlock<2, 3, 6> eliminator;
  186. eliminator.Init(num_e_blocks, true, data.matrix().block_structure());
  187. eliminator.Eliminate(BlockSparseMatrixData(data.matrix()),
  188. data.b().data(),
  189. data.diagonal().data(),
  190. data.mutable_lhs(),
  191. data.mutable_rhs()->data());
  192. for (auto _ : state) {
  193. eliminator.BackSubstitute(BlockSparseMatrixData(data.matrix()),
  194. data.b().data(),
  195. data.diagonal().data(),
  196. data.mutable_z()->data(),
  197. data.mutable_y()->data());
  198. }
  199. }
  200. BENCHMARK(BM_SchurEliminatorEliminate)->Range(10, 10000);
  201. BENCHMARK(BM_SchurEliminatorForOneFBlockEliminate)->Range(10, 10000);
  202. BENCHMARK(BM_SchurEliminatorBackSubstitute)->Range(10, 10000);
  203. BENCHMARK(BM_SchurEliminatorForOneFBlockBackSubstitute)->Range(10, 10000);
  204. } // namespace ceres::internal
  205. BENCHMARK_MAIN();