Commit Graph

726 Commits

Author SHA1 Message Date
Sameer Agarwal f6df80e8a0 Fix tiny_solver_test
Verify that the final cost is small enough instead of the norm
of the residual vector.

Change-Id: I9591d97d3e8f1c71067d99270967f1e593889843
2017-10-22 22:04:24 -07:00
Keir Mierle 40effe3b15 Tiny solver autodiff adapter
Change-Id: I29fe736d53b2be32a101ba128cf557726def9a00
2017-10-14 15:51:14 -07:00
Sameer Agarwal 8beedf5cf6 Add TinySolverCostFunctionAdapter
Change-Id: I1905044d09abe5c927cd7e2cda804cba516fd961
2017-10-14 15:12:01 -07:00
Sameer Agarwal 4d88f50f6b Two changes to TinySolver
1. Change the ordering from NUM_PARAMETERS, NUM_RESIDUALS to
NUM_RESIDUALS, NUM_PARAMETERS in docs and in code.
2. TinySolver::solve -> TinySolver::Solve

Change-Id: I4dca87b971fd9168f1200b53c362669cffc82c1b
2017-10-11 16:09:40 -07:00
Keir Mierle 7928ca003f Initial commit of tiny solver
Tiny solver is targeted towards small dense least square
solves, where the overhead of calling normal Ceres is too
high. For example, when solving for inverse camera
distortion for every pixel location in a many-megapixel
image. Anecdotally, at one point in the past, tiny solver
was ~20x faster than Ceres for the problems it's intended
for. This is due to two key aspects:

  1. Memory is allocated up front: repeated solves incur no
     allocation overhead beyond a few scalars on the stack.
  2. The cost function is fully inlined into the solver
     loop, removing even the cost function call overhead.

Tiny solver originated many years ago as part of
libmv/Blender, where it is still used for distortion solving
today, but the time has come for it to migrate into Ceres.

This commit is just the initial import into Ceres.  Follow
up patches will add further cleanups, and add CostFunction
and Jet adapters to make it easier to call tiny solver
(though by using adapters, some performance advantages will
be lost).

Change-Id: I8079535cd41382b1e0ac0ca2fca141711c72b7f8
2017-10-10 14:32:19 -07:00
Thomas Gamper d727974f30 Report timings with microsecond resolution
In case one has a small problem to solve, where
completion is reached within a few milliseconds,
then four decimal places are not enough to accurately
represent the timings of all the separate sub-steps.
Thus, we extend the reported timings to include
six decimal places.

Change-Id: Iaf88a94a1b8896ea7370c75b1de2f05d8671206e
2017-09-27 13:39:20 +00:00
Thomas Gamper 28b1147a1d Use high-resolution timer on Windows
This fixes a Windows specific issue where the
problem-summary reports timings as zero, as long as
the time difference in question is smaller than one
second.

Change-Id: Ibd91874294423af6acda2575eae80f01aabed6d3
2017-09-26 13:10:54 +00:00
Keir Mierle 4bea6d7a2d Add a comment about default constructed reference counts=
Change-Id: Ia6b8a75144755d9bcf05c893bb97a1e207da6b0f
2017-09-25 13:09:41 -07:00
Sameer Agarwal 600262e8f8 Delete cost and loss functions when not in use.
Delete CostFunctions and LossFunctions when there are no more
ResidualBlocks referring to them. This is done by maintaining
a map with reference counts for CostFunctions and
LossFunctions.

The same maps are also used at the time of the destruction
of the ProblemImpl object itself. Previously vectors of these
objects were constructed, uniqed and the objects destroyed.

The update to the maps increases the cost of calling AddResidualBlock,
this has been mitigated, actually making AddResidualBlock faster, by
reusing a temporary vector rather than allocating one on the stack
every time.

