Commit Graph

34 Commits

Author SHA1 Message Date
Nikolaus Demmel 7b8f675bfd fix formatting for (non-generated) internal source files
- Change formatting standard to Cpp11. Main difference is not having
  the space between two closing >> for nested templates. We don't
  choose c++14, because older versions of clang-format (version 9
  and earlier) don't know this value yet, and it doesn't make a
  difference in the formatting.
- Apply clang-format to all (non generated) internal source files.
- Manually fix some code sections (clang-format on/off) and c-strings
- Exclude some embedded external files with very different formatting
  (gtest/gmock)
- Add script to format all source files

Change-Id: Ic6cea41575ad6e37c9e136dbce176b0d505dc44d
2020-09-21 02:52:07 +02:00
Sameer Agarwal 94712db5c7 Convert calls to CHECK_NOTNULL to CHECK.
CHECK_NOTNULL is being deprecated and removed from the
glog library.

Change-Id: I4a6d1eec6e82a768c7861c8f776bf1f9c0b50c74
2018-08-28 08:14:19 -07:00
Sameer Agarwal bbe790e0f3 Fix three out of bounds errors in CompressedRowSparseMatrix.
All three errors are of the form where, the vector storing
the columns array for a CompressedRowSparseMatrix is being
accessed out of bounds. But in each case, the access is innocuous
because there are other tests which prevent it from being used.

However, if one compiles with debug mode on, the bounds checking
in the underlying std::vector triggers an error.

More details:

1. FromTripletSparseMatrix tried accessing the col vector of
   a matrix with zero rows, but the following code was a no-op
   because there was nothing to copy.

   Adding an early return which deals with this case fixes this.

2. RightMultiply and SquaredColumnNorm had the following loop

   while (r > cols_[idx] && idx < idx_end) {
        ++idx;
   }

   where cols_ was being accessed out of bounds, before idx < idx_end
   was being checked. Swapping the order of the comparisons fixes this
   error.

@richmattes reported this error.

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

Change-Id: I2dbc90ef24ecaffc6f46f1622339d6053b43db45
2018-07-10 06:17:34 +00:00
Sameer Agarwal f973e107d2 Enable mixed precision solves.
1. Add Solver::Options::use_mixed_precision_solves,
   and Solver::Options::max_num_refinement_iterations.
2. Make SparseCholesky::Create return a unique_ptr.
3. SparseCholesky::Create now takes LinearSolver::Options
   as an argument.
4. IterativeRefiner's constructor does not require num_cols
   as an argument.
5. SparseNormalCholeskySolver now uses a separate rhs vector.

This basic implementation results in a 10% reduction in solver time
and 30% reduction in linear solver memory usage.

Change-Id: I6830f32cae2febf082d2733262eb2c9f0482b0ea
2018-04-10 11:01:38 -07:00
Sameer Agarwal 9d4d0bfe29 Make CompressedRowSparseMatrix sensitive to StorageType
Even though we added support for storing the upper and
lower triangular parts of symmetric matrices in
CompressedRowSparseMatrix. RightMultiply, LeftMultiply
and SquaredColumnNorm were not modified to account for this.

This CL changes their implementation and adds thorough
tests.

Also methods that cannot work correctly with symmetric
storage now CHECK and fail, because that indicates
programmer error.

Change-Id: I76288472c8bac98db7376a79bdb6259e346ef2b7
2018-03-26 20:46:57 +00: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 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
Sameer Agarwal 1d7185f130 Refactor SparseNormalCholeskySolver
Now that there is a single piece of code doing the outer product
computation for all three sparse linear algebra backends, move
this code one level up the call stack and there by make the actual
per-library solver code shorter and simpler.

Also fix a minor omission in the outer product computation code
where row/column blocks were not being copied over to the
outer product matrix.

Change-Id: I22a7967bdc659385b741901afefa7af312e676e5
2017-04-27 01:45:05 -07:00
Sameer Agarwal d04347af43 Delinting from William Rucklidge.
Change-Id: I1b0243d43d55b6c9cdcda9d4b677ec444928b223
2017-04-13 11:47:01 -07:00
Sameer Agarwal a171ba095c Fix an off by one error in the transpose code.
Change-Id: I0ebcf2e8e29dc48376f60c3fa8d6100b20ec105f
2017-04-13 09:47:49 -07:00
Sameer Agarwal 9a6b4a6491 Fix a bug in the transpose code.
Change-Id: I1ce997329c7d59b831b84bf41d470ce686fea566
2017-04-13 07:27:48 -07:00
Sameer Agarwal 3de61fc9c0 Improve comments for TransposeForCompressedRowSparseStructure.
Also make the code a little more safe in its assumptions around
zeroed out arrays.

