Commit Graph

1192 Commits

Author SHA1 Message Date
Sameer Agarwal f802a09ff1 &foo[0] -> foo.data()
Use the more modern form of accessing the data array of a vector
rather than grabbing the pointer to the first element. The latter
can lead to errors if the vector is of zero length.

Change-Id: Ifc8fc969b06b3ba1a9385e8a3a8d5c50b25db5a8
2022-08-25 10:00:12 +05:30
Joydeep Biswas 344929647a Add CUDA GPU and Runtime Detection
* Added GPU device and CUDA compute capability identification.
* Added GpuMemoryAvailable() to aid downstream optimizations based
  on GPU memory availability.

Change-Id: I326dc1e4b7a6a7f5571b7e5479eb9aa300ad1075
2022-08-18 17:07:32 -05:00
Sameer Agarwal 6085e45be7 Minor CUDA cleanup.
1. ceres_cuda_kernels.cu -> cuda_kernels.cu.cc
2. Add missing ifdef guards.
3. Fix an errant namespace

Change-Id: I81a5bfbe3c795ff0b3ef41c3bcaa037b99d5b254
2022-08-17 16:27:13 -07:00
Sameer Agarwal 058a72782a One more ClangTidy fix.
Change-Id: Ic350c3ec1c8cf6a5e893b37713e93433640af184
2022-08-16 15:53:32 -07:00
Sameer Agarwal f6f2f0d161 ClangTidy cleanups
Also some clang-format cleanups.

Change-Id: Ifb728f12ec130a5af33db789086f8a9baa078678
2022-08-16 15:46:43 -07:00
Sameer Agarwal 9d74c69130 Fix some more errant CATD warnings
Change-Id: I3adc9076dfcfcb5660d0413a897cf565bea27f95
2022-08-16 15:10:54 -07:00
Sameer Agarwal a4f7440951 Remove an unused variable from compressed_row_sparse_matrix.cc
Change-Id: I7786417f99d87382beca7b3d65ef61b7cb5ea3d9
2022-08-16 15:06:17 -07:00
Sergiu Deitsch 388c142866 Fix GCC 12.1.1 LTO -Walloc-size-larger-than= warnings
With -flto=auto, GCC emits multiple warnings in release builds such as

In function ‘make_unique’,
    inlined from ‘Create’ at ceres-solver/internal/ceres/scratch_evaluate_preparer.cc:43:75,
    inlined from ‘CreateEvaluatePreparers’ at ceres-solver/internal/ceres/compressed_row_jacobian_writer.h:95:66,
    inlined from ‘__ct ’ at ceres-solver/internal/ceres/program_evaluator.h:120:9,
    inlined from ‘Evaluate.constprop.isra’ at ceres-solver/internal/ceres/problem_impl.cc:695:65:
