g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
solvers
cholmod
cholmod_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 "
cholmod_wrapper.h
"
28
29
#include <cholmod.h>
30
31
#include <cassert>
32
#include <cstring>
33
#include <memory>
34
35
#include "
cholmod_ext.h
"
36
37
namespace
g2o
{
38
namespace
cholmod
{
39
40
class
Cholmod::Impl
{
41
public
:
42
Impl
() {
43
cholmod_start(&
cholmodCommon
);
44
45
// setup ordering strategy
46
cholmodCommon
.nmethods = 1;
47
cholmodCommon
.method[0].ordering = CHOLMOD_AMD;
// CHOLMOD_COLAMD
48
//_cholmodCommon.postorder = 0;
49
50
cholmodCommon
.supernodal =
51
CHOLMOD_AUTO;
// CHOLMOD_SUPERNODAL; //CHOLMOD_SIMPLICIAL;
52
}
53
~Impl
() {
54
freeFactor
();
55
cholmod_finish(&
cholmodCommon
);
56
}
57
58
void
freeFactor
() {
59
if
(
cholmodFactor
!=
nullptr
) {
60
cholmod_free_factor(&
cholmodFactor
, &
cholmodCommon
);
61
cholmodFactor
=
nullptr
;
62
}
63
}
64
65
cholmod_common
cholmodCommon
;
66
CholmodExt
cholmodSparse
;
67
cholmod_factor*
cholmodFactor
=
nullptr
;
68
};
69
70
Cholmod::Cholmod
() :
pImpl
(
std
::make_unique<
Impl
>()) {}
71
72
Cholmod::~Cholmod
() =
default
;
73
74
void
Cholmod::freeFactor
() {
pImpl
->freeFactor(); }
75
76
bool
Cholmod::hasFactor
()
const
{
return
pImpl
->cholmodFactor !=
nullptr
; }
77
78
bool
Cholmod::amd
(
SparseView
&
sparseView
,
int
* result) {
79
cholmod_sparse auxCholmodSparse;
80
auxCholmodSparse.nzmax =
sparseView
.nzmax;
81
auxCholmodSparse.nrow =
sparseView
.nrow;
82
auxCholmodSparse.ncol =
sparseView
.ncol;
83
auxCholmodSparse.p =
sparseView
.p;
84
auxCholmodSparse.i =
sparseView
.i;
85
auxCholmodSparse.nz = 0;
86
auxCholmodSparse.x = 0;
87
auxCholmodSparse.z = 0;
88
auxCholmodSparse.stype = 1;
89
auxCholmodSparse.xtype = CHOLMOD_PATTERN;
90
auxCholmodSparse.itype = CHOLMOD_INT;
91
auxCholmodSparse.dtype = CHOLMOD_DOUBLE;
92
auxCholmodSparse.sorted = 1;
93
auxCholmodSparse.packed = 1;
94
95
int
amdStatus =
96
cholmod_amd(&auxCholmodSparse, NULL, 0, result, &
pImpl
->cholmodCommon);
97
return
amdStatus != 0;
98
}
99
100
Cholmod::SparseView
Cholmod::sparseView
() {
101
CholmodExt
& sparse =
pImpl
->cholmodSparse;
102
return
Cholmod::SparseView
(
103
sparse.nrow, sparse.ncol, sparse.nzmax,
104
*
reinterpret_cast<
int
**
>
(&sparse.p), *
reinterpret_cast<
int
**
>
(&sparse.i),
105
*
reinterpret_cast<
double
**
>
(&sparse.x), sparse.
columnsAllocated
);
106
}
107
108
Cholmod::FactorView
Cholmod::factor
() {
109
cholmod_factor&
factor
= *
pImpl
->cholmodFactor;
110
return
Cholmod::FactorView
(
factor
.n, *
reinterpret_cast<
int
**
>
(&
factor
.p),
111
*
reinterpret_cast<
int
**
>
(&
factor
.i),
112
*
reinterpret_cast<
double
**
>
(&
factor
.x),
113
*
reinterpret_cast<
int
**
>
(&
factor
.Perm));
114
}
115
116
void
Cholmod::solve
(
double
* x,
double
* b)
const
{
117
// setting up b for calling cholmod
118
cholmod_dense bcholmod;
119
bcholmod.nrow = bcholmod.d =
pImpl
->cholmodSparse.nrow;
120
bcholmod.ncol = 1;
121
bcholmod.x = b;
122
bcholmod.xtype = CHOLMOD_REAL;
123
bcholmod.dtype = CHOLMOD_DOUBLE;
124
cholmod_dense* xcholmod = cholmod_solve(CHOLMOD_A,
pImpl
->cholmodFactor,
125
&bcholmod, &
pImpl
->cholmodCommon);
126
std::memcpy(x, xcholmod->x,
127
sizeof
(
double
) * bcholmod.nrow);
// copy back to our array
128
cholmod_free_dense(&xcholmod, &
pImpl
->cholmodCommon);
129
}
130
131
bool
Cholmod::analyze
() {
132
// setup ordering strategy
133
pImpl
->cholmodCommon.nmethods = 1;
134
pImpl
->cholmodCommon.method[0].ordering = CHOLMOD_AMD;
// CHOLMOD_COLAMD
135
pImpl
->cholmodFactor =
136
cholmod_analyze(&
pImpl
->cholmodSparse,
137
&
pImpl
->cholmodCommon);
// symbolic factorization
138
return
true
;
139
}
140
141
bool
Cholmod::analyze_p
(
int
* permutation) {
142
pImpl
->cholmodCommon.nmethods = 1;
143
pImpl
->cholmodCommon.method[0].ordering = CHOLMOD_GIVEN;
144
pImpl
->cholmodFactor = cholmod_analyze_p(&
pImpl
->cholmodSparse, permutation,
145
NULL, 0, &
pImpl
->cholmodCommon);
146
return
true
;
147
}
148
149
int
Cholmod::choleskyNz
()
const
{
150
return
static_cast<
int
>
(
pImpl
->cholmodCommon.method[0].lnz);
151
}
152
153
bool
Cholmod::factorize
() {
154
cholmod_factorize(&
pImpl
->cholmodSparse,
pImpl
->cholmodFactor,
155
&
pImpl
->cholmodCommon);
156
return
pImpl
->cholmodCommon.status == CHOLMOD_OK;
157
}
158
159
bool
Cholmod::simplifyFactor
() {
160
// convert the factorization to LL, simplical, packed, monotonic
161
int
change_status = cholmod_change_factor(
162
CHOLMOD_REAL, 1, 0, 1, 1,
pImpl
->cholmodFactor, &
pImpl
->cholmodCommon);
163
assert(
pImpl
->cholmodFactor->is_ll && !
pImpl
->cholmodFactor->is_super &&
164
pImpl
->cholmodFactor->is_monotonic &&
165
"Cholesky factor has wrong format"
);
166
return
change_status != 0;
167
}
168
169
}
// namespace cholmod
170
}
// namespace g2o
cholmod_ext.h
cholmod_wrapper.h
g2o::cholmod::Cholmod::Impl
Definition
cholmod_wrapper.cpp:40
g2o::cholmod::Cholmod::Impl::cholmodSparse
CholmodExt cholmodSparse
Definition
cholmod_wrapper.cpp:66
g2o::cholmod::Cholmod::Impl::~Impl
~Impl()
Definition
cholmod_wrapper.cpp:53
g2o::cholmod::Cholmod::Impl::freeFactor
void freeFactor()
Definition
cholmod_wrapper.cpp:58
g2o::cholmod::Cholmod::Impl::Impl
Impl()
Definition
cholmod_wrapper.cpp:42
g2o::cholmod::Cholmod::Impl::cholmodFactor
cholmod_factor * cholmodFactor
Definition
cholmod_wrapper.cpp:67
g2o::cholmod::Cholmod::Impl::cholmodCommon
cholmod_common cholmodCommon
Definition
cholmod_wrapper.cpp:65
g2o::cholmod::Cholmod::sparseView
SparseView sparseView()
Definition
cholmod_wrapper.cpp:100
g2o::cholmod::Cholmod::amd
bool amd(SparseView &sparseView, int *result)
compute AMD ordering on the given SparseView, store into result
Definition
cholmod_wrapper.cpp:78
g2o::cholmod::Cholmod::factorize
bool factorize()
Definition
cholmod_wrapper.cpp:153
g2o::cholmod::Cholmod::factor
FactorView factor()
Definition
cholmod_wrapper.cpp:108
g2o::cholmod::Cholmod::analyze
bool analyze()
Definition
cholmod_wrapper.cpp:131
g2o::cholmod::Cholmod::~Cholmod
~Cholmod()
g2o::cholmod::Cholmod::analyze_p
bool analyze_p(int *permutation)
Definition
cholmod_wrapper.cpp:141
g2o::cholmod::Cholmod::simplifyFactor
bool simplifyFactor()
Definition
cholmod_wrapper.cpp:159
g2o::cholmod::Cholmod::Cholmod
Cholmod()
Definition
cholmod_wrapper.cpp:70
g2o::cholmod::Cholmod::solve
void solve(double *x, double *b) const
Definition
cholmod_wrapper.cpp:116
g2o::cholmod::Cholmod::pImpl
std::unique_ptr< Impl > pImpl
Definition
cholmod_wrapper.h:92
g2o::cholmod::Cholmod::freeFactor
void freeFactor()
Definition
cholmod_wrapper.cpp:74
g2o::cholmod::Cholmod::hasFactor
bool hasFactor() const
Definition
cholmod_wrapper.cpp:76
g2o::cholmod::Cholmod::choleskyNz
int choleskyNz() const
Definition
cholmod_wrapper.cpp:149
g2o::cholmod
Definition
graph_optimizer_sparse_incremental.h:27
g2o
Definition
dl_wrapper.cpp:55
std
Definition
jet.h:876
g2o::cholmod::CholmodExt
Our extension of the CHOLMOD matrix struct.
Definition
cholmod_ext.h:38
g2o::cholmod::CholmodExt::columnsAllocated
size_t columnsAllocated
Definition
cholmod_ext.h:63
g2o::cholmod::Cholmod::FactorView
View onto the cholesky factor.
Definition
cholmod_wrapper.h:62
g2o::cholmod::Cholmod::SparseView
View onto the sparse matrix structure of Cholmod using CCS storage.
Definition
cholmod_wrapper.h:42
Generated on
for g2o by
1.17.0