g2o
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< MatrixXD > &spinv, const std::vector< std::pair< int, int > > &blockIndices, const SparseBlockMatrix< MatrixType > &A)
 
virtual bool writeDebug () const
 write a debug dump of the system matrix if it is not PSD in solve More...
 
virtual void setWriteDebug (bool)
 
- Public Member Functions inherited from g2o::LinearSolverCholmodOnlineInterface
 LinearSolverCholmodOnlineInterface ()
 

Protected Attributes

cholmod_common _cholmodCommon
 
CholmodExt_cholmodSparse
 
cholmod_factor * _cholmodFactor
 
bool _blockOrdering
 
MatrixStructure _matrixStructure
 
Eigen::VectorXi _scalarPermutation
 
Eigen::VectorXi _blockPermutation
 

Additional Inherited Members

- 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 46 of file linear_solver_cholmod_online.h.

Constructor & Destructor Documentation

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

Definition at line 49 of file linear_solver_cholmod_online.h.

49  :
50  LinearSolver<MatrixType>()
51  {
52  _cholmodSparse = new CholmodExt();
53  _cholmodFactor = 0;
54  cholmod_start(&_cholmodCommon);
55 
56  // setup ordering strategy
57  _cholmodCommon.nmethods = 1 ;
58  _cholmodCommon.method[0].ordering = CHOLMOD_AMD; //CHOLMOD_COLAMD
59  //_cholmodCommon.postorder = 0;
60 
61  _cholmodCommon.supernodal = CHOLMOD_AUTO; //CHOLMOD_SUPERNODAL; //CHOLMOD_SIMPLICIAL;
62  batchEveryN = 100;
63  }
template<typename MatrixType>
virtual g2o::LinearSolverCholmodOnline< MatrixType >::~LinearSolverCholmodOnline ( )
inlinevirtual

Definition at line 65 of file linear_solver_cholmod_online.h.

66  {
67  delete _cholmodSparse;
68  if (_cholmodFactor) {
69  cholmod_free_factor(&_cholmodFactor, &_cholmodCommon);
70  _cholmodFactor = 0;
71  }
72  cholmod_finish(&_cholmodCommon);
73  }

Member Function Documentation

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

Definition at line 118 of file linear_solver_cholmod_online.h.

118 { return true;}
template<typename MatrixType>
int g2o::LinearSolverCholmodOnline< MatrixType >::choleskyUpdate ( cholmod_sparse *  update)
inlinevirtual

Implements g2o::LinearSolverCholmodOnlineInterface.

Definition at line 135 of file linear_solver_cholmod_online.h.

References g2o::writeCCSMatrix().

136  {
137  int result = cholmod_updown(1, update, _cholmodFactor, &_cholmodCommon);
138  //std::cerr << __PRETTY_FUNCTION__ << " " << result << std::endl;
139  if (_cholmodCommon.status == CHOLMOD_NOT_POSDEF) {
140  std::cerr << "Cholesky failure, writing debug.txt (Hessian loadable by Octave)" << std::endl;
141  writeCCSMatrix("debug.txt", _cholmodSparse->nrow, _cholmodSparse->ncol, (int*)_cholmodSparse->p, (int*)_cholmodSparse->i, (double*)_cholmodSparse->x, true);
142  return 0;
143  }
144  return result;
145  }
bool writeCCSMatrix(const string &filename, int rows, int cols, const int *Ap, const int *Ai, const double *Ax, bool upperTriangleSymmetric)
template<typename MatrixType>
void g2o::LinearSolverCholmodOnline< MatrixType >::computeSymbolicDecomposition ( const SparseBlockMatrix< MatrixType > &  A)
inline

Definition at line 173 of file linear_solver_cholmod_online.h.

References g2o::MatrixStructure::Aii, g2o::MatrixStructure::Ap, g2o::SparseBlockMatrix< MatrixType >::colBaseOfBlock(), g2o::SparseBlockMatrix< MatrixType >::colsOfBlock(), g2o::SparseBlockMatrix< MatrixType >::fillBlockStructure(), g2o::get_monotonic_time(), g2o::G2OBatchStatistics::globalStats(), and g2o::MatrixStructure::n.

