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

linear solver which allows to update the cholesky factor More...

#include <linear_solver_cholmod_online.h>

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

Public Member Functions

 LinearSolverCholmodOnline ()
 
virtual ~LinearSolverCholmodOnline ()
 
virtual bool init ()
 
bool solve (const SparseBlockMatrix< MatrixType > &A, double *x, double *b)
 
bool blockOrdering () const
 
cholmod_factor * L () const
 
size_t nonZerosInL () const
 
int choleskyUpdate (cholmod_sparse *update)
 
bool solve (double *x, double *b)
 
void computeSymbolicDecomposition (const SparseBlockMatrix< MatrixType > &A)
 
void fillCholmodExt (const SparseBlockMatrix< MatrixType > &A, bool onlyValues)
 
- 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)
 
- Public Member Functions inherited from g2o::LinearSolverCholmodOnlineInterface
 LinearSolverCholmodOnlineInterface ()
 

Protected Attributes

cholmod_common _cholmodCommon
 
cholmod::CholmodExt_cholmodSparse
 
cholmod_factor * _cholmodFactor
 
bool _blockOrdering
 
MatrixStructure _matrixStructure
 
Eigen::VectorXi _scalarPermutation
 
Eigen::VectorXi _blockPermutation
 
- 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)
 
- Public Attributes inherited from g2o::LinearSolverCholmodOnlineInterface
Eigen::VectorXi * cmember
 
int batchEveryN
 

Detailed Description

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

linear solver which allows to update the cholesky factor

Definition at line 51 of file linear_solver_cholmod_online.h.

Constructor & Destructor Documentation

◆ LinearSolverCholmodOnline()

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

Definition at line 54 of file linear_solver_cholmod_online.h.

54 : LinearSolver<MatrixType>() {
55 _cholmodSparse = new cholmod::CholmodExt();
57 cholmod_start(&_cholmodCommon);
58
59 // setup ordering strategy
60 _cholmodCommon.nmethods = 1;
61 _cholmodCommon.method[0].ordering = CHOLMOD_AMD; // CHOLMOD_COLAMD
62 //_cholmodCommon.postorder = 0;
63
64 _cholmodCommon.supernodal =
65 CHOLMOD_AUTO; // CHOLMOD_SUPERNODAL; //CHOLMOD_SIMPLICIAL;
66 batchEveryN = 100;
67 }

References g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodCommon, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodFactor, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodSparse, and g2o::LinearSolverCholmodOnlineInterface::batchEveryN.

◆ ~LinearSolverCholmodOnline()

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

Member Function Documentation

◆ blockOrdering()

template<typename MatrixType >
bool g2o::LinearSolverCholmodOnline< MatrixType >::blockOrdering ( ) const
inline

Definition at line 124 of file linear_solver_cholmod_online.h.

124{ return true; }

◆ choleskyUpdate()

template<typename MatrixType >
int g2o::LinearSolverCholmodOnline< MatrixType >::choleskyUpdate ( cholmod_sparse *  update)
inlinevirtual

Implements g2o::LinearSolverCholmodOnlineInterface.

Definition at line 139 of file linear_solver_cholmod_online.h.

139 {
140 int result = cholmod_updown(1, update, _cholmodFactor, &_cholmodCommon);
141 // std::cerr << __PRETTY_FUNCTION__ << " " << result << std::endl;
142 if (_cholmodCommon.status == CHOLMOD_NOT_POSDEF) {
143 std::cerr
144 << "Cholesky failure, writing debug.txt (Hessian loadable by Octave)"
145 << std::endl;
146 writeCCSMatrix("debug.txt", _cholmodSparse->nrow, _cholmodSparse->ncol,
147 (int*)_cholmodSparse->p, (int*)_cholmodSparse->i,
148 (double*)_cholmodSparse->x, true);
149 return 0;
150 }
151 return result;
152 }
bool writeCCSMatrix(const string &filename, int rows, int cols, const int *Ap, const int *Ai, const double *Ax, bool upperTriangleSymmetric)

References g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodCommon, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodFactor, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodSparse, and g2o::writeCCSMatrix().

