Commit Graph

65 Commits

Author SHA1 Message Date
Johannes Beck 8eef94de4e Sized cost function using variadic templates
This PR changes the interface of sized_cost_fucntion,
autodiff_cost_function and numeric_diff_costfunction from using ten
hardcoded parameter blocks to a variable number of parameter blocks
using variadic templates.

Trailing parameter blocks of size zero are now considered as error.

Change-Id: I37b9a0a420ef0eda6476a46672bbf6bd57e19760
2018-10-23 20:46:16 +02:00
Sameer Agarwal feeadb476a Swap the order of definition of IsValidParameterDimensionSequence.
This triggered a compiler error on clang + linux.

Change-Id: I8628c1b342980c50792f664d04d37465c17bf618
2018-10-10 20:50:39 -07:00
Johannes Beck 7edb3a6b92 Add parameter dims for variadic sized cost function
The class parameter dims is a helper class that holds the parameter
dimensions. The parameter dimensions are either dynamic or the
sizes are known at compile time. It is used to pass parameter block
dimensions around (e.g. between functions or classes).

Also the dimensions of the parameter blocks are checked at compile
time and must be greater than zero. This means trailing zero parameter
blocks will result in a compile error.

Change-Id: I4decf2f09e63fdb0fd652022b2be2a1cfd9c4478
2018-10-09 22:35:17 +02:00
Johannes Beck 336a6be5a4 Adding integer sequence and algorithms
This PR adds integer sequence and two algorithms (sum and exclusive
scan). Those will be needed to implement a sized cost function using
variadic templates.

Change-Id: I8e98d7c11fac94286fe9c364633406e59438f059
2018-10-08 20:42:15 +02:00
Johannes Beck e004eba85c Simplifying Init in manual contructor
All Init() function overloads are replaced by a perfect forwarding Init
function using variadic templates.

Change-Id: I42d0e236d274174982a4e56c0d73ec2776ed96f8
2018-09-27 20:49:44 +02:00
Kuang Fangjun 72d8e1cdb1 Fix typos.
Change-Id: I61296f42354eab797c55c1435ca46b3475b767e9
2018-09-20 22:52:39 +08:00
Kuang Fangjun 57067a2545 fix typos.
Change-Id: I6d3a5eb93e29596823afc5eb11f9a79806078bc0
2018-09-18 09:29:05 +08:00
Alex Stewart 16f9b34c3b Remove Intel TBB threading support.
- In light of the C++11 threads threading option this is no longer
  necessary for cross-platform threading support and did not offer a
  noticeable performance gain over either the C++11 threads
  implementation or OpenMP.

Change-Id: Icb588d520888c19a1775171795b55bcaffb3d256
2018-07-03 18:05:36 +00:00
Alex Stewart 8f41ca6abc Add Apple's Accelerate framework as a sparse linear algebra library.
- Currently DynamicSparseNormalCholeskySolver is unsupported for
  Accelerate.

Change-Id: I03b5a86bb22fef249c4aecd48947a613e8eff7a5
2018-06-29 09:43:03 +01:00
Sameer Agarwal dc8ef467e3 Move max_align_t handling into jet.h
This was currently buried in port.h. Since there is exactly one use
of the max_align_t handling, it is better from a readability
and cleanliness perspective to have this in jet.h.

Change-Id: I2f3dde75df7c333bd95393b9e4fc4cd5160d6afb
2018-04-23 10:03:45 -07:00
Sameer Agarwal 31f24521cc Deprecate macros.h and fpclassify.h
1. Replace CERES_DISALLOW_* with explicitly deleted constructors.
2. Replace use of CERES_ARRAY_SIZE and stack allocated arrays
   with std::vector.
3. Move CERES_ALIGN_* macros into manual_constructor.h, which is
   the one place they are used and will be deprecated along with that
   file.
4. Introduce isnan,isnormal,isinf and isfinite for Jets.
5. Replace IsNormal,IsFinite,IsNaN and IsInfinite with corresponding
   c++11 function calls.

Change-Id: I04f33a221aae77d247602150988b6d4aa4efeeab
2018-04-18 09:54: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
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
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
Keir Mierle 40effe3b15 Tiny solver autodiff adapter
Change-Id: I29fe736d53b2be32a101ba128cf557726def9a00
2017-10-14 15:51:14 -07: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 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
Andrew Hunter d80c112168 Tweak alignment hint implementation for Jets
I169b637a1e2a106956b536c41d6a514a266e7cc0 marked Jets (in C++11) as
aligned to 16 bytes, and enabled Eigen vectorization.  However, to
implement this, we added Eigen includes to port.h.