/usr/include/c++/12.1.1/bits/unique_ptr.h:1080:30: warning: argument 1 value ‘18446744073709551615’ exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
 1080 |     { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
      |                              ^

because a signed integer is used to specify the size of allocated arrays
instead of the expected unsigned (specifically, std::size_t) without
checking for negative values at the call site.

Change-Id: I923b1d074241535426bfea041568ef1dc7f3ec86
2022-08-16 21:23:12 +00:00
Sameer Agarwal 9ec4f7e44a Refactor BlockJacobiPreconditioner
1. Rename BlockJacobiPreconditionet to BlockSparseJacobiPreconditioner.
2. Add CompressedRowSparseJacobiPreconditioner which is a
   block Jacobi preconditioner for CompressedRowSparseMatrix objects.
3. Re-write the tests to be more comprehensive.

Change-Id: Icbc91f9ad2cefaad593c11397f8cdcf805d7e118
2022-08-16 13:47:16 -07:00
Joydeep Biswas adda97acd7 Fixed a few more missing CERES_NO_CUDA guards
Change-Id: I65c1851085f816794079ef56de0426fcdd055c87
2022-08-16 05:17:40 +00:00
Sameer Agarwal 22aeb35843 Fix a missing string assignment in solver.cc
Change-Id: I3dfc28603c6af10f361ca4877f518a5f28edfdbd
2022-08-15 22:16:43 -07:00
Joydeep Biswas 3b891f7672 Insert missing CUDA guards.
Change-Id: If97c51557376385e3958863072cfcf76dd810d15
2022-08-15 23:55:33 -05:00
Joydeep Biswas 829089053e CUDA CGNR, Part 4: CudaCgnrSolver
* Added CudaCgnrSolver, a new CUDA-accelerated CGNR.
* To use CudaCgnrSolver, the user must select CGNR as the linear_solver
  and CUDA_SPARSE as the sparse_linear_algebra_library.
* Updated ConjugateGradientSolver to work with an array of pointers to
  scratch to support CudaVectors as scratch.
* Moved CUDA initialization to run in Solver::Solve as needed.

Some performance comparisons on an Ubuntu 20.04 desktop with an
Intel i9-9940X CPU @ 3.30GHz, and an nVidia Quadro RTX 6000,
all configurations run with 24 threads, and 10 iterations.

=================================================
CGNR + CUDA_SPARSE + IDENTITY Preconditioner
problem-1778-993923-pre.txt
=================================================
Cost:
Initial                          2.563973e+08
Final                            1.724755e+06
Change                           2.546725e+08

Minimizer iterations                       11
Successful steps                            7
Unsuccessful steps                          4

Time (in seconds):
Preprocessor                         4.020158

  Residual only evaluation           1.567092 (10)
  Jacobian & residual evaluation     7.847130 (7)
  Linear solver                     31.688898 (10)
Minimizer                           46.834987

Postprocessor                        0.353974
Total                               51.209120

=================================================
SPARSE_SCHUR (CPU) + SUITE_SPARSE + AMD
problem-1778-993923-pre.txt
=================================================
Cost:
Initial                          2.563973e+08
Final                            1.651617e+06
Change                           2.547457e+08

Minimizer iterations                       11
Successful steps                           11
Unsuccessful steps                          0

Time (in seconds):
Preprocessor                        35.812003

  Residual only evaluation           1.658980 (10)
  Jacobian & residual evaluation    12.218799 (11)
  Linear solver                     76.409992 (10)
Minimizer                           98.809773

Postprocessor                        0.372712
Total                              134.994489

=================================================
ITERATIVE_SCHUR (CPU) + JACOBI Preconditioner
problem-1778-993923-pre.txt
=================================================
Cost:
Initial                          2.563973e+08
Final                            1.684447e+06
Change                           2.547128e+08

Minimizer iterations                       11
Successful steps                            8
Unsuccessful steps                          3

Time (in seconds):
Preprocessor                        15.331614

  Residual only evaluation           1.606114 (10)
  Jacobian & residual evaluation     8.502166 (8)
  Linear solver                    351.910080 (10)
Minimizer                          368.797327

Postprocessor                        0.363536
Total                              384.492478

=================================================
CGNR + CUDA_SPARSE + IDENTITY Preconditioner
problem-13682-4456117-pre.txt
=================================================
Cost:
Initial                          1.126372e+09
Final                            2.269329e+07
Change                           1.103678e+09

Minimizer iterations                       11
Successful steps                            7
Unsuccessful steps                          4

Time (in seconds):
Preprocessor                        19.140087

  Residual only evaluation           8.721920 (10)
  Jacobian & residual evaluation    41.955923 (7)
  Linear solver                    214.121861 (10)
Minimizer                          296.636890

Postprocessor                        1.971827
Total                              317.748804

Change-Id: I3a09f31aa6903f661e91f595afd39d427583e856
2022-08-15 23:44:34 -05:00
Sameer Agarwal 6ab435d774 Fix a missing CERES_NO_CUDA guard
Also run format_all.sh.

Change-Id: I13902c1d3eb0d3a97548540fee13ec67c490a5ff
2022-08-14 16:58:09 -07:00
Joydeep Biswas c560bc2be5 CUDA CGNR, Part 3: CudaSparseMatrix
* Added CudaSparseMatrix to manage and operate on sparse matrices with
  cuSparse.
* Added tests for CudaSparseMatrix.
* Added a new sparse linear operator benchmark.

Change-Id: Id09df46de3b40be1f14441528088b54dab5844af
2022-08-14 18:48:55 -05:00
Joydeep Biswas c914c7a2b3 CUDA CGNR, Part 2: CudaVector
* Added CudaVector to manage and operate on vectors with CUDA.
* Added tests for CudaVector

Change-Id: I528258c8e883011e8709bddf59edb1e933af8060
2022-08-14 12:25:45 -05:00
Joydeep Biswas 3af3dee189 Simplify the implementation to convert from BlockSparseMatrix to
CompressedRowSparseMatrix.

Since the conversion from BlockSparseMatrix to CompressedRowSparseMatrix
is not used in any performance-critical context, this CL simplifies it
by re-using existing conversions.

Change-Id: I51263bc95cc056efb31961ccda548cd7be35b2a4
2022-08-14 10:42:19 -05:00
Sergiu Deitsch 242fc07959 Remove unnecessary destructors
Change-Id: I1d1cc9b15606955ee1d99d2f81585f118bc92aef
2022-08-13 20:34:52 +00:00
Sergiu Deitsch 9c968d4066 Silence Clang warning
Clang emits the following warning:

internal/ceres/schur_complement_solver.cc:71:51: warning: class with destructor marked 'final' cannot be inherited from [-Wfinal-dtor-non-final-class]
  virtual ~BlockRandomAccessSparseMatrixAdapter() final {}

Change-Id: I9da96711fd1e924ce8504988ff171cb476b6222f
2022-08-13 21:32:11 +02:00
Sameer Agarwal 2c78c5f339 Small naming fixups.
use_power_series_expansion_initialization -> use_spse_initialization
max_linear_solve_iterations -> max_linear_solver_iterations

Change-Id: I7775fa9b1ad12e28c8d01d44349b7eaef9b57edd
2022-08-13 11:59:16 -07:00
Sergiu Deitsch 4642e4b0c1 Use if constexpr and map SparseMatrix as const
Change-Id: Id72d77ef91d2bfc1055ab67b604f26ebc0d65769
2022-08-13 20:20:53 +02:00
Mark Shachkov 92d8379532 Enable usage of schur power series expansion preconditioner.
Add an option to use schur power series expansion for initialization
of pcg solution in ITERATIVE_SCHUR linear solver.

Change-Id: Ifb8bce02bc5f5ceebc74f961eefd3f6dd2ffab4a
2022-08-13 21:05:11 +03:00
Sergiu Deitsch f1dfac8cd6 Reduce the number of individual PRNG instances
Use same instance of a PRNG throughout by passing it to methods and
functions as an argument to generate random numbers without breaking the
sequence.

Change-Id: Ib024bbc1ea2d14e4b9afb71857856a5fb77b1667
2022-08-13 17:14:06 +00:00
Sameer Agarwal 7b0bb0e3f5 ClangTidy cleanups
Change-Id: I514ca5fd91c08866b412021e6c0f5d6f97c4bf8f
2022-08-12 21:49:02 -07:00
Sameer Agarwal c5c2afcc91 Fix solver_test.cc for preconditioners and sparse linear algebra libraries
Change-Id: Ia391680f872ab49101f672f676cdd68fe332d1d4
2022-08-12 21:35:15 -07:00
Sameer Agarwal 07d333fb68 Refactor options checking for linear solvers
The code that verifies that the linear solver is configuration
specified by the user has grown into a rat's nest. This CL
attempts to bring some order to this madness.

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

Change-Id: I3f34c0e27da13a6412117dee43ef2d9ec3835b64
2022-08-12 21:14:08 -07:00
Sameer Agarwal ba7207b0ba A number of small changes.
These changes came about from testing the power bundle adjustment
integration CL.

1. Allow Solver::Options::max_linear_solver_iterations == 0.
2. Simplify the logic for when inverse(F'F) is computed.
3. norm_b -> norm_rhs in ConjugateGradientsSolver.

Change-Id: I50c19e1f24a4cc08ed60e3a3032b96b37bcada9f
2022-08-12 06:52:37 -07:00
Mark Shachkov 20e85bbe34 Add power series expansion preconditioner
Implementation of "Power Bundle Adjustment for Large-Scale 3D
Reconstruction" by Weber et. al. added in the form of preconditioner.

Change-Id: Ie85526a5fc46f74256f6dfe9173c3571f7160f3a
2022-08-11 19:34:41 +03:00
Sameer Agarwal 04899645cc LinearOperator::FooMultiply -> LinearOperator::FooMultiplyAndAccumulate
These methods were historically poorly named and every time I read code
I get confused whether they are just multiplying or multiplying and
adding. Clarifying them also gives us the changce to introduce
RightMultiply and LeftMultiply methods in the base class which will
simplify a number call sites in a subsequent CL.

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

Change-Id: Ice4fb483f1acd02527a6dd753ef0c5a66037f4b0
2022-08-10 10:03:03 -07:00
Sameer Agarwal 288a3fde6b Add missing virtual destructors to matrix adapters
Change-Id: Ieb0e67fbc9042410a6ae63e7cf0f96cc90603829
2022-08-09 22:16:38 -07:00
Sameer Agarwal 1da72ac39e Refactor ConjugateGradientsSolver
1. Convert it from a class to a template function. Where the
   template parameter is "DenseVectorType". This allows us
   to have a single implementation of Conjugate Gradients
   without worrying about where the matrix and the vectors
   are stored or what their internal representation is.

   For the case of CPU based vectors, we abstract operations
   on Eigen vectors using eigen_vector_ops.
2. Introduce ConjugateGradientsLinearOperator which is
   templated on DenseVectorType. It is the matrix vector
   multiplication abstraction.
3. Port the tests and all usages of ConjugateGradientsSolver
   to this new implementation.
4. Introduce Eigen::Vector based RightMultiply and LeftMultiply
   methods into LinearOperator which by default delete to the
   bare pointer based interfaces.
5. Add an identity preconditioner.

These changes are being made in preparation for adding a CUDA
based CGNR solver.

Change-Id: I9da36dc6c131856dd1a4aa7e645aaf12d25dd79b
2022-08-09 13:33:08 +00:00
Julio L. Paneque f62dccdb37 Fix the Sphere and Line Manifold formulations
This PR changes the Sphere and Line Manifold formulations so that their
tangent spaces represent traveled angles (for the sphere and the line
direction vector) and traveled distance (for the line origin). These
magnitudes were previously halved according to "Hartley & Zisserman
(2nd Edition)", but in the majority of the state of the art this is not
done, following the convention that magnitudes in the tangent space of
the unit sphere represent geodesic distances traveled on that manifold.
The same scale factor appears in the Quaternion Manifold implementation
and will be studied in a further PR.

This PR also adds an additional case in the Sphere Minus operator when
hy_norm == 0. The value of y_minus_x was fixed to 0 but actually its
last term can also be Pi depending on y_last.

Finally, new unit tests for the Plus and Minus operator are added, along
with new tests for the 2D Sphere (a.k.a. Circle) Manifold.

Change-Id: I9456f1675b20da49bede5d6759aabf3cdfb26eae
2022-08-09 09:36:18 +02:00
Sameer Agarwal 3e1cc89f63 A bunch of clang-tidy fixes.
Change-Id: I1442d69eb9be91a8c368187848d65196273e3de6
2022-08-08 21:13:30 -07:00
Sameer Agarwal 80380538ab One more CATD fix
Change-Id: I1307067c070328b5733e55909783e048021c9aca
2022-08-08 20:27:14 -07:00
Sameer Agarwal 560ef46fbd A bunch of minor fixes.
1. Remove an unused variable from block_sparse_matrix.cc
2. Add explicit types to the distributions to get around
   -Wctad-maybe-unsupported

Change-Id: Ib7d606fbfe2b93ba4fce408f38ee4f7626b74ff0
2022-08-08 20:16:51 -07:00
Joydeep Biswas 67bae28c12 CUDA CGNR, Part 1: Misc. CLeanup
* Fixed an alignment bug with EventLogger.
* Added expected solutions to some LinearLeastSquaresProblem tests.
* Expanded ContextImpl to initialize cuSparse.

Change-Id: I2d7556a0509e9b7c56c9cd5cfa75cb85614395dc
2022-08-08 21:48:07 -05:00
Sameer Agarwal 5d0bca14dd Remove ceres/internal/random.h in favor of <random>
Fixes https://github.com/ceres-solver/ceres-solver/issues/854

Change-Id: Id30b8dc2221f9afe4eb83f3a9304b9b2bc7e05d4
2022-08-08 07:06:21 -07:00
Mark Shachkov d881b5ccf3 Minor fixes in comments
Add Clion project directories to gitignore.

Change-Id: Ic804059bf219a0b96cf96d717936fb5fed95a4b4
2022-08-08 16:15:46 +03:00
Sameer Agarwal 37516c968c Fix a bug in InnerProductComputer.
InnerProductComputer was assuming that the input matrix had
at least one structurally non-zero block. As a result sometimes
when InnerProductComputer.SubMatrix test generated matrices
where the submatrix was structurally zero it would cause
a segmentation fault.

This changes fixes this issue and reduces the threshold for the
minimum block density so that there is a much higher chance of
such matrices occuring as part of testing.

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

Change-Id: Iec3a2431e646f0c2aac9e0b994531fa72323f329
2022-08-07 12:56:42 -07:00
Joydeep Biswas d8dad14eed CUDA Cleanup
* Renamed several interfaces to CudaBuffer for clarity and consistency.
* Added unit tests for custom Cuda kernels.
* Set specific CUDA architectures if the CMake version supports it.

Change-Id: I269fb1089b80b25e17bca772ef8d70e7894214b8
2022-08-07 07:44:32 +00:00
Joydeep Biswas c9d2ec8a9f Updates to sparse block matrix structures to support new
sparse linear solvers.

* Add methods to convert TripletSparseMatrix and BlockSparseMatrix to
  CRSMatrix structure.
* Added tests for conversion of TripletSparseMatrix and BlockSparseMatrix
  to CRSMatrix structure.
* Added documentation on the BlockSparseMatrix structure.

Change-Id: I020cfa91c301567ceeb39ff2064183c5d88c9ed5
2022-08-06 22:03:37 -05:00
Sergiu Deitsch 5fe0bd45a9 Added MinGW to Windows Github workflow
Change-Id: Id2bcd92a5464ac4888295c3dbfe9b806be95a3d9
2022-08-06 23:56:51 +02:00
Sameer Agarwal 738c027c1f Fix a logic error in iterative_refiner_test
Change-Id: I741802db37d6d9e42e38cef358e00369f6a38d06
2022-08-06 08:29:08 -07:00
Sameer Agarwal cb6ad463d0 Add mixed precision support for CPU based DenseCholesky
On problem-744-543562-pre.txt

The time spent in linear solver on my M1 Pro is

eigen        81.550970
eigen+mixed  54.107383
LAPACK       47.078127
LAPACK+mixed 28.639868

Solution quality is unaffected.

The implementation of RefinedDenseCholesky and DenseIterativeRefiner
are straightforward ports of RefinedSparseCholesky and
SparseIterativeRefiner (formerly IterativeRefiner).

It maybe possible to refactor the SparseCholesky and DenseCholesky
interfaces so that this code duplication can be removed in the
future.

Change-Id: I921334224cb97629a60390f2add822de207f7923
2022-08-05 15:30:12 -07:00
Sameer Agarwal 68c53bb395 Remove ceres::LocalParameterization
Change-Id: I3bdf2f6a8857db10c984024a27f490eefd23fefa
2022-07-29 22:29:23 +00:00
Joydeep Biswas 2f660464cc Fix build issue with CUDA testing targets when compiling without gflags.
Change-Id: I926a85c30b51802a99161679cb2a28fda6e3ef47
2022-07-20 21:31:47 +05:30
Sameer Agarwal c801192d47 Minor fixes
Change-Id: I4c825bbd19b2d902d17dce37d228e23a808c87fb
2022-07-18 06:43:12 -07:00
Sameer Agarwal ce9e902b86 Fix missing CERES_METIS_VERSION
CERES_METIS_VERSION needs to be set if either Eigen or
SuiteSparse are using it. Previously, we were conditioning it
only on EIGENMETIS being ON.

Change-Id: I380a3b138b79903aa142b560ed46f438ff549a82
2022-07-14 14:21:02 +00:00
Sameer Agarwal d9a3dfbf20 Add a missing ifdef guard to dense_cholesky_test
Change-Id: Ibd924bc52b589ea70c5e7a543e1092697a0e5941
2022-07-14 06:58:49 -07:00
Sameer Agarwal 5bd43a1fa4 Speed up DenseSparseMatrix::SquareColumnNorm.
Because we store the matrix as row major matrix, the
obvious Eigen expression performs rather poorly. A straight
c++ loop speeds things up considerably.

Also replace use of matrix() with direct use of m_.

Change-Id: I3d6166df4765ad8400ab9602a54b65fd21b1d50f
2022-07-14 06:46:49 -07:00