Commit Graph

41 Commits

Author SHA1 Message Date
Keir Mierle 7c4e8a454e Replace scoped_ptr with C++11's unique_ptr
Change-Id: Ib5a504c491e3a79af52a95accf009df473470c6b
2018-04-02 14:47:47 -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 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 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 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
Sameer Agarwal 0859fe8a57 Integrate InvertPSDMatrix into the SchurEliminator.
SchurEliminator::Init now takes a bool that tells it whether
it can assume that the diagonal blocks it is inverting can
be assumed to be full rank or not.

This information is then passed onto InvertPSDMatrix.

Change-Id: I26037b6233f2aad5584fed245f631c3959928afe
2017-04-25 10:08:45 -07:00
Sameer Agarwal ed32e6a44a Add logging for sparse Cholesky factorization using Eigen
Print information about the fill-in when using Eigen to perform
Simplicial sparse Cholesky factorization.

Change-Id: I09013b7f4ed2f6c55ae8ce8922447e23d63580a8
2015-12-17 20:22:48 +00:00
Sameer Agarwal e712ce1810 Revert 81219ff.
Eigen upstream was broken a little while ago, and it seemed to be
the case that we needed a fix for using the LLT factorization on
ARM.

This has been fixed and AFAIK there are no stable eigen releases
with this bug in it.

For full gore, see

http://eigen.tuxfamily.org/bz/show_bug.cgi?id=992

In light of the fix, the extra layer of indirection introduced earlier
is not needed and we are reverting to normal programming.

Change-Id: I16929d2145253b38339b573b27b6b8fabd523704
2015-04-07 14:13:25 -07:00
Sameer Agarwal 81219fff78 Allow using Eigen's LDLT factorization instead of LLT factorization
It seems that Eigen's LLT factorization is broken on ARM.
This patch enables the use of LDLT factorization instead of LLT
factorization. The switch is controlled at compile time using a
preprocessor define - CERES_USE_EIGEN_LDLT.

By default we continue to use LLT factorization though.

To make the switching easier without introducing the Cholesky factorization
based inversion and linear system solve routines have been abstracted into
two new functions.

Android.mk has been updated to enable the LDLT factorization, but
the cmake file has not been updated as I will leave it to Alex's
capable hands to do proper detection of ARM as a target platform.

Change-Id: Iffe3abd2ce894de2a388b454df3da909b482d5e5
2015-04-05 22:50:41 -07:00
Keir Mierle 7492b0d8de Update copyright headers with new year and URL
Since Ceres is moving to using GitHub for issues, and the Google
Code URL in the current copyright header will soon become invalid,
update all the headers.

Change-Id: I1fce70375d1bcf098591f07b4d8f01a5c1e0789c
2015-03-18 05:43:23 +00:00
Sameer Agarwal bcc865f81c Remove using namespace std;
For historical reasons we had a "using namespace std;" in port.h. This
is generally a bad idea. So removing it and along the way doing a bunch
of cpplint cleanup.

Change-Id: Ia125601a55ae62695e247fb0250df4c6f86c46c6
2015-01-07 14:26:53 -08:00
Sameer Agarwal b44cfdef25 Let ITERATIVE_SCHUR use an explicit Schur Complement matrix.
Up till now ITERATIVE_SCHUR evaluates matrix-vector products
between the Schur complement and a vector implicitly by exploiting
the algebraic expression for the Schur complement.

This cost of this evaluation scales with the number of non-zeros
in the Jacobian.

For small to medium sized problems there is a sweet spot where
computing the Schur complement is cheap enough that it is much
more efficient to explicitly compute it and use it for evaluating
the matrix-vector products.

This changes implements support for an explicit Schur complement
in ITERATIVE_SCHUR in combination with the SCHUR_JACOBI preconditioner.

API wise a new bool Solver::Options::use_explicit_schur_complement
has been added.

The implementation extends the SparseSchurComplementSolver to use
Conjugate Gradients.

Example speedup:

use_explicit_schur_complement = false

Time (in seconds):
Preprocessor                            0.585

  Residual evaluation                   0.319
  Jacobian evaluation                   1.590
  Linear solver                        25.685
Minimizer                              27.990