Change-Id: I5a3588162da496fae2b31a4bf8adef1a02b85ec6
2017-04-13 07:11:58 -07:00
Sameer Agarwal bb5c3bb001 Fix the build.
Change-Id: I87216d4a349fa7cb297c502a90cb54c02b9b4445
2017-04-13 06:20:13 -07:00
Sameer Agarwal 9a4f554b85 Change a size() == 0 to empty
Change-Id: I79c7021781093ab1cc25e6cc9700e2a83169daac
2017-04-12 14:42:00 -07:00
Sameer Agarwal 1f8fa9d26b Various grammar and lint corrections.
Thanks William Rucklidge as usual :)

Change-Id: I787b3a0302bcfd08dc1be165d39b7295e58ff20f
2017-04-12 13:37:28 -07:00
Sameer Agarwal 64a20e7ce2 Remove dead code from CompressedRowSparseMatrix.
SolveLowerTriangularInPlace
SolveLowerTriangularTransposeInPlace

were unused functions which can be removed.

Change-Id: I0fd29c1efae2a0a74666f6e3541473bebc22ae82
2017-04-11 12:10:40 -07:00
Sameer Agarwal 2755fce8d3 Add a storage type to CompressedRowSparseMatrix
By adding an enum to CompressedRowSparseMatrix, which indicates
whether the matrix is unsymmetric, upper or lower triangular
we are able to improve the readability and fix some minor
bugs in the way some matrix manipulation code was being
called.

Thank to William Rucklidge for this suggestion.

Change-Id: I355c90d11cd5d31f5a25741b0bda4fc4583e9095
2017-04-11 11:47:42 -07:00
Sameer Agarwal 5f41a60458 Refactor CreateRandomCompressedRowSparseMatrix
Move it to compressed_row_sparse_matrix.h/cc for upcoming re-use.
Also clean up the tests for ComputeOuterProduct so that they do
not depend on CXSparse anymore and use Eigen instead. This also
makes the test simpler and shorter.

Change-Id: I06bbeb3b0c6a07fb1f3da354ef0abd17d246be9a
2017-04-11 11:33:37 -07:00
Cheng Wang 07dbf31eca Optimize J' * J in sparse_normal_cholesky_solver.
1. Add stype to the outerproduct computation to control the output
matrix in upper or lower triangular matrix. For SuiteSparse,
upper triangular matrix is generated. SuiteSparse can directly use
this matrix format for cholesky without matrix transpose overhead.

2. Change the outerproduct computation to block multiplication.  This
reduces the computation complexity for the sort in preprocessing, also
allows formulation of the block outerproduct computation as dense Eigen
block matrix multiplication.

3. Solve 32 Tango problems on Qualcomm MSM8994 Cortex-A53 (1.55GHz)
   before change: 140 seconds
   after change: 131 seconds

Change-Id: I8054114cef911de6a303310a448821ca296e4744
2017-04-10 09:50:34 -07:00
Sameer Agarwal 1ac3dd223c Fix a bug in CompressedRowSparseMatrix::AppendRows
The test for CompressedRowSparseMatrix::AppendRows tries to add
a matrix of size zero, which results in an invalid pointer deferencing
even though that pointer is never written to.

Change-Id: I97dba37082bd5dad242ae1af0447a9178cd92027
2015-09-05 15:56:14 -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
Richard Stebbing 32530788d0 Add dynamic_sparsity option.
The standard sparse normal Cholesky solver assumes a fixed
sparsity pattern which is useful for a large number of problems
presented to Ceres. However, some problems are symbolically dense
but numerically sparse i.e. each residual is a function of a
large number of parameters but at any given state the residual
only depends on a sparse subset of them. For these class of
problems it is faster to re-analyse the sparsity pattern of the
jacobian at each iteration of the non-linear optimisation instead
of including all of the zero entries in the step computation.

The proposed solution adds the dynamic_sparsity option which can
be used with SPARSE_NORMAL_CHOLESKY. A
DynamicCompressedRowSparseMatrix type (which extends
CompressedRowSparseMatrix) has been introduced which allows
dynamic addition and removal of elements. A Finalize method is
provided which then consolidates the matrix so that it can be
used in place of a regular CompressedRowSparseMatrix. An
associated jacobian writer has also been provided.

Changes that were required to make this extension were adding the
SetMaxNumNonZeros method to CompressedRowSparseMatrix and adding
a JacobianFinalizer template parameter to the ProgramEvaluator.

Change-Id: Ia5a8a9523fdae8d5b027bc35e70b4611ec2a8d01
2014-04-28 07:13:09 +00:00
Richard Bowen 3e60a998ac Added support and tests: row and column blocks for sparse matrix
transpose.