◆ computeSymbolicDecomposition()

template<typename MatrixType >
void g2o::LinearSolverCholmodOnline< MatrixType >::computeSymbolicDecomposition ( const SparseBlockMatrix< MatrixType > &  A)
inline

Definition at line 181 of file linear_solver_cholmod_online.h.

181 {
182 double t = get_monotonic_time();
183
184 A.fillBlockStructure(_matrixStructure);
185
186 // get the ordering for the block matrix
187 if (_blockPermutation.size() <
188 _matrixStructure.n) // double space if resizing
190
191 int amdStatus = camd_order(_matrixStructure.n, _matrixStructure.Ap,
193 NULL, NULL, cmember->data());
194 if (amdStatus != CAMD_OK) {
195 std::cerr << "Error while computing ordering" << std::endl;
196 }
197
198 // blow up the permutation to the scalar matrix and extend to include the
199 // additional blocks
200 if (_scalarPermutation.size() == 0)
202 if (_scalarPermutation.size() < (int)_cholmodSparse->ncol)
203 _scalarPermutation.resize(2 * _cholmodSparse->ncol);
204 size_t scalarIdx = 0;
205 for (int i = 0; i < _matrixStructure.n; ++i) {
206 const int& p = _blockPermutation(i);
207 int base = A.colBaseOfBlock(p);
208 int nCols = A.colsOfBlock(p);
209 for (int j = 0; j < nCols; ++j) _scalarPermutation(scalarIdx++) = base++;
210 }
211
212 for (; scalarIdx < _cholmodSparse->ncol;
213 ++scalarIdx) // extending for the additional blocks
214 _scalarPermutation(scalarIdx) = scalarIdx;
215 assert(scalarIdx == _cholmodSparse->ncol);
216
217 // apply the ordering
218 _cholmodCommon.nmethods = 1;
219 _cholmodCommon.method[0].ordering = CHOLMOD_GIVEN;
220 _cholmodFactor = cholmod_analyze_p(
222
223 G2OBatchStatistics* globalStats = G2OBatchStatistics::globalStats();
224 if (globalStats)
225 globalStats->timeSymbolicDecomposition = get_monotonic_time() - t;
226 }
int * Aii
row indices of A, of size nz = Ap [n]
int n
A is m-by-n. n must be >= 0.
int * Ap
column pointers for A, of size n+1
double get_monotonic_time()
Definition timeutil.cpp:43
static G2OBatchStatistics * globalStats()
Definition batch_stats.h:77

References g2o::LinearSolverCholmodOnline< MatrixType >::_blockPermutation, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodCommon, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodFactor, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodSparse, g2o::LinearSolverCholmodOnline< MatrixType >::_matrixStructure, g2o::LinearSolverCholmodOnline< MatrixType >::_scalarPermutation, g2o::MatrixStructure::Aii, g2o::MatrixStructure::Ap, g2o::LinearSolverCholmodOnlineInterface::cmember, g2o::SparseBlockMatrix< MatrixType >::colBaseOfBlock(), g2o::SparseBlockMatrix< MatrixType >::colsOfBlock(), g2o::SparseBlockMatrix< MatrixType >::fillBlockStructure(), g2o::get_monotonic_time(), g2o::G2OBatchStatistics::globalStats(), g2o::MatrixStructure::n, and g2o::G2OBatchStatistics::timeSymbolicDecomposition.

Referenced by g2o::LinearSolverCholmodOnline< MatrixType >::solve().

◆ fillCholmodExt()

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

Definition at line 228 of file linear_solver_cholmod_online.h.

228 {
229 int blockDimension = MatrixType::RowsAtCompileTime;
230 assert(blockDimension > 0);
231 // size_t origM = A.rows();
232 size_t origN = A.cols();
233 int additionalSpace = blockDimension * batchEveryN;
234 size_t m = A.rows() + additionalSpace;
235 size_t n = A.cols() + additionalSpace;
236
238 // std::cerr << __PRETTY_FUNCTION__ << ": reallocating columns" <<
239 // std::endl;
242 ? n
243 : 2 * n; // pre-allocate more space if re-allocating
244 delete[] (int*)_cholmodSparse->p;
246 }
247 if (!onlyValues) {
248 size_t nzmax = A.nonZeros() + additionalSpace;
249 if (_cholmodSparse->nzmax < nzmax) {
250 // std::cerr << __PRETTY_FUNCTION__ << ": reallocating row + values" <<
251 // std::endl;
252 _cholmodSparse->nzmax =
253 _cholmodSparse->nzmax == 0
254 ? nzmax
255 : 2 * nzmax; // pre-allocate more space if re-allocating
256 delete[] (double*)_cholmodSparse->x;
257 delete[] (int*)_cholmodSparse->i;
258 _cholmodSparse->i = new int[_cholmodSparse->nzmax];
259 _cholmodSparse->x = new double[_cholmodSparse->nzmax];
260 }
261 }
262 _cholmodSparse->ncol = n;
263 _cholmodSparse->nrow = m;
264
265 int nz = 0;
266 if (onlyValues)
267 nz = A.fillCCS((double*)_cholmodSparse->x, true);
268 else
269 nz = A.fillCCS((int*)_cholmodSparse->p, (int*)_cholmodSparse->i,
270 (double*)_cholmodSparse->x, true);
271
272 int* cp = (int*)_cholmodSparse->p;
273 int* ci = (int*)_cholmodSparse->i;
274 double* cx = (double*)_cholmodSparse->x;
275
276 cp = &cp[origN];
277 ci = &ci[nz];
278 cx = &cx[nz];
279
280 // diagonal for the next blocks
281 for (int i = 0; i < additionalSpace; ++i) {
282 *cp++ = nz;
283 *ci++ = origN + i;
284 *cx++ = 1e-6;
285 ++nz;
286 }
287 *cp = nz;
288 }