Postprocessor                           0.010
Total                                  28.585

use_explicit_schur_complement = true

Time (in seconds):
Preprocessor                            0.638

  Residual evaluation                   0.318
  Jacobian evaluation                   1.507
  Linear solver                         5.930
Minimizer                               8.144

Postprocessor                           0.010
Total                                   8.791

Which indicates an end-to-end speedup of more than 3x, with the linear
solver being sped up by > 4x.

The idea to explore this optimization was inspired by the recent paper:

Mining structure fragments for smart bundle adjustment
L. Carlone, P. Alcantarilla, H. Chiu, K. Zsolt, F. Dellaert
British Machine Vision Conference, 2014

which uses a more complicated algorithm to compute parts of the
Schur complement to speed up the matrix-vector product.

Change-Id: I95324af0ab351faa1600f5204039a1d2a64ae61d
2014-09-29 10:29:32 -07:00
Sameer Agarwal cfb36463f9 Small fixes from William Rucklidge.
Change-Id: I0be52f0f1e53cedccffe4807dc664a2f3fb4a8e4
2014-08-05 14:42:33 -07:00
Sameer Agarwal a521fc3afc Simplify, cleanup and instrument SchurComplementSolver.
The instrumentation revealed that EIGEN_SPARSE can be upto
an order of magnitude slower than CX_SPARSE on some bundle
adjustment problems.

The problem comes down to the quality of AMD ordering that
CXSparse/Eigen implements. It does particularly badly
on the Schur complement. In the CXSparse implementation
we got around this by considering the block sparsity structure
and computing the AMD ordering on it and lifting it to the
full matrix.

This is currently not possible with the release version of
Eigen, as the support for using preordered/natural orderings
is in the master branch but has not been released yet.

Change-Id: I25588d3e723e50606f327db5759f174f58439e29
2014-08-01 16:42:00 -07:00
Sameer Agarwal 031598295c Enable Eigen as sparse linear algebra library.
SPARSE_NORMAL_CHOLESKY and SPARSE_SCHUR can now be used
with EIGEN_SPARSE as the backend.

The performance is not as good as CXSparse. This needs to be
investigated. Is it because the quality of AMD ordering that
we are computing is not as good as the one for CXSparse? This
could be because we are working with the scalar matrix instead
of the block matrix.

Also, the upper/lower triangular story is not completely clear.
Both of these issues will be benchmarked and tackled in the
near future.

Also included in this change is a bunch of cleanup to the
SparseNormalCholeskySolver and SparseSchurComplementSolver
classes around the use of the of defines used to conditionally
compile out parts of the code.

The system_test has been updated to test EIGEN_SPARSE also.

Change-Id: I46a57e9c4c97782696879e0b15cfc7a93fe5496a
2014-07-31 22:05:34 -07:00
Sameer Agarwal 63cff485b6 Fix a leak in SchurComplementSolver.
cholmod_lhs was not being freed before
the SolveImpl function returned if the
underlying linear solver failed.

Change-Id: Id45db4f69a2821e43dc00fc975718584d426f44d
2014-02-03 11:27:39 -08:00
Sameer Agarwal 33e01b9c5e Rename LinearSolverTerminationType enums.
This increases clarity, drops redundant enums and makes things
cleaner all around.

Change-Id: I761f195ddf17ea6bd8e4e55bf5a72863660c4c3b
2013-11-27 10:24:03 -08:00
Sameer Agarwal 89a592f410 LinearSolver::Summary::status -> LinearSolver::Summary::message.
And a bunch of minor lint cleanups as they showed up.

Change-Id: I430a6b05710923c72daf6a5df4dfcd16fbf44b3a
2013-11-26 11:35:49 -08:00
Sameer Agarwal b16e118b96 Better error checking and reporting for linear solvers.
A lot of error checking cruft has accumulated over the years
in the various linear solvers. This change makes the error reporting
more robust and consistent across the various solvers.

Preconditioners are not covered by this change and will be the
subject of a future change.

