123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- #ifndef CERES_INTERNAL_LINEAR_SOLVER_H_
- #define CERES_INTERNAL_LINEAR_SOLVER_H_
- #include <cstddef>
- #include <map>
- #include <memory>
- #include <string>
- #include <vector>
- #include "ceres/block_sparse_matrix.h"
- #include "ceres/casts.h"
- #include "ceres/compressed_row_sparse_matrix.h"
- #include "ceres/context_impl.h"
- #include "ceres/dense_sparse_matrix.h"
- #include "ceres/execution_summary.h"
- #include "ceres/internal/disable_warnings.h"
- #include "ceres/internal/export.h"
- #include "ceres/triplet_sparse_matrix.h"
- #include "ceres/types.h"
- #include "glog/logging.h"
- namespace ceres::internal {
- enum class LinearSolverTerminationType {
-
- SUCCESS,
-
-
- NO_CONVERGENCE,
-
-
- FAILURE,
-
-
-
-
- FATAL_ERROR
- };
- inline std::ostream& operator<<(std::ostream& s,
- LinearSolverTerminationType type) {
- switch (type) {
- case LinearSolverTerminationType::SUCCESS:
- s << "LINEAR_SOLVER_SUCCESS";
- break;
- case LinearSolverTerminationType::NO_CONVERGENCE:
- s << "LINEAR_SOLVER_NO_CONVERGENCE";
- break;
- case LinearSolverTerminationType::FAILURE:
- s << "LINEAR_SOLVER_FAILURE";
- break;
- case LinearSolverTerminationType::FATAL_ERROR:
- s << "LINEAR_SOLVER_FATAL_ERROR";
- break;
- default:
- s << "UNKNOWN LinearSolverTerminationType";
- }
- return s;
- }
- enum class OrderingType {
- NATURAL,
-
-
- AMD,
-
- NESDIS,
- };
- inline std::ostream& operator<<(std::ostream& s, OrderingType type) {
- switch (type) {
- case OrderingType::NATURAL:
- s << "NATURAL";
- break;
- case OrderingType::AMD:
- s << "AMD";
- break;
- case OrderingType::NESDIS:
- s << "NESDIS";
- break;
- default:
- s << "UNKNOWN OrderingType";
- }
- return s;
- }
- class LinearOperator;
- class CERES_NO_EXPORT LinearSolver {
- public:
- struct Options {
- LinearSolverType type = SPARSE_NORMAL_CHOLESKY;
- PreconditionerType preconditioner_type = JACOBI;
- VisibilityClusteringType visibility_clustering_type = CANONICAL_VIEWS;
- DenseLinearAlgebraLibraryType dense_linear_algebra_library_type = EIGEN;
- SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type =
- SUITE_SPARSE;
- OrderingType ordering_type = OrderingType::NATURAL;
-
- bool dynamic_sparsity = false;
- bool use_explicit_schur_complement = false;
-
-
- int min_num_iterations = 1;
- int max_num_iterations = 1;
-
-
-
-
- int max_num_spse_iterations = 5;
-
-
-
- bool use_spse_initialization = false;
-
-
-
-
- double spse_tolerance = 0.1;
-
- int num_threads = 1;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- std::vector<int> elimination_groups;
-
-
-
-
-
- int residual_reset_period = 10;
-
-
-
-
-
-
-
-
-
- int row_block_size = Eigen::Dynamic;
- int e_block_size = Eigen::Dynamic;
- int f_block_size = Eigen::Dynamic;
- bool use_mixed_precision_solves = false;
- int max_num_refinement_iterations = 0;
- int subset_preconditioner_start_row_block = -1;
- ContextImpl* context = nullptr;
- };
-
- struct PerSolveOptions {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- double* D = nullptr;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- LinearOperator* preconditioner = nullptr;
-
-
-
-
-
-
-
-
- double r_tolerance = 0.0;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- double q_tolerance = 0.0;
- };
-
-
-
- struct Summary {
- double residual_norm = -1.0;
- int num_iterations = -1;
- LinearSolverTerminationType termination_type =
- LinearSolverTerminationType::FAILURE;
- std::string message;
- };
-
-
-
-
-
- static LinearSolverType LinearSolverForZeroEBlocks(
- LinearSolverType linear_solver_type);
- virtual ~LinearSolver();
-
- virtual Summary Solve(LinearOperator* A,
- const double* b,
- const PerSolveOptions& per_solve_options,
- double* x) = 0;
-
-
-
-
- virtual std::map<std::string, CallStatistics> Statistics() const {
- return {};
- }
-
- static std::unique_ptr<LinearSolver> Create(const Options& options);
- };
- template <typename MatrixType>
- class TypedLinearSolver : public LinearSolver {
- public:
- LinearSolver::Summary Solve(
- LinearOperator* A,
- const double* b,
- const LinearSolver::PerSolveOptions& per_solve_options,
- double* x) override {
- ScopedExecutionTimer total_time("LinearSolver::Solve", &execution_summary_);
- CHECK(A != nullptr);
- CHECK(b != nullptr);
- CHECK(x != nullptr);
- return SolveImpl(down_cast<MatrixType*>(A), b, per_solve_options, x);
- }
- std::map<std::string, CallStatistics> Statistics() const override {
- return execution_summary_.statistics();
- }
- private:
- virtual LinearSolver::Summary SolveImpl(
- MatrixType* A,
- const double* b,
- const LinearSolver::PerSolveOptions& per_solve_options,
- double* x) = 0;
- ExecutionSummary execution_summary_;
- };
- using BlockSparseMatrixSolver = TypedLinearSolver<BlockSparseMatrix>;
- using CompressedRowSparseMatrixSolver = TypedLinearSolver<CompressedRowSparseMatrix>;
- using DenseSparseMatrixSolver = TypedLinearSolver<DenseSparseMatrix>;
- using TripletSparseMatrixSolver = TypedLinearSolver<TripletSparseMatrix>;
- }
- #include "ceres/internal/reenable_warnings.h"
- #endif
|