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/subset_preconditioner.h" |
| 32 | |
| 33 | #include <memory> |
| 34 | #include <string> |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 35 | #include <utility> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 37 | #include "ceres/compressed_row_sparse_matrix.h" |
| 38 | #include "ceres/inner_product_computer.h" |
| 39 | #include "ceres/linear_solver.h" |
| 40 | #include "ceres/sparse_cholesky.h" |
| 41 | #include "ceres/types.h" |
| 42 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 43 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 44 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 45 | SubsetPreconditioner::SubsetPreconditioner(Preconditioner::Options options, |
| 46 | const BlockSparseMatrix& A) |
| 47 | : options_(std::move(options)), num_cols_(A.num_cols()) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 48 | CHECK_GE(options_.subset_preconditioner_start_row_block, 0) |
| 49 | << "Congratulations, you found a bug in Ceres. Please report it."; |
| 50 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 51 | LinearSolver::Options sparse_cholesky_options; |
| 52 | sparse_cholesky_options.sparse_linear_algebra_library_type = |
| 53 | options_.sparse_linear_algebra_library_type; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 54 | sparse_cholesky_options.ordering_type = options_.ordering_type; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 55 | sparse_cholesky_ = SparseCholesky::Create(sparse_cholesky_options); |
| 56 | } |
| 57 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 58 | SubsetPreconditioner::~SubsetPreconditioner() = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 59 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 60 | void SubsetPreconditioner::RightMultiplyAndAccumulate(const double* x, |
| 61 | double* y) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 62 | CHECK(x != nullptr); |
| 63 | CHECK(y != nullptr); |
| 64 | std::string message; |
| 65 | sparse_cholesky_->Solve(x, y, &message); |
| 66 | } |
| 67 | |
| 68 | bool SubsetPreconditioner::UpdateImpl(const BlockSparseMatrix& A, |
| 69 | const double* D) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 70 | auto* m = const_cast<BlockSparseMatrix*>(&A); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 71 | const CompressedRowBlockStructure* bs = m->block_structure(); |
| 72 | |
| 73 | // A = [P] |
| 74 | // [Q] |
| 75 | |
| 76 | // Now add D to A if needed. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 77 | if (D != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 78 | // A = [P] |
| 79 | // [Q] |
| 80 | // [D] |
| 81 | std::unique_ptr<BlockSparseMatrix> regularizer( |
| 82 | BlockSparseMatrix::CreateDiagonalMatrix(D, bs->cols)); |
| 83 | m->AppendRows(*regularizer); |
| 84 | } |
| 85 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 86 | if (inner_product_computer_ == nullptr) { |
| 87 | inner_product_computer_ = InnerProductComputer::Create( |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 88 | *m, |
| 89 | options_.subset_preconditioner_start_row_block, |
| 90 | bs->rows.size(), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 91 | sparse_cholesky_->StorageType()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Compute inner_product = [Q'*Q + D'*D] |
| 95 | inner_product_computer_->Compute(); |
| 96 | |
| 97 | // Unappend D if needed. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 98 | if (D != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 99 | // A = [P] |
| 100 | // [Q] |
| 101 | m->DeleteRowBlocks(bs->cols.size()); |
| 102 | } |
| 103 | |
| 104 | std::string message; |
| 105 | // Compute L. s.t., LL' = Q'*Q + D'*D |
| 106 | const LinearSolverTerminationType termination_type = |
| 107 | sparse_cholesky_->Factorize(inner_product_computer_->mutable_result(), |
| 108 | &message); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 109 | if (termination_type != LinearSolverTerminationType::SUCCESS) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 110 | LOG(ERROR) << "Preconditioner factorization failed: " << message; |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 117 | } // namespace ceres::internal |