1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 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/schur_jacobi_preconditioner.h"
32
33 #include <utility>
34 #include <vector>
35
36 #include "ceres/block_random_access_diagonal_matrix.h"
37 #include "ceres/block_sparse_matrix.h"
38 #include "ceres/linear_solver.h"
39 #include "ceres/schur_eliminator.h"
40 #include "glog/logging.h"
41
42 namespace ceres {
43 namespace internal {
44
SchurJacobiPreconditioner(const CompressedRowBlockStructure & bs,const Preconditioner::Options & options)45 SchurJacobiPreconditioner::SchurJacobiPreconditioner(
46 const CompressedRowBlockStructure& bs,
47 const Preconditioner::Options& options)
48 : options_(options) {
49 CHECK_GT(options_.elimination_groups.size(), 1);
50 CHECK_GT(options_.elimination_groups[0], 0);
51 const int num_blocks = bs.cols.size() - options_.elimination_groups[0];
52 CHECK_GT(num_blocks, 0) << "Jacobian should have at least 1 f_block for "
53 << "SCHUR_JACOBI preconditioner.";
54 CHECK(options_.context != NULL);
55
56 std::vector<int> blocks(num_blocks);
57 for (int i = 0; i < num_blocks; ++i) {
58 blocks[i] = bs.cols[i + options_.elimination_groups[0]].size;
59 }
60
61 m_.reset(new BlockRandomAccessDiagonalMatrix(blocks));
62 InitEliminator(bs);
63 }
64
~SchurJacobiPreconditioner()65 SchurJacobiPreconditioner::~SchurJacobiPreconditioner() {}
66
67 // Initialize the SchurEliminator.
InitEliminator(const CompressedRowBlockStructure & bs)68 void SchurJacobiPreconditioner::InitEliminator(
69 const CompressedRowBlockStructure& bs) {
70 LinearSolver::Options eliminator_options;
71 eliminator_options.elimination_groups = options_.elimination_groups;
72 eliminator_options.num_threads = options_.num_threads;
73 eliminator_options.e_block_size = options_.e_block_size;
74 eliminator_options.f_block_size = options_.f_block_size;
75 eliminator_options.row_block_size = options_.row_block_size;
76 eliminator_options.context = options_.context;
77 eliminator_.reset(SchurEliminatorBase::Create(eliminator_options));
78 const bool kFullRankETE = true;
79 eliminator_->Init(
80 eliminator_options.elimination_groups[0], kFullRankETE, &bs);
81 }
82
83 // Update the values of the preconditioner matrix and factorize it.
UpdateImpl(const BlockSparseMatrix & A,const double * D)84 bool SchurJacobiPreconditioner::UpdateImpl(const BlockSparseMatrix& A,
85 const double* D) {
86 const int num_rows = m_->num_rows();
87 CHECK_GT(num_rows, 0);
88
89 // Compute a subset of the entries of the Schur complement.
90 eliminator_->Eliminate(
91 BlockSparseMatrixData(A), nullptr, D, m_.get(), nullptr);
92 m_->Invert();
93 return true;
94 }
95
RightMultiply(const double * x,double * y) const96 void SchurJacobiPreconditioner::RightMultiply(const double* x,
97 double* y) const {
98 m_->RightMultiply(x, y);
99 }
100
num_rows() const101 int SchurJacobiPreconditioner::num_rows() const { return m_->num_rows(); }
102
103 } // namespace internal
104 } // namespace ceres
105