References g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodSparse, g2o::LinearSolverCholmodOnlineInterface::batchEveryN, g2o::SparseBlockMatrix< MatrixType >::cols(), g2o::cholmod::CholmodExt::columnsAllocated, g2o::SparseBlockMatrix< MatrixType >::fillCCS(), g2o::SparseBlockMatrix< MatrixType >::nonZeros(), and g2o::SparseBlockMatrix< MatrixType >::rows().

Referenced by g2o::LinearSolverCholmodOnline< MatrixType >::solve().

◆ init()

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

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

Implements g2o::LinearSolver< MatrixType >.

Definition at line 78 of file linear_solver_cholmod_online.h.

78{ return true; }

◆ L()

template<typename MatrixType >
cholmod_factor * g2o::LinearSolverCholmodOnline< MatrixType >::L ( ) const
inlinevirtual

◆ nonZerosInL()

template<typename MatrixType >
size_t g2o::LinearSolverCholmodOnline< MatrixType >::nonZerosInL ( ) const
inlinevirtual

return the number of non-zeros in the current factorization

Implements g2o::LinearSolverCholmodOnlineInterface.

Definition at line 131 of file linear_solver_cholmod_online.h.

131 {
132 size_t nnz = 0;
133 int* nz = (int*)_cholmodFactor->nz;
134 if (!nz) return 0;
135 for (size_t i = 0; i < _cholmodFactor->n; ++i) nnz += nz[i];
136 return nnz;
137 }

References g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodFactor.

◆ solve() [1/2]

template<typename MatrixType >
bool g2o::LinearSolverCholmodOnline< 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 80 of file linear_solver_cholmod_online.h.

