16
16
#include < map>
17
17
#include < math.h>
18
18
#include < algorithm>
19
- // #include <Eigen/Sparse>
20
- // #include <Eigen/Dense>
19
+ #include < omp.h>
21
20
#include < limits>
22
21
namespace py = pybind11;
23
- using SparseMatrixR = Eigen::SparseMatrix<double ,Eigen::ColMajor>;
24
-
22
+ using SparseMatrixCol = Eigen::SparseMatrix<double ,Eigen::ColMajor>;
23
+ size_t num_threads = 10 ; // TODO add it as an argument to be set by user
25
24
26
25
class kr_balancing {
27
26
public:
28
27
29
- kr_balancing (const SparseMatrixR & input){
28
+ kr_balancing (const SparseMatrixCol & input){
30
29
std::cout<< " read input" <<std::endl;
31
30
A = input;
32
31
e.resize (A.rows (),1 );
33
32
e.setOnes ();
34
33
/* Replace zeros with 0.00001 on the main diagonal*/
35
- SparseMatrixR I;
34
+ SparseMatrixCol I;
36
35
I.resize (A.rows (),A.cols ());
37
36
I.setIdentity ();
38
37
I = I*0.00001 ;
39
38
A = A + I;
40
39
x0 = e.sparseView ();
41
40
}
42
- size_t outter_loop_count = 0 ;
41
+
43
42
void outter_loop (){
43
+ size_t outter_loop_count = 0 ;
44
44
double stop_tol = tol*0.5 ;
45
45
double eta = etamax;
46
46
x = x0;
47
+ assert (x.isVector ());
47
48
double rt = std::pow (tol,2 );
48
49
v = x.cwiseProduct (A*x);
49
50
rk = Eigen::VectorXd::Constant (v.rows (),1 ) - v; // Vecotr of 1 - v
51
+ assert (rk.isVector ());
50
52
rho_km1 = rk.conjugate ().transpose ()*rk;
51
53
rho_km2 = rho_km1;
52
54
double rout = rho_km1.coeff (0 ,0 );
@@ -59,10 +61,11 @@ class kr_balancing{
59
61
inner_loop ();
60
62
61
63
x=x.cwiseProduct (y);
64
+ assert (x.isVector ());
62
65
63
66
v=x.cwiseProduct (A*x);
64
67
rk = Eigen::VectorXd::Constant (v.rows (),1 ) - v;
65
- rho_km1 = rk.conjugate (). transpose ()*rk;
68
+ rho_km1 = rk.transpose ()*rk;
66
69
rout = rho_km1.coeff (0 ,0 );
67
70
MVP = MVP + k + 1 ;
68
71
// Update inner iteration stopping criterion.
@@ -86,15 +89,15 @@ class kr_balancing{
86
89
if (k == 1 ){
87
90
Z = rk.cwiseQuotient (v);
88
91
p=Z;
89
- rho_km1 = rk.conjugate (). transpose ()*Z;
92
+ rho_km1 = rk.transpose ()*Z;
90
93
}else {
91
94
Eigen::VectorXd beta=rho_km1.cwiseQuotient (rho_km2);
92
95
p = Z + (beta (0 )*p);
93
96
94
97
}
95
98
// Update search direction efficiently.
96
- SparseMatrixR w=x.cwiseProduct (A*(x.cwiseProduct (p))) + v.cwiseProduct (p);
97
- SparseMatrixR alpha_mat = rho_km1.cwiseQuotient (p.conjugate ().transpose ()*w);
99
+ SparseMatrixCol w=x.cwiseProduct (A*(x.cwiseProduct (p))) + v.cwiseProduct (p);
100
+ SparseMatrixCol alpha_mat = rho_km1.cwiseQuotient (p.conjugate ().transpose ()*w);
98
101
double alpha = alpha_mat.coeff (0 ,0 );
99
102
Eigen::VectorXd ap = alpha * p;
100
103
// Test distance to boundary of cone.
@@ -136,11 +139,42 @@ class kr_balancing{
136
139
137
140
}// End of the inner 'while'
138
141
}
139
- const SparseMatrixR* get_output (){
140
- output = A.cwiseProduct (x*x.transpose ());
141
- std::cout << " export output of shape " << output.rows () <<
142
- " by " << output.rows ()<<std::endl;
143
- return &output;
142
+ const SparseMatrixCol* get_output (){
143
+ // std::cout << "export output of shape "<< A.cwiseProduct(x*x.transpose()).rows() <<
144
+ // " by " << A.cwiseProduct(x*x.transpose()).rows()<<std::endl;
145
+ // std::cout << (x*x.transpose()).triangularView<Eigen::Lower>()<<std::endl;
146
+ // SparseMatrixCol D = x*x.transpose();
147
+ // Eigen::SparseTriangularView<Eigen::SparseMatrix<double>, Eigen::Lower> MView = D.triangularView<Eigen::Lower>();
148
+ // std::cout << D.triangularView<Eigen::Lower>() <<std::endl;
149
+ // std::cout << "export output"<<std::endl;
150
+ // output = (A.triangularView<Eigen::Lower>()).cwiseProduct((x*x.transpose()).triangularView<Eigen::Lower>());
151
+ // std::cout << A.cwiseProduct(x*x.transpose()) << std::endl;
152
+ // SparseMatrixCol diag_x;
153
+ // diag_x.resize(x.rows(),x.rows());
154
+ // diag_x = Eigen::MatrixXd(Eigen::MatrixXd(x).asDiagonal()).sparseView();
155
+ // diag_x.makeCompressed();
156
+ // std::cout << "diag x size " << diag_x.rows() << " x " << diag_x.cols()<<std::endl;
157
+
158
+ // Eigen::MatrixXd dense_mat_x = dense_x.replicate(1,x.rows());
159
+ // Eigen::MatrixXd dense_mat_x = dense_x*dense_x.transpose();
160
+ // std::cout << diag_x <<std::endl;
161
+ // SparseMatrixCol test = dense_x.sparseView();
162
+
163
+ // std::cout << test <<std::endl;
164
+ // SparseMatrixCol temp = (dense_x.asDiagonal()*A).sparseView();
165
+ // SparseMatrixCol temp1 = temp.cwiseProduct(x);
166
+ // std::cout << "temp" <<std::endl;
167
+ assert (A.rows () == A.cols ());
168
+ A = SparseMatrixCol (A.triangularView <Eigen::Lower>());
169
+ #pragma omp parallel for num_threads(num_threads) schedule(dynamic)
170
+ for (int k=0 ; k<A.outerSize (); ++k){
171
+ for (SparseMatrixCol::InnerIterator it (A,k); it; ++it){
172
+ it.valueRef () = it.value ()*x.coeff (it.row (),0 )*x.coeff (it.col (),0 );
173
+
174
+ }
175
+ }
176
+
177
+ return &A;
144
178
}
145
179
private:
146
180
std::vector<double > res;
@@ -150,28 +184,28 @@ class kr_balancing{
150
184
double tol = 1e-6 ;
151
185
double g = 0.9 ;
152
186
double etamax = 0.1 ;
153
- SparseMatrixR x0;
187
+ SparseMatrixCol x0;
154
188
Eigen::MatrixXd e;
155
- SparseMatrixR A;
156
- SparseMatrixR rho_km1;
157
- SparseMatrixR rho_km2;
189
+ SparseMatrixCol A;
190
+ SparseMatrixCol rho_km1;
191
+ SparseMatrixCol rho_km2;
158
192
unsigned int k;
159
193
Eigen::VectorXd y;
160
- SparseMatrixR p;
161
- SparseMatrixR Z;
194
+ SparseMatrixCol p;
195
+ SparseMatrixCol Z;
162
196
double innertol;
163
197
unsigned int i = 0 ; // Outer itteration count
164
198
unsigned int MVP = 0 ;
165
- SparseMatrixR v;
166
- SparseMatrixR x;
199
+ SparseMatrixCol v;
200
+ SparseMatrixCol x;
167
201
Eigen::SparseVector<double > rk;
168
- SparseMatrixR output;
202
+ SparseMatrixCol output;
169
203
};
170
204
171
205
172
206
PYBIND11_MODULE (KRBalancing, m) {
173
207
py::class_<kr_balancing>(m, " kr_balancing" )
174
- .def (py::init< const SparseMatrixR & >())
208
+ .def (py::init< const SparseMatrixCol & >())
175
209
.def (" outter_loop" , &kr_balancing::outter_loop)
176
210
.def (" get_output" ,&kr_balancing::get_output, py::return_value_policy::reference_internal);
177
211
0 commit comments