Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 3 | // http://ceres-solver.org/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Author: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #include "ceres/sparse_cholesky.h" |
| 32 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 33 | #include <memory> |
| 34 | #include <utility> |
| 35 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | #include "ceres/accelerate_sparse.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 37 | #include "ceres/eigensparse.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | #include "ceres/float_suitesparse.h" |
| 39 | #include "ceres/iterative_refiner.h" |
| 40 | #include "ceres/suitesparse.h" |
| 41 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 42 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 43 | |
| 44 | std::unique_ptr<SparseCholesky> SparseCholesky::Create( |
| 45 | const LinearSolver::Options& options) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 46 | std::unique_ptr<SparseCholesky> sparse_cholesky; |
| 47 | |
| 48 | switch (options.sparse_linear_algebra_library_type) { |
| 49 | case SUITE_SPARSE: |
| 50 | #ifndef CERES_NO_SUITESPARSE |
| 51 | if (options.use_mixed_precision_solves) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 52 | sparse_cholesky = |
| 53 | FloatSuiteSparseCholesky::Create(options.ordering_type); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 54 | } else { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 55 | sparse_cholesky = SuiteSparseCholesky::Create(options.ordering_type); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 56 | } |
| 57 | break; |
| 58 | #else |
| 59 | LOG(FATAL) << "Ceres was compiled without support for SuiteSparse."; |
| 60 | #endif |
| 61 | |
| 62 | case EIGEN_SPARSE: |
| 63 | #ifdef CERES_USE_EIGEN_SPARSE |
| 64 | if (options.use_mixed_precision_solves) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 65 | sparse_cholesky = |
| 66 | FloatEigenSparseCholesky::Create(options.ordering_type); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 67 | } else { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 68 | sparse_cholesky = EigenSparseCholesky::Create(options.ordering_type); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 69 | } |
| 70 | break; |
| 71 | #else |
| 72 | LOG(FATAL) << "Ceres was compiled without support for " |
| 73 | << "Eigen's sparse Cholesky factorization routines."; |
| 74 | #endif |
| 75 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 76 | case ACCELERATE_SPARSE: |
| 77 | #ifndef CERES_NO_ACCELERATE_SPARSE |
| 78 | if (options.use_mixed_precision_solves) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 79 | sparse_cholesky = |
| 80 | AppleAccelerateCholesky<float>::Create(options.ordering_type); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | } else { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 82 | sparse_cholesky = |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 83 | AppleAccelerateCholesky<double>::Create(options.ordering_type); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 84 | } |
| 85 | break; |
| 86 | #else |
| 87 | LOG(FATAL) << "Ceres was compiled without support for Apple's Accelerate " |
| 88 | << "framework solvers."; |
| 89 | #endif |
| 90 | |
| 91 | default: |
| 92 | LOG(FATAL) << "Unknown sparse linear algebra library type : " |
| 93 | << SparseLinearAlgebraLibraryTypeToString( |
| 94 | options.sparse_linear_algebra_library_type); |
| 95 | } |
| 96 | |
| 97 | if (options.max_num_refinement_iterations > 0) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 98 | auto refiner = std::make_unique<SparseIterativeRefiner>( |
| 99 | options.max_num_refinement_iterations); |
| 100 | sparse_cholesky = std::make_unique<RefinedSparseCholesky>( |
| 101 | std::move(sparse_cholesky), std::move(refiner)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 102 | } |
| 103 | return sparse_cholesky; |
| 104 | } |
| 105 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 106 | SparseCholesky::~SparseCholesky() = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 107 | |
| 108 | LinearSolverTerminationType SparseCholesky::FactorAndSolve( |
| 109 | CompressedRowSparseMatrix* lhs, |
| 110 | const double* rhs, |
| 111 | double* solution, |
| 112 | std::string* message) { |
| 113 | LinearSolverTerminationType termination_type = Factorize(lhs, message); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 114 | if (termination_type == LinearSolverTerminationType::SUCCESS) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 115 | termination_type = Solve(rhs, solution, message); |
| 116 | } |
| 117 | return termination_type; |
| 118 | } |
| 119 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 120 | RefinedSparseCholesky::RefinedSparseCholesky( |
| 121 | std::unique_ptr<SparseCholesky> sparse_cholesky, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 122 | std::unique_ptr<SparseIterativeRefiner> iterative_refiner) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 123 | : sparse_cholesky_(std::move(sparse_cholesky)), |
| 124 | iterative_refiner_(std::move(iterative_refiner)) {} |
| 125 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 126 | RefinedSparseCholesky::~RefinedSparseCholesky() = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 127 | |
| 128 | CompressedRowSparseMatrix::StorageType RefinedSparseCholesky::StorageType() |
| 129 | const { |
| 130 | return sparse_cholesky_->StorageType(); |
| 131 | } |
| 132 | |
| 133 | LinearSolverTerminationType RefinedSparseCholesky::Factorize( |
| 134 | CompressedRowSparseMatrix* lhs, std::string* message) { |
| 135 | lhs_ = lhs; |
| 136 | return sparse_cholesky_->Factorize(lhs, message); |
| 137 | } |
| 138 | |
| 139 | LinearSolverTerminationType RefinedSparseCholesky::Solve(const double* rhs, |
| 140 | double* solution, |
| 141 | std::string* message) { |
| 142 | CHECK(lhs_ != nullptr); |
| 143 | auto termination_type = sparse_cholesky_->Solve(rhs, solution, message); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 144 | if (termination_type != LinearSolverTerminationType::SUCCESS) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 145 | return termination_type; |
| 146 | } |
| 147 | |
| 148 | iterative_refiner_->Refine(*lhs_, rhs, sparse_cholesky_.get(), solution); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 149 | return LinearSolverTerminationType::SUCCESS; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 152 | } // namespace ceres::internal |