Blame docs/html/_sources/solving_faqs.txt

Packit ea1746
.. _chapter-solving_faqs:
Packit ea1746
Packit ea1746
.. default-domain:: cpp
Packit ea1746
Packit ea1746
.. cpp:namespace:: ceres
Packit ea1746
Packit ea1746
=======
Packit ea1746
Solving
Packit ea1746
=======
Packit ea1746
Packit ea1746
#. How do I evaluate the Jacobian for a solved problem?
Packit ea1746
Packit ea1746
   Using :func:`Problem::Evaluate`.
Packit ea1746
Packit ea1746
#. How do I choose the right linear solver?
Packit ea1746
Packit ea1746
   When using the ``TRUST_REGION`` minimizer, the choice of linear
Packit ea1746
   solver is an important decision. It affects solution quality and
Packit ea1746
   runtime. Here is a simple way to reason about it.
Packit ea1746
Packit ea1746
   1. For small (a few hundred parameters) or dense problems use
Packit ea1746
      ``DENSE_QR``.
Packit ea1746
Packit ea1746
   2. For general sparse problems (i.e., the Jacobian matrix has a
Packit ea1746
      substantial number of zeros) use
Packit ea1746
      ``SPARSE_NORMAL_CHOLESKY``. This requires that you have
Packit ea1746
      ``SuiteSparse`` or ``CXSparse`` installed.
Packit ea1746
Packit ea1746
   3. For bundle adjustment problems with up to a hundred or so
Packit ea1746
      cameras, use ``DENSE_SCHUR``.
Packit ea1746
Packit ea1746
   4. For larger bundle adjustment problems with sparse Schur
Packit ea1746
      Complement/Reduced camera matrices use ``SPARSE_SCHUR``. This
Packit ea1746
      requires that you build Ceres with support for ``SuiteSparse``,
Packit ea1746
      ``CXSparse`` or Eigen's sparse linear algebra libraries.
Packit ea1746
Packit ea1746
      If you do not have access to these libraries for whatever
Packit ea1746
      reason, ``ITERATIVE_SCHUR`` with ``SCHUR_JACOBI`` is an
Packit ea1746
      excellent alternative.
Packit ea1746
Packit ea1746
   5. For large bundle adjustment problems (a few thousand cameras or
Packit ea1746
      more) use the ``ITERATIVE_SCHUR`` solver. There are a number of
Packit ea1746
      preconditioner choices here. ``SCHUR_JACOBI`` offers an
Packit ea1746
      excellent balance of speed and accuracy. This is also the
Packit ea1746
      recommended option if you are solving medium sized problems for
Packit ea1746
      which ``DENSE_SCHUR`` is too slow but ``SuiteSparse`` is not
Packit ea1746
      available.
Packit ea1746
Packit ea1746
      .. NOTE::
Packit ea1746
Packit ea1746
        If you are solving small to medium sized problems, consider
Packit ea1746
        setting ``Solver::Options::use_explicit_schur_complement`` to
Packit ea1746
        ``true``, it can result in a substantial performance boost.
Packit ea1746
Packit ea1746
      If you are not satisfied with ``SCHUR_JACOBI``'s performance try
Packit ea1746
      ``CLUSTER_JACOBI`` and ``CLUSTER_TRIDIAGONAL`` in that
Packit ea1746
      order. They require that you have ``SuiteSparse``
Packit ea1746
      installed. Both of these preconditioners use a clustering
Packit ea1746
      algorithm. Use ``SINGLE_LINKAGE`` before ``CANONICAL_VIEWS``.
Packit ea1746
Packit ea1746
#. Use :func:`Solver::Summary::FullReport` to diagnose performance problems.
Packit ea1746
Packit ea1746
   When diagnosing Ceres performance issues - runtime and convergence,
Packit ea1746
   the first place to start is by looking at the output of
Packit ea1746
   ``Solver::Summary::FullReport``. Here is an example
Packit ea1746
Packit ea1746
   .. code-block:: bash
Packit ea1746
Packit ea1746
     ./bin/bundle_adjuster --input ../data/problem-16-22106-pre.txt
Packit ea1746
Packit ea1746
     iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
Packit ea1746
        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
Packit ea1746
        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
Packit ea1746
        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
Packit ea1746
        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
Packit ea1746
        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
Packit ea1746
        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
Packit ea1746
Packit ea1746
     Ceres Solver v1.12.0 Solve Report
