block_jacobi_preconditioner_benchmark.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <memory>
  31. #include <random>
  32. #include "Eigen/Dense"
  33. #include "benchmark/benchmark.h"
  34. #include "ceres/block_jacobi_preconditioner.h"
  35. #include "ceres/block_sparse_matrix.h"
  36. #include "ceres/fake_bundle_adjustment_jacobian.h"
  37. #include "ceres/internal/config.h"
  38. #include "ceres/internal/eigen.h"
  39. namespace ceres::internal {
  40. constexpr int kNumCameras = 1000;
  41. constexpr int kNumPoints = 10000;
  42. constexpr int kCameraSize = 6;
  43. constexpr int kPointSize = 3;
  44. constexpr double kVisibility = 0.1;
  45. constexpr int kNumRowBlocks = 100000;
  46. constexpr int kNumColBlocks = 10000;
  47. constexpr int kMinRowBlockSize = 1;
  48. constexpr int kMaxRowBlockSize = 5;
  49. constexpr int kMinColBlockSize = 1;
  50. constexpr int kMaxColBlockSize = 15;
  51. constexpr double kBlockDensity = 5.0 / kNumColBlocks;
  52. static void BM_BlockSparseJacobiPreconditionerBA(benchmark::State& state) {
  53. std::mt19937 prng;
  54. auto jacobian = CreateFakeBundleAdjustmentJacobian(
  55. kNumCameras, kNumPoints, kCameraSize, kPointSize, kVisibility, prng);
  56. Preconditioner::Options preconditioner_options;
  57. ContextImpl context;
  58. preconditioner_options.context = &context;
  59. preconditioner_options.num_threads = static_cast<int>(state.range(0));
  60. context.EnsureMinimumThreads(preconditioner_options.num_threads);
  61. BlockSparseJacobiPreconditioner p(preconditioner_options, *jacobian);
  62. Vector d = Vector::Ones(jacobian->num_cols());
  63. for (auto _ : state) {
  64. p.Update(*jacobian, d.data());
  65. }
  66. }
  67. BENCHMARK(BM_BlockSparseJacobiPreconditionerBA)
  68. ->Arg(1)
  69. ->Arg(2)
  70. ->Arg(4)
  71. ->Arg(8)
  72. ->Arg(16);
  73. static void BM_BlockCRSJacobiPreconditionerBA(benchmark::State& state) {
  74. std::mt19937 prng;
  75. auto jacobian = CreateFakeBundleAdjustmentJacobian(
  76. kNumCameras, kNumPoints, kCameraSize, kPointSize, kVisibility, prng);
  77. auto jacobian_crs = jacobian->ToCompressedRowSparseMatrix();
  78. Preconditioner::Options preconditioner_options;
  79. ContextImpl context;
  80. preconditioner_options.context = &context;
  81. preconditioner_options.num_threads = static_cast<int>(state.range(0));
  82. context.EnsureMinimumThreads(preconditioner_options.num_threads);
  83. BlockCRSJacobiPreconditioner p(preconditioner_options, *jacobian_crs);
  84. Vector d = Vector::Ones(jacobian_crs->num_cols());
  85. for (auto _ : state) {
  86. p.Update(*jacobian_crs, d.data());
  87. }
  88. }
  89. BENCHMARK(BM_BlockCRSJacobiPreconditionerBA)
  90. ->Arg(1)
  91. ->Arg(2)
  92. ->Arg(4)
  93. ->Arg(8)
  94. ->Arg(16);
  95. static void BM_BlockSparseJacobiPreconditionerUnstructured(
  96. benchmark::State& state) {
  97. BlockSparseMatrix::RandomMatrixOptions options;
  98. options.num_row_blocks = kNumRowBlocks;
  99. options.num_col_blocks = kNumColBlocks;
  100. options.min_row_block_size = kMinRowBlockSize;
  101. options.min_col_block_size = kMinColBlockSize;
  102. options.max_row_block_size = kMaxRowBlockSize;
  103. options.max_col_block_size = kMaxColBlockSize;
  104. options.block_density = kBlockDensity;
  105. std::mt19937 prng;
  106. auto jacobian = BlockSparseMatrix::CreateRandomMatrix(options, prng);
  107. Preconditioner::Options preconditioner_options;
  108. ContextImpl context;
  109. preconditioner_options.context = &context;
  110. preconditioner_options.num_threads = static_cast<int>(state.range(0));
  111. context.EnsureMinimumThreads(preconditioner_options.num_threads);
  112. BlockSparseJacobiPreconditioner p(preconditioner_options, *jacobian);
  113. Vector d = Vector::Ones(jacobian->num_cols());
  114. for (auto _ : state) {
  115. p.Update(*jacobian, d.data());
  116. }
  117. }
  118. BENCHMARK(BM_BlockSparseJacobiPreconditionerUnstructured)
  119. ->Arg(1)
  120. ->Arg(2)
  121. ->Arg(4)
  122. ->Arg(8)
  123. ->Arg(16);
  124. static void BM_BlockCRSJacobiPreconditionerUnstructured(
  125. benchmark::State& state) {
  126. BlockSparseMatrix::RandomMatrixOptions options;
  127. options.num_row_blocks = kNumRowBlocks;
  128. options.num_col_blocks = kNumColBlocks;
  129. options.min_row_block_size = kMinRowBlockSize;
  130. options.min_col_block_size = kMinColBlockSize;
  131. options.max_row_block_size = kMaxRowBlockSize;
  132. options.max_col_block_size = kMaxColBlockSize;
  133. options.block_density = kBlockDensity;
  134. std::mt19937 prng;
  135. auto jacobian = BlockSparseMatrix::CreateRandomMatrix(options, prng);
  136. auto jacobian_crs = jacobian->ToCompressedRowSparseMatrix();
  137. Preconditioner::Options preconditioner_options;
  138. ContextImpl context;
  139. preconditioner_options.context = &context;
  140. preconditioner_options.num_threads = static_cast<int>(state.range(0));
  141. context.EnsureMinimumThreads(preconditioner_options.num_threads);
  142. BlockCRSJacobiPreconditioner p(preconditioner_options, *jacobian_crs);
  143. Vector d = Vector::Ones(jacobian_crs->num_cols());
  144. for (auto _ : state) {
  145. p.Update(*jacobian_crs, d.data());
  146. }
  147. }
  148. BENCHMARK(BM_BlockCRSJacobiPreconditionerUnstructured)
  149. ->Arg(1)
  150. ->Arg(2)
  151. ->Arg(4)
  152. ->Arg(8)
  153. ->Arg(16);
  154. } // namespace ceres::internal
  155. BENCHMARK_MAIN();