Change-Id: Ibeb2572a1e67758953dde8d12e3abc6d1df9052d
2013-11-26 10:00:03 -08:00
Sameer Agarwal 79bde35f29 SuiteSparse errors do not cause a fatal crash.
1. Move LinearSolverTerminationType to ceres::internal.
2. Add FATAL_ERROR as a new enum to LinearSolverTerminationType.
3. Pipe SuiteSparse errors via a LinearSolverTerminationType so
   to distinguish between fatal and non-fatal errors.
4. Update levenberg marquardt and dogleg strategies to deal
   with FATAL_ERROR.
5. Update trust_region_minimizer to terminate when FATAL_ERROR
   is encountered.
6. Remove SuiteSparse::SolveCholesky as it screws up the error
   handling.
7. Fix all clients calling SuiteSparse to handle the result of
   SuiteSparse::Cholesky correctly.
8. Remove fatal failures in SuiteSparse when symbolic factorization
   fails.
9. Fix all clients of SuiteSparse to deal with null symbolic factors.

This is a temporary fix to deal with some production problems. A more
extensive cleanup and testing regime will be put in place in a
subsequent CL.

Change-Id: I1f60d539799dd95db7ecc340911e261fa4824f92
2013-11-21 22:16:24 -08:00
Sergey Sharybin f258e4624f Move most of suitesparse/cxsparse ifdef code to their headers
Main purpose of this is to make implementation files free from
endless ifdef blocks every time this libraries are needed to be
included. This would hopefully prevent compilation errors in
the future caused by missing ifdef around header include.

This also includes some stubs added to suitesparse/cxsparse
headers to make code even more free from ifdefs.

Change-Id: Ic8554e7df31d8c4751583fe004b99e71b3c9087b
2013-08-15 17:54:55 +00:00
Sameer Agarwal 367b65e17a Multiple dense linear algebra backends.
1. When a LAPACK implementation is present, then
DENSE_QR, DENSE_NORMAL_CHOLESKY and DENSE_SCHUR
can use it for doing dense linear algebra operations.

2. The user can switch dense linear algebra libraries
by setting Solver::Options::dense_linear_algebra_library_type.

3. Solver::Options::sparse_linear_algebra_library is now
Solver::Options::sparse_linear_algebra_library_type to be consistent
with all the other enums in Solver::Options.

4. Updated documentation as well as Solver::Summary::FullReport
to reflect these changes.

Change-Id: I5ab930bc15e90906b648bc399b551e6bd5d6498f
2013-08-13 14:57:03 -07:00
Sameer Agarwal 080d1d04bd Use more performant, less conservative Eigen solvers.
colPivHouseholderQR -> householderQR
ldlt -> llt.

The resulting performance differences are significant enough
to justify switching.

LAPACK's dgels routine used for solving linear least squares
problems does not use pivoting either.

Similarly, we are not actually using the fact that the matrix
being factorized can be indefinite when using LDLT factorization, so
its not clear that the performance hit is worth it.

These two changes result in Eigen being able to use blocking
algorithms, which for Cholesky factorization, brings the performance
closer to hardware optimized LAPACK. Similarly for dense QR
factorization, on intel there is a 2x speedup.

Change-Id: I4459ee0fc8eb87d58e2b299dfaa9e656d539dc5e
2013-08-13 21:27:55 +00:00
Sameer Agarwal d5b93bf9ec More pre-ordering support.
1. CX_SPARSE supports pre-ordering of the jacobian.
2. Add support for constrained approximate minimum degree ordering
   for SuiteSparse versions >= 4.2.0
3. Using 2, support for pre-ordering for SPARSE_SCHUR when used
   with SUITE_SPARSE.
4. Using 2, support for user orderings in SPARSE_NORMAL_CHOLESKY.
5. Minor cleanups in documentation and code all around.
6. Test update and refactoring.

Change-Id: Ibfe3ac95d59d54ab14d1d60a07f767688070f29f
2013-04-29 11:18:13 -07:00
Sameer Agarwal 344c09f5bc Block ordering for SPARSE_SCHUR + CX_SPARSE.
Uptil now only SuiteSparse when used with SPARSE_SCHUR would use
the block structure of the reduced camera matrix to find a fill-reducing
ordering.

This leads to substantial speedup for some bundle adjustment
problems.

Credit for this technique goes to the authors of g2o. I learned
about it from reading their source code.

