Skip to content

Commit 355d495

Browse files
authored
Develop (#12)
* print the system platform * print out the platform * changed darwin to Darwin * remove extra_link_args for mac * Test the robustness of result (#11) * Fixed the bug with omp * fixed rescaling * typo * removed a print * Removed a print from setup.py
1 parent 3dcd84b commit 355d495

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def get_include(): # TODO
1717

1818
def __extra_compile_args():
1919
extra_compile_args = []
20-
print("Platform is "+ platform.system())
2120

2221
if platform.system() == 'Darwin':
2322
extra_compile_args = ["-std=c++11"]

src/krbalancing.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ kr_balancing::kr_balancing(const SparseMatrixCol & input){
1010
I.setIdentity();
1111
I = I*0.00001;
1212
A = A + I;
13+
rescaled = false;
1314
}
1415

1516

@@ -130,12 +131,15 @@ void kr_balancing::inner_loop(){
130131

131132
void kr_balancing::compute_normalised_matrix(bool & rescale){
132133
assert(A.rows() == A.cols());
133-
A = SparseMatrixCol(A.triangularView<Eigen::Upper>());
134-
if(rescale ==true){
134+
if(rescale ==true && rescaled == false){
135135
rescale_norm_vector();
136+
rescaled = true;
137+
}else{
138+
A = SparseMatrixCol(A.triangularView<Eigen::Upper>());
136139
}
137140
#pragma omp parallel for num_threads(num_threads) schedule(dynamic)
138141
for (int k=0; k<A.outerSize(); ++k){
142+
#pragma omp critical
139143
for(SparseMatrixCol::InnerIterator it(A,k); it; ++it){
140144
it.valueRef() = it.value()*x.coeff(it.row(),0)*x.coeff(it.col(),0);
141145
}
@@ -145,10 +149,14 @@ void kr_balancing::compute_normalised_matrix(bool & rescale){
145149

146150
//Rescaling the normalisation factor (x)
147151
void kr_balancing::rescale_norm_vector(){
148-
double original_sum = 0;
149-
double norm_vector_sum = 0;
150-
#pragma omp parallel for num_threads(num_threads) schedule(dynamic)
152+
double original_sum = 0.0;
153+
double norm_vector_sum = 0.0;
154+
assert(A.rows() == A.cols());
155+
A = SparseMatrixCol(A.triangularView<Eigen::Upper>());
156+
157+
#pragma omp parallel for num_threads(num_threads)
151158
for (int k=0; k<A.outerSize(); ++k){
159+
#pragma omp critical
152160
for(SparseMatrixCol::InnerIterator it(A,k); it; ++it){
153161
if(it.row()==it.col()){
154162
original_sum += it.value();
@@ -159,6 +167,7 @@ void kr_balancing::rescale_norm_vector(){
159167
}
160168
}
161169
}
170+
162171
std::cout << "normalisation factor is "<< std::sqrt(norm_vector_sum/original_sum) <<std::endl;
163172
x /= std::sqrt(norm_vector_sum/original_sum);
164173

@@ -172,8 +181,9 @@ const SparseMatrixCol* kr_balancing::get_normalised_matrix(bool & rescale){
172181

173182

174183
const SparseMatrixCol* kr_balancing::get_normalisation_vector(bool & rescale){
175-
if(rescale ==true){
184+
if(rescale ==true && rescaled == false){
176185
rescale_norm_vector();
186+
rescaled = true;
177187
}
178188
return &x;
179189
}

src/krbalancing.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class kr_balancing{
5959
SparseMatrixCol x;
6060
Eigen::SparseVector<double> rk;
6161
SparseMatrixCol output;
62+
bool rescaled;
6263
};
6364

6465
#endif

0 commit comments

Comments
 (0)