174  {
175  double t = get_monotonic_time();
176 
177  A.fillBlockStructure(_matrixStructure);
178 
179  // get the ordering for the block matrix
180  if (_blockPermutation.size() < _matrixStructure.n) // double space if resizing
182 
183  int amdStatus = camd_order(_matrixStructure.n, _matrixStructure.Ap, _matrixStructure.Aii, _blockPermutation.data(), NULL, NULL, cmember->data());
184  if (amdStatus != CAMD_OK) {
185  std::cerr << "Error while computing ordering" << std::endl;
186  }
187 
188  // blow up the permutation to the scalar matrix and extend to include the additional blocks
189  if (_scalarPermutation.size() == 0)
190  _scalarPermutation.resize(_cholmodSparse->ncol);
191  if (_scalarPermutation.size() < (int)_cholmodSparse->ncol)
192  _scalarPermutation.resize(2*_cholmodSparse->ncol);
193  size_t scalarIdx = 0;
194  for (int i = 0; i < _matrixStructure.n; ++i) {
195  const int& p = _blockPermutation(i);
196  int base = A.colBaseOfBlock(p);
197  int nCols = A.colsOfBlock(p);
198  for (int j = 0; j < nCols; ++j)
199  _scalarPermutation(scalarIdx++) = base++;
200  }
201 
202  for (; scalarIdx < _cholmodSparse->ncol; ++scalarIdx) // extending for the additional blocks
203  _scalarPermutation(scalarIdx) = scalarIdx;
204  assert(scalarIdx == _cholmodSparse->ncol);
205 
206  // apply the ordering
207  _cholmodCommon.nmethods = 1;
208  _cholmodCommon.method[0].ordering = CHOLMOD_GIVEN;
209  _cholmodFactor = cholmod_analyze_p(_cholmodSparse, _scalarPermutation.data(), NULL, 0, &_cholmodCommon);
210 
211  G2OBatchStatistics* globalStats = G2OBatchStatistics::globalStats();
212  if (globalStats)
213  globalStats->timeSymbolicDecomposition = get_monotonic_time() - t;
214 
215  }
double get_monotonic_time()
Definition: timeutil.cpp:113
int * Ap
column pointers for A, of size n+1
int n
A is m-by-n. n must be >= 0.
static G2OBatchStatistics * globalStats()
Definition: batch_stats.h:73
int * Aii
row indices of A, of size nz = Ap [n]
template<typename MatrixType>
void g2o::LinearSolverCholmodOnline< MatrixType >::fillCholmodExt ( const SparseBlockMatrix< MatrixType > &  A,
bool  onlyValues 
)
inline

Definition at line 217 of file linear_solver_cholmod_online.h.

References g2o::SparseBlockMatrix< MatrixType >::cols(), g2o::CholmodExt::columnsAllocated, g2o::SparseBlockMatrix< MatrixType >::fillCCS(), g2o::SparseBlockMatrix< MatrixType >::nonZeros(), and g2o::SparseBlockMatrix< MatrixType >::rows().