Turns out this broke some other tricks Ceres uses (redefining Eigen
constants for better performance), so we don't want to do that. Move
most of the implementation to jet.h where it is safe.

Change-Id: I47c6fc4180db1ff674bc660723dd5a2b84254e0d
2015-12-11 14:50:19 -08:00
Andrew Hunter 5667505f35 Align Jet matrices where supported
We currently don't align the infinitesimal part of a Jet to a 16-byte
boundary (and thus force Eigen to avoid using SSE ops)--as a member of a
larger struct, we couldn't guarantee Jets would be allocated on
appropriately-aligned boundaries.  However, C++11 adds better support
for requesting alignment: we can use it to guarantee the members will be
properly aligned, and tell Eigen to vectorize.

There is a significant gotcha here: the standard gives wide latitude to
implementations as to which alignments they choose to support.  If we
ask for 16 and the system only supports 8, we may have misaligned
Jets. So we test (using alignof(std::max_align_t)) that the current
system supports 16-byte aligned values; if not, we fall back to the
current solution.

Two other small notes:
- This is obviously gated on C++11 support, and
  thus we put the logic in port.h and export some useful #defines.

- GCC 4.8.x has a
  bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019) that has
  max_align_t in the wrong namespace.  This will not be a problem with a
  modern GCC, but add a small workaround since many systems still ship 4.8.

This results (on a x86 workstation) in a 60% speedup in Jacobian
evaluation on bin/simple_bundle_adjuster problem-16-22106-pre.txt.

Change-Id: I169b637a1e2a106956b536c41d6a514a266e7cc0
2015-12-08 01:09:07 +00:00
Sameer Agarwal 67622b080c Fix a pointer access bug in Ridders' algorithm.
A pointer to an Eigen matrix was being used as an array.

Change-Id: Ifaea14fa3416eda5953de49afb78dc5a6ea816eb
2015-09-05 21:37:22 +00:00
Sameer Agarwal 2701429f77 Use Eigen::Dynamic instead of ceres::DYNAMIC in numeric_diff.h
Change-Id: Iccb0284a8fb4c2160748dfae24bcd595f1d4cb5c
2015-08-30 21:33:57 -07:00
Tal Ben-Nun 4f049db7c2 Adaptive numeric differentiation using Ridders' method.
This method numerically computes function derivatives in different
scales, extrapolating between intermediate results to conserve function
evaluations. Adaptive differentiation is essential to produce accurate
results for functions with noisy derivatives.

Full changelist:
-Created a new type of NumericDiffMethod (RIDDERS).
-Implemented EvaluateRiddersJacobianColumn in NumericDiff.
-Created unit tests with f(x) = x^2 + [random noise] and
 f(x) = exp(x).

Change-Id: I2d6e924d7ff686650272f29a8c981351e6f72091
2015-08-30 14:06:13 +03:00
Alex Stewart c80ce3525d Fix unused parameter compiler warnings in numeric_diff.h
Change-Id: I2b35babba17229387c6d346c46a2c6960db96e47
2015-07-11 18:27:00 +01:00
Sameer Agarwal 9064b4ed27 Improve numeric differentation near zero.
Before this change, the default step size
for a function F(x) at x was

step_size = |x| * relative_step_size

if step_size was exactly zero, then to prevent
division by zero we would fall back to relative_step_size.

This however is not good enough, as values of x say 1e-64
would lead to step sizes ~ 1e-70 and dividing by such numbers
leads to inaccurate results. For even smaller numbers, like
1e-300, which I have observed can occur as the optimization
algorithm makes progress, this leads to NaNs.

The key change in this CL is to change the fallback mechanism
to be

step_size = max(|x| * relative_step_size, min_step_size)

where

min_step_size = sqrt(DBL_EPSILON)

This is the recommended minimum value for the step size
for double precision arithmetic on the interwebs.

This results in a small loss of precision in the transcendental
functions test, but that is unavoidable as we are not taking
sufficiently small steps anymore.

On the whole though this will improve the numerical performance
of the algorithm.

To validate this approach, one of the parameter values for the
EasyFunctorTest has been set to 1e-64, which causes the test
to start failing without the corrected fallback logic.

This change should also address some if not all of

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