Change-Id: Ife641b08a9e86826478521a405f21ba60667f0e8
2014-04-04 17:56:49 -07:00
Sameer Agarwal f14f6bf9b7 Speed up SPARSE_NORMAL_CHOLESKY when using CX_SPARSE.
When using sparse cholesky factorization to solve the linear
least squares problem:

  Ax = b

There are two sources of computational complexity.

1. Computing H = A'A
2. Computing the sparse Cholesky factorization of H.

Doing 1. using CX_SPARSE is particularly expensive, as it uses
a generic cs_multiply function which computes the structure of
the matrix H everytime, reallocates memory and does not take
advantage of the fact that the matrix being computed is a symmetric
outer product.

This change adds a custom symmetric outer product algorithm for
CompressedRowSparseMatrix.

It has a symbolic phase, where it computes the sparsity structure
of the output matrix and a "program" which allows the actual
multiplication routine to determine exactly which entry in the
values array each term in the product contributes to.

With these two bits of information, the outer product H = A'A
can be computed extremely fast without any reasoning about
the structure of H.

Further gains in efficiency are made by exploiting the block
structure of A.

With this change, SPARSE_NORMAL_CHOLESKY with CX_SPARSE as the
backend results in > 300% speedup for some problems.

The symbolic analysis phase of the solver is a bit more expensive
now but the increased cost is made up in 3-4 iterations.

Change-Id: I5e4a72b4d03ba41b378a2634330bc22b299c0f12
2013-12-30 10:00:06 -08:00
Sameer Agarwal 2b16b0080b CompressedRowSparseMatrix::AppendRows and DeleteRows bugfix.
CompressedRowSparseMatrix can store the row and column block structure
but the AppendRows and DeleteRows methods did not pay attention to them.
This meant that it was possible to get to a CompressedRowSparseMatrix
whose block structure did not match the contents of the matrix.

This change fixes this problem.

Change-Id: I1b3c807fc03d8c049ee20511e2bc62806d211b81
2013-12-24 22:40:50 -08:00
Sameer Agarwal a427c877f9 Lint cleanup.
Change-Id: Ie489f1ff182d99251ed8c0728cc6ea8e1c262ce0
2013-06-24 18:04:28 -07:00
Sameer Agarwal c367b12eeb Incomplete LQ Factorization.
Drop support for protocol buffers.
Add CompressedRowSparseMatrix::CreateBlockDiagonalMatrix.
Add CompressedRowSparseMatrix::SolveLowerTriangularInPlace.
Add CompressedRowSparseMatrix::SolveLowerTriangularTranposeInPlace.
Add CompressedRowSparseMatrix::Transpose.

Change-Id: I2328afca9fac632685eac72ebb00998bd3510187
2013-06-23 21:45:57 +00:00
Sameer Agarwal dd2b17d7dd CERES_DONT_HAVE_PROTOCOL_BUFFERS -> CERES_NO_PROTOCOL_BUFFERS.
Change-Id: I6c9f50e4c006faf4e75a8f417455db18357f3187
2012-08-17 13:21:33 -07:00
Sameer Agarwal 4997cbc437 Return jacobians and gradients to the user.
1. Added CRSMatrix object which will store the initial
   and final jacobians if requested by the user.
2. Conversion routine and test for converting a
   CompressedRowSparseMatrix to CRSMatrix.
3. New Evaluator::Evaluate function to do the actual evaluation.
4. Changes to Program::StateVectorToParmeterBlocks and
   Program::SetParameterBlockStatePtrstoUserStatePtrs so that
   they do not try to set the state of constant parameter blocks.
5. Tests for Evaluator::Evaluate.
6. Minor cleanups in SolverImpl.
7. Minor cpplint cleanups triggered by this CL.

Change-Id: I3ac446484692f943c28f2723b719676f8c83ca3d
2012-07-16 12:17:34 -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
Sameer Agarwal 82f4b88c34 Extend support writing linear least squares problems to disk.
1. Make the mechanism for writing problems to disk, generic and
controllable using an enum DumpType visible in the API.

2. Instead of single file containing protocol buffers, now matrices can
be written in a matlab/octave friendly format. This is now the default.

3. The support for writing problems to disk is moved into
linear_least_squares_problem.cc/h

4. SparseMatrix now has a ToTextFile virtual method which is
implemented by each of its subclasses to write a (i,j,s) triplets.

5. Minor changes to simple_bundle_adjuster to enable logging at startup.
2012-05-08 17:40:16 -07:00
Keir Mierle 8ebb073038 Initial commit of Ceres Solver. 2012-04-30 23:09:08 -07:00