80 {
81 cholmod_free_factor(&_cholmodFactor, &_cholmodCommon);
83 fillCholmodExt(A, false);
84
86 assert(_cholmodFactor && "Symbolic cholesky failed");
87 double t = get_monotonic_time();
88
89 // setting up b for calling cholmod
90 cholmod_dense bcholmod;
91 bcholmod.nrow = bcholmod.d = _cholmodSparse->nrow;
92 bcholmod.ncol = 1;
93 bcholmod.x = b;
94 bcholmod.xtype = CHOLMOD_REAL;
95 bcholmod.dtype = CHOLMOD_DOUBLE;
96
97 cholmod_factorize(_cholmodSparse, _cholmodFactor, &_cholmodCommon);
98 if (_cholmodCommon.status == CHOLMOD_NOT_POSDEF) {
99 std::cerr << "solve(): Cholesky failure, writing debug.txt (Hessian "
100 "loadable by Octave)"
101 << std::endl;
102 writeCCSMatrix("debug.txt", _cholmodSparse->nrow, _cholmodSparse->ncol,
103 (int*)_cholmodSparse->p, (int*)_cholmodSparse->i,
104 (double*)_cholmodSparse->x, true);
105 return false;
106 }
107
108 cholmod_dense* xcholmod =
109 cholmod_solve(CHOLMOD_A, _cholmodFactor, &bcholmod, &_cholmodCommon);
110 memcpy(x, xcholmod->x,
111 sizeof(double) * bcholmod.nrow); // copy back to our array
112 cholmod_free_dense(&xcholmod, &_cholmodCommon);
113
114 G2OBatchStatistics* globalStats = G2OBatchStatistics::globalStats();
115 if (globalStats) {
116 globalStats->timeNumericDecomposition = get_monotonic_time() - t;
117 globalStats->choleskyNNZ =
118 static_cast<size_t>(_cholmodCommon.method[0].lnz);
119 }
120
121 return true;
122 }
void computeSymbolicDecomposition(const SparseBlockMatrix< MatrixType > &A)
void fillCholmodExt(const SparseBlockMatrix< MatrixType > &A, bool onlyValues)

References g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodCommon, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodFactor, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodSparse, g2o::G2OBatchStatistics::choleskyNNZ, g2o::LinearSolverCholmodOnline< MatrixType >::computeSymbolicDecomposition(), g2o::LinearSolverCholmodOnline< MatrixType >::fillCholmodExt(), g2o::get_monotonic_time(), g2o::G2OBatchStatistics::globalStats(), g2o::G2OBatchStatistics::timeNumericDecomposition, and g2o::writeCCSMatrix().

◆ solve() [2/2]

template<typename MatrixType >
bool g2o::LinearSolverCholmodOnline< MatrixType >::solve ( double *  x,
double *  b 
)
inlinevirtual

Implements g2o::LinearSolverCholmodOnlineInterface.

Definition at line 154 of file linear_solver_cholmod_online.h.

154 {
155 // setting up b for calling cholmod
156 cholmod_dense bcholmod;
157 bcholmod.nrow = bcholmod.d = _cholmodSparse->nrow;
158 bcholmod.ncol = 1;
159 bcholmod.x = b;
160 bcholmod.xtype = CHOLMOD_REAL;
161 bcholmod.dtype = CHOLMOD_DOUBLE;
162
163 cholmod_dense* xcholmod =
164 cholmod_solve(CHOLMOD_A, _cholmodFactor, &bcholmod, &_cholmodCommon);
165 memcpy(x, xcholmod->x,
166 sizeof(double) * bcholmod.nrow); // copy back to our array
167 cholmod_free_dense(&xcholmod, &_cholmodCommon);
168 return true;
169 }

References g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodCommon, g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodFactor, and g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodSparse.

Member Data Documentation

◆ _blockOrdering

template<typename MatrixType >
bool g2o::LinearSolverCholmodOnline< MatrixType >::_blockOrdering
protected

Definition at line 176 of file linear_solver_cholmod_online.h.

◆ _blockPermutation

template<typename MatrixType >
Eigen::VectorXi g2o::LinearSolverCholmodOnline< MatrixType >::_blockPermutation
protected

◆ _cholmodCommon

template<typename MatrixType >
cholmod_common g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodCommon
protected

◆ _cholmodFactor

template<typename MatrixType >
cholmod_factor* g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodFactor
protected

◆ _cholmodSparse

template<typename MatrixType >
cholmod::CholmodExt* g2o::LinearSolverCholmodOnline< MatrixType >::_cholmodSparse
protected

◆ _matrixStructure

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

◆ _scalarPermutation

template<typename MatrixType >
Eigen::VectorXi g2o::LinearSolverCholmodOnline< MatrixType >::_scalarPermutation
protected

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