Change-Id: I4a9013ef358626c1ba7b8abad60b3904163d63f6
2015-05-13 21:37:15 -07:00
Tal Ben-Nun b2dcef36e7 Refactored DynamicNumericDiffCostFunction to use NumericDiff
Change-Id: I2fc4b203e984beaa7af96fb3cbe8ce14e5bca614
2015-05-07 01:48:27 +03: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 05a07ecc77 Remove using std::string from port.h
Change-Id: I7376f5e7eace22ec1fc05a61eaa858594f08682d
2015-01-07 15:10:46 -08: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 12eb389b4e Fix Eigen Row/ColMajor bug in NumericDiffCostFunction.
If the parameter block size is 1, asking Eigen to create
a row-major matrix triggers a compile time error. Previously
we were handling the case where the number of rows in the
jacobian block was known statically, but the problem is present
when the nummber of rows is dynamic.

This CL fixes this problem.

Thanks to Dominik Reitzle for reporting this.

Change-Id: I99c3eec3558e66ebf4efa51c4dee8ce292ffe0c1
2014-08-28 16:34:22 +00:00
Björn Piltz c0b883816f Disabled warning C4251 Added the files disable_warnings.h and reenable_warnings.h which need to be included by every file that uses the macro CERES_EXPORT.
Change-Id: I176326a600d094a4524bac873564fcbf8efd2456
2014-05-19 22:53:41 +02:00
Alex Stewart d48e705022 Configure config.h and include it from the build directory.
- Previously we overwrote the default (empty) config.h in the source
  tree with a configured config.h, generated using the current compile
  options.
- This was undesirable as it could lead to inadvertant commits of the
  generated config.h.

- This patch moves the default config.h to <src>/config/ceres/internal,
  separate from the other headers, thus if Ceres is compiled without
  CMake this directory will now also have to be included.  This
  directory is _not_ added to the CMake include directories for Ceres
  (thus the default config.h is never used when compiling with CMake).
- When using CMake, the generated config.h is now placed in
  <build>/config/ceres/internal, which is in turn added to the include
  directories for Ceres when it is compiled, and the resulting config.h
  is copied to ceres/internal when installed.

Change-Id: Ib1ba45e66e383ade2ebb08603af9165c1df616f2
2014-05-13 20:23:20 +01:00
Alex Stewart ea76585068 Adding autogenerated Ceres config.h to #define Ceres compile options.
- Previously we passed all compile options to Ceres via add_definitions
  in CMake.  This was fine for private definitions (used only by Ceres)
  but required additional work for public definitions to ensure they
  were correctly propagated to clients via CMake using
  target_compile_definitions() (>= 2.8.11) or add_definitions().
- A drawback to these approaches is that they did not work for chained
  dependencies on Ceres, as in if in the users project B <- A <- Ceres,
  then although the required Ceres public compile definitions would
  be used when compiling A, they would not be propagated to B.

- This patch replaces the addition of compile definitions via
  add_definitions() with an autogenerated config.h header which
  is installed with Ceres and defines all of the enabled Ceres compile
  options.
- This removes the need for the user to propagate any compile
  definitions in their projects, and additionally allows post-install
  inspect of the options with which Ceres was compiled.

Change-Id: Idbdb6abdad0eb31e7540370e301afe87a07f2260
2014-05-09 10:57:31 +01:00
Björn Piltz 5d7eed87b4 Suppport for MSVC DLLs.
Change-Id: Ibbcc4ba4e59f5bbf1cb91fe81c7d3b9042d03493
2014-04-28 19:56:24 +00:00
Sergey Sharybin ceb7a3beaa Fix compilation error when using G++ compiler
This compiler defines shared_ptr in std::tr1 namespace, but
for this <tr1/memory> is to be included. Further, this compiler
also does have <memory> header which confused previous shared
pointer check.

Simplified logic around defines now, so currently we've got:
- CERES_TR1_MEMORY_HEADER defined if <tr1/memory> is to be
  used for shared_ptr, otherwise <memory> is to be used.
- CERES_TR1_SHARED_PTR defined if shared_ptr is defined in
  std::tr1 namespace, otherwise it's defined in std namespace.

All the shared_ptr checks are now moved to own file FindSharedPtr
which simplifies main CMakeLists.

Change-Id: I558a74793baaa0bd088801910a356be4ef17c31b
2014-04-28 15:37:59 +00:00
Sameer Agarwal 2569076ff0 More NDK fixes.
Fix variable names in port.h and fix fpclassify when
using gnustl. This was tested by switching to gnustl
in the JNI build.

