Commit Graph

318 Commits

Author SHA1 Message Date
Sameer Agarwal 9814a91fcf Use C++11's inline member initialization syntax
Migrate all Option and Summary structs to use
inline member initialization syntax.

This reduces the amount of code, and collocates the
default values with the documentation for the corresponding
member variable.

Change-Id: I8e6b9ee3b31464699d678667f6166ace5fc137c9
2018-04-06 16:50:42 -07:00
Sameer Agarwal e49507cbea More C++ification.
"> >" -> ">>"
"> > >" -> ">>>"

Change-Id: Id1ddd9dbf030fe21d57207741b4ca8403068e55b
2018-04-03 10:58:15 -07:00
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 ffae101c75 Removed CERES_USE_CXX11
Remove the special handling for tr1::shared_ptr.

Change-Id: Ic405dcfe881dd6560d236956f84bfb8911d0bcbb
2018-03-29 15:16:49 -07:00
Sameer Agarwal c2f5cd10e7 Update docs and code in preparation for 1.14.0
Change-Id: I81b430fbff62fb5e3f4c75945f6d40361d546918
2018-03-13 10:10:13 -07:00
Keir Mierle 7bdceb46cf Evaluation callback API
This adds a callback mechanism to for users to get notified just
before jacobian and residual evaluations. This will enable
aggressive caching and sharing of compute between cost functions.

Change-Id: I67993726920218edf71ab9ae70c34c204756c71a
2018-03-07 12:41:22 -08:00
Sameer Agarwal 3d933750a7 Deprecate Solver::Options::num_linear_solver_threads
1. Solver::Options::num_threads now controls parallelism in Ceres
   Solver. The user specified value of
   Solver::Options::num_linear_solver_threads is ignored.
2. If the user specifies Solver::Options::num_linear_solver_threads
   and it is different from Solver::Options::num_threads,
   a warning is printed.
3. Solver::Summary:num_linear_solver_threads_given and
   Solver::Summary::num_linear_solver_threads_used are also
   deprecated and are always set to Solver::Summary::num_threads_given
   and Solver::Summary::num_threads_used.

Change-Id: I20b9336d9336e400e6f0a15b63857c0c43eb271c
2018-02-27 20:38:05 -08:00
Mike Vitus 5fcd3155f4 Fixes the configuration check in port.h.
Fixes a bug introduced in 12280 that incorrectly used xor for multiple
values.

Tested it manually by verifying it would fail for 2, 3, or 4 of them
defined.

Change-Id: I90c2662f4744aceaf0119dbbe072beced3f67c9a
2018-02-27 10:51:02 -08:00
Mike Vitus 7b2717582a Adds C++11 parallel for implementation.
Implements ParallelFor using the C++11 based ThreadPool.  The C++11
parallel for is 50-70% faster than single threaded, and 20-30% slower
than TBB.

Tested by compiling with OpenMP, TBB, and C++11 Threading support and
ran the unit tests.  Ran bazel as well.

Change-Id: I7fd6c9037ff9f200ce6999b5f39918995bb6b8ea
2018-02-26 16:12:03 -08:00
Mike Vitus f408f89e8b Adds a Ceres Context structure.
A Ceres Context holds common global state that can be re-used within
Ceres.  The Context current contains a thread pool if compiling with
C++11 threading support.  Threads are expensive to create and destroy so
it is good to maintain across multiple Ceres solves.

Tested by compiling with and without TBB support and ran unit tests. Ran
bazel as well.

Change-Id: I82f598dfae642aa0e81a6039dc174608a5e8dbfb
2018-02-26 10:37:53 -08:00
Emil Ernerfeldt 39ccb79ae6 Implement some C++11 math functions for Jet
Change-Id: Ie164aeaac980cd2fe48a86230e1948c9fd75424a
2018-02-25 21:48:48 +00:00
Alex Stewart 1141f4ad1a Fix integer conversion warning in MSVC.
- Raised by cnovel as issue #346:
  https://github.com/ceres-solver/ceres-solver/issues/346.

