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 #ifndef CERES_INTERNAL_SCHUR_COMPLEMENT_SOLVER_H_
32 #define CERES_INTERNAL_SCHUR_COMPLEMENT_SOLVER_H_
33 
34 #include <memory>
35 #include <set>
36 #include <utility>
37 #include <vector>
38 
39 #include "ceres/block_random_access_diagonal_matrix.h"
40 #include "ceres/block_random_access_matrix.h"
41 #include "ceres/block_sparse_matrix.h"
42 #include "ceres/block_structure.h"
43 #include "ceres/internal/port.h"
44 #include "ceres/linear_solver.h"
45 #include "ceres/schur_eliminator.h"
46 #include "ceres/types.h"
47 
48 #ifdef CERES_USE_EIGEN_SPARSE
49 #include "Eigen/OrderingMethods"
50 #include "Eigen/SparseCholesky"
51 #endif
52 
53 namespace ceres {
54 namespace internal {
55 
56 class BlockSparseMatrix;
57 class SparseCholesky;
58 
59 // Base class for Schur complement based linear least squares
60 // solvers. It assumes that the input linear system Ax = b can be
61 // partitioned into
62 //
63 //  E y + F z = b
64 //
65 // Where x = [y;z] is a partition of the variables.  The paritioning
66 // of the variables is such that, E'E is a block diagonal
67 // matrix. Further, the rows of A are ordered so that for every
68 // variable block in y, all the rows containing that variable block
69 // occur as a vertically contiguous block. i.e the matrix A looks like
70 //
71 //              E                 F
72 //  A = [ y1   0   0   0 |  z1    0    0   0    z5]
73 //      [ y1   0   0   0 |  z1   z2    0   0     0]
74 //      [  0  y2   0   0 |   0    0   z3   0     0]
75 //      [  0   0  y3   0 |  z1   z2   z3  z4    z5]
76 //      [  0   0  y3   0 |  z1    0    0   0    z5]
77 //      [  0   0   0  y4 |   0    0    0   0    z5]
78 //      [  0   0   0  y4 |   0   z2    0   0     0]
79 //      [  0   0   0  y4 |   0    0    0   0     0]
80 //      [  0   0   0   0 |  z1    0    0   0     0]
81 //      [  0   0   0   0 |   0    0   z3  z4    z5]
82 //
83 // This structure should be reflected in the corresponding
84 // CompressedRowBlockStructure object associated with A. The linear
85 // system Ax = b should either be well posed or the array D below
86 // should be non-null and the diagonal matrix corresponding to it
87 // should be non-singular.
88 //
89 // SchurComplementSolver has two sub-classes.
90 //
91 // DenseSchurComplementSolver: For problems where the Schur complement
92 // matrix is small and dense, or if CHOLMOD/SuiteSparse is not
93 // installed. For structure from motion problems, this is solver can
94 // be used for problems with upto a few hundred cameras.
95 //
96 // SparseSchurComplementSolver: For problems where the Schur
97 // complement matrix is large and sparse. It requires that Ceres be
98 // build with at least one sparse linear algebra library, as it
99 // computes a sparse Cholesky factorization of the Schur complement.
100 //
101 // This solver can be used for solving structure from motion problems
102 // with tens of thousands of cameras, though depending on the exact
103 // sparsity structure, it maybe better to use an iterative solver.
104 //
105 // The two solvers can be instantiated by calling
106 // LinearSolver::CreateLinearSolver with LinearSolver::Options::type
107 // set to DENSE_SCHUR and SPARSE_SCHUR
108 // respectively. LinearSolver::Options::elimination_groups[0] should
109 // be at least 1.
110 class CERES_EXPORT_INTERNAL SchurComplementSolver
111     : public BlockSparseMatrixSolver {
112  public:
SchurComplementSolver(const LinearSolver::Options & options)113   explicit SchurComplementSolver(const LinearSolver::Options& options)
114       : options_(options) {
115     CHECK_GT(options.elimination_groups.size(), 1);
116     CHECK_GT(options.elimination_groups[0], 0);
117     CHECK(options.context != NULL);
118   }
119   SchurComplementSolver(const SchurComplementSolver&) = delete;
120   void operator=(const SchurComplementSolver&) = delete;
121 
122   // LinearSolver methods
~SchurComplementSolver()123   virtual ~SchurComplementSolver() {}
124   LinearSolver::Summary SolveImpl(
125       BlockSparseMatrix* A,
126       const double* b,
127       const LinearSolver::PerSolveOptions& per_solve_options,
128       double* x) override;
129 
130  protected:
options()131   const LinearSolver::Options& options() const { return options_; }
132 
lhs()133   const BlockRandomAccessMatrix* lhs() const { return lhs_.get(); }
set_lhs(BlockRandomAccessMatrix * lhs)134   void set_lhs(BlockRandomAccessMatrix* lhs) { lhs_.reset(lhs); }
rhs()135   const double* rhs() const { return rhs_.get(); }
set_rhs(double * rhs)136   void set_rhs(double* rhs) { rhs_.reset(rhs); }
137 
138  private:
139   virtual void InitStorage(const CompressedRowBlockStructure* bs) = 0;
140   virtual LinearSolver::Summary SolveReducedLinearSystem(
141       const LinearSolver::PerSolveOptions& per_solve_options,
142       double* solution) = 0;
143 
144   LinearSolver::Options options_;
145 
146   std::unique_ptr<SchurEliminatorBase> eliminator_;
147   std::unique_ptr<BlockRandomAccessMatrix> lhs_;
148   std::unique_ptr<double[]> rhs_;
149 };
150 
151 // Dense Cholesky factorization based solver.
152 class DenseSchurComplementSolver : public SchurComplementSolver {
153  public:
DenseSchurComplementSolver(const LinearSolver::Options & options)154   explicit DenseSchurComplementSolver(const LinearSolver::Options& options)
155       : SchurComplementSolver(options) {}
156   DenseSchurComplementSolver(const DenseSchurComplementSolver&) = delete;
157   void operator=(const DenseSchurComplementSolver&) = delete;
158 
~DenseSchurComplementSolver()159   virtual ~DenseSchurComplementSolver() {}
160 
161  private:
162   void InitStorage(const CompressedRowBlockStructure* bs) final;
163   LinearSolver::Summary SolveReducedLinearSystem(
164       const LinearSolver::PerSolveOptions& per_solve_options,
165       double* solution) final;
166 };
167 
168 // Sparse Cholesky factorization based solver.
169 class SparseSchurComplementSolver : public SchurComplementSolver {
170  public:
171   explicit SparseSchurComplementSolver(const LinearSolver::Options& options);
172   SparseSchurComplementSolver(const SparseSchurComplementSolver&) = delete;
173   void operator=(const SparseSchurComplementSolver&) = delete;
174 
175   virtual ~SparseSchurComplementSolver();
176 
177  private:
178   void InitStorage(const CompressedRowBlockStructure* bs) final;
179   LinearSolver::Summary SolveReducedLinearSystem(
180       const LinearSolver::PerSolveOptions& per_solve_options,
181       double* solution) final;
182   LinearSolver::Summary SolveReducedLinearSystemUsingConjugateGradients(
183       const LinearSolver::PerSolveOptions& per_solve_options, double* solution);
184 
185   // Size of the blocks in the Schur complement.
186   std::vector<int> blocks_;
187   std::unique_ptr<SparseCholesky> sparse_cholesky_;
188   std::unique_ptr<BlockRandomAccessDiagonalMatrix> preconditioner_;
189 };
190 
191 }  // namespace internal
192 }  // namespace ceres
193 
194 #endif  // CERES_INTERNAL_SCHUR_COMPLEMENT_SOLVER_H_
195