218  {
219  int blockDimension = MatrixType::RowsAtCompileTime;
220  assert(blockDimension > 0);
221  //size_t origM = A.rows();
222  size_t origN = A.cols();
223  int additionalSpace = blockDimension * batchEveryN;
224  size_t m = A.rows() + additionalSpace;
225  size_t n = A.cols() + additionalSpace;
226 
227  if (_cholmodSparse->columnsAllocated < n) {
228  //std::cerr << __PRETTY_FUNCTION__ << ": reallocating columns" << std::endl;
229  _cholmodSparse->columnsAllocated = _cholmodSparse->columnsAllocated == 0 ? n : 2 * n; // pre-allocate more space if re-allocating
230  delete[] (int*)_cholmodSparse->p;
232  }
233  if (! onlyValues) {
234  size_t nzmax = A.nonZeros() + additionalSpace;
235  if (_cholmodSparse->nzmax < nzmax) {
236  //std::cerr << __PRETTY_FUNCTION__ << ": reallocating row + values" << std::endl;
237  _cholmodSparse->nzmax = _cholmodSparse->nzmax == 0 ? nzmax : 2 * nzmax; // pre-allocate more space if re-allocating
238  delete[] (double*)_cholmodSparse->x;
239  delete[] (int*)_cholmodSparse->i;
240  _cholmodSparse->i = new int[_cholmodSparse->nzmax];
241  _cholmodSparse->x = new double[_cholmodSparse->nzmax];
242  }
243  }
244  _cholmodSparse->ncol = n;
245  _cholmodSparse->nrow = m;
246 
247  int nz = 0;
248  if (onlyValues)
249  nz = A.fillCCS((double*)_cholmodSparse->x, true);
250  else
251  nz = A.fillCCS((int*)_cholmodSparse->p, (int*)_cholmodSparse->i, (double*)_cholmodSparse->x, true);
252 
253  int* cp = (int*)_cholmodSparse->p;
254  int* ci = (int*)_cholmodSparse->i;
255  double* cx = (double*)_cholmodSparse->x;
256 
257  cp = &cp[origN];
258  ci = &ci[nz];
259  cx = &cx[nz];
260 
261  // diagonal for the next blocks
262  for (int i = 0; i < additionalSpace; ++i) {
263  *cp++ = nz;
264  *ci++ = origN + i;
265  *cx++ = 1e-6;
266  ++nz;
267  }
268  *cp = nz;
269 
270  }
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 75 of file linear_solver_cholmod_online.h.

76  {
77  return true;
78  }
template<typename MatrixType>
cholmod_factor* g2o::LinearSolverCholmodOnline< MatrixType >::L ( ) const
inlinevirtual
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 125 of file linear_solver_cholmod_online.h.

References if().

125  {
126  size_t nnz= 0;
127  int* nz = (int*)_cholmodFactor->nz;
128  if (! nz)
129  return 0;
130  for (size_t i = 0; i < _cholmodFactor->n; ++i)
131  nnz += nz[i];
132  return nnz;
133  }
if(POLICY CMP0020) cmake_policy(SET CMP0020 OLD) endif() if(Qt4_FOUND) endif() if(Qt5_FOUND) endif() ADD_LIBRARY(viewer_library $
Definition: CMakeLists.txt: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.

References g2o::G2OBatchStatistics::choleskyNNZ, g2o::get_monotonic_time(), g2o::G2OBatchStatistics::globalStats(), g2o::G2OBatchStatistics::timeNumericDecomposition, and g2o::writeCCSMatrix().

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

Implements g2o::LinearSolverCholmodOnlineInterface.

Definition at line 147 of file linear_solver_cholmod_online.h.

148  {
149  // setting up b for calling cholmod
150  cholmod_dense bcholmod;
151  bcholmod.nrow = bcholmod.d = _cholmodSparse->nrow;
152  bcholmod.ncol = 1;
153  bcholmod.x = b;
154  bcholmod.xtype = CHOLMOD_REAL;
155  bcholmod.dtype = CHOLMOD_DOUBLE;
156 
157  cholmod_dense* xcholmod = cholmod_solve(CHOLMOD_A, _cholmodFactor, &bcholmod, &_cholmodCommon);
158  memcpy(x, xcholmod->x, sizeof(double) * bcholmod.nrow); // copy back to our array
159  cholmod_free_dense(&xcholmod, &_cholmodCommon);
160  return true;
161  }

Member Data Documentation

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

Definition at line 168 of file linear_solver_cholmod_online.h.

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

Definition at line 170 of file linear_solver_cholmod_online.h.

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

Definition at line 165 of file linear_solver_cholmod_online.h.

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

Definition at line 167 of file linear_solver_cholmod_online.h.

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

Definition at line 166 of file linear_solver_cholmod_online.h.

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

Definition at line 169 of file linear_solver_cholmod_online.h.

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

Definition at line 170 of file linear_solver_cholmod_online.h.


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