Skip to content

Commit 1ce4848

Browse files
author
Leily Rabbani
committed
x*xt is done via for loop
1 parent 134e381 commit 1ce4848

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def get_include(): #TODO
2121
# Path to eigen3 headers
2222
get_include(),
2323
],
24+
extra_compile_args=['-fopenmp'],
2425
language='c++'
2526
),
2627
]
@@ -69,7 +70,7 @@ def build_extensions(self):
6970
ct = self.compiler.compiler_type
7071
opts = self.c_opts.get(ct, [])
7172
if ct == 'unix':
72-
opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
73+
opts.append('-DVERSION_INFO="%s" -fopenmp' % self.distribution.get_version())
7374
opts.append(cpp_flag(self.compiler))
7475
if has_flag(self.compiler, '-fvisibility=hidden'):
7576
opts.append('-fvisibility=hidden')

src/KRBalancing.cpp

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,39 @@
1616
#include <map>
1717
#include <math.h>
1818
#include <algorithm>
19-
//#include <Eigen/Sparse>
20-
//#include <Eigen/Dense>
19+
#include <omp.h>
2120
#include <limits>
2221
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
2524

2625
class kr_balancing{
2726
public:
2827

29-
kr_balancing(const SparseMatrixR & input){
28+
kr_balancing(const SparseMatrixCol & input){
3029
std::cout<< "read input"<<std::endl;
3130
A = input;
3231
e.resize(A.rows(),1);
3332
e.setOnes();
3433
/*Replace zeros with 0.00001 on the main diagonal*/
35-
SparseMatrixR I;
34+
SparseMatrixCol I;
3635
I.resize(A.rows(),A.cols());
3736
I.setIdentity();
3837
I = I*0.00001;
3938
A = A + I;
4039
x0 = e.sparseView();
4140
}
42-
size_t outter_loop_count = 0;
41+
4342
void outter_loop(){
43+
size_t outter_loop_count = 0;
4444
double stop_tol = tol*0.5;
4545
double eta = etamax;
4646
x = x0;
47+
assert(x.isVector());
4748
double rt = std::pow(tol,2);
4849
v = x.cwiseProduct(A*x);
4950
rk = Eigen::VectorXd::Constant(v.rows(),1) - v; //Vecotr of 1 - v
51+
assert(rk.isVector());
5052
rho_km1 = rk.conjugate().transpose()*rk;
5153
rho_km2 = rho_km1;
5254
double rout = rho_km1.coeff(0,0);
@@ -59,10 +61,11 @@ class kr_balancing{
5961
inner_loop();
6062

6163
x=x.cwiseProduct(y);
64+
assert(x.isVector());
6265

6366
v=x.cwiseProduct(A*x);
6467
rk = Eigen::VectorXd::Constant(v.rows(),1) - v;
65-
rho_km1 = rk.conjugate().transpose()*rk;
68+
rho_km1 = rk.transpose()*rk;
6669
rout = rho_km1.coeff(0,0);
6770
MVP = MVP + k + 1;
6871
//Update inner iteration stopping criterion.
@@ -86,15 +89,15 @@ class kr_balancing{
8689
if(k == 1){
8790
Z = rk.cwiseQuotient(v);
8891
p=Z;
89-
rho_km1 = rk.conjugate().transpose()*Z;
92+
rho_km1 = rk.transpose()*Z;
9093
}else{
9194
Eigen::VectorXd beta=rho_km1.cwiseQuotient(rho_km2);
9295
p = Z + (beta(0)*p);
9396

9497
}
9598
//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);
98101
double alpha = alpha_mat.coeff(0,0);
99102
Eigen::VectorXd ap = alpha * p;
100103
//Test distance to boundary of cone.
@@ -136,11 +139,42 @@ class kr_balancing{
136139

137140
}//End of the inner 'while'
138141
}
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;
144178
}
145179
private:
146180
std::vector<double> res;
@@ -150,28 +184,28 @@ class kr_balancing{
150184
double tol = 1e-6;
151185
double g = 0.9;
152186
double etamax = 0.1;
153-
SparseMatrixR x0;
187+
SparseMatrixCol x0;
154188
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;
158192
unsigned int k;
159193
Eigen::VectorXd y;
160-
SparseMatrixR p;
161-
SparseMatrixR Z;
194+
SparseMatrixCol p;
195+
SparseMatrixCol Z;
162196
double innertol;
163197
unsigned int i = 0; //Outer itteration count
164198
unsigned int MVP = 0;
165-
SparseMatrixR v;
166-
SparseMatrixR x;
199+
SparseMatrixCol v;
200+
SparseMatrixCol x;
167201
Eigen::SparseVector<double> rk;
168-
SparseMatrixR output;
202+
SparseMatrixCol output;
169203
};
170204

171205

172206
PYBIND11_MODULE(KRBalancing, m) {
173207
py::class_<kr_balancing>(m, "kr_balancing")
174-
.def(py::init< const SparseMatrixR & >())
208+
.def(py::init< const SparseMatrixCol & >())
175209
.def("outter_loop", &kr_balancing::outter_loop)
176210
.def("get_output",&kr_balancing::get_output, py::return_value_policy::reference_internal);
177211

0 commit comments

Comments
 (0)