Change-Id: I28b5287511713d28069ae428e2ff69224c0d03b4
2017-09-25 17:44:15 +00:00
Yury Prokazov 4ffec20a44 Add TBB threading support.
There are platforms where OpenMP is not available. This
patch adds support for Intel Threading Building Blocks (TBB)
as an alternative threading backend.

Change-Id: I94497d7cba0c3cfaccfc992169236f17fe948ae9
2017-09-25 12:43:14 +02:00
Alex Stewart cc73c77d50 Update LAPACK option to refer to direct use by Ceres only.
- Previously the LAPACK option meant would Ceres link against LAPACK,
  whether directly or indirectly via SuiteSparse (if SUITESPARSE=ON),
  as such if LAPACK=OFF, the use of SuiteSparse was disabled, even if
  it was found.
- To support the use-case of using a limited LAPACK implementation that
  satisfies SuiteSparse’s requirements, but potentially not Ceres’ we
  now adopt the more conventional terminology whereby the LAPACK option
  refers only to whether Ceres itself will directly call LAPACK
  routines, not whether it or any of its dependencies will.
- This means that the LAPACK and SUITESPARSE options are now
  independent.
- Also unnecessary calls to find_package(BLAS), as find_package(LAPACK)
  already searches for BLAS, and appends the resulting libraries to
  LAPACK_LIBRARIES if they are found.

Change-Id: I9cf5fa5e4cb621812f6f0526db8d16a7a39c9c8f
2017-08-13 18:27:21 +00:00
Chris Sweeney 2d703b17b5 Add new Schur specialization for 2, 4, 6.
The row, E, F block pattern 2, 4, 6 is a common one for
bundle adjustment with reprojection error (2 residuals),
homogeneous 3d points (4 params in the E-block), and camera
poses (3 rotation + 3 position = 6 params for the
F-block). This provides a major speedup for BA in the
TheiaSfM library and likely in other applications.

Change-Id: If5df8bfadc7f154856b74c3b38479c14856db47d
2017-08-08 14:55:44 -07:00
pmoulon afe93546b6 Use const keyword for 'int thread_id' variables.
Change-Id: I3afdf8a472cbc4f325b462bc9c42c03bc464f4b2
2017-08-06 00:57:38 +02:00
Sameer Agarwal f402c17247 InvertPSDMatrix uses dynamic matrices when using SVD
The JacobiSVD algorithm in Eigen does not accept fixed sized
matrices when performing a thin SVD. The assert enforcing this
is only triggered in non-Release builds.

So this change calls JacobiSVD with dynamically sized matrices
as a template parameter rather than a fixed size matrix.

https://github.com/ceres-solver/ceres-solver/issues/304#issuecomment-317965814

Thanks to @debalance for reporting this and @leokoppel for
providing a reproduction.

Change-Id: Ifc3d9ff20d5597f08c0f8573bf2fd99a3ed3d4d3
2017-07-26 21:05:38 -07:00
Sameer Agarwal 87f823617e Fix dynamic_sparsity_test.
Skip the test in dynamic_sparsity_test when there are no sparse
linear algebra libraries available.

Also fix a minor typo in version_history.rst

Change-Id: Ie7cc14e655c58b6bd9625ce9f9025f94d0624d2d
2017-07-10 11:32:27 -07:00
Alex Stewart 5eae62348d Add compound with scalar operators for Jets.
Change-Id: Ie47771d8c9df22ddeb6b643ae8a751860d892d67
2017-07-06 19:10:34 +01:00
Sameer Agarwal c8202e6926 Get rid of redundant function evaluations in LineSearchMinimizer
1. Replace LineSearch::Summary::optimal_step_size with
   LineSearch:Summary::optimal_point which is a FunctionSample.
2. Add the actual vector position and vector gradient of the
   point in the FunctionSample
3. Use the above two to get rid of an extraneous function evalation
   in LineSearchMinimizer.

Runtime performance is almost 2x improved as a result.

Thanks to @svenpilz for reporting this.