Change-Id: I5403efefd4d9552c9c6fc6e02a65498bdf171584
2013-04-26 19:47:45 -07:00
Sameer Agarwal c1e10d9f57 Death to BlockSparseMatrixBase
Change-Id: I13b2b951297ae81bfab0a7b4991a791ed91d594c
2013-04-24 19:15:39 +00:00
Sameer Agarwal 9189f4ea4b Enable pre-ordering for SPARSE_NORMAL_CHOLESKY.
Sparse Cholesky factorization algorithms use a fill-reducing
ordering to permute the columns of the Jacobian matrix. There
are two ways of doing this.

1. Compute the Jacobian matrix in some order and then have the
   factorization algorithm permute the columns of the Jacobian.

2. Compute the Jacobian with its columns already permuted.

The first option incurs a significant memory penalty. The
factorization algorithm has to make a copy of the permuted
Jacobian matrix.

Starting with this change Ceres pre-permutes the columns of the
Jacobian matrix and generally speaking, there is no performance
penalty for doing so.

In some rare cases, it is worth using a more complicated
reordering algorithm which has slightly better runtime
performance at the expense of an extra copy of the Jacobian
matrix. Setting Solver::Options::use_postordering to true
enables this tradeoff.

This change also removes Solver::Options::use_block_amd
as an option. All matrices are ordered using their block
structure. The ability to order them by their scalar
sparsity structure has been removed.

Here is what performance on looks like on some BAL problems.

Memory
======
                                     HEAD         pre-ordering
16-22106                      137957376.0          113516544.0
49-7776                        56688640.0           46628864.0
245-198739                   1718005760.0         1383550976.0
257-65132                     387715072.0          319512576.0
356-226730                   2014826496.0         1626087424.0
744-543562                   4903358464.0         3957878784.0
1024-110968                   968626176.0          822071296.0

Time
====
                                     HEAD         pre-ordering
16-22106                              3.8                  3.7
49-7776                               1.9                  1.8
245-198739                           82.6                 81.9
257-65132                            14.0                 13.4
356-226730                           98.8                 95.8
744-543562                          325.2                301.6
1024-110968                          42.1                 37.1

Change-Id: I6b2e25f3fed7310f88905386a7898ac94d37467e
2013-04-19 19:27:23 -07:00
Sameer Agarwal 222ca20e8f SuiteSparse cleanup.
1. Silence CHOLMOD's indefiniteness warnings.
2. Add a comment about how the error handling in suitesparse.cc
   needs to be improved.
3. Move the analysis logging into suitesparse.cc and out of the
   three callsites.

Change-Id: Idd396b8ea4bf59fc1ffc7f9fcbbc7b38ed71643c
2013-04-01 11:42:19 -07:00
Sameer Agarwal 564a83fcc6 Lint cleanup from William Rucklidge.
Change-Id: I8d4a0aa3e264775d20e99a6b5265f3023de92560
2013-03-26 11:41:50 -07:00
Pablo Speciale 015d57f173 Avoiding the Warning: "deprecated conversion from string constant to char*"
Change-Id: Ifa47f9b0724f79c5c695828628c89818ddefd844
2013-03-19 21:22:11 +00:00
Sameer Agarwal 700d50d807 Lint cleanup from William Rucklidge
Change-Id: Iacbf77246109f687708696eee7fb6144d23e7ec5
2013-03-12 16:12:42 -07:00
Sameer Agarwal 8140f0fc97 Modularize the build.
1. Add -DLINE_SEARCH_MINIMIZER to CMake to make the line search
   minimizer optional.
2. Better handling of -DSUITESPARSE/-DCXSPARSE in top level cmake
   file.
3. Disable code which will never be used if SuiteSparse and/or
   CXSparse is not available.
4. Update build docs.
5. Update jni/Android.mk
6. Minor lint cleanup from William Rucklidge.

Change-Id: If60460a858000df82faed7a6bb056dd2bfdde562
2013-03-12 11:11:57 -07:00
Sameer Agarwal 42a84b87fa Expand reporting of timing information.
1. Add an ExecutionSummary object to record execution
   information about Ceres objects.
2. Add an EventLogger object to log events in a function call.
3. Add a ScopedExecutionTimer object to log times in ExecutionSummary.
4. Instrument ProgramEvaluator and all the linear solvers
   to report their timing statistics.