Thanks to Carlos Hernandez for suggesting the gnustl fixes.

Change-Id: I690b73caf495ccc79061f45288e416da1604cc72
2014-04-25 23:55:48 -07:00
Sameer Agarwal e55596f886 Change the defaults for shared_ptr.
By default shared_ptr is now assumed to be
in the standard <memory> header and in the
std namespace.

Previously the way the ifdefs were structured if the appropriate
variable was not defined, it would default to <t1/memory>.

The new defaults are more future proof.

Change-Id: If457806191196be2b6425b8289ea7a3488a27445
2014-04-25 16:17:19 -07:00
Sameer Agarwal bb05be341b Solver::Options uses shared_ptr to handle ownership.
Solver::Options::linear_solver_ordering and
Solver::Options::inner_iteration_ordering
were bare pointers even though Solver::Options took ownership of these
objects.

This lead to buggy user code and the inability to copy Solver::Options
objects around.

With this change, these naked pointers have been replaced by a
shared_ptr object which will managed the lifetime of these objects. This
also leads to simplification of the lifetime handling of these objects
inside the solver.

The Android.mk and Application.mk files have also been updated
to use a newer NDK revision which ships with LLVM's libc++.

Change-Id: I25161fb3ddf737be0b3e5dfd8e7a0039b22548cd
2014-04-25 15:54:39 -07:00
Sameer Agarwal af04d7f187 Remove DCHECK_GE checks from fixed_array.h
This triggers -Wtype-limits warnings on comparisons
which are always true, since the test being done is
n >= 0, where n is of type size_t, which is always
true.

This causes problems when compiling Ceres on linux
with miniglog.

Change-Id: Ia1d1d1483e03469c71fde029b62ca6d84e9b27e0
2013-11-05 13:47:30 -08:00
Sameer Agarwal e5ce1170bc Minor bug fix to autodiff.h
Change-Id: Ib41050a2f2ba1898c71ff19d74f8eca2496212c0
2013-10-29 07:40:51 -07:00
Sameer Agarwal 3a2158d728 NumericDiffCostFunction supports dynamic number of residuals.
1. Update AutoDiffCostFunction template parameters to be consistent
with NumericDiffCostFunction.

2. Update the documentation for NumericDiffCostFunction and
AutoDiffCostFunction.

Change-Id: I113038abb5bedebb0f6f326f2a4ac31480d785fc
2013-10-03 20:09:59 +00:00
Sameer Agarwal c9122c3b7b MUST_USE_RESULT -> CERES_MUST_USE_RESULT
Change-Id: I23dfde2e699b37840a192fa082e932f5a96dd4a9
2013-09-23 22:13:24 -07:00
Sameer Agarwal 3c2ad4018c Speed up Automatic differentiation by 7%.
1. Templatize Make1stOrderPerturbation.
2. Convert a hard CHECK into DCHECK.

Change-Id: I02cd67f2b87bc5722f1a090057d55f23e98d2c3b
2013-07-15 08:09:38 -07:00
Sameer Agarwal a1eaa262ea Update glog path
Change-Id: I47a69cb267c71a9f8f3350c4f645e8cf83e44cc9
2013-05-09 10:09:38 -07:00
Taylor Braun-Jones 0a4f5f8f74 Fix operator() signature in several sections of the documentation
Change-Id: I73f9d150a738f7b136fbc1f98fc60b0f306bd7f9
2013-03-06 05:10:57 +00:00
Sameer Agarwal 31730ef55d DenseSparseMatrix is now column-major.
1. Introduce new typdefs in eigen.h to allow for column
   major matrices.

2. Clean up old unused typedefs, and the aligned typedefs
   since they do not actually add any real performance.

3. Made eigen.h conform to the google style guide by removing
   the using directives. They were polluting the ceres namespace.

4. Made the template specialization generator work again.

Change-Id: Ic2268c784534b737ebd6e1a043e2a327adaeca37
2013-03-03 17:08:32 -08:00
Sameer Agarwal 509f68cfe3 Problem::Evaluate implementation.
1. Add Problem::Evaluate and tests.
2. Remove Solver::Summary::initial/final_*
3. Remove Solver::Options::return_* members.
4. Various cpplint cleanups.

Change-Id: I4266de53489896f72d9c6798c5efde6748d68a47
2013-02-24 19:04:21 +00:00