https://github.com/ceres-solver/ceres-solver/issues/296

Change-Id: Iebf2db7acecb2c95c9b1683b73cdc5faab78b02e
2017-07-05 15:40:58 -07:00
Sameer Agarwal 621b79b972 Refactor FunctionSample & LineSearchFunction
1. Move FunctionSample to its own .h/.cc files.
2. Migrate LineSearchFunction::Evaluate to use FunctionSample
   for input and output.

Change-Id: I8bfb97e1900d95a4686c9621dda5b584458b45c0
2017-07-05 07:56:58 -07:00
Sameer Agarwal 67d313d8f6 Change test naming in sparse_normal_cholesky_solver_test
Change-Id: I384a7bdbeb9b7ada5e6d272671ce389d4066a31c
2017-07-05 07:56:17 -07:00
Sameer Agarwal 58bacdacae Fix an alignment issue in Summary::FullReport
Change-Id: If5f93aee46f056b4019de19e372988e827ff4ef5
2017-06-29 21:22:01 -07:00
Sameer Agarwal 19382f0460 Make the Evaluator statistics key strings consistent
We keep track of evaluator call and time statistics
via a hashmap containing magic strings. These strings
need to be consistent across the GradientProblemSolver
and Solver as the LineSearchMinimizer is used by both
of these solvers. Previously they were inconsistent
in a manner that GradientProblemSolver was not getting
information about the evaluation timing, and in the
process of fixing that I made it so that the TrustRegionMinimizer
when solving bounds constrained probelms will access/update
this information correctly.

So while I look for a more elegant solution, this CL
is meant to fix the inconsistency by making sure that the
same magic strings are used everywhere.

Change-Id: I120ca0bd1c2f77fde2db15edd9e33286a49dbae9
2017-06-29 07:34:14 -07:00
Sameer Agarwal a9977da3eb Fix and enhance GradientProblemSolver::Summary::FullReport
1. Fix a bug which was causing the cost and gradient evaluation
   time to not be reported.
2. Add the number of times cost and gradients are evaluated to
   the Summary object and to the output of FullReport.

Change-Id: Id0703cd2dafbf437f3e537fbdc30ae81d5f4f540
2017-06-28 06:26:04 -07:00
Sameer Agarwal 62af68d8df Remove unused file: collections_port.cc
Change-Id: I7698b05170b2ec3d29b747bbe152cf9c661a30ae
2017-06-24 22:04:18 -07:00
Sameer Agarwal f7957e29ec Build cleanup
1. Remove unused variable.
2. Make inner_product_computer compatible with older versions of Eigen,
   which don't have a named enum for Eigen::Upper/Lower.

Change-Id: I927af297f93fc74f7f4b29b39e400ef2d75edbd4
2017-06-22 00:49:11 -07:00
Sameer Agarwal 1ece5a95fb Delete obsolete code
Remove outer product computation code from CompressedRowSparseMatrix.
In the process also remove the crsb_cols and crsb_rows vectors from
the matrix, which were added to carry the block sparsity of the matrix
so that the outer product could be computed fast.

InnerProductComputer and its reliance on BlockSparseMatrix has
rendered all of this code moot.

Change-Id: If3ee0dc8ad4ff79594fd1eebc15a647c4495d726
2017-06-22 00:21:09 -07:00
Sameer Agarwal 08e60379ba Integrate InnerProductComputer
Despite its relative size, this is very significant change
to Ceres.

Why
===

Up till now, when the user chose SPARSE_NORMAL_CHOLESKY,
the Jacobian was evaluated in a CompressedRowSparseMatrix,
which was then use to compute the normal equations which were
passed to a sparse linear algebra library for factorization.

The reason to do this was because in the case of SuiteSparse,
we were able to pass the Jacobian matrix directly without
computing the normal equations and SuiteSparse/CHOLMOD did the
normal equation computation.

