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

basic solver for Ax = b which has to reimplemented for different linear algebra libraries More...

#include <linear_solver_cholmod.h>

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

Public Member Functions

 LinearSolverCholmod ()
 
 LinearSolverCholmod (LinearSolverCholmod< MatrixType > const &)=delete
 
LinearSolverCholmodoperator= (LinearSolverCholmod< MatrixType > const &)=delete
 
virtual ~LinearSolverCholmod ()
 
virtual bool init ()
 
bool solve (const SparseBlockMatrix< MatrixType > &A, double *x, double *b)
 
virtual bool saveMatrix (const std::string &fileName)
 
- Public Member Functions inherited from g2o::LinearSolverCCS< MatrixType >
 LinearSolverCCS ()
 
 ~LinearSolverCCS ()
 
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 blockOrdering () const
 do the AMD ordering on the blocks or on the scalar matrix
 
void setBlockOrdering (bool blockOrdering)
 
- Public Member Functions inherited from g2o::LinearSolver< MatrixType >
 LinearSolver ()
 
virtual ~LinearSolver ()
 
bool writeDebug () const
 write a debug dump of the system matrix if it is not PSD in solve
 
void setWriteDebug (bool b)
 

Protected Member Functions

void computeSymbolicDecomposition (const SparseBlockMatrix< MatrixType > &A)
 
void fillCholmodExt (const SparseBlockMatrix< MatrixType > &A, bool onlyValues)
 
bool computeCholmodFactor (const SparseBlockMatrix< MatrixType > &A, double &t)
 compute the cholmodFactor for the given matrix A
 
bool solveBlocks_impl (const SparseBlockMatrix< MatrixType > &A, std::function< void(MarginalCovarianceCholesky &)> compute)
 
void freeCholdmodFactor ()
 
- Protected Member Functions inherited from g2o::LinearSolverCCS< MatrixType >
void initMatrixStructure (const SparseBlockMatrix< MatrixType > &A)
 

Protected Attributes

cholmod::Cholmod _cholmod
 
MatrixStructure _matrixStructure
 
VectorXI _scalarPermutation
 
VectorXI _blockPermutation
 
- Protected Attributes inherited from g2o::LinearSolverCCS< MatrixType >
SparseBlockMatrixCCS< MatrixType > * _ccsMatrix
 
bool _blockOrdering
 
- 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::LinearSolverCholmod< MatrixType >

basic solver for Ax = b which has to reimplemented for different linear algebra libraries

Definition at line 47 of file linear_solver_cholmod.h.

Constructor & Destructor Documentation

◆ LinearSolverCholmod() [1/2]

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

Definition at line 49 of file linear_solver_cholmod.h.

49: LinearSolverCCS<MatrixType>() {}

◆ LinearSolverCholmod() [2/2]

template<typename MatrixType >
g2o::LinearSolverCholmod< MatrixType >::LinearSolverCholmod ( LinearSolverCholmod< MatrixType > const &  )
delete

◆ ~LinearSolverCholmod()

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

Member Function Documentation

◆ computeCholmodFactor()

template<typename MatrixType >
bool g2o::LinearSolverCholmod< MatrixType >::computeCholmodFactor ( const SparseBlockMatrix< MatrixType > &  A,
double &  t 
)
inlineprotected

compute the cholmodFactor for the given matrix A

Definition at line 164 of file linear_solver_cholmod.h.

164 {
165 // _cholmodFactor used as bool, if not existing will copy the whole
166 // structure, otherwise only the values
167 bool hasFactor = _cholmod.hasFactor();
168 fillCholmodExt(A, hasFactor);
169
170 if (!hasFactor) {
172 assert(_cholmod.hasFactor() && "Symbolic cholesky failed");
173 }
174
175 t = get_monotonic_time();
176 bool factorStatus = _cholmod.factorize();
177 if (!factorStatus) {
178 if (this->writeDebug()) {
179 G2O_ERROR(
180 "Cholesky failure, writing debug.txt (Hessian loadable by Octave)");
181 saveMatrix("debug.txt");
182 } else {
183 G2O_DEBUG("Cholesky failure");
184 }
185 return false;
186 }
187 return true;
188 }
void computeSymbolicDecomposition(const SparseBlockMatrix< MatrixType > &A)
void fillCholmodExt(const SparseBlockMatrix< MatrixType > &A, bool onlyValues)
virtual bool saveMatrix(const std::string &fileName)
bool writeDebug() const
write a debug dump of the system matrix if it is not PSD in solve
#define G2O_ERROR(...)
Definition logger.h:89
#define G2O_DEBUG(...)
Definition logger.h:90
double get_monotonic_time()
Definition timeutil.cpp:43

