-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssprima.cpp
225 lines (185 loc) · 5.89 KB
/
ssprima.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Illustration of (direct) use of SuiteSparse and SQPR
// based on example from SuiteSparse docs
#include <vector>
#include <tuple>
#include <cassert>
#include <memory>
#include <SuiteSparseQR.hpp>
#include <klu.h>
// Use std::unique_ptr plus a custom deleter to clean up the C-style manual memory management
template<typename Common>
struct ss_deleter {
ss_deleter(Common * cc) : cc_(cc) {}
void operator()(cholmod_triplet *& p) const {
cholmod_l_free_triplet(&p, cc_);
}
void operator()(cholmod_sparse *& p) const {
cholmod_l_free_sparse(&p, cc_);
}
void operator()(cholmod_dense *& p) const {
cholmod_l_free_dense(&p, cc_);
}
void operator()(klu_l_symbolic *& p) const {
klu_l_free_symbolic (&p, cc_);
}
void operator()(klu_l_numeric *& p) const {
klu_l_free_numeric (&p, cc_);
}
private:
Common * cc_;
};
template<typename T, typename Common>
using ss_ptr = std::unique_ptr<T, ss_deleter<Common>>;
template<typename T, typename Common>
ss_ptr<T, typename Common::wrapped_t>
make_ss_ptr( T* p, Common & c ) {
return std::move(ss_ptr<T, typename Common::wrapped_t>(p, c.get()));
}
// take care of calling start and finish cleanly
// also supply deleters as needed
template<typename Common>
struct common_wrapper {
using wrapped_t = Common;
common_wrapper() {}
~common_wrapper() {}
// no copies, no assignments
common_wrapper(common_wrapper const& other) = delete;
common_wrapper & operator=(common_wrapper const&) = delete;
wrapped_t * get() {
return &common_;
}
ss_deleter<Common> deleter() {
return ss_deleter<Common>(&common_);
}
private:
Common common_;
};
template<>
common_wrapper<cholmod_common>::common_wrapper() {
cholmod_l_start(&common_);
}
template<>
common_wrapper<cholmod_common>::~common_wrapper() {
cholmod_l_finish(&common_);
}
template<>
common_wrapper<klu_l_common>::common_wrapper() {
klu_l_defaults(&common_);
}
struct triplet {
int row;
int col;
double value;
};
int main ()
{
using namespace std;
common_wrapper<cholmod_common> ccommon;
// start CHOLMOD
cholmod_common * cc = ccommon.get() ;
vector<triplet> Gentries{
{0, 0, 0.01},
{0, 1, -0.01},
{0, 12, 1},
{1, 0, -0.01},
{1, 1, 0.012},
{1, 2, -0.002},
{2, 1, -0.002},
{2, 2, 0.004},
{2, 3, -0.002},
{3, 2, -0.002},
{3, 3, 0.004},
{3, 4, -0.002},
{4, 3, -0.002},
{4, 4, 0.004},
{4, 5, -0.002},
{5, 4, -0.002},
{5, 5, 0.002},
{6, 6, 0.01},
{6, 7, -0.01},
{6, 13, 1},
{7, 6, -0.01},
{7, 7, 0.012},
{7, 8, -0.002},
{8, 7, -0.002},
{8, 8, 0.004},
{8, 9, -0.002},
{9, 8, -0.002},
{9, 9, 0.004},
{9, 10, -0.002},
{10, 9, -0.002},
{10, 10, 0.004},
{10, 11, -0.002},
{11, 10, -0.002},
{11, 11, 0.002},
{11, 14, 1},
{12, 0, -1},
{13, 6, -1},
{14, 11, -1}
};
vector<triplet> Bentries{
{12, 0, -1},
{13, 1, -1},
{14, 2, -1}};
// now load into Cholmod "triplet matrices"
auto Gct = make_ss_ptr(
cholmod_l_allocate_triplet(
15, 15, Gentries.size(),
0, // stype: both upper and lower are stored
CHOLMOD_REAL,
cc),
ccommon);
for ( size_t i = 0; i < Gentries.size(); ++i) {
reinterpret_cast<long *>(Gct->i)[i] = Gentries[i].row;
reinterpret_cast<long *>(Gct->j)[i] = Gentries[i].col;
reinterpret_cast<double *>(Gct->x)[i] = Gentries[i].value;
}
Gct->nnz = Gentries.size();
// convert triplet matrix to sparse
auto G = make_ss_ptr(
cholmod_l_triplet_to_sparse(Gct.get(), Gentries.size(), cc),
ccommon);
// RHS needs to be dense due to API, so build B that way:
// initialize A (the eventual result) with the contents of B
auto Adense = make_ss_ptr(
cholmod_l_zeros( 15, 3, CHOLMOD_REAL, cc ),
ccommon);
for ( size_t i = 0; i < Bentries.size(); ++i) {
reinterpret_cast<double*>(Adense->x)[15*Bentries[i].col+Bentries[i].row] = Bentries[i].value;
}
// calculate G^-1*B via LU with the KLU package from SuiteSparse
// It claims to be "well suited for circuit simulation", which this is
common_wrapper<klu_l_common> kcommon;
auto KS = make_ss_ptr(klu_l_analyze( 15,
reinterpret_cast<long*>(G->p),
reinterpret_cast<long*>(G->i),
kcommon.get() ),
kcommon);
auto KN = make_ss_ptr(klu_l_factor( reinterpret_cast<long*>(G->p),
reinterpret_cast<long*>(G->i),
reinterpret_cast<double*>(G->x),
KS.get(),
kcommon.get() ),
kcommon);
klu_l_solve ( KS.get(), // Symbolic factorization
KN.get(), // Numeric
15,
3,
reinterpret_cast<double*>(Adense->x),
kcommon.get() );
// convert to cholmod_sparse
auto A = make_ss_ptr(
cholmod_l_dense_to_sparse( Adense.get(), 1, cc ),
ccommon);
// run QR
cholmod_sparse * Q; // outputs
cholmod_sparse * R;
assert( SuiteSparseQR<double> ( SPQR_ORDERING_DEFAULT, SPQR_DEFAULT_TOL, 3, A.get(),
&Q, &R, nullptr, cc ) >= 0);
// print out Q matrix
cholmod_l_write_sparse( stdout, Q , nullptr, nullptr, cc );
// free everything and finish CHOLMOD
cholmod_l_free_sparse (&Q, cc) ;
cholmod_l_free_sparse (&R, cc) ;
return (0) ;
}