g2o
Loading...
Searching...
No Matches
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
g2o::LinearSolverPCG< MatrixType > Class Template Reference

linear solver using PCG, pre-conditioner is block Jacobi More...

#include <linear_solver_pcg.h>

Inheritance diagram for g2o::LinearSolverPCG< MatrixType >:
Inheritance graph
[legend]
Collaboration diagram for g2o::LinearSolverPCG< MatrixType >:
Collaboration graph
[legend]

Public Member Functions

 LinearSolverPCG ()
 
virtual ~LinearSolverPCG ()
 
virtual bool init ()
 
bool solve (const SparseBlockMatrix< MatrixType > &A, double *x, double *b)
 
double tolerance () const
 return the tolerance for terminating PCG before convergence
 
void setTolerance (double tolerance)
 
int maxIterations () const
 
void setMaxIterations (int maxIter)
 
bool absoluteTolerance () const
 
void setAbsoluteTolerance (bool absoluteTolerance)
 
- Public Member Functions inherited from g2o::LinearSolver< MatrixType >
 LinearSolver ()
 
virtual ~LinearSolver ()
 
virtual bool solveBlocks (double **&blocks, const SparseBlockMatrix< MatrixType > &A)
 
virtual bool solvePattern (SparseBlockMatrix< MatrixX > &spinv, const std::vector< std::pair< int, int > > &blockIndices, const SparseBlockMatrix< MatrixType > &A)
 
bool writeDebug () const
 write a debug dump of the system matrix if it is not PSD in solve
 
void setWriteDebug (bool b)
 

Protected Types

using MatrixVector = std::vector< MatrixType >
 
using MatrixPtrVector = std::vector< const MatrixType * >
 

Protected Member Functions

void multDiag (const std::vector< int > &colBlockIndices, MatrixVector &A, const VectorX &src, VectorX &dest)
 
void multDiag (const std::vector< int > &colBlockIndices, MatrixPtrVector &A, const VectorX &src, VectorX &dest)
 
void mult (const std::vector< int > &colBlockIndices, const VectorX &src, VectorX &dest)
 

Protected Attributes

double _tolerance
 
double _residual
 
bool _absoluteTolerance
 
int _maxIter
 
MatrixPtrVector _diag
 
MatrixVector _J
 
std::vector< std::pair< int, int > > _indices
 
MatrixPtrVector _sparseMat
 
- Protected Attributes inherited from g2o::LinearSolver< MatrixType >
bool _writeDebug
 

Additional Inherited Members

- Static Public Member Functions inherited from g2o::LinearSolver< MatrixType >
static void allocateBlocks (const SparseBlockMatrix< MatrixType > &A, double **&blocks)
 allocate block memory structure
 
static void deallocateBlocks (const SparseBlockMatrix< MatrixType > &A, double **&blocks)
 de-allocate the block structure
 
template<typename BlockDerived , typename ScalarDerived >
static void blockToScalarPermutation (const SparseBlockMatrix< MatrixType > &A, const Eigen::MatrixBase< BlockDerived > &p, const Eigen::MatrixBase< ScalarDerived > &scalar)
 

Detailed Description

template<typename MatrixType>
class g2o::LinearSolverPCG< MatrixType >

linear solver using PCG, pre-conditioner is block Jacobi

Definition at line 43 of file linear_solver_pcg.h.

Member Typedef Documentation

◆ MatrixPtrVector

template<typename MatrixType >
using g2o::LinearSolverPCG< MatrixType >::MatrixPtrVector = std::vector<const MatrixType*>
protected

Definition at line 77 of file linear_solver_pcg.h.

◆ MatrixVector

template<typename MatrixType >
using g2o::LinearSolverPCG< MatrixType >::MatrixVector = std::vector<MatrixType>
protected

Definition at line 76 of file linear_solver_pcg.h.

Constructor & Destructor Documentation

◆ LinearSolverPCG()

template<typename MatrixType >
g2o::LinearSolverPCG< MatrixType >::LinearSolverPCG ( )
inline

◆ ~LinearSolverPCG()

template<typename MatrixType >
virtual g2o::LinearSolverPCG< MatrixType >::~LinearSolverPCG ( )
inlinevirtual

Definition at line 52 of file linear_solver_pcg.h.

52{}

Member Function Documentation

◆ absoluteTolerance()

template<typename MatrixType >
bool g2o::LinearSolverPCG< MatrixType >::absoluteTolerance ( ) const
inline

◆ init()

template<typename MatrixType >
virtual bool g2o::LinearSolverPCG< MatrixType >::init ( )
inlinevirtual

init for operating on matrices with a different non-zero pattern like before

Implements g2o::LinearSolver< MatrixType >.

Definition at line 54 of file linear_solver_pcg.h.

54 {
55 _residual = -1.0;
56 _indices.clear();
57 _sparseMat.clear();
58 return true;
59 }
MatrixPtrVector _sparseMat
std::vector< std::pair< int, int > > _indices

