blob: 779a34ae741ad2502d488f3cd096257af72bd95e [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2017 Google Inc. All rights reserved.
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 Schuh1d1e6ea2020-12-23 21:56:30 -080035
Austin Schuh70cc9552019-01-21 19:46:48 -080036#include "ceres/compressed_row_sparse_matrix.h"
37#include "ceres/inner_product_computer.h"
38#include "ceres/linear_solver.h"
39#include "ceres/sparse_cholesky.h"
40#include "ceres/types.h"
41
42namespace ceres {
43namespace internal {
44
45SubsetPreconditioner::SubsetPreconditioner(
46 const Preconditioner::Options& options, const BlockSparseMatrix& A)
47 : options_(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 Schuh1d1e6ea2020-12-23 21:56:30 -080054 sparse_cholesky_options.use_postordering = options_.use_postordering;
Austin Schuh70cc9552019-01-21 19:46:48 -080055 sparse_cholesky_ = SparseCholesky::Create(sparse_cholesky_options);
56}
57
58SubsetPreconditioner::~SubsetPreconditioner() {}
59
60void SubsetPreconditioner::RightMultiply(const double* x, double* y) const {
61 CHECK(x != nullptr);
62 CHECK(y != nullptr);
63 std::string message;
64 sparse_cholesky_->Solve(x, y, &message);
65}
66
67bool SubsetPreconditioner::UpdateImpl(const BlockSparseMatrix& A,
68 const double* D) {
69 BlockSparseMatrix* m = const_cast<BlockSparseMatrix*>(&A);
70 const CompressedRowBlockStructure* bs = m->block_structure();
71
72 // A = [P]
73 // [Q]
74
75 // Now add D to A if needed.
76 if (D != NULL) {
77 // A = [P]
78 // [Q]
79 // [D]
80 std::unique_ptr<BlockSparseMatrix> regularizer(
81 BlockSparseMatrix::CreateDiagonalMatrix(D, bs->cols));
82 m->AppendRows(*regularizer);
83 }
84
85 if (inner_product_computer_.get() == NULL) {
86 inner_product_computer_.reset(InnerProductComputer::Create(
87 *m,
88 options_.subset_preconditioner_start_row_block,
89 bs->rows.size(),
90 sparse_cholesky_->StorageType()));
91 }
92
93 // Compute inner_product = [Q'*Q + D'*D]
94 inner_product_computer_->Compute();
95
96 // Unappend D if needed.
97 if (D != NULL) {
98 // A = [P]
99 // [Q]
100 m->DeleteRowBlocks(bs->cols.size());
101 }
102
103 std::string message;
104 // Compute L. s.t., LL' = Q'*Q + D'*D
105 const LinearSolverTerminationType termination_type =
106 sparse_cholesky_->Factorize(inner_product_computer_->mutable_result(),
107 &message);
108 if (termination_type != LINEAR_SOLVER_SUCCESS) {
109 LOG(ERROR) << "Preconditioner factorization failed: " << message;
110 return false;
111 }
112
113 return true;
114}
115
116} // namespace internal
117} // namespace ceres