Packit ea1746
     ----------------------------------
Packit ea1746
                                          Original                  Reduced
Packit ea1746
     Parameter blocks                        22122                    22122
Packit ea1746
     Parameters                              66462                    66462
Packit ea1746
     Residual blocks                         83718                    83718
Packit ea1746
     Residual                               167436                   167436
Packit ea1746
Packit ea1746
     Minimizer                        TRUST_REGION
Packit ea1746
Packit ea1746
     Sparse linear algebra library    SUITE_SPARSE
Packit ea1746
     Trust region strategy     LEVENBERG_MARQUARDT
Packit ea1746
Packit ea1746
                                             Given                     Used
Packit ea1746
     Linear solver                    SPARSE_SCHUR             SPARSE_SCHUR
Packit ea1746
     Threads                                     1                        1
Packit ea1746
     Linear solver threads                       1                        1
Packit ea1746
     Linear solver ordering              AUTOMATIC                22106, 16
Packit ea1746
Packit ea1746
     Cost:
Packit ea1746
     Initial                          4.185660e+06
Packit ea1746
     Final                            1.803391e+04
Packit ea1746
     Change                           4.167626e+06
Packit ea1746
Packit ea1746
     Minimizer iterations                        5
Packit ea1746
     Successful steps                            5
Packit ea1746
     Unsuccessful steps                          0
Packit ea1746
Packit ea1746
     Time (in seconds):
Packit ea1746
     Preprocessor                            0.283
Packit ea1746
Packit ea1746
       Residual evaluation                   0.061
Packit ea1746
       Jacobian evaluation                   0.361
Packit ea1746
       Linear solver                         0.382
Packit ea1746
     Minimizer                               0.895
Packit ea1746
Packit ea1746
     Postprocessor                           0.002
Packit ea1746
     Total                                   1.220
Packit ea1746
Packit ea1746
     Termination:                   NO_CONVERGENCE (Maximum number of iterations reached.)
Packit ea1746
Packit ea1746
  Let us focus on run-time performance. The relevant lines to look at
Packit ea1746
  are
Packit ea1746
Packit ea1746
Packit ea1746
   .. code-block:: bash
Packit ea1746
Packit ea1746
     Time (in seconds):
Packit ea1746
     Preprocessor                            0.283
Packit ea1746
Packit ea1746
       Residual evaluation                   0.061
Packit ea1746
       Jacobian evaluation                   0.361
Packit ea1746
       Linear solver                         0.382
Packit ea1746
     Minimizer                               0.895
Packit ea1746
Packit ea1746
     Postprocessor                           0.002
Packit ea1746
     Total                                   1.220
Packit ea1746
Packit ea1746
Packit ea1746
  Which tell us that of the total 1.2 seconds, about .3 seconds was
Packit ea1746
  spent in the linear solver and the rest was mostly spent in
Packit ea1746
  preprocessing and jacobian evaluation.
Packit ea1746
Packit ea1746
  The preprocessing seems particularly expensive. Looking back at the
Packit ea1746
  report, we observe
Packit ea1746
Packit ea1746
   .. code-block:: bash
Packit ea1746
Packit ea1746
     Linear solver ordering              AUTOMATIC                22106, 16
Packit ea1746
Packit ea1746
  Which indicates that we are using automatic ordering for the
Packit ea1746
  ``SPARSE_SCHUR`` solver. This can be expensive at times. A straight
Packit ea1746
  forward way to deal with this is to give the ordering manually. For
Packit ea1746
  ``bundle_adjuster`` this can be done by passing the flag
Packit ea1746
  ``-ordering=user``. Doing so and looking at the timing block of the
Packit ea1746
  full report gives us
Packit ea1746
Packit ea1746
   .. code-block:: bash
Packit ea1746
Packit ea1746
     Time (in seconds):
Packit ea1746
     Preprocessor                            0.051
Packit ea1746
Packit ea1746
       Residual evaluation                   0.053
Packit ea1746
       Jacobian evaluation                   0.344
Packit ea1746
       Linear solver                         0.372
Packit ea1746
     Minimizer                               0.854
Packit ea1746
Packit ea1746
     Postprocessor                           0.002
Packit ea1746
     Total                                   0.935
Packit ea1746
Packit ea1746
Packit ea1746
Packit ea1746
  The preprocessor time has gone down by more than 5.5x!.