References g2o::LinearSolverPCG< MatrixType >::_indices, g2o::LinearSolverPCG< MatrixType >::_residual, and g2o::LinearSolverPCG< MatrixType >::_sparseMat.

◆ maxIterations()

template<typename MatrixType >
int g2o::LinearSolverPCG< MatrixType >::maxIterations ( ) const
inline

Definition at line 67 of file linear_solver_pcg.h.

67{ return _maxIter; }

References g2o::LinearSolverPCG< MatrixType >::_maxIter.

◆ mult()

template<typename MatrixType >
void LinearSolverPCG::mult ( const std::vector< int > &  colBlockIndices,
const VectorX src,
VectorX dest 
)
protected

Definition at line 186 of file linear_solver_pcg.hpp.

187 {
188 // first multiply with the diagonal
189 multDiag(colBlockIndices, _diag, src, dest);
190
191 // now multiply with the upper triangular block
192 for (size_t i = 0; i < _sparseMat.size(); ++i) {
193 const int& srcOffset = _indices[i].second;
194 const int& destOffsetT = srcOffset;
195 const int& destOffset = _indices[i].first;
196 const int& srcOffsetT = destOffset;
197
199 _sparseMat[i];
200 // destVec += *a * srcVec (according to the sub-vector parts)
201 internal::pcg_axpy(*a, src, srcOffset, dest, destOffset);
202 // destVec += *a.transpose() * srcVec (according to the sub-vector parts)
203 internal::pcg_atxpy(*a, src, srcOffsetT, dest, destOffsetT);
204 }
205}
void multDiag(const std::vector< int > &colBlockIndices, MatrixVector &A, const VectorX &src, VectorX &dest)
MatrixType SparseMatrixBlock
this is the type of the elementary block, it is an Eigen::Matrix.
void pcg_axpy(const MatrixType &A, const VectorX &x, int xoff, VectorX &y, int yoff)
void pcg_atxpy(const MatrixType &A, const VectorX &x, int xoff, VectorX &y, int yoff)

References internal::pcg_atxpy(), and internal::pcg_axpy().

◆ multDiag() [1/2]

template<typename MatrixType >
void LinearSolverPCG::multDiag ( const std::vector< int > &  colBlockIndices,
MatrixPtrVector A,
const VectorX src,
VectorX dest 
)
protected

Definition at line 175 of file linear_solver_pcg.hpp.

177 {
178 int row = 0;
179 for (size_t i = 0; i < A.size(); ++i) {
180 internal::pcg_axy(*A[i], src, row, dest, row);
181 row = colBlockIndices[i];
182 }
183}
void pcg_axy(const MatrixType &A, const VectorX &x, int xoff, VectorX &y, int yoff)

References internal::pcg_axy().

◆ multDiag() [2/2]

template<typename MatrixType >
void LinearSolverPCG::multDiag ( const std::vector< int > &  colBlockIndices,
MatrixVector A,
const VectorX src,
VectorX dest 
)
protected

Definition at line 164 of file linear_solver_pcg.hpp.

166 {
167 int row = 0;
168 for (size_t i = 0; i < A.size(); ++i) {
169 internal::pcg_axy(A[i], src, row, dest, row);
170 row = colBlockIndices[i];
171 }
172}

References internal::pcg_axy().

◆ setAbsoluteTolerance()

template<typename MatrixType >
void g2o::LinearSolverPCG< MatrixType >::setAbsoluteTolerance ( bool  absoluteTolerance)
inline

◆ setMaxIterations()

template<typename MatrixType >
void g2o::LinearSolverPCG< MatrixType >::setMaxIterations ( int  maxIter)
inline

Definition at line 68 of file linear_solver_pcg.h.

68{ _maxIter = maxIter; }

References g2o::LinearSolverPCG< MatrixType >::_maxIter.

◆ setTolerance()

template<typename MatrixType >
void g2o::LinearSolverPCG< MatrixType >::setTolerance ( double  tolerance)
inline

Definition at line 65 of file linear_solver_pcg.h.

double tolerance() const
return the tolerance for terminating PCG before convergence

References g2o::LinearSolverPCG< MatrixType >::_tolerance, and g2o::LinearSolverPCG< MatrixType >::tolerance().

◆ solve()

template<typename MatrixType >
bool LinearSolverPCG::solve ( const SparseBlockMatrix< MatrixType > &  A,
double *  x,
double *  b 
)
virtual

Assumes that A is the same matrix for several calls. Among other assumptions, the non-zero pattern does not change! If the matrix changes call init() before. solve system Ax = b, x and b have to allocated beforehand!!

Implements g2o::LinearSolver< MatrixType >.

Definition at line 88 of file linear_solver_pcg.hpp.