This turned out to be slow, so Cheng Wang implemented a high
performance version of the matrix-matrix multiply to compute
the normal equations, and all the sparse linear algebra libraries
now are passed the normal equations.

So that raises the question, as to what the best representation
of the Jacobian which is suitable for the normal equation computation.

Turns out BlockSparseMatrix is ideal. It brings two advantages.

1. Jacobian evaluation into a BlockSparseMatrix is considerably
   faster when using a BlockSparseMatrix than
   CompressedRowSparseMatrix. This is because we save on a bunch
   of memory copies.

2. To make the matrix multiplication fast and use the block structure
   Cheng Wang had to essentially make the CompressedRowSparseMatrix
   carry a bunch of sidecar information about the block sparsity,
   essentially making it behave like a BlockSparseMatrix. The resulting
   code had fairly complicated indexing and complicated the semantics
   of CompressedRowSparseMatrix. The new InnerProductComputer class
   does away with all that and once this CL goes in, I will be able to
   remove all that code and simplify the semantics of
   CompressedRowSparseMatrix.

Changes
=======

1. Use InnerProductComputer in SparseNormalCholeskySolver.
2. Change the evaluator instantiated for SPARSE_NORMAL_CHOLESKY with
   static sparsity inside evaluator.cc
3. The former change necessitates that we change ProblemImpl::Evaluate
   to create the evaluate it needs on its own, because it was
   depending on passing "SPARSE_NORMAL_CHOLESKY" as linear solver type
   to the evaluator factor to get an Evaluator which can use
   CompressedRowSparseMatrix objects for storing the Jacobian.
4. Update the tests for SparseNormalCholeskySolver.
5. Separate out the tests for DynamicSparseNormalCholeskySolver into its
   own file.

Change-Id: I2ef7ef8fbfbb4967d0c1ec2068c1c778248fdf5b
2017-06-21 23:41:36 -07:00
Sameer Agarwal b5b394c738 Add InnerProductComputer
Add a class that given a block sparse matrix m will compute
the product m'*m efficiently.

This code is refactoring and cleanup of the code in
CompressedRowSparseMatrix devoted to computing the inner product.

In that class, the code is mistakenly said to be computing
the outer product. It is also devoted to computing the inner
product of a CompressedRowSparseMatrix with itself.

This code works with BlockSparseMatrix objects instead, which
are simpler to deal with as they are better structured to handle
block sparse matrices.

Change-Id: I920fee1a396bb0fcae9e6f7e46a308c7391d21aa
2017-06-21 23:17:54 -07:00
Sameer Agarwal 04325145c9 Performance improvements to BlockSparseMatrix
Re-allocations only happen if the already allocated buffer is
not large enough.

Change-Id: I5ff170a400e32a0ad64ee7c2e8ab59216db1e51b
2017-06-21 22:01:32 -07:00
Sameer Agarwal 1182fc2884 SPARSE_SCHUR + CX_SPARSE = Faster
There was a bug in the trust region preprocessor where no fill
reducing ordering was computed for the case of SPARSE_SCHUR + CX_SPARSE
but this was not signaled to SchurComplementSolver, so it was using
a naive/natural ordering. To fix this two changes are made:

1. TrustRegionProcessor's logic for signaling the ordering to the
   linear solver has been re-worked. The surrounding code has also
   been re-organized for better readability.
2. In SchurComplementSolver::SolveReducedSystem the row and column
   block structure has been added to the CompressedRowSparseMatrix
   containing the Schur complement so that block AMD can be used.

As a result of these changes the linear solve time for
problem-744-543562-pre.txt has been brought down from 58 seconds to
35 seconds.