5. Connect the timing statistics to Summary::FullReport.
6. Add high precision timer on unix systems using
   gettimeofday() call.
7. Various minor clean ups all around.

Change-Id: I5e09804b730b09535484124be7dbc1c58eccd1d4
2013-02-06 01:00:38 -08:00
Sameer Agarwal bdabc29d90 Change LOG(ERROR) to LOG(WARNING) in schur_complement_solver.cc
Change-Id: Ie248989b230ec2357a00a15ef35087e85682315c
2012-11-07 11:26:32 -08:00
Sameer Agarwal 0c52f1e29b LinearSolver::Options::num_eliminate_blocks is dead.
Replaced with LinearSolver::Options::elimination_groups.

Change-Id: I7c07542ec19279a35ddf6da498d417a79395c77f
2012-09-17 11:40:03 -07:00
Petter Strandmark 1e3cbd9a44 Caching the symbolic Cholesky factorization when using CXSparse
Average factorization times for bundle adjustment test problem:
SuiteSparse: 0.2794 s.
CXSparse: 0.4039 s.
CXSparse cached: 0.2399 s.

CXSparse will still be slower, though, because it has to compute
the transpose and J^T * J.

Change-Id: If9cdaa3dd520bee84b56e5fd4953b56a93db6bde
2012-08-30 09:45:20 -07:00
Sameer Agarwal cb83b2801e Fix broken build.
Fix broken build and verbosity issues.

1. While cleaning up the last CL, I broke a macro.
2. cholmod_common_print was being called too often.

Change-Id: Ia76d8863c72f31b0c02977094b22035ceef835cf
2012-06-06 22:37:33 -07:00
Sameer Agarwal 7a3c43b847 Block oriented fill reducing orderings.
By virtue of the modeling layer in Ceres being block oriented,
all the matrices used by Ceres are also block oriented.
When doing sparse direct factorization of these matrices, the
fill-reducing ordering algorithms can either be run on the
block or the scalar form of these matrices. Running it on the
block form exposes more of the super-nodal structure of the
matrix to the Cholesky factorization routines. This leads to
substantial gains in factorization performance.

This changelist adds support for approximate minimium degree
orderings to be computed on the block structure of the
Schur complement matrix. This affects, SchurComplementSolver
and VisibilityBasedPreconditioner and SparseNormalCholesky
 when using SuiteSparse.

A bool, use_block_amd has been added to Solver::Options and
bundle_adjuster.cc has been updated to allow testing with it.

When combined with a multithreaded Schur elimination, speed ups
can be seen quite uniformly across the board. For some problems
this can be dramatic, reducing the factorization time from 70
seconds down to 17 seconds.

Change-Id: I15ebb0afcbc85ada032ec8d179ee3a2f7c8d3e46
2012-06-06 22:02:17 -07:00
Sameer Agarwal b051873a55 Multiple sparse linear algebra backends.
1. Added support for CXSparse - SparseNormalCholesky and
   SchurComplementSolver support SuiteSparse and CXSparse now.
   I am not sure I will add suport for visibility based
   preconditioning using CXSparse. Its not a high priority.

2. New enum SparseLinearAlgebraLibraryType which allows the user
   to indicate which sparse linear algebra library should be used.

3. Updated tests for SolverImpl and system_test.

4. Build system changes to automatically detect CXSparse and
   link to it by default -- just like SuiteSparse.

5. Minor bug fixes dealing in the cmake files and VBP.

6. Changed the order of the system test.

7. Deduped the unsymmetric linear solver test.

Change-Id: I33252a103c87b722ecb7ed7b5f0ae7fd91249244
2012-05-29 19:44:43 -07:00
Sameer Agarwal a9d8ef847f 1. Remove constant_sparsity from LinearSolver::Options. It introduces
unnecessarily complexity in the structure of linear solvers and preconditioners.
This is the first step towards cleaning up the Preconditioner interface.

2. Minor tweaks and cleanups to the various linear solvers.
2012-05-14 02:28:05 -07:00
Keir Mierle 8ebb073038 Initial commit of Ceres Solver. 2012-04-30 23:09:08 -07:00