blob: 24c245dc30e1c6893d9c34ad315983ab54f9db67 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// 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// Abstract interface for objects solving linear systems of various
32// kinds.
33
34#ifndef CERES_INTERNAL_LINEAR_SOLVER_H_
35#define CERES_INTERNAL_LINEAR_SOLVER_H_
36
37#include <cstddef>
38#include <map>
39#include <string>
40#include <vector>
41#include "ceres/block_sparse_matrix.h"
42#include "ceres/casts.h"
43#include "ceres/compressed_row_sparse_matrix.h"
44#include "ceres/context_impl.h"
45#include "ceres/dense_sparse_matrix.h"
46#include "ceres/execution_summary.h"
47#include "ceres/triplet_sparse_matrix.h"
48#include "ceres/types.h"
49#include "glog/logging.h"
50
51namespace ceres {
52namespace internal {
53
54enum LinearSolverTerminationType {
55 // Termination criterion was met.
56 LINEAR_SOLVER_SUCCESS,
57
58 // Solver ran for max_num_iterations and terminated before the
59 // termination tolerance could be satisfied.
60 LINEAR_SOLVER_NO_CONVERGENCE,
61
62 // Solver was terminated due to numerical problems, generally due to
63 // the linear system being poorly conditioned.
64 LINEAR_SOLVER_FAILURE,
65
66 // Solver failed with a fatal error that cannot be recovered from,
67 // e.g. CHOLMOD ran out of memory when computing the symbolic or
68 // numeric factorization or an underlying library was called with
69 // the wrong arguments.
70 LINEAR_SOLVER_FATAL_ERROR
71};
72
73// This enum controls the fill-reducing ordering a sparse linear
74// algebra library should use before computing a sparse factorization
75// (usually Cholesky).
76enum OrderingType {
77 NATURAL, // Do not re-order the matrix. This is useful when the
78 // matrix has been ordered using a fill-reducing ordering
79 // already.
80 AMD // Use the Approximate Minimum Degree algorithm to re-order
81 // the matrix.
82};
83
84class LinearOperator;
85
86// Abstract base class for objects that implement algorithms for
87// solving linear systems
88//
89// Ax = b
90//
91// It is expected that a single instance of a LinearSolver object
92// maybe used multiple times for solving multiple linear systems with
93// the same sparsity structure. This allows them to cache and reuse
94// information across solves. This means that calling Solve on the
95// same LinearSolver instance with two different linear systems will
96// result in undefined behaviour.
97//
98// Subclasses of LinearSolver use two structs to configure themselves.
99// The Options struct configures the LinearSolver object for its
100// lifetime. The PerSolveOptions struct is used to specify options for
101// a particular Solve call.
102class LinearSolver {
103 public:
104 struct Options {
105 LinearSolverType type = SPARSE_NORMAL_CHOLESKY;
106 PreconditionerType preconditioner_type = JACOBI;
107 VisibilityClusteringType visibility_clustering_type = CANONICAL_VIEWS;
108 DenseLinearAlgebraLibraryType dense_linear_algebra_library_type = EIGEN;
109 SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type =
110 SUITE_SPARSE;
111
112 // See solver.h for information about these flags.
113 bool use_postordering = false;
114 bool dynamic_sparsity = false;
115 bool use_explicit_schur_complement = false;
116
117 // Number of internal iterations that the solver uses. This
118 // parameter only makes sense for iterative solvers like CG.
119 int min_num_iterations = 1;
120 int max_num_iterations = 1;
121
122 // If possible, how many threads can the solver use.
123 int num_threads = 1;
124
125 // Hints about the order in which the parameter blocks should be
126 // eliminated by the linear solver.
127 //
128 // For example if elimination_groups is a vector of size k, then
129 // the linear solver is informed that it should eliminate the
130 // parameter blocks 0 ... elimination_groups[0] - 1 first, and
131 // then elimination_groups[0] ... elimination_groups[1] - 1 and so
132 // on. Within each elimination group, the linear solver is free to
133 // choose how the parameter blocks are ordered. Different linear
134 // solvers have differing requirements on elimination_groups.
135 //
136 // The most common use is for Schur type solvers, where there
137 // should be at least two elimination groups and the first
138 // elimination group must form an independent set in the normal
139 // equations. The first elimination group corresponds to the
140 // num_eliminate_blocks in the Schur type solvers.
141 std::vector<int> elimination_groups;
142
143 // Iterative solvers, e.g. Preconditioned Conjugate Gradients
144 // maintain a cheap estimate of the residual which may become
145 // inaccurate over time. Thus for non-zero values of this
146 // parameter, the solver can be told to recalculate the value of
147 // the residual using a |b - Ax| evaluation.
148 int residual_reset_period = 10;
149
150 // If the block sizes in a BlockSparseMatrix are fixed, then in
151 // some cases the Schur complement based solvers can detect and
152 // specialize on them.
153 //
154 // It is expected that these parameters are set programmatically
155 // rather than manually.
156 //
157 // Please see schur_complement_solver.h and schur_eliminator.h for
158 // more details.
159 int row_block_size = Eigen::Dynamic;
160 int e_block_size = Eigen::Dynamic;
161 int f_block_size = Eigen::Dynamic;
162
163 bool use_mixed_precision_solves = false;
164 int max_num_refinement_iterations = 0;
165 ContextImpl* context = nullptr;
166 };
167
168 // Options for the Solve method.
169 struct PerSolveOptions {
170 // This option only makes sense for unsymmetric linear solvers
171 // that can solve rectangular linear systems.
172 //
173 // Given a matrix A, an optional diagonal matrix D as a vector,
174 // and a vector b, the linear solver will solve for
175 //
176 // | A | x = | b |
177 // | D | | 0 |
178 //
179 // If D is null, then it is treated as zero, and the solver returns
180 // the solution to
181 //
182 // A x = b
183 //
184 // In either case, x is the vector that solves the following
185 // optimization problem.
186 //
187 // arg min_x ||Ax - b||^2 + ||Dx||^2
188 //
189 // Here A is a matrix of size m x n, with full column rank. If A
190 // does not have full column rank, the results returned by the
191 // solver cannot be relied on. D, if it is not null is an array of
192 // size n. b is an array of size m and x is an array of size n.
193 double* D = nullptr;
194
195 // This option only makes sense for iterative solvers.
196 //
197 // In general the performance of an iterative linear solver
198 // depends on the condition number of the matrix A. For example
199 // the convergence rate of the conjugate gradients algorithm
200 // is proportional to the square root of the condition number.
201 //
202 // One particularly useful technique for improving the
203 // conditioning of a linear system is to precondition it. In its
204 // simplest form a preconditioner is a matrix M such that instead
205 // of solving Ax = b, we solve the linear system AM^{-1} y = b
206 // instead, where M is such that the condition number k(AM^{-1})
207 // is smaller than the conditioner k(A). Given the solution to
208 // this system, x = M^{-1} y. The iterative solver takes care of
209 // the mechanics of solving the preconditioned system and
210 // returning the corrected solution x. The user only needs to
211 // supply a linear operator.
212 //
213 // A null preconditioner is equivalent to an identity matrix being
214 // used a preconditioner.
215 LinearOperator* preconditioner = nullptr;
216
217
218 // The following tolerance related options only makes sense for
219 // iterative solvers. Direct solvers ignore them.
220
221 // Solver terminates when
222 //
223 // |Ax - b| <= r_tolerance * |b|.
224 //
225 // This is the most commonly used termination criterion for
226 // iterative solvers.
227 double r_tolerance = 0.0;
228
229 // For PSD matrices A, let
230 //
231 // Q(x) = x'Ax - 2b'x
232 //
233 // be the cost of the quadratic function defined by A and b. Then,
234 // the solver terminates at iteration i if
235 //
236 // i * (Q(x_i) - Q(x_i-1)) / Q(x_i) < q_tolerance.
237 //
238 // This termination criterion is more useful when using CG to
239 // solve the Newton step. This particular convergence test comes
240 // from Stephen Nash's work on truncated Newton
241 // methods. References:
242 //
243 // 1. Stephen G. Nash & Ariela Sofer, Assessing A Search
244 // Direction Within A Truncated Newton Method, Operation
245 // Research Letters 9(1990) 219-221.
246 //
247 // 2. Stephen G. Nash, A Survey of Truncated Newton Methods,
248 // Journal of Computational and Applied Mathematics,
249 // 124(1-2), 45-59, 2000.
250 //
251 double q_tolerance = 0.0;
252 };
253
254 // Summary of a call to the Solve method. We should move away from
255 // the true/false method for determining solver success. We should
256 // let the summary object do the talking.
257 struct Summary {
258 double residual_norm = -1.0;
259 int num_iterations = -1;
260 LinearSolverTerminationType termination_type = LINEAR_SOLVER_FAILURE;
261 std::string message;
262 };
263
264 // If the optimization problem is such that there are no remaining
265 // e-blocks, a Schur type linear solver cannot be used. If the
266 // linear solver is of Schur type, this function implements a policy
267 // to select an alternate nearest linear solver to the one selected
268 // by the user. The input linear_solver_type is returned otherwise.
269 static LinearSolverType LinearSolverForZeroEBlocks(
270 LinearSolverType linear_solver_type);
271
272 virtual ~LinearSolver();
273
274 // Solve Ax = b.
275 virtual Summary Solve(LinearOperator* A,
276 const double* b,
277 const PerSolveOptions& per_solve_options,
278 double* x) = 0;
279
280 // This method returns copies instead of references so that the base
281 // class implementation does not have to worry about life time
282 // issues. Further, this calls are not expected to be frequent or
283 // performance sensitive.
284 virtual std::map<std::string, CallStatistics> Statistics() const {
285 return std::map<std::string, CallStatistics>();
286 }
287
288 // Factory
289 static LinearSolver* Create(const Options& options);
290};
291
292// This templated subclass of LinearSolver serves as a base class for
293// other linear solvers that depend on the particular matrix layout of
294// the underlying linear operator. For example some linear solvers
295// need low level access to the TripletSparseMatrix implementing the
296// LinearOperator interface. This class hides those implementation
297// details behind a private virtual method, and has the Solve method
298// perform the necessary upcasting.
299template <typename MatrixType>
300class TypedLinearSolver : public LinearSolver {
301 public:
302 virtual ~TypedLinearSolver() {}
303 virtual LinearSolver::Summary Solve(
304 LinearOperator* A,
305 const double* b,
306 const LinearSolver::PerSolveOptions& per_solve_options,
307 double* x) {
308 ScopedExecutionTimer total_time("LinearSolver::Solve", &execution_summary_);
309 CHECK(A != nullptr);
310 CHECK(b != nullptr);
311 CHECK(x != nullptr);
312 return SolveImpl(down_cast<MatrixType*>(A), b, per_solve_options, x);
313 }
314
315 virtual std::map<std::string, CallStatistics> Statistics() const {
316 return execution_summary_.statistics();
317 }
318
319 private:
320 virtual LinearSolver::Summary SolveImpl(
321 MatrixType* A,
322 const double* b,
323 const LinearSolver::PerSolveOptions& per_solve_options,
324 double* x) = 0;
325
326 ExecutionSummary execution_summary_;
327};
328
329// Linear solvers that depend on acccess to the low level structure of
330// a SparseMatrix.
331typedef TypedLinearSolver<BlockSparseMatrix> BlockSparseMatrixSolver; // NOLINT
332typedef TypedLinearSolver<CompressedRowSparseMatrix> CompressedRowSparseMatrixSolver; // NOLINT
333typedef TypedLinearSolver<DenseSparseMatrix> DenseSparseMatrixSolver; // NOLINT
334typedef TypedLinearSolver<TripletSparseMatrix> TripletSparseMatrixSolver; // NOLINT
335
336} // namespace internal
337} // namespace ceres
338
339#endif // CERES_INTERNAL_LINEAR_SOLVER_H_