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