Change-Id: I90815ab6412834dd793ca7c327002d551da0675b
2018-02-24 18:51:54 +00:00
Sameer Agarwal 2145c10539 Improve ExecutionSummary
1. Replace two maps by 1.
2. Update number of calls and the time for the call at the cost
   of a single map lookup.
3. Add Solver::Summary::num_linear_solves.

Fixes https://github.com/ceres-solver/ceres-solver/issues/340

Change-Id: I71eb9be7fb363a8cb066591c4c1761f256c81677
2018-02-05 16:37:27 -08:00
Sameer Agarwal 0251359830 A bunch of minor changes.
1. Fix a typo in auto_diff_cost_function.h
2. Fix and update Solver::Summary::FullReport() text labels.
3. Add logging of the number of residual and jacobian evaluations
   to the full report. The GradientProblemSolver already does this.

Change-Id: I41059af5f0ebe0417accbbc30b0808a4b04b9edb
2018-02-05 15:51:08 -08:00
Sameer Agarwal 78d49f4682 Update EigenTypes to deal with 1 column matrices
Most matrices in Ceres are row-major. Eigen does now
allow statically sized row-major matrices with a single
column.

This CL changes the EigenTypes struct to change the definition
of typedefs to change to ColMajor if this is the case.

This will allow Schur specializations (2,1,6) for example.

Change-Id: I24e7b62d4e1cb5660541062336225bbc9c41c6bf
2018-01-31 04:57:54 -08:00
Sameer Agarwal 202dd9f3a1 Add GradientProblemSolver::Options::update_state_every_iteration
The non-linear least squares solver had the ability to update the
user's parameters every iteration. Now GradientProblemSolver can
do the same.

Also a few minor Sphinx markup related cleanup to the documentation
which were found in the process of updating Sphinx on my machine
and adding the docs for this feature.

This fixes https://github.com/ceres-solver/ceres-solver/issues/246

Change-Id: Ib6b90ac22be8bfb60b14f25ad52082ba371af164
2018-01-25 21:30:09 -08:00
Rodrigo Queiro 237eb17bdd Consistently use quotes to include Eigen
This makes these files match the convention in the rest of the codebase.

Change-Id: I3a0634274e0e604c575dd3c78fa87f485770e7ff
2018-01-12 18:25:16 +00:00
Janick Martinez Esturo c2da96082b Fix Eigen >= 3.3 compilation if EIGEN_DONT_VECTORIZE set
Would result in compilation error

include/ceres/jet.h:296:59: error: requested alignment is not a positive power of 2
   alignas(kAlignment) Eigen::Matrix<T, N, 1, kAlignOrNot> v;

because CERES_JET_ALIGN_BYTES = EIGEN_MAX_ALIGN_BYTES = 0 in that case.

Change-Id: I247653d3b7555e80891d3fb78676acfa302a9e1d
2018-01-12 17:50:01 +01:00
Alex Stewart e9009829b5 Fix use of unnamed type as template argument warnings on Clang.
- When compiling Ceres without C++11 but with glog, then the unnamed
  enum results in a compiler warning when
  google::GetReferenceableValue(.) is invoked with the unnamed enum as a
  template parameter (unnamed-type-template-args) which was not legal
  prior to C++11.

Change-Id: Iddbed05e2199865ab915f2c5817ce917bb0609b5
2017-12-01 10:12:54 +00:00
Keir Mierle cb497df619 Add link for CLA in docs; minor fixes
Also fixes a typo in cost_function.h

Change-Id: I45f08fd27fd2f8e5cb5b3f08e0d4f9a3e8822a15
2017-10-24 17:18:02 -07:00
Sameer Agarwal 030b41dd0e Improve compatibility with ceres::Solver
1. Default linear solver is Eigen::LDLT
2. Options::max_iterations -> Options::max_num_iterations
3. Options::error_threshold -> Options::cost_threshold
4. Options::relative_step_threshold -> Options::parameter_threshold
5. Options::initial_scale_factor -> Options::initial_trust_region_radius
6. The default values of the above parameters have been changed
   to match those in ceres::Solver::Options
