@@ -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
131132void 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)
147151void 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
174183const 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}
0 commit comments