blob: 068f6cec38631e496e8fe9ce0eb3723ce2144d41 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 Schuh3de38b02024-06-25 18:25:10 -070035#include <utility>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036
Austin Schuh70cc9552019-01-21 19:46:48 -080037#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 Schuh3de38b02024-06-25 18:25:10 -070043namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080044
Austin Schuh3de38b02024-06-25 18:25:10 -070045SubsetPreconditioner::SubsetPreconditioner(Preconditioner::Options options,
46 const BlockSparseMatrix& A)
47 : options_(std::move(options)), num_cols_(A.num_cols()) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080048 CHECK_GE(options_.subset_preconditioner_start_row_block, 0)
49 << "Congratulations, you found a bug in Ceres. Please report it.";
50
Austin Schuh70cc9552019-01-21 19:46:48 -080051 LinearSolver::Options sparse_cholesky_options;
52 sparse_cholesky_options.sparse_linear_algebra_library_type =
53 options_.sparse_linear_algebra_library_type;
Austin Schuh3de38b02024-06-25 18:25:10 -070054 sparse_cholesky_options.ordering_type = options_.ordering_type;
Austin Schuh70cc9552019-01-21 19:46:48 -080055 sparse_cholesky_ = SparseCholesky::Create(sparse_cholesky_options);
56}
57
Austin Schuh3de38b02024-06-25 18:25:10 -070058SubsetPreconditioner::~SubsetPreconditioner() = default;
Austin Schuh70cc9552019-01-21 19:46:48 -080059
Austin Schuh3de38b02024-06-25 18:25:10 -070060void SubsetPreconditioner::RightMultiplyAndAccumulate(const double* x,
61 double* y) const {
Austin Schuh70cc9552019-01-21 19:46:48 -080062 CHECK(x != nullptr);
63 CHECK(y != nullptr);
64 std::string message;
65 sparse_cholesky_->Solve(x, y, &message);
66}
67
68bool SubsetPreconditioner::UpdateImpl(const BlockSparseMatrix& A,
69 const double* D) {
Austin Schuh3de38b02024-06-25 18:25:10 -070070 auto* m = const_cast<BlockSparseMatrix*>(&A);
Austin Schuh70cc9552019-01-21 19:46:48 -080071 const CompressedRowBlockStructure* bs = m->block_structure();
72
73 // A = [P]
74 // [Q]
75
76 // Now add D to A if needed.
Austin Schuh3de38b02024-06-25 18:25:10 -070077 if (D != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -080078 // 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 Schuh3de38b02024-06-25 18:25:10 -070086 if (inner_product_computer_ == nullptr) {
87 inner_product_computer_ = InnerProductComputer::Create(
Austin Schuh70cc9552019-01-21 19:46:48 -080088 *m,
89 options_.subset_preconditioner_start_row_block,
90 bs->rows.size(),
Austin Schuh3de38b02024-06-25 18:25:10 -070091 sparse_cholesky_->StorageType());
Austin Schuh70cc9552019-01-21 19:46:48 -080092 }
93
94 // Compute inner_product = [Q'*Q + D'*D]
95 inner_product_computer_->Compute();
96
97 // Unappend D if needed.
Austin Schuh3de38b02024-06-25 18:25:10 -070098 if (D != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -080099 // 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 Schuh3de38b02024-06-25 18:25:10 -0700109 if (termination_type != LinearSolverTerminationType::SUCCESS) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800110 LOG(ERROR) << "Preconditioner factorization failed: " << message;
111 return false;
112 }
113
114 return true;
115}
116
Austin Schuh3de38b02024-06-25 18:25:10 -0700117} // namespace ceres::internal