7. Status::RUNNING has been removed
8. Update now returns a bool instead of a Status enum and
   the status handling has been included in the main loop.
9. Summary::gradient_norm has been changed to Summary::gradient_max_norm
   to match the convergence test
10. A member variable cost_ has been added which is computed by Update
11. The test for parameter_tolerance based convergence is made
    more robust near zero.
12. Use of double has been replaced by Scalar.
13. Minor clang-formatting

Change-Id: I3cb0e2fd0a0204476bb8718761dc740cdf5e42ce
2017-10-22 21:40:49 -07:00
Keir Mierle dc9bf012c4 Fix tiny solver build break
The solver code must rely on the vectors for
sizing, since not all cost functions will have
NumParameters() or NumResiduals().

Change-Id: Id254ce37507443910edb0064de7907d64558851e
2017-10-19 23:46:53 -07:00
Sameer Agarwal ba73ce120c Improve the convergence performance of TinySolver
1. Default constructor and initialization for Summary.
2. Add Jacobi scaling.
3. Add bounds on the lm diagonal
4. Use the diagonal of J'J as the regularizer instead of identity.
5. Update the computation of rho to match the change in regularization.

As a result of these changes, the performance of TinySolver is
now the same as ceres::Solver, solving 53 out of 54 problems.

Change-Id: Ie08c3389ac2e3964ffa04411734c06b65835358a
2017-10-19 13:52:31 -07:00
Keir Mierle b485002bb7 Replace template use of >>
Older compilers do not support >> to terminate
templates, only > >. Ceres supports old compilers.

Change-Id: I7e43dc9fdac06507b32dd0c9bf1a3bc2a544916b
2017-10-14 16:08:17 -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 cc0bd492bd A number of minor changes to TinySolver
1. Instead of Core/LU just include Eigen/Dense
2. Rename SolverParameters to Options and params to options.
3. Rename Results to Summary.
4. Summary::error_magnitude -> Summary::final_cost.
5. Add Summary::initial_cost.
6. Change definitions of Summary::initial_cost and Summary::final_cost
   to match those used by Ceres::Solver.

Change-Id: Id64b78398f47810ca25938a15423c514fc8c164d
2017-10-14 14:14:14 -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
Sameer Agarwal 2a956099d4 Add missing Eigen traits to Jets
Add highest and lowest traits to the Jet implementation.

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

Change-Id: I7c68fa8e2baa7742880d3faa21f366352e48aacf
2017-09-27 03:57:49 +00:00
Alex Stewart 5eae62348d Add compound with scalar operators for Jets.
Change-Id: Ie47771d8c9df22ddeb6b643ae8a751860d892d67
2017-07-06 19:10:34 +01: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 3f6d01f313 Add a missing CERES_EXPORT to GradientChecker
Change-Id: Ib6b21354191b7ef5a0781cb3e53488144aa3f7b6
2017-05-14 23:31:13 -07:00
Sameer Agarwal 39388bd2ff Address comments from William Rucklidge
Change-Id: I0c4f686b89798435741c55a5ca45cf000af842dc
2017-05-09 17:23:43 -07:00
Ricardo Sanchez-Saez 08d02d7177 Fix implicit precission loss warning on 64-bit archs
Change-Id: Ia9212be3678ea6c92e9aef0e3f5c14447c9d9f88
2017-04-22 16:25:18 -07:00
Sameer Agarwal 85d276c84d Remove future looking comments from types.h
Change-Id: I2c801980778e2575ef8eaa52eafbdb3724f1ae01
2017-04-18 07:46:06 -07:00
Sameer Agarwal 14d8297cf9 Refactor Covariance::Options::algorithm_type.
THIS IS AN API BREAKING CHANGE.

Decouple the algorithm from the sparse linear algebra
library being used to perform the computation.

Before this change

Covariance::AlgorithmType had values

DENSE_SVD
EIGEN_SPARSE_QR
SUITE_SPARSE_QR

This has been replaced by two enums now.

