g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
solvers
csparse
linear_solver_csparse.h
Go to the documentation of this file.
1
// g2o - General Graph Optimization
2
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
7
// met:
8
//
9
// * Redistributions of source code must retain the above copyright notice,
10
// this list of conditions and the following disclaimer.
11
// * Redistributions in binary form must reproduce the above copyright
12
// notice, this list of conditions and the following disclaimer in the
13
// documentation and/or other materials provided with the distribution.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27
#ifndef G2O_LINEAR_SOLVERCSPARSE_H
28
#define G2O_LINEAR_SOLVERCSPARSE_H
29
30
#include <cassert>
31
#include <iostream>
32
33
#include "
csparse_wrapper.h
"
34
#include "
g2o/core/batch_stats.h
"
35
#include "
g2o/core/linear_solver.h
"
36
#include "
g2o/core/marginal_covariance_cholesky.h
"
37
#include "
g2o/stuff/logger.h
"
38
#include "
g2o/stuff/timeutil.h
"
39
#include "
g2o_csparse_api.h
"
40
41
namespace
g2o
{
42
46
template
<
typename
MatrixType>
47
class
LinearSolverCSparse
:
public
LinearSolverCCS
<MatrixType> {
48
public
:
49
LinearSolverCSparse
() :
LinearSolverCCS
<MatrixType>() {}
50
51
LinearSolverCSparse
(
LinearSolverCSparse<MatrixType>
const
&) =
delete
;
52
LinearSolverCSparse
&
operator=
(
LinearSolverCSparse<MatrixType>
const
&) =
53
delete
;
54
55
virtual
~LinearSolverCSparse
() =
default
;
56
57
virtual
bool
init
() {
58
csparse
.freeSymbolic();
59
return
true
;
60
}
61
62
bool
solve
(
const
SparseBlockMatrix<MatrixType>
& A,
double
* x,
double
* b) {
63
prepareSolve
(A);
64
65
double
t =
get_monotonic_time
();
66
bool
ok =
csparse
.solve(x, b);
67
if
(!ok && this->
writeDebug
()) {
68
G2O_ERROR
(
69
"Cholesky failure, writing debug.txt (Hessian loadable by Octave)"
);
70
csparse
.writeSparse(
"debug.txt"
);
71
}
else
{
72
G2O_DEBUG
(
"Cholesky failure"
);
73
}
74
75
G2OBatchStatistics
* globalStats =
G2OBatchStatistics::globalStats
();
76
if
(globalStats) {
77
globalStats->
timeNumericDecomposition
=
get_monotonic_time
() - t;
78
globalStats->
choleskyNNZ
=
static_cast<
size_t
>
(
csparse
.choleskyNz());
79
}
80
81
return
ok;
82
}
83
84
protected
:
85
csparse::CSparse
csparse
;
86
MatrixStructure
_matrixStructure
;
87
88
void
prepareSolve
(
const
SparseBlockMatrix<MatrixType>
& A) {
89
bool
hasSymbolic =
csparse
.hasSymbolic();
90
fillCSparse
(A, hasSymbolic);
91
// perform symbolic cholesky once
92
if
(!hasSymbolic) {
93
computeSymbolicDecomposition
(A);
94
assert(
csparse
.hasSymbolic() &&
"Symbolic cholesky failed"
);
95
}
96
}
97
98
void
computeSymbolicDecomposition
(
const
SparseBlockMatrix<MatrixType>
& A) {
99
double
t =
get_monotonic_time
();
100
if
(!this->
blockOrdering
()) {
101
csparse.
analyze
();
102
}
else
{
103
A.
fillBlockStructure
(
_matrixStructure
);
104
105
// prepare block structure for the CSparse call
106
double
* structureX =
nullptr
;
107
int
structureNz =
_matrixStructure
.nzMax();
108
int
structureAllocated =
_matrixStructure
.n;
109
csparse::CSparse::SparseView
auxBlock(
110
_matrixStructure
.n,
_matrixStructure
.n, structureNz,
111
_matrixStructure
.Ap,
_matrixStructure
.Aii, structureX,
112
structureAllocated);
113
114
// AMD ordering on the block structure
115
VectorXI
blockPermutation;
116
csparse
.amd(auxBlock, blockPermutation);
117
118
// blow up the permutation to the scalar matrix
119
VectorXI
scalarPermutation;
120
this->
blockToScalarPermutation
(A, blockPermutation, scalarPermutation);
121
csparse
.analyze_p(scalarPermutation.data());
122
}
123
G2OBatchStatistics
* globalStats =
G2OBatchStatistics::globalStats
();
124
if
(globalStats) {
125
globalStats->
timeSymbolicDecomposition
=
get_monotonic_time
() - t;
126
}
127
}
128
129
void
fillCSparse
(
const
SparseBlockMatrix<MatrixType>
& A,
bool
onlyValues) {
130
if
(!onlyValues) this->
initMatrixStructure
(A);
131
int
m = A.
rows
();
132
int
n = A.
cols
();
133
assert(m > 0 && n > 0 &&
"Hessian has 0 rows/cols"
);
134
135
csparse::CSparse::SparseView
ccsA =
csparse
.sparseView();
136
137
if
(ccsA.
columnsAllocated
< n) {
138
// pre-allocate more space if re-allocating
139
ccsA.
columnsAllocated
= ccsA.
columnsAllocated
== 0 ? n : 2 * n;
140
delete
[] ccsA.
p
;
141
ccsA.
p
=
new
int
[ccsA.
columnsAllocated
+ 1];
142
}
143
144
if
(!onlyValues) {
145
int
nzmax = A.
nonZeros
();
146
if
(ccsA.
nzmax
< nzmax) {
147
// pre-allocate more space if re-allocating
148
ccsA.
nzmax
= ccsA.
nzmax
== 0 ? nzmax : 2 * nzmax;
149
delete
[] ccsA.
x
;
150
delete
[] ccsA.
i
;
151
ccsA.
i
=
new
int
[ccsA.
nzmax
];
152
ccsA.
x
=
new
double
[ccsA.
nzmax
];
153
}
154
}
155
ccsA.
m
= m;
156
ccsA.
n
= n;
157
158
if
(onlyValues) {
159
this->
_ccsMatrix
->fillCCS(ccsA.
x
,
true
);
160
}
else
{
161
int
nz = this->
_ccsMatrix
->fillCCS(ccsA.
p
, ccsA.
i
, ccsA.
x
,
true
);
162
(void)nz;
163
assert(nz <= ccsA.
nzmax
);
164
}
165
}
166
172
bool
solveBlocks_impl
(
173
const
SparseBlockMatrix<MatrixType>
& A,
174
std::function<
void
(
MarginalCovarianceCholesky
&)> compute) {
175
prepareSolve
(A);
176
bool
ok =
csparse
.factorize();
177
if
(ok) {
178
csparse::CSparse::FactorView
factor =
csparse
.factor();
179
MarginalCovarianceCholesky
mcc;
180
mcc.
setCholeskyFactor
(factor.
n
, factor.
p
, factor.
i
, factor.
x
,
181
factor.
pinv
);
182
compute(mcc);
183
}
else
{
184
G2O_WARN
(
"inverse fail (numeric decomposition)"
);
185
}
186
csparse
.freeFactor();
187
G2OBatchStatistics
* globalStats =
G2OBatchStatistics::globalStats
();
188
if
(globalStats) {
189
globalStats->
choleskyNNZ
=
static_cast<
size_t
>
(
csparse
.choleskyNz());
190
}
191
return
ok;
192
}
193
};
194
195
}
// namespace g2o
196
197
#endif
batch_stats.h
g2o::LinearSolverCCS::initMatrixStructure
void initMatrixStructure(const SparseBlockMatrix< MatrixType > &A)
Definition
linear_solver.h:183
g2o::LinearSolverCCS::_ccsMatrix
SparseBlockMatrixCCS< MatrixType > * _ccsMatrix
Definition
linear_solver.h:180
g2o::LinearSolverCCS::LinearSolverCCS
LinearSolverCCS()
Definition
linear_solver.h:152
g2o::LinearSolverCCS::blockOrdering
bool blockOrdering() const
do the AMD ordering on the blocks or on the scalar matrix
Definition
linear_solver.h:176
g2o::LinearSolverCSparse::computeSymbolicDecomposition
void computeSymbolicDecomposition(const SparseBlockMatrix< MatrixType > &A)
Definition
linear_solver_csparse.h:98
g2o::LinearSolverCSparse::LinearSolverCSparse
LinearSolverCSparse(LinearSolverCSparse< MatrixType > const &)=delete
g2o::LinearSolverCSparse::~LinearSolverCSparse
virtual ~LinearSolverCSparse()=default
g2o::LinearSolverCSparse::prepareSolve
void prepareSolve(const SparseBlockMatrix< MatrixType > &A)
Definition
linear_solver_csparse.h:88
g2o::LinearSolverCSparse::init
virtual bool init()
Definition
linear_solver_csparse.h:57
g2o::LinearSolverCSparse::_matrixStructure
MatrixStructure _matrixStructure
Definition
linear_solver_csparse.h:86
g2o::LinearSolverCSparse::solve
bool solve(const SparseBlockMatrix< MatrixType > &A, double *x, double *b)
Definition
linear_solver_csparse.h:62
g2o::LinearSolverCSparse::operator=
LinearSolverCSparse & operator=(LinearSolverCSparse< MatrixType > const &)=delete
g2o::LinearSolverCSparse::fillCSparse
void fillCSparse(const SparseBlockMatrix< MatrixType > &A, bool onlyValues)
Definition
linear_solver_csparse.h:129
g2o::LinearSolverCSparse::solveBlocks_impl
bool solveBlocks_impl(const SparseBlockMatrix< MatrixType > &A, std::function< void(MarginalCovarianceCholesky &)> compute)
Definition
linear_solver_csparse.h:172
g2o::LinearSolverCSparse::csparse
csparse::CSparse csparse
Definition
linear_solver_csparse.h:85
g2o::LinearSolverCSparse::LinearSolverCSparse
LinearSolverCSparse()
Definition
linear_solver_csparse.h:49
g2o::LinearSolver::writeDebug
bool writeDebug() const
write a debug dump of the system matrix if it is not PSD in solve
Definition
linear_solver.h:93
g2o::LinearSolver::blockToScalarPermutation
static void blockToScalarPermutation(const SparseBlockMatrix< MatrixType > &A, const Eigen::MatrixBase< BlockDerived > &p, const Eigen::MatrixBase< ScalarDerived > &scalar)
Definition
linear_solver.h:122
g2o::MarginalCovarianceCholesky
computing the marginal covariance given a cholesky factor (lower triangle of the factor)
Definition
marginal_covariance_cholesky.h:42
g2o::MarginalCovarianceCholesky::setCholeskyFactor
void setCholeskyFactor(int n, int *Lp, int *Li, double *Lx, int *permInv)
Definition
marginal_covariance_cholesky.cpp:51
g2o::MatrixStructure
representing the structure of a matrix in column compressed structure (only the upper triangular part...
Definition
matrix_structure.h:40
g2o::SparseBlockMatrix
Sparse matrix which uses blocks.
Definition
sparse_block_matrix.h:64
g2o::SparseBlockMatrix::nonZeros
size_t nonZeros() const
number of non-zero elements
Definition
sparse_block_matrix.hpp:408
g2o::SparseBlockMatrix::rows
int rows() const
rows of the matrix
Definition
sparse_block_matrix.h:74
g2o::SparseBlockMatrix::cols
int cols() const
columns of the matrix
Definition
sparse_block_matrix.h:70
g2o::SparseBlockMatrix::fillBlockStructure
void fillBlockStructure(MatrixStructure &ms) const
exports the non zero blocks in the structure matrix ms
Definition
sparse_block_matrix.hpp:574
g2o::csparse::CSparse
Definition
csparse_wrapper.h:38
g2o::csparse::CSparse::analyze
bool analyze()
Definition
csparse_wrapper.cpp:160
csparse_wrapper.h
g2o_csparse_api.h
linear_solver.h
G2O_ERROR
#define G2O_ERROR(...)
G2O_WARN
#define G2O_WARN(...)
G2O_DEBUG
#define G2O_DEBUG(...)
logger.h
marginal_covariance_cholesky.h
g2o
Definition
dl_wrapper.cpp:55
g2o::VectorXI
Eigen::Matrix< int, Eigen::Dynamic, 1, Eigen::ColMajor > VectorXI
Definition
eigen_types.h:41
g2o::get_monotonic_time
double get_monotonic_time()
Definition
timeutil.cpp:43
g2o::G2OBatchStatistics
statistics about the optimization
Definition
batch_stats.h:40
g2o::G2OBatchStatistics::globalStats
static G2OBatchStatistics * globalStats()
Definition
batch_stats.h:77
g2o::G2OBatchStatistics::timeNumericDecomposition
double timeNumericDecomposition
numeric decomposition (0 if not done)
Definition
batch_stats.h:58
g2o::G2OBatchStatistics::timeSymbolicDecomposition
double timeSymbolicDecomposition
symbolic decomposition (0 if not done)
Definition
batch_stats.h:57
g2o::G2OBatchStatistics::choleskyNNZ
size_t choleskyNNZ
number of non-zeros in the cholesky factor
Definition
batch_stats.h:75
g2o::csparse::CSparse::FactorView
View onto the cholesky factor.
Definition
csparse_wrapper.h:64
g2o::csparse::CSparse::FactorView::pinv
int *& pinv
Definition
csparse_wrapper.h:69
g2o::csparse::CSparse::FactorView::p
int *& p
Definition
csparse_wrapper.h:66
g2o::csparse::CSparse::FactorView::x
double *& x
Definition
csparse_wrapper.h:68
g2o::csparse::CSparse::FactorView::i
int *& i
Definition
csparse_wrapper.h:67
g2o::csparse::CSparse::FactorView::n
int & n
Definition
csparse_wrapper.h:65
g2o::csparse::CSparse::SparseView
View onto the sparse matrix structure of CSparse using CCS storage.
Definition
csparse_wrapper.h:44
g2o::csparse::CSparse::SparseView::columnsAllocated
int & columnsAllocated
Definition
csparse_wrapper.h:51
g2o::csparse::CSparse::SparseView::x
double *& x
Definition
csparse_wrapper.h:50
g2o::csparse::CSparse::SparseView::i
int *& i
Definition
csparse_wrapper.h:49
g2o::csparse::CSparse::SparseView::p
int *& p
Definition
csparse_wrapper.h:48
g2o::csparse::CSparse::SparseView::nzmax
int & nzmax
Definition
csparse_wrapper.h:47
g2o::csparse::CSparse::SparseView::m
int & m
Definition
csparse_wrapper.h:45
g2o::csparse::CSparse::SparseView::n
int & n
Definition
csparse_wrapper.h:46
timeutil.h
utility functions for handling time related stuff
Generated on
for g2o by
1.17.0