References g2o::LinearSolverCholmod< MatrixType >::_cholmod, g2o::LinearSolverCholmod< MatrixType >::computeSymbolicDecomposition(), g2o::cholmod::Cholmod::factorize(), g2o::LinearSolverCholmod< MatrixType >::fillCholmodExt(), G2O_DEBUG, G2O_ERROR, g2o::get_monotonic_time(), g2o::cholmod::Cholmod::hasFactor(), g2o::LinearSolverCholmod< MatrixType >::saveMatrix(), and g2o::LinearSolver< MatrixType >::writeDebug().

Referenced by g2o::LinearSolverCholmod< MatrixType >::solve(), and g2o::LinearSolverCholmod< MatrixType >::solveBlocks_impl().

◆ computeSymbolicDecomposition()

template<typename MatrixType >
void g2o::LinearSolverCholmod< MatrixType >::computeSymbolicDecomposition ( const SparseBlockMatrix< MatrixType > &  A)
inlineprotected

Definition at line 91 of file linear_solver_cholmod.h.

91 {
92 double t = get_monotonic_time();
93 if (!this->blockOrdering()) {
95 } else {
96 A.fillBlockStructure(_matrixStructure);
97
98 // get the ordering for the block matrix
99 if (_blockPermutation.size() == 0)
101 if (_blockPermutation.size() <
102 _matrixStructure.n) // double space if resizing
104
105 // prepare AMD call via CHOLMOD
106 size_t structureDim = _matrixStructure.n;
107 size_t structureNz = _matrixStructure.nzMax();
108 size_t structureAllocated = structureDim;
109 double* structureX = nullptr;
110 cholmod::Cholmod::SparseView amdView(
111 structureDim, structureDim, structureNz, _matrixStructure.Ap,
112 _matrixStructure.Aii, structureX, structureAllocated);
113 bool amdStatus = _cholmod.amd(amdView, _blockPermutation.data());
114 if (!amdStatus) return;
115
116 // blow up the permutation to the scalar matrix
118
119 // apply the ordering
121 }
122 G2OBatchStatistics* globalStats = G2OBatchStatistics::globalStats();
123 if (globalStats)
124 globalStats->timeSymbolicDecomposition = get_monotonic_time() - t;
125 }
bool blockOrdering() const
do the AMD ordering on the blocks or on the scalar matrix
static void blockToScalarPermutation(const SparseBlockMatrix< MatrixType > &A, const Eigen::MatrixBase< BlockDerived > &p, const Eigen::MatrixBase< ScalarDerived > &scalar)
int * Aii
row indices of A, of size nz = Ap [n]
int n
A is m-by-n. n must be >= 0.
int nzMax() const
max number of non-zeros blocks
int * Ap
column pointers for A, of size n+1
bool amd(SparseView &sparseView, int *result)
compute AMD ordering on the given SparseView, store into result
bool analyze_p(int *permutation)
static G2OBatchStatistics * globalStats()
Definition batch_stats.h:77

References g2o::LinearSolverCholmod< MatrixType >::_blockPermutation, g2o::LinearSolverCholmod< MatrixType >::_cholmod, g2o::LinearSolverCholmod< MatrixType >::_matrixStructure, g2o::LinearSolverCholmod< MatrixType >::_scalarPermutation, g2o::MatrixStructure::Aii, g2o::cholmod::Cholmod::amd(), g2o::cholmod::Cholmod::analyze(), g2o::cholmod::Cholmod::analyze_p(), g2o::MatrixStructure::Ap, g2o::LinearSolverCCS< MatrixType >::blockOrdering(), g2o::LinearSolver< MatrixType >::blockToScalarPermutation(), g2o::SparseBlockMatrix< MatrixType >::fillBlockStructure(), g2o::get_monotonic_time(), g2o::G2OBatchStatistics::globalStats(), g2o::MatrixStructure::n, g2o::MatrixStructure::nzMax(), and g2o::G2OBatchStatistics::timeSymbolicDecomposition.

Referenced by g2o::LinearSolverCholmod< MatrixType >::computeCholmodFactor().

◆ fillCholmodExt()

template<typename MatrixType >
void g2o::LinearSolverCholmod< MatrixType >::fillCholmodExt ( const SparseBlockMatrix< MatrixType > &  A,
bool  onlyValues 
)
inlineprotected

Definition at line 127 of file linear_solver_cholmod.h.