Covariance::Options::sparse_linear_algebra_library_type
which can take values EIGEN_SPARSE, SUITE_SPARSE or CX_SPARSE.
The last one is currently not supported.

And Covariance::Options::algorithm_type takes values

DENSE_SVD
SPARSE_QR

This sets the stage for future extensions of the covariance
computation algorithm.

Also as part of this change, the covariance computation chapter
has been made a top level chapter on its own instead of being
buried deep inside the Solving Non-linear Least Squares problem.

Change-Id: Ibfbf60902d8d17694d9ff585047a5a57d329ab22
2017-04-17 09:43:22 -07:00
Julien Pilet e5dceb3ab0 Fix Jet/Eigen compatibility for Eigen > 3.3
Honour the digits10 NumTraits member introduced in Eigen 3.3.

Change-Id: If20183332beef8335b7758cdb58859a43fc59970
2017-02-28 10:34:26 +01:00
Sameer Agarwal 5f87f35cec Changes from William Rucklidge
Change-Id: Ia5d724edef947870fe13050a71aa1cba878352a8
2017-02-27 11:09:08 -08:00
Sameer Agarwal 1cfec3c666 Improvements to Schur template specializations
1. Refactor the python code that generates the template specializations
   to remove code duplication.
2. Improved the logic for template specialization selection where
   Eigen::Dynamic now serves as a wildcard.
3. Added schur_templates.h/cc which allows querying the set of available
   template specializations without instantiating a linear solver.
4. Added Solver::Summary::schur_structre_given and
   Solver::Summary::schur_structure_used and expose them in
   Solver::Summary::FullReport for better performance debugging.
5. Updated the templates with newer dates and some minor comments cleanup
   which lead to the the template specializations to be re-generated.

Change-Id: Iaf3c6f714353597899916c300465da01f151c3de
2017-02-23 14:52:59 -08:00
Sameer Agarwal 62a70bc74c Lint cleanup from William Rucklidge
Change-Id: I11ebf9bdb09cfd465a32a61e0a9a045ab650deac
2017-02-15 11:01:27 -08:00
Alex Stewart c1ebbfc79b Require Eigen >= 3.3 to define ScalarBinaryOpTraits in Jet.
- ScalarBinaryOpTraits was introduced in Eigen 3.3, and thus breaks
  compilation of Ceres for Eigen versions < 3.3.
- Reported as issue #252 by AustinDeric:
  https://github.com/ceres-solver/ceres-solver/issues/252

Change-Id: Ie9b7dace17709c348b18b297f2d5306b054ce4b0
2017-02-13 16:14:40 +00:00
Chris Sweeney 9a195d91de Specify ScalarBinaryOpTraits for Jet types.
This commit extends the use of Jets with Eigen matrices and arrays
by enabling the use of binary operators (such as scalar addition,
multiplication, as well as matrix multiplication) when one Eigen
matrix/array is of type Jet and the other is a scalar type. This
should increase performance since Jet types have optimized
scalar-to-jet binary operations.

Change-Id: Ia756064845d845cefcf0abb16d366331d2824b52
2017-02-10 19:50:33 +00:00
Je Hyeong Hong ccfd7f4dbb CERES_EXPORT fix to compile Ceres as DLL
Below fixes are required to compile Ceres as a dynamic library using
MSVC.
1. Added CERES_EXPORT to EigenQuaternionParameterization.
2. Removed CERES_EXPORT from CubicInterpolator and BicubicInterpolator.

EigenQuaternionParameterization has its source embedded in ceres.dll 
whereas Cubic+BicubicInterpolators are defined in the header file and 
therefore do not require __declspec(dllimport).

Change-Id: Idd1f387890c36aab51a1a4177ea7c06933b96f39
2017-02-02 03:07:52 +00:00
Alex Stewart 9321b620ac Fix detection of deprecated Bessel function names on MSVC.
- MSVC deprecated the standard POSIX names for Bessel functions in
  favour of underscore prefixed versions.
- Previously we were checking for the presence of the newer underscore
  functions via a macro, which fails as the functions are not defined
  as macros.
- Now we check via a check_cxx_source_compiles() if the newer underscore
  prefixed versions exist when compiling on MSVC and use a new Ceres
  configuration #define to switch on their presence in jet.h

Change-Id: I430880bde2981d12f4d03dbc94d903b9842e887e
2017-01-27 22:31:53 +00:00
Sameer Agarwal 2dd907744d Ensure that partial evaluation of residuals triggers an error
ResidualBlock evaluation has logic to ensure that CostFunction
should always fill out the residual and jacobian arrays completely
by using a special value to pre-populate these arrays.

This works for CostFunctions with analytical Jacobians but not for
AutoDiffCostFunction and NumericDiffCostFunction Jacobians.

There is no way to fix this for NumericDiffCostFunctions without
introducing significant performance penalties but the residual
evaluation fails, which should be enough to catch such errors.

For AutoDiffCostFunction the way the Jets are default initialized
was sidestepping this check. So now, the Jet that is used to
capture the output residuals is now initialized with
kImpossibleValue, which will ensure that if the user forgets
to fill all output fields, it triggers an evaluation error.

This change required that ceres::internal::kImpossibleValue be moved
out of array_utils.h/cc to types.h.

Change-Id: I35bb0946cf0785a5d43c7b5459a2272848fb2a9b
2017-01-16 10:00:36 -08:00
Alex Stewart 3fa415bd0c Enable support for AVX instructions for Jets.
- Eigen versions < 3.3 only supported SIMD instructions that required
  16-byte alignment (SSE), whereas Eigen >= 3.3 also supports AVX
  instructions which require 32+-byte alignment.
- Previously we only ever requested 16-byte alignment for Jets (if >=
  C++11 was enabled) which resulted in Eigen assertions being triggered
  on some compilers as reported as Issue #251:
  https://github.com/ceres-solver/ceres-solver/issues/251.
- Now we use Eigen’s EIGEN_MAX_ALIGN_BYTES macro, defined in Eigen >=
  3.3 to specify the byte alignment for Jets when C++11 is enabled for
  Eigen >= 3.3.  For Eigen versions < 3.3, we maintain the previous
  behaviour of 16.

Change-Id: I749af7a70ae794e0c2a59301128db781e338422c
2017-01-05 19:12:45 +00:00
Alex Stewart 6519e78920 Fix use of alignas(0) which is not ignored on GCC.
- alignas(0) should be ignored, however it results in a build error on
  GCC, so instead default to the alignment of double in Jets if
  we cannot align to 16-byte boundaries on the platform, but are
  compiling with C++11.

Change-Id: I2e54c69516ea2e1447a8bdc138b2dd70050c6dad
2016-11-12 21:45:57 +00:00
Alex Stewart b4e27240b2 Use default alignment if alignof(std::max_align_t) < 16 with C++11.
- As per Andrew Hunter’s comments in the commit which added Jet
  alignment when using C++11 here:
  https://ceres-solver-review.googlesource.com/#/c/7100, there is wide
  lattitude in the standard about what the maximum supported alignment
  can be.
- Previously, we were forcing the alignment to 1, if the value of
  alignof(std::max_align_t), which we use as a proxy for the maximum
  supported alignment on the platform, was < 16.
- An alignment of 1 is not valid for Jets, as it would weaken the
  natural alignment of the types within a Jet, which would typically be
  4 (32-bit systems) or 8 (64-bit systems), thus resulting in a compiler
  error.
- This was reported as issue 235 for Clang 3.8 on i386:
  https://github.com/ceres-solver/ceres-solver/issues/235.

Change-Id: Ie39e5499c64f9231f29ebf4392992b5c9ce2e385
2016-11-12 17:18:25 +00:00
Sameer Agarwal 78abf0cdfe Introduce a common base class for DynamicAutoDiffCostFunction and DynamicNumericDiffCostFunction.
In the process also move some documentation from the file header to
just above the class declaration.

This change is in response to https://github.com/ceres-solver/ceres-solver/issues/233

Change-Id: I08cff1a94c57b67dd2bd8be4dba8c3fce46b68ab
2016-11-11 07:35:22 -08:00