gtsam
Loading...
Searching...
No Matches
Preconditioner.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <gtsam/base/Matrix.h>
13#include <gtsam/base/Vector.h>
14#include <memory>
15#include <iosfwd>
16#include <map>
17#include <string>
18
19namespace gtsam {
20
23class KeyInfo;
24class VectorValues;
25
26/* parameters for the preconditioner */
27struct GTSAM_EXPORT PreconditionerParameters {
28
29 typedef std::shared_ptr<PreconditionerParameters> shared_ptr;
30
31 enum Kernel { /* Preconditioner Kernel */
32 GTSAM = 0,
33 CHOLMOD /* experimental */
34 } kernel_ ;
35
36 enum Verbosity {
37 SILENT = 0,
38 COMPLEXITY = 1,
39 ERROR = 2
40 } verbosity_ ;
41
42 PreconditionerParameters(): kernel_(GTSAM), verbosity_(SILENT) {}
43 PreconditionerParameters(const PreconditionerParameters &p) : kernel_(p.kernel_), verbosity_(p.verbosity_) {}
44 virtual ~PreconditionerParameters() {}
45
46 /* general interface */
47 inline Kernel kernel() const { return kernel_; }
48 inline Verbosity verbosity() const { return verbosity_; }
49
50 void print() const;
51
52 virtual void print(std::ostream &os) const;
53
54 static Kernel kernelTranslator(const std::string &s);
55 static Verbosity verbosityTranslator(const std::string &s);
56 static std::string kernelTranslator(Kernel k);
57 static std::string verbosityTranslator(Verbosity v);
58
59 /* for serialization */
60 friend std::ostream& operator<<(std::ostream &os, const PreconditionerParameters &p);
61 };
62
63/* PCG aims to solve the problem: A x = b by reparametrizing it as
64 * L^{-1} A L^{-T} y = L^{-1} b or M^{-1} A x = M^{-1} b,
65 * where A \approx L L^{T}, or A \approx M
66 * The goal of this class is to provide a general interface to all preconditioners */
67class GTSAM_EXPORT Preconditioner {
68public:
69 typedef std::shared_ptr<Preconditioner> shared_ptr;
70 typedef std::vector<size_t> Dimensions;
71
72 /* Generic Constructor and Destructor */
73 Preconditioner() {}
74 virtual ~Preconditioner() {}
75
76 /*
77 * Abstract interface for raw vectors. VectorValues is a speed bottleneck
78 * and Yong-Dian has profiled preconditioners (outside GTSAM) with the the
79 * three methods below. In GTSAM, unfortunately, we are still using the
80 * VectorValues methods called in iterative-inl.h
81 */
82
84 virtual void solve(const Vector& y, Vector &x) const = 0;
85
87 virtual void transposeSolve(const Vector& y, Vector& x) const = 0;
88
90 virtual void build(
91 const GaussianFactorGraph &gfg,
92 const KeyInfo &info,
93 const std::map<Key,Vector> &lambda
94 ) = 0;
95};
96
97/*******************************************************************************************/
98struct GTSAM_EXPORT DummyPreconditionerParameters : public PreconditionerParameters {
99 typedef PreconditionerParameters Base;
100 typedef std::shared_ptr<DummyPreconditionerParameters> shared_ptr;
101 DummyPreconditionerParameters() : Base() {}
102 ~DummyPreconditionerParameters() override {}
103};
104
105/*******************************************************************************************/
106class GTSAM_EXPORT DummyPreconditioner : public Preconditioner {
107public:
108 typedef Preconditioner Base;
109 typedef std::shared_ptr<DummyPreconditioner> shared_ptr;
110
111public:
112
113 DummyPreconditioner() : Base() {}
114 ~DummyPreconditioner() override {}
115
116 /* Computation Interfaces for raw vector */
117 void solve(const Vector& y, Vector &x) const override { x = y; }
118 void transposeSolve(const Vector& y, Vector& x) const override { x = y; }
119 void build(
120 const GaussianFactorGraph &gfg,
121 const KeyInfo &info,
122 const std::map<Key,Vector> &lambda
123 ) override {}
124};
125
126/*******************************************************************************************/
127struct GTSAM_EXPORT BlockJacobiPreconditionerParameters : public PreconditionerParameters {
128 typedef PreconditionerParameters Base;
129 BlockJacobiPreconditionerParameters() : Base() {}
130 ~BlockJacobiPreconditionerParameters() override {}
131};
132
133/*******************************************************************************************/
134class GTSAM_EXPORT BlockJacobiPreconditioner : public Preconditioner {
135 friend class GaussianFactorGraphSystem;
136
137public:
138 typedef Preconditioner Base;
139 BlockJacobiPreconditioner() ;
140 ~BlockJacobiPreconditioner() override ;
141
142 /* Computation Interfaces for raw vector */
143 void solve(const Vector& y, Vector &x) const override;
144 void transposeSolve(const Vector& y, Vector& x) const override;
145 void build(
146 const GaussianFactorGraph &gfg,
147 const KeyInfo &info,
148 const std::map<Key,Vector> &lambda
149 ) override;
150
161 void build(const std::vector<Matrix>& blocks, const KeyInfo& info);
162
163protected:
164
165 void clean() ;
166
168 void solveInPlaceRange(Vector& x, size_t begin, size_t end,
169 bool transpose) const;
170
171 std::vector<size_t> dims_;
172 std::vector<size_t> scalarOffsets_;
173 std::vector<size_t> bufferOffsets_;
174 double *buffer_;
175 size_t bufferSize_;
176 size_t nnz_;
177};
178
179/*********************************************************************************************/
180/* factory method to create preconditioners */
181std::shared_ptr<Preconditioner> createPreconditioner(const std::shared_ptr<PreconditionerParameters> parameters);
182
183}
typedef and functions to augment Eigen's MatrixXd
typedef and functions to augment Eigen's VectorXd
Global functions in a separate testing namespace.
Definition chartTesting.h:28
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition GaussianFactorGraph.h:77
Ordered key dimensions and offsets in one flattened variable vector.
Definition KeyInfo.h:51
Compiled flat-vector system used by preconditioned conjugate gradients.
Definition PCGSolver.h:145
virtual void solve(const Vector &y, Vector &x) const =0
implement x = L^{-1} y
virtual void build(const GaussianFactorGraph &gfg, const KeyInfo &info, const std::map< Key, Vector > &lambda)=0
build/factorize the preconditioner
virtual void transposeSolve(const Vector &y, Vector &x) const =0
implement x = L^{-T} y
void build(const GaussianFactorGraph &gfg, const KeyInfo &info, const std::map< Key, Vector > &lambda) override
build/factorize the preconditioner
Definition Preconditioner.h:119
void solve(const Vector &y, Vector &x) const override
implement x = L^{-1} y
Definition Preconditioner.h:117
void transposeSolve(const Vector &y, Vector &x) const override
implement x = L^{-T} y
Definition Preconditioner.h:118
void transposeSolve(const Vector &y, Vector &x) const override
implement x = L^{-T} y
Definition Preconditioner.cpp:120
void solve(const Vector &y, Vector &x) const override
implement x = L^{-1} y
Definition Preconditioner.cpp:114
void build(const GaussianFactorGraph &gfg, const KeyInfo &info, const std::map< Key, Vector > &lambda) override
build/factorize the preconditioner
Definition Preconditioner.cpp:126
void solveInPlaceRange(Vector &x, size_t begin, size_t end, bool transpose) const
Apply a triangular solve to a contiguous range of independent blocks.
Definition Preconditioner.cpp:90
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:73