solving_faqs.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. .. _chapter-solving_faqs:
  2. .. default-domain:: cpp
  3. .. cpp:namespace:: ceres
  4. =======
  5. Solving
  6. =======
  7. #. How do I evaluate the Jacobian for a solved problem?
  8. Using :func:`Problem::Evaluate`.
  9. #. How do I choose the right linear solver?
  10. When using the ``TRUST_REGION`` minimizer, the choice of linear
  11. solver is an important decision. It affects solution quality and
  12. runtime. Here is a simple way to reason about it.
  13. 1. For small (a few hundred parameters) or dense problems use
  14. ``DENSE_QR``.
  15. 2. For general sparse problems (i.e., the Jacobian matrix has a
  16. substantial number of zeros) use
  17. ``SPARSE_NORMAL_CHOLESKY``.
  18. 3. For bundle adjustment problems with up to a hundred or so
  19. cameras, use ``DENSE_SCHUR``.
  20. 4. For larger bundle adjustment problems with sparse Schur
  21. Complement/Reduced camera matrices use ``SPARSE_SCHUR``.
  22. If you do not have access to these libraries for whatever
  23. reason, ``ITERATIVE_SCHUR`` with ``SCHUR_JACOBI`` is an
  24. excellent alternative.
  25. 5. For large bundle adjustment problems (a few thousand cameras or
  26. more) use the ``ITERATIVE_SCHUR`` solver. There are a number of
  27. preconditioner choices here. ``SCHUR_JACOBI`` offers an
  28. excellent balance of speed and accuracy. This is also the
  29. recommended option if you are solving medium sized problems for
  30. which ``DENSE_SCHUR`` is too slow but ``SuiteSparse`` is not
  31. available.
  32. .. NOTE::
  33. If you are solving small to medium sized problems, consider
  34. setting ``Solver::Options::use_explicit_schur_complement`` to
  35. ``true``, it can result in a substantial performance boost.
  36. If you are not satisfied with ``SCHUR_JACOBI``'s performance try
  37. ``CLUSTER_JACOBI`` and ``CLUSTER_TRIDIAGONAL`` in that
  38. order. They require that you have ``SuiteSparse``
  39. installed. Both of these preconditioners use a clustering
  40. algorithm. Use ``SINGLE_LINKAGE`` before ``CANONICAL_VIEWS``.
  41. #. Use :func:`Solver::Summary::FullReport` to diagnose performance problems.
  42. When diagnosing Ceres performance issues - runtime and convergence,
  43. the first place to start is by looking at the output of
  44. ``Solver::Summary::FullReport``. Here is an example
  45. .. code-block:: bash
  46. ./bin/bundle_adjuster --input ../data/problem-16-22106-pre.txt
  47. iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time
  48. 0 4.185660e+06 0.00e+00 2.16e+07 0.00e+00 0.00e+00 1.00e+04 0 7.50e-02 3.58e-01
  49. 1 1.980525e+05 3.99e+06 5.34e+06 2.40e+03 9.60e-01 3.00e+04 1 1.84e-01 5.42e-01
  50. 2 5.086543e+04 1.47e+05 2.11e+06 1.01e+03 8.22e-01 4.09e+04 1 1.53e-01 6.95e-01
  51. 3 1.859667e+04 3.23e+04 2.87e+05 2.64e+02 9.85e-01 1.23e+05 1 1.71e-01 8.66e-01
  52. 4 1.803857e+04 5.58e+02 2.69e+04 8.66e+01 9.93e-01 3.69e+05 1 1.61e-01 1.03e+00
  53. 5 1.803391e+04 4.66e+00 3.11e+02 1.02e+01 1.00e+00 1.11e+06 1 1.49e-01 1.18e+00
  54. Ceres Solver v1.12.0 Solve Report
  55. ----------------------------------
  56. Original Reduced
  57. Parameter blocks 22122 22122
  58. Parameters 66462 66462
  59. Residual blocks 83718 83718
  60. Residual 167436 167436
  61. Minimizer TRUST_REGION
  62. Sparse linear algebra library SUITE_SPARSE
  63. Trust region strategy LEVENBERG_MARQUARDT
  64. Given Used
  65. Linear solver SPARSE_SCHUR SPARSE_SCHUR
  66. Threads 1 1
  67. Linear solver threads 1 1
  68. Linear solver ordering AUTOMATIC 22106, 16
  69. Cost:
  70. Initial 4.185660e+06
  71. Final 1.803391e+04
  72. Change 4.167626e+06
  73. Minimizer iterations 5
  74. Successful steps 5
  75. Unsuccessful steps 0
  76. Time (in seconds):
  77. Preprocessor 0.283
  78. Residual evaluation 0.061
  79. Jacobian evaluation 0.361
  80. Linear solver 0.382
  81. Minimizer 0.895
  82. Postprocessor 0.002
  83. Total 1.220
  84. Termination: NO_CONVERGENCE (Maximum number of iterations reached.)
  85. Let us focus on run-time performance. The relevant lines to look at
  86. are
  87. .. code-block:: bash
  88. Time (in seconds):
  89. Preprocessor 0.283
  90. Residual evaluation 0.061
  91. Jacobian evaluation 0.361
  92. Linear solver 0.382
  93. Minimizer 0.895
  94. Postprocessor 0.002
  95. Total 1.220
  96. Which tell us that of the total 1.2 seconds, about .3 seconds was
  97. spent in the linear solver and the rest was mostly spent in
  98. preprocessing and jacobian evaluation.
  99. The preprocessing seems particularly expensive. Looking back at the
  100. report, we observe
  101. .. code-block:: bash
  102. Linear solver ordering AUTOMATIC 22106, 16
  103. Which indicates that we are using automatic ordering for the
  104. ``SPARSE_SCHUR`` solver. This can be expensive at times. A straight
  105. forward way to deal with this is to give the ordering manually. For
  106. ``bundle_adjuster`` this can be done by passing the flag
  107. ``-ordering=user``. Doing so and looking at the timing block of the
  108. full report gives us
  109. .. code-block:: bash
  110. Time (in seconds):
  111. Preprocessor 0.051
  112. Residual evaluation 0.053
  113. Jacobian evaluation 0.344
  114. Linear solver 0.372
  115. Minimizer 0.854
  116. Postprocessor 0.002
  117. Total 0.935
  118. The preprocessor time has gone down by more than 5.5x!.