Change-Id: I4d82efce05175260f97b1f925f8a1b4a9d650cae
2017-06-20 09:13:02 -07:00
Sameer Agarwal ef31944726 A number of changes to BlockSparseMatrix.
1. Add BlockSparseMatrix::CreateDiagonalMatrix
2. Add BlockSparseMatrix::AppendRows
3. Add BlockSparseMatrix::DeleteRowBlocks
4. Add BlockSparseMatrix::CreateRandomMatrix
5. Add a non-default constructor to Compressedlist.

Change-Id: I7cc7656616d059cef4471335f6d5b636807953e6
2017-06-14 10:57:27 -07:00
Sameer Agarwal 15dc18a532 Change how ifdefs are used in dense_linear_solver_test.
It appears that interspersing ifdefs with macros causes problems
with Visual Studio. This patch changes the way we condition
the tests for dense linear solvers based on whether LAPACK is
available or not.

Change-Id: I306247496265c3551edad6bc8fcec9d4cf09e68d
2017-06-12 21:30:27 -07:00
Sameer Agarwal d90e5a708b Deal with zero sized ranges in Uniform correctly.
Change-Id: I6d5c622869e4795c252fa5c26fffbe4844abc3c9
2017-06-11 16:26:48 -07:00
Sameer Agarwal 59220af761 Remove an unused field from SuiteSparseCholesky
Change-Id: I63128de3e9806347560ab6f0ebee82ead44f2e45
2017-06-11 15:19:35 -07:00
Sameer Agarwal b56d561aab Lint changes from Jim Roseborough
Change-Id: I7d63b1a32416d0361f48990fbf6b8ea83a3abb8e
2017-06-08 16:47:00 -07:00
Sameer Agarwal 7174e65598 Refactor compressed_col_sparse_matrix_utils_test
Remove the dependence on SuiteSparse.
Update the CMakeLists.txt file

https://github.com/ceres-solver/ceres-solver/issues/276

Change-Id: Ifd480d465fb19ad6cd195e1025e0ad2dc4e4a2cc
2017-06-08 05:33:58 -07:00
Sameer Agarwal 1fb83c1a53 symmetric_linear_solver_test -> conjugate_gradients_solver_test
Change-Id: Ie3b7b4e0079a74ae3f9e353cfbda91c4b7cdf988
2017-06-08 05:30:15 -07:00
Sameer Agarwal efa091122d Refactor unsymmetric_linear_solver_test
1. Break up unsymmetric_linear_solver_test into
   a. dense_linear_solver_test which covers DENSE_QR and
      DENSE_NORMAL_CHOLESKY.
   b. sparse_normal_cholesky_solver_test which covers
      SPARSE_NORMAL_CHOLESKY.

2. dense_linear_solver_test has been completely re-written. It now
   uses value parameterized tests for better logging. The number of
   test problems as been increased to 2. Last but not the least
   the actual test of correctness is not based on a golden solution
   computed using another linear solver. We now compute the residual
   and ensure that it is small.

https://github.com/ceres-solver/ceres-solver/issues/279

Change-Id: I9546a43e8ae85c31b2096a99405e47da326755ee
2017-06-08 08:18:37 -04:00
Sameer Agarwal 99aca60b2c Do not create a SparseCholesky object when not needed.
SchurComplementSolver implements a variant of ITERATIVE_SCHUR
when explicit_schur_complement is set to true. In this case
the SparseCholesky object should not be instantiated. Even
though there is no CPU cost, it can be the case that ITERATIVE_SCHUR
is being used when there are not sparse linear algebra libraries
are available, and this can result in a crash.

Change-Id: I349d5f79201782689b3ab0ccc2c5001804b44c7b
2017-06-02 11:08:11 -07:00
Sameer Agarwal 366f4cbb03 More lint changes from William Rucklidge
Change-Id: Ica72b02f9509f1eb1961b531072826519df3fddb
2017-06-01 20:03:43 -07:00
Sameer Agarwal f93ad78f23 Lint changes from William Rucklidge
Change-Id: I54ee9d7b90232bb0139398a78a09cf4035d23ea6
2017-05-31 19:44:23 -07:00
Sameer Agarwal 9a0ef02b8d Lint changes from Jim Roseborough
Change-Id: Ibca11aa9205f5af6180cb2dcf7ceaf1e8724f092
2017-05-30 15:13:45 -07:00
Sameer Agarwal 07c35a1e8c Remove namspace handling code for std::tuple and std::get
gtest defines aliases in ::testing which will do the right thing.