89 {
90 const bool indexRequired = _indices.size() == 0;
91 _diag.clear();
92 _J.clear();
93
94 // put the block matrix once in a linear structure, makes mult faster
95 int colIdx = 0;
96 for (size_t i = 0; i < A.blockCols().size(); ++i) {
98 A.blockCols()[i];
99 if (col.size() > 0) {
100 typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it;
101 for (it = col.begin(); it != col.end(); ++it) {
102 if (it->first == (int)i) { // only the upper triangular block is needed
103 _diag.push_back(it->second);
104 _J.push_back(it->second->inverse());
105 break;
106 }
107 if (indexRequired) {
108 _indices.push_back(std::make_pair(
109 it->first > 0 ? A.rowBlockIndices()[it->first - 1] : 0, colIdx));
110 _sparseMat.push_back(it->second);
111 }
112 }
113 }
114 colIdx = A.colBlockIndices()[i];
115 }
116
117 int n = A.rows();
118 assert(n > 0 && "Hessian has 0 rows/cols");
119 Eigen::Map<VectorX> xvec(x, A.cols());
120 const Eigen::Map<VectorX> bvec(b, n);
121 xvec.setZero();
122
123 VectorX r, d, q, s;
124 d.setZero(n);
125 q.setZero(n);
126 s.setZero(n);
127
128 r = bvec;
129 multDiag(A.colBlockIndices(), _J, r, d);
130 double dn = r.dot(d);
131 double d0 = _tolerance * dn;
132
133 if (_absoluteTolerance) {
134 if (_residual > 0.0 && _residual > d0) d0 = _residual;
135 }
136
137 int maxIter = _maxIter < 0 ? A.rows() : _maxIter;
138
139 int iteration;
140 for (iteration = 0; iteration < maxIter; ++iteration) {
141 G2O_DEBUG("residual [{}]: {}", iteration, dn);
142 if (dn <= d0) break; // done
143 mult(A.colBlockIndices(), d, q);
144 double a = dn / d.dot(q);
145 xvec += a * d;
146 // TODO: reset residual here every 50 iterations
147 r -= a * q;
148 multDiag(A.colBlockIndices(), _J, r, s);
149 double dold = dn;
150 dn = r.dot(s);
151 double ba = dn / dold;
152 d = s + ba * d;
153 }
154 _residual = 0.5 * dn;
155 G2OBatchStatistics* globalStats = G2OBatchStatistics::globalStats();
156 if (globalStats) {
157 globalStats->iterationsLinearSolver = iteration;
158 }
159
160 return true;
161}
void mult(const std::vector< int > &colBlockIndices, const VectorX &src, VectorX &dest)
std::map< int, SparseMatrixBlock * > IntBlockMap
#define G2O_DEBUG(...)
Definition logger.h:90
VectorN< Eigen::Dynamic > VectorX
Definition eigen_types.h:55
static G2OBatchStatistics * globalStats()
Definition batch_stats.h:77

References g2o::SparseBlockMatrix< MatrixType >::blockCols(), g2o::SparseBlockMatrix< MatrixType >::colBlockIndices(), g2o::SparseBlockMatrix< MatrixType >::cols(), G2O_DEBUG, g2o::G2OBatchStatistics::iterationsLinearSolver, g2o::SparseBlockMatrix< MatrixType >::rowBlockIndices(), and g2o::SparseBlockMatrix< MatrixType >::rows().

◆ tolerance()

template<typename MatrixType >
double g2o::LinearSolverPCG< MatrixType >::tolerance ( ) const
inline

return the tolerance for terminating PCG before convergence

Definition at line 64 of file linear_solver_pcg.h.

64{ return _tolerance; }

References g2o::LinearSolverPCG< MatrixType >::_tolerance.

Referenced by g2o::LinearSolverPCG< MatrixType >::setTolerance().

Member Data Documentation

◆ _absoluteTolerance

template<typename MatrixType >
bool g2o::LinearSolverPCG< MatrixType >::_absoluteTolerance
protected

◆ _diag

template<typename MatrixType >
MatrixPtrVector g2o::LinearSolverPCG< MatrixType >::_diag
protected

Definition at line 84 of file linear_solver_pcg.h.

◆ _indices

template<typename MatrixType >
std::vector<std::pair<int, int> > g2o::LinearSolverPCG< MatrixType >::_indices
protected

Definition at line 87 of file linear_solver_pcg.h.

Referenced by g2o::LinearSolverPCG< MatrixType >::init().

◆ _J

template<typename MatrixType >
MatrixVector g2o::LinearSolverPCG< MatrixType >::_J
protected

Definition at line 85 of file linear_solver_pcg.h.

◆ _maxIter

template<typename MatrixType >
int g2o::LinearSolverPCG< MatrixType >::_maxIter
protected

◆ _residual

template<typename MatrixType >
double g2o::LinearSolverPCG< MatrixType >::_residual
protected

◆ _sparseMat

template<typename MatrixType >
MatrixPtrVector g2o::LinearSolverPCG< MatrixType >::_sparseMat
protected

Definition at line 88 of file linear_solver_pcg.h.

Referenced by g2o::LinearSolverPCG< MatrixType >::init().

◆ _tolerance

template<typename MatrixType >
double g2o::LinearSolverPCG< MatrixType >::_tolerance
protected

The documentation for this class was generated from the following files: