evaluation_benchmark.cc 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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: dmitriy.korchemkin@gmail.com (Dmitriy Korchemkin)
  30. #include <memory>
  31. #include <random>
  32. #include <string>
  33. #include <vector>
  34. #include "benchmark/benchmark.h"
  35. #include "ceres/block_sparse_matrix.h"
  36. #include "ceres/bundle_adjustment_test_util.h"
  37. #include "ceres/cuda_block_sparse_crs_view.h"
  38. #include "ceres/cuda_partitioned_block_sparse_crs_view.h"
  39. #include "ceres/cuda_sparse_matrix.h"
  40. #include "ceres/cuda_vector.h"
  41. #include "ceres/evaluator.h"
  42. #include "ceres/implicit_schur_complement.h"
  43. #include "ceres/partitioned_matrix_view.h"
  44. #include "ceres/power_series_expansion_preconditioner.h"
  45. #include "ceres/preprocessor.h"
  46. #include "ceres/problem.h"
  47. #include "ceres/problem_impl.h"
  48. #include "ceres/program.h"
  49. #include "ceres/sparse_matrix.h"
  50. namespace ceres::internal {
  51. template <typename Derived, typename Base>
  52. std::unique_ptr<Derived> downcast_unique_ptr(std::unique_ptr<Base>& base) {
  53. return std::unique_ptr<Derived>(dynamic_cast<Derived*>(base.release()));
  54. }
  55. // Benchmark library might invoke benchmark function multiple times.
  56. // In order to save time required to parse BAL data, we ensure that
  57. // each dataset is being loaded at most once.
  58. // Each type of jacobians is also cached after first creation
  59. struct BALData {
  60. using PartitionedView = PartitionedMatrixView<2, 3, 9>;
  61. explicit BALData(const std::string& path) {
  62. bal_problem = std::make_unique<BundleAdjustmentProblem>(path);
  63. CHECK(bal_problem != nullptr);
  64. auto problem_impl = bal_problem->mutable_problem()->mutable_impl();
  65. auto preprocessor = Preprocessor::Create(MinimizerType::TRUST_REGION);
  66. preprocessed_problem = std::make_unique<PreprocessedProblem>();
  67. Solver::Options options = bal_problem->options();
  68. options.linear_solver_type = ITERATIVE_SCHUR;
  69. CHECK(preprocessor->Preprocess(
  70. options, problem_impl, preprocessed_problem.get()));
  71. auto program = preprocessed_problem->reduced_program.get();
  72. parameters.resize(program->NumParameters());
  73. program->ParameterBlocksToStateVector(parameters.data());
  74. const int num_residuals = program->NumResiduals();
  75. b.resize(num_residuals);
  76. std::mt19937 rng;
  77. std::normal_distribution<double> rnorm;
  78. for (int i = 0; i < num_residuals; ++i) {
  79. b[i] = rnorm(rng);
  80. }
  81. const int num_parameters = program->NumParameters();
  82. D.resize(num_parameters);
  83. for (int i = 0; i < num_parameters; ++i) {
  84. D[i] = rnorm(rng);
  85. }
  86. }
  87. std::unique_ptr<BlockSparseMatrix> CreateBlockSparseJacobian(
  88. ContextImpl* context, bool sequential) {
  89. auto problem = bal_problem->mutable_problem();
  90. auto problem_impl = problem->mutable_impl();
  91. CHECK(problem_impl != nullptr);
  92. Evaluator::Options options;
  93. options.linear_solver_type = ITERATIVE_SCHUR;
  94. options.num_threads = 1;
  95. options.context = context;
  96. options.num_eliminate_blocks = bal_problem->num_points();
  97. std::string error;
  98. auto program = preprocessed_problem->reduced_program.get();
  99. auto evaluator = Evaluator::Create(options, program, &error);
  100. CHECK(evaluator != nullptr);
  101. auto jacobian = evaluator->CreateJacobian();
  102. auto block_sparse = downcast_unique_ptr<BlockSparseMatrix>(jacobian);
  103. CHECK(block_sparse != nullptr);
  104. if (sequential) {
  105. auto block_structure_sequential =
  106. std::make_unique<CompressedRowBlockStructure>(
  107. *block_sparse->block_structure());
  108. int num_nonzeros = 0;
  109. for (auto& row_block : block_structure_sequential->rows) {
  110. const int row_block_size = row_block.block.size;
  111. for (auto& cell : row_block.cells) {
  112. const int col_block_size =
  113. block_structure_sequential->cols[cell.block_id].size;
  114. cell.position = num_nonzeros;
  115. num_nonzeros += col_block_size * row_block_size;
  116. }
  117. }
  118. block_sparse = std::make_unique<BlockSparseMatrix>(
  119. block_structure_sequential.release(),
  120. #ifndef CERES_NO_CUDA
  121. true
  122. #else
  123. false
  124. #endif
  125. );
  126. }
  127. std::mt19937 rng;
  128. std::normal_distribution<double> rnorm;
  129. const int nnz = block_sparse->num_nonzeros();
  130. auto values = block_sparse->mutable_values();
  131. for (int i = 0; i < nnz; ++i) {
  132. values[i] = rnorm(rng);
  133. }
  134. return block_sparse;
  135. }
  136. std::unique_ptr<CompressedRowSparseMatrix> CreateCompressedRowSparseJacobian(
  137. ContextImpl* context) {
  138. auto block_sparse = BlockSparseJacobian(context);
  139. return block_sparse->ToCompressedRowSparseMatrix();
  140. }
  141. const BlockSparseMatrix* BlockSparseJacobian(ContextImpl* context) {
  142. if (!block_sparse_jacobian) {
  143. block_sparse_jacobian = CreateBlockSparseJacobian(context, true);
  144. }
  145. return block_sparse_jacobian.get();
  146. }
  147. const BlockSparseMatrix* BlockSparseJacobianPartitioned(
  148. ContextImpl* context) {
  149. if (!block_sparse_jacobian_partitioned) {
  150. block_sparse_jacobian_partitioned =
  151. CreateBlockSparseJacobian(context, false);
  152. }
  153. return block_sparse_jacobian_partitioned.get();
  154. }
  155. const CompressedRowSparseMatrix* CompressedRowSparseJacobian(
  156. ContextImpl* context) {
  157. if (!crs_jacobian) {
  158. crs_jacobian = CreateCompressedRowSparseJacobian(context);
  159. }
  160. return crs_jacobian.get();
  161. }
  162. std::unique_ptr<PartitionedView> PartitionedMatrixViewJacobian(
  163. const LinearSolver::Options& options) {
  164. auto block_sparse = BlockSparseJacobianPartitioned(options.context);
  165. return std::make_unique<PartitionedView>(options, *block_sparse);
  166. }
  167. BlockSparseMatrix* BlockDiagonalEtE(const LinearSolver::Options& options) {
  168. if (!block_diagonal_ete) {
  169. auto partitioned_view = PartitionedMatrixViewJacobian(options);
  170. block_diagonal_ete = partitioned_view->CreateBlockDiagonalEtE();
  171. }
  172. return block_diagonal_ete.get();
  173. }
  174. BlockSparseMatrix* BlockDiagonalFtF(const LinearSolver::Options& options) {
  175. if (!block_diagonal_ftf) {
  176. auto partitioned_view = PartitionedMatrixViewJacobian(options);
  177. block_diagonal_ftf = partitioned_view->CreateBlockDiagonalFtF();
  178. }
  179. return block_diagonal_ftf.get();
  180. }
  181. const ImplicitSchurComplement* ImplicitSchurComplementWithoutDiagonal(
  182. const LinearSolver::Options& options) {
  183. auto block_sparse = BlockSparseJacobianPartitioned(options.context);
  184. implicit_schur_complement =
  185. std::make_unique<ImplicitSchurComplement>(options);
  186. implicit_schur_complement->Init(*block_sparse, nullptr, b.data());
  187. return implicit_schur_complement.get();
  188. }
  189. const ImplicitSchurComplement* ImplicitSchurComplementWithDiagonal(
  190. const LinearSolver::Options& options) {
  191. auto block_sparse = BlockSparseJacobianPartitioned(options.context);
  192. implicit_schur_complement_diag =
  193. std::make_unique<ImplicitSchurComplement>(options);
  194. implicit_schur_complement_diag->Init(*block_sparse, D.data(), b.data());
  195. return implicit_schur_complement_diag.get();
  196. }
  197. Vector parameters;
  198. Vector D;
  199. Vector b;
  200. std::unique_ptr<BundleAdjustmentProblem> bal_problem;
  201. std::unique_ptr<PreprocessedProblem> preprocessed_problem;
  202. std::unique_ptr<BlockSparseMatrix> block_sparse_jacobian_partitioned;
  203. std::unique_ptr<BlockSparseMatrix> block_sparse_jacobian;
  204. std::unique_ptr<CompressedRowSparseMatrix> crs_jacobian;
  205. std::unique_ptr<BlockSparseMatrix> block_diagonal_ete;
  206. std::unique_ptr<BlockSparseMatrix> block_diagonal_ftf;
  207. std::unique_ptr<ImplicitSchurComplement> implicit_schur_complement;
  208. std::unique_ptr<ImplicitSchurComplement> implicit_schur_complement_diag;
  209. };
  210. static void Residuals(benchmark::State& state,
  211. BALData* data,
  212. ContextImpl* context) {
  213. const int num_threads = static_cast<int>(state.range(0));
  214. Evaluator::Options options;
  215. options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
  216. options.num_threads = num_threads;
  217. options.context = context;
  218. options.num_eliminate_blocks = 0;
  219. std::string error;
  220. CHECK(data->preprocessed_problem != nullptr);
  221. auto program = data->preprocessed_problem->reduced_program.get();
  222. CHECK(program != nullptr);
  223. auto evaluator = Evaluator::Create(options, program, &error);
  224. CHECK(evaluator != nullptr);
  225. double cost = 0.;
  226. Vector residuals = Vector::Zero(program->NumResiduals());
  227. Evaluator::EvaluateOptions eval_options;
  228. for (auto _ : state) {
  229. CHECK(evaluator->Evaluate(eval_options,
  230. data->parameters.data(),
  231. &cost,
  232. residuals.data(),
  233. nullptr,
  234. nullptr));
  235. }
  236. }
  237. static void ResidualsAndJacobian(benchmark::State& state,
  238. BALData* data,
  239. ContextImpl* context) {
  240. const int num_threads = static_cast<int>(state.range(0));
  241. Evaluator::Options options;
  242. options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
  243. options.num_threads = num_threads;
  244. options.context = context;
  245. options.num_eliminate_blocks = 0;
  246. std::string error;
  247. CHECK(data->preprocessed_problem != nullptr);
  248. auto program = data->preprocessed_problem->reduced_program.get();
  249. CHECK(program != nullptr);
  250. auto evaluator = Evaluator::Create(options, program, &error);
  251. CHECK(evaluator != nullptr);
  252. double cost = 0.;
  253. Vector residuals = Vector::Zero(program->NumResiduals());
  254. auto jacobian = evaluator->CreateJacobian();
  255. Evaluator::EvaluateOptions eval_options;
  256. for (auto _ : state) {
  257. CHECK(evaluator->Evaluate(eval_options,
  258. data->parameters.data(),
  259. &cost,
  260. residuals.data(),
  261. nullptr,
  262. jacobian.get()));
  263. }
  264. }
  265. static void Plus(benchmark::State& state, BALData* data, ContextImpl* context) {
  266. const int num_threads = static_cast<int>(state.range(0));
  267. Evaluator::Options options;
  268. options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
  269. options.num_threads = num_threads;
  270. options.context = context;
  271. options.num_eliminate_blocks = 0;
  272. std::string error;
  273. CHECK(data->preprocessed_problem != nullptr);
  274. auto program = data->preprocessed_problem->reduced_program.get();
  275. CHECK(program != nullptr);
  276. auto evaluator = Evaluator::Create(options, program, &error);
  277. CHECK(evaluator != nullptr);
  278. Vector state_plus_delta = Vector::Zero(program->NumParameters());
  279. Vector delta = Vector::Random(program->NumEffectiveParameters());
  280. for (auto _ : state) {
  281. CHECK(evaluator->Plus(
  282. data->parameters.data(), delta.data(), state_plus_delta.data()));
  283. }
  284. CHECK_GT(state_plus_delta.squaredNorm(), 0.);
  285. }
  286. static void PSEPreconditioner(benchmark::State& state,
  287. BALData* data,
  288. ContextImpl* context) {
  289. LinearSolver::Options options;
  290. options.num_threads = static_cast<int>(state.range(0));
  291. options.elimination_groups.push_back(data->bal_problem->num_points());
  292. options.context = context;
  293. auto jacobian = data->ImplicitSchurComplementWithDiagonal(options);
  294. Preconditioner::Options preconditioner_options(options);
  295. PowerSeriesExpansionPreconditioner preconditioner(
  296. jacobian, 10, 0, preconditioner_options);
  297. Vector y = Vector::Zero(jacobian->num_cols());
  298. Vector x = Vector::Random(jacobian->num_cols());
  299. for (auto _ : state) {
  300. preconditioner.RightMultiplyAndAccumulate(x.data(), y.data());
  301. }
  302. CHECK_GT(y.squaredNorm(), 0.);
  303. }
  304. static void PMVRightMultiplyAndAccumulateF(benchmark::State& state,
  305. BALData* data,
  306. ContextImpl* context) {
  307. LinearSolver::Options options;
  308. options.num_threads = static_cast<int>(state.range(0));
  309. options.elimination_groups.push_back(data->bal_problem->num_points());
  310. options.context = context;
  311. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  312. Vector y = Vector::Zero(jacobian->num_rows());
  313. Vector x = Vector::Random(jacobian->num_cols_f());
  314. for (auto _ : state) {
  315. jacobian->RightMultiplyAndAccumulateF(x.data(), y.data());
  316. }
  317. CHECK_GT(y.squaredNorm(), 0.);
  318. }
  319. static void PMVLeftMultiplyAndAccumulateF(benchmark::State& state,
  320. BALData* data,
  321. ContextImpl* context) {
  322. LinearSolver::Options options;
  323. options.num_threads = static_cast<int>(state.range(0));
  324. options.elimination_groups.push_back(data->bal_problem->num_points());
  325. options.context = context;
  326. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  327. Vector y = Vector::Zero(jacobian->num_cols_f());
  328. Vector x = Vector::Random(jacobian->num_rows());
  329. for (auto _ : state) {
  330. jacobian->LeftMultiplyAndAccumulateF(x.data(), y.data());
  331. }
  332. CHECK_GT(y.squaredNorm(), 0.);
  333. }
  334. static void PMVRightMultiplyAndAccumulateE(benchmark::State& state,
  335. BALData* data,
  336. ContextImpl* context) {
  337. LinearSolver::Options options;
  338. options.num_threads = static_cast<int>(state.range(0));
  339. options.elimination_groups.push_back(data->bal_problem->num_points());
  340. options.context = context;
  341. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  342. Vector y = Vector::Zero(jacobian->num_rows());
  343. Vector x = Vector::Random(jacobian->num_cols_e());
  344. for (auto _ : state) {
  345. jacobian->RightMultiplyAndAccumulateE(x.data(), y.data());
  346. }
  347. CHECK_GT(y.squaredNorm(), 0.);
  348. }
  349. static void PMVLeftMultiplyAndAccumulateE(benchmark::State& state,
  350. BALData* data,
  351. ContextImpl* context) {
  352. LinearSolver::Options options;
  353. options.num_threads = static_cast<int>(state.range(0));
  354. options.elimination_groups.push_back(data->bal_problem->num_points());
  355. options.context = context;
  356. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  357. Vector y = Vector::Zero(jacobian->num_cols_e());
  358. Vector x = Vector::Random(jacobian->num_rows());
  359. for (auto _ : state) {
  360. jacobian->LeftMultiplyAndAccumulateE(x.data(), y.data());
  361. }
  362. CHECK_GT(y.squaredNorm(), 0.);
  363. }
  364. static void PMVUpdateBlockDiagonalEtE(benchmark::State& state,
  365. BALData* data,
  366. ContextImpl* context) {
  367. LinearSolver::Options options;
  368. options.num_threads = static_cast<int>(state.range(0));
  369. options.elimination_groups.push_back(data->bal_problem->num_points());
  370. options.context = context;
  371. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  372. auto block_diagonal_ete = data->BlockDiagonalEtE(options);
  373. for (auto _ : state) {
  374. jacobian->UpdateBlockDiagonalEtE(block_diagonal_ete);
  375. }
  376. }
  377. static void PMVUpdateBlockDiagonalFtF(benchmark::State& state,
  378. BALData* data,
  379. ContextImpl* context) {
  380. LinearSolver::Options options;
  381. options.num_threads = static_cast<int>(state.range(0));
  382. options.elimination_groups.push_back(data->bal_problem->num_points());
  383. options.context = context;
  384. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  385. auto block_diagonal_ftf = data->BlockDiagonalFtF(options);
  386. for (auto _ : state) {
  387. jacobian->UpdateBlockDiagonalFtF(block_diagonal_ftf);
  388. }
  389. }
  390. static void ISCRightMultiplyNoDiag(benchmark::State& state,
  391. BALData* data,
  392. ContextImpl* context) {
  393. LinearSolver::Options options;
  394. options.num_threads = static_cast<int>(state.range(0));
  395. options.elimination_groups.push_back(data->bal_problem->num_points());
  396. options.context = context;
  397. auto jacobian = data->ImplicitSchurComplementWithoutDiagonal(options);
  398. Vector y = Vector::Zero(jacobian->num_rows());
  399. Vector x = Vector::Random(jacobian->num_cols());
  400. for (auto _ : state) {
  401. jacobian->RightMultiplyAndAccumulate(x.data(), y.data());
  402. }
  403. CHECK_GT(y.squaredNorm(), 0.);
  404. }
  405. static void ISCRightMultiplyDiag(benchmark::State& state,
  406. BALData* data,
  407. ContextImpl* context) {
  408. LinearSolver::Options options;
  409. options.num_threads = static_cast<int>(state.range(0));
  410. options.elimination_groups.push_back(data->bal_problem->num_points());
  411. options.context = context;
  412. auto jacobian = data->ImplicitSchurComplementWithDiagonal(options);
  413. Vector y = Vector::Zero(jacobian->num_rows());
  414. Vector x = Vector::Random(jacobian->num_cols());
  415. for (auto _ : state) {
  416. jacobian->RightMultiplyAndAccumulate(x.data(), y.data());
  417. }
  418. CHECK_GT(y.squaredNorm(), 0.);
  419. }
  420. static void JacobianToCRS(benchmark::State& state,
  421. BALData* data,
  422. ContextImpl* context) {
  423. auto jacobian = data->BlockSparseJacobian(context);
  424. std::unique_ptr<CompressedRowSparseMatrix> matrix;
  425. for (auto _ : state) {
  426. matrix = jacobian->ToCompressedRowSparseMatrix();
  427. }
  428. CHECK(matrix != nullptr);
  429. }
  430. #ifndef CERES_NO_CUDA
  431. static void PMVRightMultiplyAndAccumulateFCuda(benchmark::State& state,
  432. BALData* data,
  433. ContextImpl* context) {
  434. LinearSolver::Options options;
  435. options.elimination_groups.push_back(data->bal_problem->num_points());
  436. options.context = context;
  437. options.num_threads = 1;
  438. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  439. auto underlying_matrix = data->BlockSparseJacobianPartitioned(context);
  440. CudaPartitionedBlockSparseCRSView view(
  441. *underlying_matrix, jacobian->num_col_blocks_e(), context);
  442. Vector x = Vector::Random(jacobian->num_cols_f());
  443. CudaVector cuda_x(context, x.size());
  444. CudaVector cuda_y(context, jacobian->num_rows());
  445. cuda_x.CopyFromCpu(x);
  446. cuda_y.SetZero();
  447. auto matrix = view.matrix_f();
  448. for (auto _ : state) {
  449. matrix->RightMultiplyAndAccumulate(cuda_x, &cuda_y);
  450. }
  451. CHECK_GT(cuda_y.Norm(), 0.);
  452. }
  453. static void PMVLeftMultiplyAndAccumulateFCuda(benchmark::State& state,
  454. BALData* data,
  455. ContextImpl* context) {
  456. LinearSolver::Options options;
  457. options.elimination_groups.push_back(data->bal_problem->num_points());
  458. options.context = context;
  459. options.num_threads = 1;
  460. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  461. auto underlying_matrix = data->BlockSparseJacobianPartitioned(context);
  462. CudaPartitionedBlockSparseCRSView view(
  463. *underlying_matrix, jacobian->num_col_blocks_e(), context);
  464. Vector x = Vector::Random(jacobian->num_rows());
  465. CudaVector cuda_x(context, x.size());
  466. CudaVector cuda_y(context, jacobian->num_cols_f());
  467. cuda_x.CopyFromCpu(x);
  468. cuda_y.SetZero();
  469. auto matrix = view.matrix_f();
  470. for (auto _ : state) {
  471. matrix->LeftMultiplyAndAccumulate(cuda_x, &cuda_y);
  472. }
  473. CHECK_GT(cuda_y.Norm(), 0.);
  474. }
  475. static void PMVRightMultiplyAndAccumulateECuda(benchmark::State& state,
  476. BALData* data,
  477. ContextImpl* context) {
  478. LinearSolver::Options options;
  479. options.elimination_groups.push_back(data->bal_problem->num_points());
  480. options.context = context;
  481. options.num_threads = 1;
  482. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  483. auto underlying_matrix = data->BlockSparseJacobianPartitioned(context);
  484. CudaPartitionedBlockSparseCRSView view(
  485. *underlying_matrix, jacobian->num_col_blocks_e(), context);
  486. Vector x = Vector::Random(jacobian->num_cols_e());
  487. CudaVector cuda_x(context, x.size());
  488. CudaVector cuda_y(context, jacobian->num_rows());
  489. cuda_x.CopyFromCpu(x);
  490. cuda_y.SetZero();
  491. auto matrix = view.matrix_e();
  492. for (auto _ : state) {
  493. matrix->RightMultiplyAndAccumulate(cuda_x, &cuda_y);
  494. }
  495. CHECK_GT(cuda_y.Norm(), 0.);
  496. }
  497. static void PMVLeftMultiplyAndAccumulateECuda(benchmark::State& state,
  498. BALData* data,
  499. ContextImpl* context) {
  500. LinearSolver::Options options;
  501. options.elimination_groups.push_back(data->bal_problem->num_points());
  502. options.context = context;
  503. options.num_threads = 1;
  504. auto jacobian = data->PartitionedMatrixViewJacobian(options);
  505. auto underlying_matrix = data->BlockSparseJacobianPartitioned(context);
  506. CudaPartitionedBlockSparseCRSView view(
  507. *underlying_matrix, jacobian->num_col_blocks_e(), context);
  508. Vector x = Vector::Random(jacobian->num_rows());
  509. CudaVector cuda_x(context, x.size());
  510. CudaVector cuda_y(context, jacobian->num_cols_e());
  511. cuda_x.CopyFromCpu(x);
  512. cuda_y.SetZero();
  513. auto matrix = view.matrix_e();
  514. for (auto _ : state) {
  515. matrix->LeftMultiplyAndAccumulate(cuda_x, &cuda_y);
  516. }
  517. CHECK_GT(cuda_y.Norm(), 0.);
  518. }
  519. // We want CudaBlockSparseCRSView to be not slower than explicit conversion to
  520. // CRS on CPU
  521. static void JacobianToCRSView(benchmark::State& state,
  522. BALData* data,
  523. ContextImpl* context) {
  524. auto jacobian = data->BlockSparseJacobian(context);
  525. std::unique_ptr<CudaBlockSparseCRSView> matrix;
  526. for (auto _ : state) {
  527. matrix = std::make_unique<CudaBlockSparseCRSView>(*jacobian, context);
  528. }
  529. CHECK(matrix != nullptr);
  530. }
  531. static void JacobianToCRSMatrix(benchmark::State& state,
  532. BALData* data,
  533. ContextImpl* context) {
  534. auto jacobian = data->BlockSparseJacobian(context);
  535. std::unique_ptr<CudaSparseMatrix> matrix;
  536. std::unique_ptr<CompressedRowSparseMatrix> matrix_cpu;
  537. for (auto _ : state) {
  538. matrix_cpu = jacobian->ToCompressedRowSparseMatrix();
  539. matrix = std::make_unique<CudaSparseMatrix>(context, *matrix_cpu);
  540. }
  541. CHECK(matrix != nullptr);
  542. }
  543. // Updating values in CudaBlockSparseCRSView should be +- as fast as just
  544. // copying values (time spent in value permutation has to be hidden by PCIe
  545. // transfer)
  546. static void JacobianToCRSViewUpdate(benchmark::State& state,
  547. BALData* data,
  548. ContextImpl* context) {
  549. auto jacobian = data->BlockSparseJacobian(context);
  550. auto matrix = CudaBlockSparseCRSView(*jacobian, context);
  551. for (auto _ : state) {
  552. matrix.UpdateValues(*jacobian);
  553. }
  554. }
  555. static void JacobianToCRSMatrixUpdate(benchmark::State& state,
  556. BALData* data,
  557. ContextImpl* context) {
  558. auto jacobian = data->BlockSparseJacobian(context);
  559. auto matrix_cpu = jacobian->ToCompressedRowSparseMatrix();
  560. auto matrix = std::make_unique<CudaSparseMatrix>(context, *matrix_cpu);
  561. for (auto _ : state) {
  562. CHECK_EQ(cudaSuccess,
  563. cudaMemcpy(matrix->mutable_values(),
  564. matrix_cpu->values(),
  565. matrix->num_nonzeros() * sizeof(double),
  566. cudaMemcpyHostToDevice));
  567. }
  568. }
  569. #endif
  570. static void JacobianSquaredColumnNorm(benchmark::State& state,
  571. BALData* data,
  572. ContextImpl* context) {
  573. const int num_threads = static_cast<int>(state.range(0));
  574. auto jacobian = data->BlockSparseJacobian(context);
  575. Vector x = Vector::Zero(jacobian->num_cols());
  576. for (auto _ : state) {
  577. jacobian->SquaredColumnNorm(x.data(), context, num_threads);
  578. }
  579. CHECK_GT(x.squaredNorm(), 0.);
  580. }
  581. static void JacobianScaleColumns(benchmark::State& state,
  582. BALData* data,
  583. ContextImpl* context) {
  584. const int num_threads = static_cast<int>(state.range(0));
  585. auto jacobian_const = data->BlockSparseJacobian(context);
  586. auto jacobian = const_cast<BlockSparseMatrix*>(jacobian_const);
  587. Vector x = Vector::Ones(jacobian->num_cols());
  588. for (auto _ : state) {
  589. jacobian->ScaleColumns(x.data(), context, num_threads);
  590. }
  591. }
  592. static void JacobianRightMultiplyAndAccumulate(benchmark::State& state,
  593. BALData* data,
  594. ContextImpl* context) {
  595. const int num_threads = static_cast<int>(state.range(0));
  596. auto jacobian = data->BlockSparseJacobian(context);
  597. Vector y = Vector::Zero(jacobian->num_rows());
  598. Vector x = Vector::Random(jacobian->num_cols());
  599. for (auto _ : state) {
  600. jacobian->RightMultiplyAndAccumulate(
  601. x.data(), y.data(), context, num_threads);
  602. }
  603. CHECK_GT(y.squaredNorm(), 0.);
  604. }
  605. static void JacobianLeftMultiplyAndAccumulate(benchmark::State& state,
  606. BALData* data,
  607. ContextImpl* context) {
  608. const int num_threads = static_cast<int>(state.range(0));
  609. auto jacobian = data->BlockSparseJacobian(context);
  610. Vector y = Vector::Zero(jacobian->num_cols());
  611. Vector x = Vector::Random(jacobian->num_rows());
  612. for (auto _ : state) {
  613. jacobian->LeftMultiplyAndAccumulate(
  614. x.data(), y.data(), context, num_threads);
  615. }
  616. CHECK_GT(y.squaredNorm(), 0.);
  617. }
  618. #ifndef CERES_NO_CUDA
  619. static void JacobianRightMultiplyAndAccumulateCuda(benchmark::State& state,
  620. BALData* data,
  621. ContextImpl* context) {
  622. auto crs_jacobian = data->CompressedRowSparseJacobian(context);
  623. CudaSparseMatrix cuda_jacobian(context, *crs_jacobian);
  624. CudaVector cuda_x(context, 0);
  625. CudaVector cuda_y(context, 0);
  626. Vector x(crs_jacobian->num_cols());
  627. Vector y(crs_jacobian->num_rows());
  628. x.setRandom();
  629. y.setRandom();
  630. cuda_x.CopyFromCpu(x);
  631. cuda_y.CopyFromCpu(y);
  632. double sum = 0;
  633. for (auto _ : state) {
  634. cuda_jacobian.RightMultiplyAndAccumulate(cuda_x, &cuda_y);
  635. sum += cuda_y.Norm();
  636. CHECK_EQ(cudaDeviceSynchronize(), cudaSuccess);
  637. }
  638. CHECK_NE(sum, 0.0);
  639. }
  640. static void JacobianLeftMultiplyAndAccumulateCuda(benchmark::State& state,
  641. BALData* data,
  642. ContextImpl* context) {
  643. auto crs_jacobian = data->CompressedRowSparseJacobian(context);
  644. CudaSparseMatrix cuda_jacobian(context, *crs_jacobian);
  645. CudaVector cuda_x(context, 0);
  646. CudaVector cuda_y(context, 0);
  647. Vector x(crs_jacobian->num_rows());
  648. Vector y(crs_jacobian->num_cols());
  649. x.setRandom();
  650. y.setRandom();
  651. cuda_x.CopyFromCpu(x);
  652. cuda_y.CopyFromCpu(y);
  653. double sum = 0;
  654. for (auto _ : state) {
  655. cuda_jacobian.LeftMultiplyAndAccumulate(cuda_x, &cuda_y);
  656. sum += cuda_y.Norm();
  657. CHECK_EQ(cudaDeviceSynchronize(), cudaSuccess);
  658. }
  659. CHECK_NE(sum, 0.0);
  660. }
  661. #endif
  662. } // namespace ceres::internal
  663. // Older versions of benchmark library might come without ::benchmark::Shutdown
  664. // function. We provide an empty fallback variant of Shutdown function in
  665. // order to support both older and newer versions
  666. namespace benchmark_shutdown_fallback {
  667. template <typename... Args>
  668. void Shutdown(Args... args) {}
  669. }; // namespace benchmark_shutdown_fallback
  670. int main(int argc, char** argv) {
  671. ::benchmark::Initialize(&argc, argv);
  672. std::vector<std::unique_ptr<ceres::internal::BALData>> benchmark_data;
  673. if (argc == 1) {
  674. LOG(FATAL) << "No input datasets specified. Usage: " << argv[0]
  675. << " [benchmark flags] path_to_BAL_data_1.txt ... "
  676. "path_to_BAL_data_N.txt";
  677. return -1;
  678. }
  679. ceres::internal::ContextImpl context;
  680. context.EnsureMinimumThreads(16);
  681. #ifndef CERES_NO_CUDA
  682. std::string message;
  683. context.InitCuda(&message);
  684. #endif
  685. for (int i = 1; i < argc; ++i) {
  686. const std::string path(argv[i]);
  687. const std::string name_residuals = "Residuals<" + path + ">";
  688. benchmark_data.emplace_back(
  689. std::make_unique<ceres::internal::BALData>(path));
  690. auto data = benchmark_data.back().get();
  691. ::benchmark::RegisterBenchmark(
  692. name_residuals.c_str(), ceres::internal::Residuals, data, &context)
  693. ->Arg(1)
  694. ->Arg(2)
  695. ->Arg(4)
  696. ->Arg(8)
  697. ->Arg(16);
  698. const std::string name_jacobians = "ResidualsAndJacobian<" + path + ">";
  699. ::benchmark::RegisterBenchmark(name_jacobians.c_str(),
  700. ceres::internal::ResidualsAndJacobian,
  701. data,
  702. &context)
  703. ->Arg(1)
  704. ->Arg(2)
  705. ->Arg(4)
  706. ->Arg(8)
  707. ->Arg(16);
  708. const std::string name_plus = "Plus<" + path + ">";
  709. ::benchmark::RegisterBenchmark(
  710. name_plus.c_str(), ceres::internal::Plus, data, &context)
  711. ->Arg(1)
  712. ->Arg(2)
  713. ->Arg(4)
  714. ->Arg(8)
  715. ->Arg(16);
  716. const std::string name_right_product =
  717. "JacobianRightMultiplyAndAccumulate<" + path + ">";
  718. ::benchmark::RegisterBenchmark(
  719. name_right_product.c_str(),
  720. ceres::internal::JacobianRightMultiplyAndAccumulate,
  721. data,
  722. &context)
  723. ->Arg(1)
  724. ->Arg(2)
  725. ->Arg(4)
  726. ->Arg(8)
  727. ->Arg(16);
  728. const std::string name_right_product_partitioned_f =
  729. "PMVRightMultiplyAndAccumulateF<" + path + ">";
  730. ::benchmark::RegisterBenchmark(
  731. name_right_product_partitioned_f.c_str(),
  732. ceres::internal::PMVRightMultiplyAndAccumulateF,
  733. data,
  734. &context)
  735. ->Arg(1)
  736. ->Arg(2)
  737. ->Arg(4)
  738. ->Arg(8)
  739. ->Arg(16);
  740. #ifndef CERES_NO_CUDA
  741. const std::string name_right_product_partitioned_f_cuda =
  742. "PMVRightMultiplyAndAccumulateFCuda<" + path + ">";
  743. ::benchmark::RegisterBenchmark(
  744. name_right_product_partitioned_f_cuda.c_str(),
  745. ceres::internal::PMVRightMultiplyAndAccumulateFCuda,
  746. data,
  747. &context);
  748. #endif
  749. const std::string name_right_product_partitioned_e =
  750. "PMVRightMultiplyAndAccumulateE<" + path + ">";
  751. ::benchmark::RegisterBenchmark(
  752. name_right_product_partitioned_e.c_str(),
  753. ceres::internal::PMVRightMultiplyAndAccumulateE,
  754. data,
  755. &context)
  756. ->Arg(1)
  757. ->Arg(2)
  758. ->Arg(4)
  759. ->Arg(8)
  760. ->Arg(16);
  761. #ifndef CERES_NO_CUDA
  762. const std::string name_right_product_partitioned_e_cuda =
  763. "PMVRightMultiplyAndAccumulateECuda<" + path + ">";
  764. ::benchmark::RegisterBenchmark(
  765. name_right_product_partitioned_e_cuda.c_str(),
  766. ceres::internal::PMVRightMultiplyAndAccumulateECuda,
  767. data,
  768. &context);
  769. #endif
  770. const std::string name_update_block_diagonal_ftf =
  771. "PMVUpdateBlockDiagonalFtF<" + path + ">";
  772. ::benchmark::RegisterBenchmark(name_update_block_diagonal_ftf.c_str(),
  773. ceres::internal::PMVUpdateBlockDiagonalFtF,
  774. data,
  775. &context)
  776. ->Arg(1)
  777. ->Arg(2)
  778. ->Arg(4)
  779. ->Arg(8)
  780. ->Arg(16);
  781. const std::string name_pse =
  782. "PSEPreconditionerRightMultiplyAndAccumulate<" + path + ">";
  783. ::benchmark::RegisterBenchmark(
  784. name_pse.c_str(), ceres::internal::PSEPreconditioner, data, &context)
  785. ->Arg(1)
  786. ->Arg(2)
  787. ->Arg(4)
  788. ->Arg(8)
  789. ->Arg(16);
  790. const std::string name_isc_no_diag =
  791. "ISCRightMultiplyAndAccumulate<" + path + ">";
  792. ::benchmark::RegisterBenchmark(name_isc_no_diag.c_str(),
  793. ceres::internal::ISCRightMultiplyNoDiag,
  794. data,
  795. &context)
  796. ->Arg(1)
  797. ->Arg(2)
  798. ->Arg(4)
  799. ->Arg(8)
  800. ->Arg(16);
  801. const std::string name_update_block_diagonal_ete =
  802. "PMVUpdateBlockDiagonalEtE<" + path + ">";
  803. ::benchmark::RegisterBenchmark(name_update_block_diagonal_ete.c_str(),
  804. ceres::internal::PMVUpdateBlockDiagonalEtE,
  805. data,
  806. &context)
  807. ->Arg(1)
  808. ->Arg(2)
  809. ->Arg(4)
  810. ->Arg(8)
  811. ->Arg(16);
  812. const std::string name_isc_diag =
  813. "ISCRightMultiplyAndAccumulateDiag<" + path + ">";
  814. ::benchmark::RegisterBenchmark(name_isc_diag.c_str(),
  815. ceres::internal::ISCRightMultiplyDiag,
  816. data,
  817. &context)
  818. ->Arg(1)
  819. ->Arg(2)
  820. ->Arg(4)
  821. ->Arg(8)
  822. ->Arg(16);
  823. #ifndef CERES_NO_CUDA
  824. const std::string name_right_product_cuda =
  825. "JacobianRightMultiplyAndAccumulateCuda<" + path + ">";
  826. ::benchmark::RegisterBenchmark(
  827. name_right_product_cuda.c_str(),
  828. ceres::internal::JacobianRightMultiplyAndAccumulateCuda,
  829. data,
  830. &context)
  831. ->Arg(1);
  832. #endif
  833. const std::string name_left_product =
  834. "JacobianLeftMultiplyAndAccumulate<" + path + ">";
  835. ::benchmark::RegisterBenchmark(
  836. name_left_product.c_str(),
  837. ceres::internal::JacobianLeftMultiplyAndAccumulate,
  838. data,
  839. &context)
  840. ->Arg(1)
  841. ->Arg(2)
  842. ->Arg(4)
  843. ->Arg(8)
  844. ->Arg(16);
  845. const std::string name_left_product_partitioned_f =
  846. "PMVLeftMultiplyAndAccumulateF<" + path + ">";
  847. ::benchmark::RegisterBenchmark(
  848. name_left_product_partitioned_f.c_str(),
  849. ceres::internal::PMVLeftMultiplyAndAccumulateF,
  850. data,
  851. &context)
  852. ->Arg(1)
  853. ->Arg(2)
  854. ->Arg(4)
  855. ->Arg(8)
  856. ->Arg(16);
  857. #ifndef CERES_NO_CUDA
  858. const std::string name_left_product_partitioned_f_cuda =
  859. "PMVLeftMultiplyAndAccumulateFCuda<" + path + ">";
  860. ::benchmark::RegisterBenchmark(
  861. name_left_product_partitioned_f_cuda.c_str(),
  862. ceres::internal::PMVLeftMultiplyAndAccumulateFCuda,
  863. data,
  864. &context);
  865. #endif
  866. const std::string name_left_product_partitioned_e =
  867. "PMVLeftMultiplyAndAccumulateE<" + path + ">";
  868. ::benchmark::RegisterBenchmark(
  869. name_left_product_partitioned_e.c_str(),
  870. ceres::internal::PMVLeftMultiplyAndAccumulateE,
  871. data,
  872. &context)
  873. ->Arg(1)
  874. ->Arg(2)
  875. ->Arg(4)
  876. ->Arg(8)
  877. ->Arg(16);
  878. #ifndef CERES_NO_CUDA
  879. const std::string name_left_product_partitioned_e_cuda =
  880. "PMVLeftMultiplyAndAccumulateECuda<" + path + ">";
  881. ::benchmark::RegisterBenchmark(
  882. name_left_product_partitioned_e_cuda.c_str(),
  883. ceres::internal::PMVLeftMultiplyAndAccumulateECuda,
  884. data,
  885. &context);
  886. #endif
  887. #ifndef CERES_NO_CUDA
  888. const std::string name_left_product_cuda =
  889. "JacobianLeftMultiplyAndAccumulateCuda<" + path + ">";
  890. ::benchmark::RegisterBenchmark(
  891. name_left_product_cuda.c_str(),
  892. ceres::internal::JacobianLeftMultiplyAndAccumulateCuda,
  893. data,
  894. &context)
  895. ->Arg(1);
  896. #endif
  897. const std::string name_squared_column_norm =
  898. "JacobianSquaredColumnNorm<" + path + ">";
  899. ::benchmark::RegisterBenchmark(name_squared_column_norm.c_str(),
  900. ceres::internal::JacobianSquaredColumnNorm,
  901. data,
  902. &context)
  903. ->Arg(1)
  904. ->Arg(2)
  905. ->Arg(4)
  906. ->Arg(8)
  907. ->Arg(16);
  908. const std::string name_scale_columns = "JacobianScaleColumns<" + path + ">";
  909. ::benchmark::RegisterBenchmark(name_scale_columns.c_str(),
  910. ceres::internal::JacobianScaleColumns,
  911. data,
  912. &context)
  913. ->Arg(1)
  914. ->Arg(2)
  915. ->Arg(4)
  916. ->Arg(8)
  917. ->Arg(16);
  918. const std::string name_to_crs = "JacobianToCRS<" + path + ">";
  919. ::benchmark::RegisterBenchmark(
  920. name_to_crs.c_str(), ceres::internal::JacobianToCRS, data, &context);
  921. #ifndef CERES_NO_CUDA
  922. const std::string name_to_crs_view = "JacobianToCRSView<" + path + ">";
  923. ::benchmark::RegisterBenchmark(name_to_crs_view.c_str(),
  924. ceres::internal::JacobianToCRSView,
  925. data,
  926. &context);
  927. const std::string name_to_crs_matrix = "JacobianToCRSMatrix<" + path + ">";
  928. ::benchmark::RegisterBenchmark(name_to_crs_matrix.c_str(),
  929. ceres::internal::JacobianToCRSMatrix,
  930. data,
  931. &context);
  932. const std::string name_to_crs_view_update =
  933. "JacobianToCRSViewUpdate<" + path + ">";
  934. ::benchmark::RegisterBenchmark(name_to_crs_view_update.c_str(),
  935. ceres::internal::JacobianToCRSViewUpdate,
  936. data,
  937. &context);
  938. const std::string name_to_crs_matrix_update =
  939. "JacobianToCRSMatrixUpdate<" + path + ">";
  940. ::benchmark::RegisterBenchmark(name_to_crs_matrix_update.c_str(),
  941. ceres::internal::JacobianToCRSMatrixUpdate,
  942. data,
  943. &context);
  944. #endif
  945. }
  946. ::benchmark::RunSpecifiedBenchmarks();
  947. using namespace ::benchmark;
  948. using namespace benchmark_shutdown_fallback;
  949. Shutdown();
  950. return 0;
  951. }