127 {
128 if (!onlyValues) this->initMatrixStructure(A);
129 size_t m = A.rows();
130 size_t n = A.cols();
131 assert(m > 0 && n > 0 && "Hessian has 0 rows/cols");
132
133 cholmod::Cholmod::SparseView cholmodSparse = _cholmod.sparseView();
134
135 if (cholmodSparse.columnsAllocated < n) {
136 // pre-allocate more space if re-allocating
137 cholmodSparse.columnsAllocated =
138 cholmodSparse.columnsAllocated == 0 ? n : 2 * n;
139 delete[] cholmodSparse.p;
140 cholmodSparse.p = new int[cholmodSparse.columnsAllocated + 1];
141 }
142 if (!onlyValues) {
143 size_t nzmax = A.nonZeros();
144 if (cholmodSparse.nzmax < nzmax) {
145 // pre-allocate more space if re-allocating
146 cholmodSparse.nzmax = cholmodSparse.nzmax == 0 ? nzmax : 2 * nzmax;
147 delete[] cholmodSparse.x;
148 delete[] cholmodSparse.i;
149 cholmodSparse.i = new int[cholmodSparse.nzmax];
150 cholmodSparse.x = new double[cholmodSparse.nzmax];
151 }
152 }
153 cholmodSparse.ncol = n;
154 cholmodSparse.nrow = m;
155
156 if (onlyValues)
157 this->_ccsMatrix->fillCCS((double*)cholmodSparse.x, true);
158 else
159 this->_ccsMatrix->fillCCS((int*)cholmodSparse.p, (int*)cholmodSparse.i,
160 (double*)cholmodSparse.x, true);
161 }
void initMatrixStructure(const SparseBlockMatrix< MatrixType > &A)
SparseBlockMatrixCCS< MatrixType > * _ccsMatrix

References g2o::LinearSolverCCS< MatrixType >::_ccsMatrix, g2o::LinearSolverCholmod< MatrixType >::_cholmod, g2o::SparseBlockMatrix< MatrixType >::cols(), g2o::cholmod::Cholmod::SparseView::columnsAllocated, g2o::cholmod::Cholmod::SparseView::i, g2o::LinearSolverCCS< MatrixType >::initMatrixStructure(), g2o::cholmod::Cholmod::SparseView::ncol, g2o::SparseBlockMatrix< MatrixType >::nonZeros(), g2o::cholmod::Cholmod::SparseView::nrow, g2o::cholmod::Cholmod::SparseView::nzmax, g2o::cholmod::Cholmod::SparseView::p, g2o::SparseBlockMatrix< MatrixType >::rows(), g2o::cholmod::Cholmod::sparseView(), and g2o::cholmod::Cholmod::SparseView::x.

Referenced by g2o::LinearSolverCholmod< MatrixType >::computeCholmodFactor().

◆ freeCholdmodFactor()

template<typename MatrixType >
void g2o::LinearSolverCholmod< MatrixType >::freeCholdmodFactor ( )
inlineprotected

◆ init()

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

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

Implements g2o::LinearSolver< MatrixType >.

Definition at line 57 of file linear_solver_cholmod.h.

57 {
59 return true;
60 }

References g2o::LinearSolverCholmod< MatrixType >::freeCholdmodFactor().

◆ operator=()

template<typename MatrixType >
LinearSolverCholmod & g2o::LinearSolverCholmod< MatrixType >::operator= ( LinearSolverCholmod< MatrixType > const &  )
delete

◆ saveMatrix()

template<typename MatrixType >
virtual bool g2o::LinearSolverCholmod< MatrixType >::saveMatrix ( const std::string &  fileName)
inlinevirtual

Definition at line 78 of file linear_solver_cholmod.h.

78 {
79 cholmod::Cholmod::SparseView sparseView = _cholmod.sparseView();
80 writeCCSMatrix(fileName, sparseView.nrow, sparseView.ncol, sparseView.p,
81 sparseView.i, sparseView.x, true);
82 return true;
83 }
bool writeCCSMatrix(const string &filename, int rows, int cols, const int *Ap, const int *Ai, const double *Ax, bool upperTriangleSymmetric)

References g2o::LinearSolverCholmod< MatrixType >::_cholmod, g2o::cholmod::Cholmod::SparseView::i, g2o::cholmod::Cholmod::SparseView::ncol, g2o::cholmod::Cholmod::SparseView::nrow, g2o::cholmod::Cholmod::SparseView::p, g2o::cholmod::Cholmod::sparseView(), g2o::writeCCSMatrix(), and g2o::cholmod::Cholmod::SparseView::x.

Referenced by g2o::LinearSolverCholmod< MatrixType >::computeCholmodFactor().

◆ solve()

