blob: bfc8464db173c35dfcfd8f6a9ef1b01f71b978e0 [file] [log] [blame]
Sameer Agarwal290b9752013-02-17 16:50:37 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2013 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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_PRECONDITIONER_H_
32#define CERES_INTERNAL_PRECONDITIONER_H_
33
34#include <vector>
35#include "ceres/linear_operator.h"
36#include "ceres/sparse_matrix.h"
37
38namespace ceres {
39namespace internal {
40
41class BlockSparseMatrixBase;
42class SparseMatrix;
43
44class Preconditioner : public LinearOperator {
45 public:
46 struct Options {
47 Options()
48 : type(JACOBI),
49 sparse_linear_algebra_library(SUITE_SPARSE),
50 use_block_amd(true),
51 num_threads(1),
Sameer Agarwal31730ef2013-02-28 11:20:28 -080052 row_block_size(Eigen::Dynamic),
53 e_block_size(Eigen::Dynamic),
54 f_block_size(Eigen::Dynamic) {
Sameer Agarwal290b9752013-02-17 16:50:37 -080055 }
56
57 PreconditionerType type;
58
59 SparseLinearAlgebraLibraryType sparse_linear_algebra_library;
60
61 // See solver.h for explanation of this option.
62 bool use_block_amd;
63
64 // If possible, how many threads the preconditioner can use.
65 int num_threads;
66
67 // Hints about the order in which the parameter blocks should be
68 // eliminated by the linear solver.
69 //
70 // For example if elimination_groups is a vector of size k, then
71 // the linear solver is informed that it should eliminate the
Sameer Agarwalbeb45052013-02-22 13:37:01 -080072 // parameter blocks 0 ... elimination_groups[0] - 1 first, and
Sameer Agarwal931c3092013-02-25 09:46:21 -080073 // then elimination_groups[0] ... elimination_groups[1] - 1 and so
Sameer Agarwalbeb45052013-02-22 13:37:01 -080074 // on. Within each elimination group, the linear solver is free to
75 // choose how the parameter blocks are ordered. Different linear
76 // solvers have differing requirements on elimination_groups.
Sameer Agarwal290b9752013-02-17 16:50:37 -080077 //
78 // The most common use is for Schur type solvers, where there
79 // should be at least two elimination groups and the first
80 // elimination group must form an independent set in the normal
81 // equations. The first elimination group corresponds to the
82 // num_eliminate_blocks in the Schur type solvers.
83 vector<int> elimination_groups;
84
85 // If the block sizes in a BlockSparseMatrix are fixed, then in
86 // some cases the Schur complement based solvers can detect and
87 // specialize on them.
88 //
89 // It is expected that these parameters are set programmatically
90 // rather than manually.
91 //
92 // Please see schur_complement_solver.h and schur_eliminator.h for
93 // more details.
94 int row_block_size;
95 int e_block_size;
96 int f_block_size;
97 };
98
99 virtual ~Preconditioner();
100
101 // Update the numerical value of the preconditioner for the linear
102 // system:
103 //
104 // | A | x = |b|
105 // |diag(D)| |0|
106 //
107 // for some vector b. It is important that the matrix A have the
108 // same block structure as the one used to construct this object.
109 //
110 // D can be NULL, in which case its interpreted as a diagonal matrix
111 // of size zero.
112 virtual bool Update(const BlockSparseMatrixBase& A, const double* D) = 0;
113
114 // LinearOperator interface. Since the operator is symmetric,
115 // LeftMultiply and num_cols are just calls to RightMultiply and
116 // num_rows respectively. Update() must be called before
117 // RightMultiply can be called.
118 virtual void RightMultiply(const double* x, double* y) const = 0;
119 virtual void LeftMultiply(const double* x, double* y) const {
120 return RightMultiply(x, y);
121 }
122
123 virtual int num_rows() const = 0;
124 virtual int num_cols() const {
125 return num_rows();
126 }
127};
128
129// Wrap a SparseMatrix object as a preconditioner.
130class SparseMatrixPreconditionerWrapper : public Preconditioner {
131 public:
132 // Wrapper does NOT take ownership of the matrix pointer.
Sameer Agarwalbeb45052013-02-22 13:37:01 -0800133 explicit SparseMatrixPreconditionerWrapper(const SparseMatrix* matrix);
Sameer Agarwal290b9752013-02-17 16:50:37 -0800134 virtual ~SparseMatrixPreconditionerWrapper();
135
136 // Preconditioner interface
137 virtual bool Update(const BlockSparseMatrixBase& A, const double* D);
138 virtual void RightMultiply(const double* x, double* y) const;
139 virtual int num_rows() const;
140
141 private:
142 const SparseMatrix* matrix_;
143};
144
145} // namespace internal
146} // namespace ceres
147
148#endif // CERES_INTERNAL_PRECONDITIONER_H_