g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
solvers
csparse
csparse_wrapper.cpp
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
#include "
csparse_wrapper.h
"
28
29
#include <cassert>
30
#include <cstddef>
31
#include <cstring>
32
#include <memory>
33
34
#include "
csparse_extension.h
"
35
#include "
csparse_helper.h
"
36
#include "
g2o/core/eigen_types.h
"
37
38
namespace
g2o
{
39
namespace
csparse
{
40
44
struct
CSparseExt
:
public
cs {
45
CSparseExt
() {
46
nzmax = 0;
47
m = 0;
48
n = 0;
49
p = 0;
50
i = 0;
51
x = 0;
52
nz = -1;
// tag as CCS formatted matrix
53
}
54
CSparseExt
(
CSparseExt
const
&) =
delete
;
55
CSparseExt
&
operator=
(
CSparseExt
const
&) =
delete
;
56
~CSparseExt
() {
57
delete
[] p;
58
delete
[] i;
59
delete
[] x;
60
}
61
int
columnsAllocated
= 0;
62
};
63
67
class
CSparse::Impl
{
68
public
:
69
Impl
() =
default
;
70
~Impl
() {
71
if
(
symbolicDecomposition
) {
72
cs_sfree(
symbolicDecomposition
);
73
symbolicDecomposition
=
nullptr
;
74
}
75
if
(
numericCholesky
) {
76
cs_nfree(
numericCholesky
);
77
numericCholesky
=
nullptr
;
78
}
79
delete
[]
csWorkspace
;
80
csWorkspace
=
nullptr
;
81
delete
[]
csIntWorkspace
;
82
csIntWorkspace
=
nullptr
;
83
}
84
85
void
prepareWorkspace
() {
86
// re-allocate the temporary workspace for cholesky
87
if
(
csWorkspaceSize
<
ccsA
.n) {
88
csWorkspaceSize
=
csWorkspaceSize
== 0 ?
ccsA
.n : 2 *
ccsA
.n;
89
delete
[]
csWorkspace
;
90
csWorkspace
=
new
double
[
csWorkspaceSize
];
91
delete
[]
csIntWorkspace
;
92
csIntWorkspace
=
new
int
[2 *
csWorkspaceSize
];
93
}
94
}
95
96
css*
symbolicDecomposition
=
nullptr
;
97
int
csWorkspaceSize
= 0;
98
double
*
csWorkspace
=
nullptr
;
99
int
*
csIntWorkspace
=
nullptr
;
100
csn*
numericCholesky
=
nullptr
;
101
CSparseExt
ccsA
;
102
};
103
104
CSparse::CSparse
() :
pImpl
(
std
::make_unique<
Impl
>()) {}
105
106
CSparse::~CSparse
() =
default
;
107
108
void
CSparse::freeFactor
() {
109
if
(
pImpl
->numericCholesky) {
110
cs_nfree(
pImpl
->numericCholesky);
111
pImpl
->numericCholesky =
nullptr
;
112
}
113
}
114
115
bool
CSparse::hasFactor
()
const
{
return
pImpl
->numericCholesky !=
nullptr
; }
116
117
bool
CSparse::amd
(
const
SparseView
&
sparseView
,
VectorXI
& result) {
118
// prepare block structure for the CSparse call
119
cs auxBlock;
120
auxBlock.nzmax =
sparseView
.nzmax;
121
auxBlock.m =
sparseView
.m;
122
auxBlock.n =
sparseView
.n;
123
auxBlock.p =
sparseView
.p;
124
auxBlock.i =
sparseView
.i;
125
auxBlock.x = NULL;
// no values
126
auxBlock.nz = -1;
// CCS format
127
128
// AMD ordering on the block structure
129
int
* permutation = cs_amd(1, &auxBlock);
130
if
(!permutation)
return
false
;
131
result.resize(auxBlock.m);
132
VectorXI::ConstMapType permutation_map(permutation, result.size());
133
result = permutation_map;
134
cs_free(permutation);
135
return
true
;
136
}
137
138
CSparse::SparseView
CSparse::sparseView
() {
139
CSparseExt
& sparse =
pImpl
->ccsA;
140
return
CSparse::SparseView
(sparse.m, sparse.n, sparse.nzmax, sparse.p,
141
sparse.i, sparse.x, sparse.
columnsAllocated
);
142
}
143
144
CSparse::FactorView
CSparse::factor
() {
145
csn*
factor
=
pImpl
->numericCholesky;
146
return
CSparse::FactorView
(
factor
->L->n,
factor
->L->p,
factor
->L->i,
147
factor
->L->x,
pImpl
->symbolicDecomposition->pinv);
148
}
149
150
bool
CSparse::solve
(
double
* x,
double
* b)
const
{
151
pImpl
->prepareWorkspace();
152
153
if
(x != b) memcpy(x, b,
pImpl
->ccsA.n *
sizeof
(
double
));
154
int
ok =
csparse_extension::cs_cholsolsymb
(
155
&
pImpl
->ccsA, x,
pImpl
->symbolicDecomposition,
pImpl
->csWorkspace,
156
pImpl
->csIntWorkspace);
157
return
static_cast<
bool
>
(ok);
158
}
159
160
bool
CSparse::analyze
() {
161
freeSymbolic
();
162
pImpl
->symbolicDecomposition = cs_schol(1, &
pImpl
->ccsA);
163
return
pImpl
->symbolicDecomposition !=
nullptr
;
164
}
165
166
bool
CSparse::analyze_p
(
int
* permutation) {
167
freeSymbolic
();
168
pImpl
->symbolicDecomposition =
static_cast<
css*
>
(cs_calloc(1,
sizeof
(css)));
169
int
n =
pImpl
->ccsA.n;
170
pImpl
->symbolicDecomposition->pinv = cs_pinv(permutation, n);
171
cs* C = cs_symperm(&
pImpl
->ccsA,
pImpl
->symbolicDecomposition->pinv, 0);
172
pImpl
->symbolicDecomposition->parent = cs_etree(C, 0);
173
int
* post = cs_post(
pImpl
->symbolicDecomposition->parent, n);
174
int
* c = cs_counts(C,
pImpl
->symbolicDecomposition->parent, post, 0);
175
cs_free(post);
176
cs_spfree(C);
177
pImpl
->symbolicDecomposition->cp =
178
static_cast<
int
*
>
(cs_malloc(n + 1,
sizeof
(
int
)));
179
pImpl
->symbolicDecomposition->unz =
pImpl
->symbolicDecomposition->lnz =
180
cs_cumsum(
pImpl
->symbolicDecomposition->cp, c, n);
181
cs_free(c);
182
if
(
pImpl
->symbolicDecomposition->lnz < 0) {
183
cs_sfree(
pImpl
->symbolicDecomposition);
184
pImpl
->symbolicDecomposition =
nullptr
;
185
}
186
return
pImpl
->symbolicDecomposition !=
nullptr
;
187
}
188
189
int
CSparse::choleskyNz
()
const
{
190
if
(
pImpl
->symbolicDecomposition)
return
pImpl
->symbolicDecomposition->lnz;
191
return
-1;
192
}
193
194
bool
CSparse::factorize
() {
195
pImpl
->prepareWorkspace();
196
freeFactor
();
197
pImpl
->numericCholesky =
csparse_extension::cs_chol_workspace
(
198
&
pImpl
->ccsA,
pImpl
->symbolicDecomposition,
pImpl
->csIntWorkspace,
199
pImpl
->csWorkspace);
200
201
return
pImpl
->numericCholesky !=
nullptr
;
202
}
203
204
bool
CSparse::hasSymbolic
()
const
{
205
return
pImpl
->symbolicDecomposition !=
nullptr
;
206
}
207
208
void
CSparse::freeSymbolic
() {
209
if
(
pImpl
->symbolicDecomposition !=
nullptr
) {
210
cs_sfree(
pImpl
->symbolicDecomposition);
211
pImpl
->symbolicDecomposition =
nullptr
;
212
}
213
}
214
215
bool
CSparse::writeSparse
(
const
std::string& filename)
const
{
216
return
csparse_extension::writeCs2Octave
(filename.c_str(), &
pImpl
->ccsA,
217
true
);
218
}
219
220
}
// namespace csparse
221
}
// namespace g2o
g2o::csparse::CSparse::Impl
Definition
csparse_wrapper.cpp:67
g2o::csparse::CSparse::Impl::csWorkspace
double * csWorkspace
Definition
csparse_wrapper.cpp:98
g2o::csparse::CSparse::Impl::csWorkspaceSize
int csWorkspaceSize
Definition
csparse_wrapper.cpp:97
g2o::csparse::CSparse::Impl::Impl
Impl()=default
g2o::csparse::CSparse::Impl::csIntWorkspace
int * csIntWorkspace
Definition
csparse_wrapper.cpp:99
g2o::csparse::CSparse::Impl::symbolicDecomposition
css * symbolicDecomposition
Definition
csparse_wrapper.cpp:96
g2o::csparse::CSparse::Impl::ccsA
CSparseExt ccsA
Definition
csparse_wrapper.cpp:101
g2o::csparse::CSparse::Impl::numericCholesky
csn * numericCholesky
Definition
csparse_wrapper.cpp:100
g2o::csparse::CSparse::Impl::~Impl
~Impl()
Definition
csparse_wrapper.cpp:70
g2o::csparse::CSparse::Impl::prepareWorkspace
void prepareWorkspace()
Definition
csparse_wrapper.cpp:85
g2o::csparse::CSparse::freeSymbolic
void freeSymbolic()
Definition
csparse_wrapper.cpp:208
g2o::csparse::CSparse::hasFactor
bool hasFactor() const
Definition
csparse_wrapper.cpp:115
g2o::csparse::CSparse::amd
bool amd(const SparseView &sparseView, VectorXI &result)
compute AMD ordering on the given SparseView, store into result
Definition
csparse_wrapper.cpp:117
g2o::csparse::CSparse::freeFactor
void freeFactor()
Definition
csparse_wrapper.cpp:108
g2o::csparse::CSparse::solve
bool solve(double *x, double *b) const
Definition
csparse_wrapper.cpp:150
g2o::csparse::CSparse::~CSparse
~CSparse()
g2o::csparse::CSparse::CSparse
CSparse()
Definition
csparse_wrapper.cpp:104
g2o::csparse::CSparse::analyze
bool analyze()
Definition
csparse_wrapper.cpp:160
g2o::csparse::CSparse::hasSymbolic
bool hasSymbolic() const
Definition
csparse_wrapper.cpp:204
g2o::csparse::CSparse::factor
FactorView factor()
Definition
csparse_wrapper.cpp:144
g2o::csparse::CSparse::pImpl
std::unique_ptr< Impl > pImpl
Definition
csparse_wrapper.h:97
g2o::csparse::CSparse::sparseView
SparseView sparseView()
Definition
csparse_wrapper.cpp:138
g2o::csparse::CSparse::analyze_p
bool analyze_p(int *permutation)
Definition
csparse_wrapper.cpp:166
g2o::csparse::CSparse::writeSparse
bool writeSparse(const std::string &filename) const
Definition
csparse_wrapper.cpp:215
g2o::csparse::CSparse::factorize
bool factorize()
Definition
csparse_wrapper.cpp:194
g2o::csparse::CSparse::choleskyNz
int choleskyNz() const
Definition
csparse_wrapper.cpp:189
csparse_extension.h
csparse_helper.h
csparse_wrapper.h
eigen_types.h
g2o::csparse_extension::cs_chol_workspace
csn * cs_chol_workspace(const cs *A, const css *S, int *cin, double *xin)
Definition
csparse_extension.cpp:67
g2o::csparse_extension::writeCs2Octave
bool writeCs2Octave(const char *filename, const cs *A, bool upperTriangular)
Definition
csparse_helper.cpp:36
g2o::csparse_extension::cs_cholsolsymb
int cs_cholsolsymb(const cs *A, double *b, const css *S, double *x, int *work)
Definition
csparse_extension.cpp:38
g2o::csparse
Definition
csparse_wrapper.cpp:39
g2o
Definition
dl_wrapper.cpp:55
g2o::VectorXI
Eigen::Matrix< int, Eigen::Dynamic, 1, Eigen::ColMajor > VectorXI
Definition
eigen_types.h:41
std
Definition
jet.h:876
g2o::csparse::CSparseExt
Our C++ version of the csparse struct.
Definition
csparse_wrapper.cpp:44
g2o::csparse::CSparseExt::CSparseExt
CSparseExt(CSparseExt const &)=delete
g2o::csparse::CSparseExt::~CSparseExt
~CSparseExt()
Definition
csparse_wrapper.cpp:56
g2o::csparse::CSparseExt::CSparseExt
CSparseExt()
Definition
csparse_wrapper.cpp:45
g2o::csparse::CSparseExt::operator=
CSparseExt & operator=(CSparseExt const &)=delete
g2o::csparse::CSparseExt::columnsAllocated
int columnsAllocated
Definition
csparse_wrapper.cpp:61
g2o::csparse::CSparse::FactorView
View onto the cholesky factor.
Definition
csparse_wrapper.h:64
g2o::csparse::CSparse::SparseView
View onto the sparse matrix structure of CSparse using CCS storage.
Definition
csparse_wrapper.h:44
Generated on
for g2o by
1.17.0