dense_linear_solver_benchmark.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "Eigen/Dense"
  31. #include "benchmark/benchmark.h"
  32. #include "ceres/context_impl.h"
  33. #include "ceres/dense_sparse_matrix.h"
  34. #include "ceres/internal/config.h"
  35. #include "ceres/linear_solver.h"
  36. namespace ceres::internal {
  37. template <ceres::DenseLinearAlgebraLibraryType kLibraryType,
  38. ceres::LinearSolverType kSolverType>
  39. static void BM_DenseSolver(benchmark::State& state) {
  40. const int num_rows = static_cast<int>(state.range(0));
  41. const int num_cols = static_cast<int>(state.range(1));
  42. DenseSparseMatrix jacobian(num_rows, num_cols);
  43. *jacobian.mutable_matrix() = Eigen::MatrixXd::Random(num_rows, num_cols);
  44. Eigen::VectorXd rhs = Eigen::VectorXd::Random(num_rows, 1);
  45. Eigen::VectorXd solution(num_cols);
  46. LinearSolver::Options options;
  47. options.type = kSolverType;
  48. options.dense_linear_algebra_library_type = kLibraryType;
  49. ContextImpl context;
  50. options.context = &context;
  51. auto solver = LinearSolver::Create(options);
  52. LinearSolver::PerSolveOptions per_solve_options;
  53. Eigen::VectorXd diagonal = Eigen::VectorXd::Ones(num_cols) * 100;
  54. per_solve_options.D = diagonal.data();
  55. for (auto _ : state) {
  56. solver->Solve(&jacobian, rhs.data(), per_solve_options, solution.data());
  57. }
  58. }
  59. // Some reasonable matrix sizes. I picked them out of thin air.
  60. static void MatrixSizes(benchmark::internal::Benchmark* b) {
  61. // {num_rows, num_cols}
  62. b->Args({1, 1});
  63. b->Args({2, 1});
  64. b->Args({3, 1});
  65. b->Args({6, 2});
  66. b->Args({10, 3});
  67. b->Args({12, 4});
  68. b->Args({20, 5});
  69. b->Args({40, 5});
  70. b->Args({100, 10});
  71. b->Args({150, 15});
  72. b->Args({200, 16});
  73. b->Args({225, 18});
  74. b->Args({300, 20});
  75. b->Args({400, 20});
  76. b->Args({600, 22});
  77. b->Args({800, 25});
  78. }
  79. BENCHMARK_TEMPLATE2(BM_DenseSolver, ceres::EIGEN, ceres::DENSE_QR)
  80. ->Apply(MatrixSizes);
  81. BENCHMARK_TEMPLATE2(BM_DenseSolver, ceres::EIGEN, ceres::DENSE_NORMAL_CHOLESKY)
  82. ->Apply(MatrixSizes);
  83. #ifndef CERES_NO_LAPACK
  84. BENCHMARK_TEMPLATE2(BM_DenseSolver, ceres::LAPACK, ceres::DENSE_QR)
  85. ->Apply(MatrixSizes);
  86. BENCHMARK_TEMPLATE2(BM_DenseSolver, ceres::LAPACK, ceres::DENSE_NORMAL_CHOLESKY)
  87. ->Apply(MatrixSizes);
  88. #endif // CERES_NO_LAPACK
  89. #ifndef CERES_NO_CUDA
  90. BENCHMARK_TEMPLATE2(BM_DenseSolver, ceres::CUDA, ceres::DENSE_NORMAL_CHOLESKY)
  91. ->Apply(MatrixSizes);
  92. BENCHMARK_TEMPLATE2(BM_DenseSolver, ceres::CUDA, ceres::DENSE_QR)
  93. ->Apply(MatrixSizes);
  94. #endif // CERES_NO_CUDA
  95. } // namespace ceres::internal
  96. BENCHMARK_MAIN();