template<typename MatrixType >
bool g2o::LinearSolverCholmod< MatrixType >::solve ( const SparseBlockMatrix< MatrixType > &  A,
double *  x,
double *  b 
)
inlinevirtual

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 62 of file linear_solver_cholmod.h.

62 {
63 double t;
64 bool cholState = computeCholmodFactor(A, t);
65 if (!cholState) return false;
66
67 _cholmod.solve(x, b);
68
69 G2OBatchStatistics* globalStats = G2OBatchStatistics::globalStats();
70 if (globalStats) {
71 globalStats->timeNumericDecomposition = get_monotonic_time() - t;
72 globalStats->choleskyNNZ = _cholmod.choleskyNz();
73 }
74
75 return true;
76 }
bool computeCholmodFactor(const SparseBlockMatrix< MatrixType > &A, double &t)
compute the cholmodFactor for the given matrix A
void solve(double *x, double *b) const

References g2o::LinearSolverCholmod< MatrixType >::_cholmod, g2o::G2OBatchStatistics::choleskyNNZ, g2o::cholmod::Cholmod::choleskyNz(), g2o::LinearSolverCholmod< MatrixType >::computeCholmodFactor(), g2o::get_monotonic_time(), g2o::G2OBatchStatistics::globalStats(), g2o::cholmod::Cholmod::solve(), and g2o::G2OBatchStatistics::timeNumericDecomposition.

◆ solveBlocks_impl()

template<typename MatrixType >
bool g2o::LinearSolverCholmod< MatrixType >::solveBlocks_impl ( const SparseBlockMatrix< MatrixType > &  A,
std::function< void(MarginalCovarianceCholesky &)>  compute 
)
inlineprotectedvirtual

Implements g2o::LinearSolverCCS< MatrixType >.

Definition at line 190 of file linear_solver_cholmod.h.

192 {
193 double t;
194 bool cholState = computeCholmodFactor(A, t);
195 if (!cholState) return false;
196
197 // convert the factorization to LL, simplical, packed, monotonic
198 int change_status = _cholmod.simplifyFactor();
199 if (!change_status) return false;
200
201 cholmod::Cholmod::FactorView cholmodFactor = _cholmod.factor();
202 cholmod::Cholmod::SparseView cholmodSparse = _cholmod.sparseView();
203
204 // invert the permutation
205 int* p = cholmodFactor.perm;
206 VectorXI pinv(cholmodSparse.ncol);
207 for (size_t i = 0; i < cholmodSparse.ncol; ++i) pinv(p[i]) = i;
208
209 // compute the marginal covariance
210 MarginalCovarianceCholesky mcc;
211 mcc.setCholeskyFactor(cholmodSparse.ncol, cholmodFactor.p, cholmodFactor.i,
212 cholmodFactor.x, pinv.data());
213 compute(mcc);
214
215 G2OBatchStatistics* globalStats = G2OBatchStatistics::globalStats();
216 if (globalStats) {
217 globalStats->choleskyNNZ = _cholmod.choleskyNz();
218 }
219 return true;
220 }
Eigen::Matrix< int, Eigen::Dynamic, 1, Eigen::ColMajor > VectorXI
Definition eigen_types.h:41

References g2o::LinearSolverCholmod< MatrixType >::_cholmod, g2o::G2OBatchStatistics::choleskyNNZ, g2o::cholmod::Cholmod::choleskyNz(), g2o::LinearSolverCholmod< MatrixType >::computeCholmodFactor(), g2o::cholmod::Cholmod::factor(), g2o::G2OBatchStatistics::globalStats(), g2o::cholmod::Cholmod::FactorView::i, g2o::cholmod::Cholmod::SparseView::ncol, g2o::cholmod::Cholmod::FactorView::p, g2o::cholmod::Cholmod::FactorView::perm, g2o::MarginalCovarianceCholesky::setCholeskyFactor(), g2o::cholmod::Cholmod::simplifyFactor(), g2o::cholmod::Cholmod::sparseView(), and g2o::cholmod::Cholmod::FactorView::x.

Member Data Documentation

◆ _blockPermutation

template<typename MatrixType >
VectorXI g2o::LinearSolverCholmod< MatrixType >::_blockPermutation
protected

◆ _cholmod

template<typename MatrixType >
cholmod::Cholmod g2o::LinearSolverCholmod< MatrixType >::_cholmod
protected

◆ _matrixStructure

template<typename MatrixType >
MatrixStructure g2o::LinearSolverCholmod< MatrixType >::_matrixStructure
protected

◆ _scalarPermutation

template<typename MatrixType >
VectorXI g2o::LinearSolverCholmod< MatrixType >::_scalarPermutation
protected

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