Change-Id: Ic8c803cbda377602d32a1096f57f0a9b3f8bdb0c
2017-05-30 11:29:59 -07:00
Sameer Agarwal 96e908d796 Add an ifdef to handle tr1 namespace in sparse_cholesky_test.cc
Parametric tests in gunit use tuple, which can be in the std::tr1
or the std namespaces depending on the version of STL one is using.

This change adds conditions the choice of namespace on whether
CXX11 mode is enabled or not.

It is entirely possible that we will have to come back and add
detection for this along the lines of shared_ptr.

Change-Id: I7fc85a32cf9f3f3bf30f86d9ba972ac67c6635fb
2017-05-30 02:52:41 +00:00
Sameer Agarwal 01f2414e97 Minor refactoring/cleanup in IterativeSchurComplementSolver
The code for creating and updating preconditioners has been pulled
out in into its own function for better readability.

Change-Id: I5335de3b158a8485cf6d052ee37d0d8fd57145e9
2017-05-30 02:52:25 +00:00
Sameer Agarwal 88dad7e41d Fix the build for dynamic_sparsity.
The addition of crsb_rows and crsb_cols to CompressedRowSparseMatrix
broke the build for problems with dynamic sparsity.

The fix is to remove unnecessarily filling of row_blocks and col_blocks,
which was triggering a check inside CompressedRowSparseMatrix around
block handling, since block structure makes no sense for matrices with
dynamic sparsity anyways.

Also added a test "dynamic_sparsity_test" based on i
examples/ellipse_approximation.cc

Thanks to Richard Stebbing for reporting this.

Change-Id: Ic1d49e97690ac17e0ea2949772271bd915277d68
2017-05-29 19:47:34 -07:00
Sameer Agarwal 97cefd4b35 Fix a memory leak in suitesparse.cc
Change-Id: I6eb8b33d927840d6c3631679de482038d9f3673a
2017-05-24 01:17:03 -07:00
Sameer Agarwal 29c21f5680 Add SparseCholesky
SparseCholesky is an interface to sparse cholesky factorization
routines across sparse linear algebra libraries. Each sparse
linear algebra library is responsible for implementing its own
instance of this interface.

As a result the various places - SparseNormalCholeskySolver,
SparseSchurComplementSolver and VisibilityBasedPreconditioner
are significantly simplified.

Change-Id: I8b465705eae83bba9e1adfffcc741a05c70faf2e
2017-05-24 00:00:25 -07:00
Sameer Agarwal 39388bd2ff Address comments from William Rucklidge
Change-Id: I0c4f686b89798435741c55a5ca45cf000af842dc
2017-05-09 17:23:43 -07:00
Sameer Agarwal 0780d22c54 Remove dead code.
Change-Id: Ic6b2bccb44a97604bf966d37d2ae06f568dd999f
2017-05-08 06:47:44 -07:00
Sameer Agarwal 086ff01aca Matrix generation cleanup
1. Convert a CompressedRowSparseMatrix constructor which
takes a TripletSparseMatrix as input into a factory method
which allows the input to be transposed.

2. Move the random matrix creation routine for CompressedRowSparseMatrix
from being a standalone function to a static method.

3. Add a corresponding random matrix generation static method to
TripletSparseMatrix.

4. Add a new constructor to TripletSparseMatrix, which takes as input
the row, col and values arrays.

Change-Id: Iec7b184646818f432a5e6822bea3b2f3128a82aa
2017-05-01 22:53:21 -07:00