Skip to content

Commit

Permalink
change sparse matrix plus, minus, multiply, divide by removing dynami…
Browse files Browse the repository at this point in the history
…c memory allocation.
  • Loading branch information
changgang committed Mar 10, 2024
1 parent 9d19d26 commit 81d5ab4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 45 deletions.
14 changes: 7 additions & 7 deletions code/steps/STEPS.depend
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"header/basic/sparse_matrix_csparse.h"
"header/basic/complex_sparse_matrix_csparse.h"

1710075086 d:\steps\code\steps\header\basic\sparse_matrix_csparse.h
1710076670 d:\steps\code\steps\header\basic\sparse_matrix_csparse.h
"cs.h"
<vector>
<ctime>
Expand Down Expand Up @@ -3211,7 +3211,7 @@
<iostream>
<fstream>

1710075111 source:d:\steps\code\steps\source\basic\sparse_matrix_csparse.cpp
1710076780 source:d:\steps\code\steps\source\basic\sparse_matrix_csparse.cpp
"header/basic/sparse_matrix_csparse.h"
"header/basic/constants.h"
"header/basic/utility.h"
Expand Down Expand Up @@ -3478,7 +3478,7 @@

1574318720 d:\steps\code\thirdparty\suitesparse\umfpack\include\umfpack_global.h

1664187911 source:d:\steps\code\steps\source\basic\sparse_matrix_test.cpp
1710076869 source:d:\steps\code\steps\source\basic\sparse_matrix_test.cpp
"header/basic/test_macro.h"
"header/basic/sparse_matrix_test.h"
"header/basic/utility.h"
Expand Down Expand Up @@ -4098,7 +4098,7 @@
<cstdio>
<cstdlib>

1710075394 source:d:\steps\code\steps\source\device\generator.cpp
1710076196 source:d:\steps\code\steps\source\device\generator.cpp
"header/device/generator.h"
"header/basic/utility.h"
"header/STEPS.h"
Expand Down Expand Up @@ -4244,7 +4244,7 @@
<iostream>
<cstdio>

1709521343 source:d:\steps\code\steps\source\device\load.cpp
1710076232 source:d:\steps\code\steps\source\device\load.cpp
"header/device/load.h"
"header/basic/utility.h"
"header/STEPS.h"
Expand Down Expand Up @@ -4662,7 +4662,7 @@
"header/basic/utility.h"
"header/steps_namespace.h"

1664187911 source:d:\steps\code\steps\source\model\load_model\cim6.cpp
1710076265 source:d:\steps\code\steps\source\model\load_model\cim6.cpp
"header/model/load_model/CIM6.h"
"header/basic/utility.h"
"header/STEPS.h"
Expand All @@ -4675,7 +4675,7 @@
"header/basic/utility.h"
"header/steps_namespace.h"

1664187911 source:d:\steps\code\steps\source\model\load_model\ieel.cpp
1710076279 source:d:\steps\code\steps\source\model\load_model\ieel.cpp
"header/model/load_model/IEEL.h"
"header/basic/utility.h"
"header/STEPS.h"
Expand Down
8 changes: 4 additions & 4 deletions code/steps/header/basic/sparse_matrix_csparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class SPARSE_MATRIX_CSPARSE : public SPARSE_MATRIX


vector<double>& operator/(vector<double>&b, SPARSE_MATRIX_CSPARSE& A);
SPARSE_MATRIX_CSPARSE* operator+(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B);
SPARSE_MATRIX_CSPARSE* operator-(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B);
SPARSE_MATRIX_CSPARSE* operator*(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B);
SPARSE_MATRIX_CSPARSE* operator/(double b, SPARSE_MATRIX_CSPARSE& A);
SPARSE_MATRIX_CSPARSE operator+(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B);
SPARSE_MATRIX_CSPARSE operator-(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B);
SPARSE_MATRIX_CSPARSE operator*(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B);
SPARSE_MATRIX_CSPARSE operator/(double b, SPARSE_MATRIX_CSPARSE& A);
SPARSE_MATRIX_CSPARSE inv(SPARSE_MATRIX_CSPARSE&A);
SPARSE_MATRIX_CSPARSE concatenate_matrix_diagnally(vector<SPARSE_MATRIX_CSPARSE*> matrix);
SPARSE_MATRIX_CSPARSE build_identity_matrix(SPARSE_MATRIX_CSPARSE&A);
Expand Down
40 changes: 18 additions & 22 deletions code/steps/source/basic/sparse_matrix_csparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,58 +760,55 @@ vector<double>& operator/(vector<double>&b, SPARSE_MATRIX_CSPARSE& A)
return A.solve_Ax_eq_b(b);
}

SPARSE_MATRIX_CSPARSE* operator+(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B)
SPARSE_MATRIX_CSPARSE operator+(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B)
{
cs* a = A.get_cs_real_matrix();
cs* b = B.get_cs_real_matrix();

cs* c = cs_add(a,b,1.0, 1.0);

SPARSE_MATRIX_CSPARSE* C=new SPARSE_MATRIX_CSPARSE;
C->clear();
C->set_cs_real_matrix(c);
cout<<"Warning. If you see this message, it means you calls operator '+' of SPARSE_MATRIX_CSPARSE. Remember to call 'delete' to delete the returned new pointer of SPARSE_MATRIX_CSPARSE.\n";
SPARSE_MATRIX_CSPARSE C;
C.clear();
C.set_cs_real_matrix(c);
return C;
}

SPARSE_MATRIX_CSPARSE* operator-(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B)
SPARSE_MATRIX_CSPARSE operator-(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B)
{
cs* a = A.get_cs_real_matrix();
cs* b = B.get_cs_real_matrix();

cs* c = cs_add(a,b,1.0, -1.0);

SPARSE_MATRIX_CSPARSE* C=new SPARSE_MATRIX_CSPARSE;
C->clear();
C->set_cs_real_matrix(c);
cout<<"Warning. If you see this message, it means you calls operator '-' of SPARSE_MATRIX_CSPARSE. Remember to call 'delete' to delete the returned new pointer of SPARSE_MATRIX_CSPARSE.\n";
SPARSE_MATRIX_CSPARSE C;
C.clear();
C.set_cs_real_matrix(c);
return C;
}

SPARSE_MATRIX_CSPARSE* operator*(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B)
SPARSE_MATRIX_CSPARSE operator*(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE& B)
{
cs* a = A.get_cs_real_matrix();
cs* b = B.get_cs_real_matrix();

cs* c = cs_multiply(a,b);

SPARSE_MATRIX_CSPARSE* C=new SPARSE_MATRIX_CSPARSE;
C->clear();
C->set_cs_real_matrix(c);
cout<<"Warning. If you see this message, it means you calls operator '*' of SPARSE_MATRIX_CSPARSE. Remember to call 'delete' to delete the returned new pointer of SPARSE_MATRIX_CSPARSE.\n";
SPARSE_MATRIX_CSPARSE C;
C.clear();
C.set_cs_real_matrix(c);
return C;
}

SPARSE_MATRIX_CSPARSE* operator/(double b, SPARSE_MATRIX_CSPARSE& A)
SPARSE_MATRIX_CSPARSE operator/(double b, SPARSE_MATRIX_CSPARSE& A)
{
SPARSE_MATRIX_CSPARSE* B = new SPARSE_MATRIX_CSPARSE;
SPARSE_MATRIX_CSPARSE B;
int n = A.get_matrix_size();

double temp = INFINITE_THRESHOLD;
for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
B->add_entry(i,j,temp);
B->compress_and_merge_duplicate_entries();
B.add_entry(i,j,temp);
B.compress_and_merge_duplicate_entries();

for(int j=0; j<n; ++j)
{
Expand All @@ -821,13 +818,12 @@ SPARSE_MATRIX_CSPARSE* operator/(double b, SPARSE_MATRIX_CSPARSE& A)
I.push_back(0.0);
I[j]=1.0;
vector<double> c = I/A;
int k_start = B->get_starting_index_of_column(j);
int k_start = B.get_starting_index_of_column(j);
for(int i=0; i<n; ++i)
{
B->change_entry_value(k_start+i, c[i]*b);
B.change_entry_value(k_start+i, c[i]*b);
}
}
cout<<"Warning. If you see this message, it means you calls inverse operator '/' of SPARSE_MATRIX_CSPARSE. Remember to call 'delete' to delete the returned new pointer of SPARSE_MATRIX_CSPARSE.\n";
return B;
}

Expand Down
16 changes: 4 additions & 12 deletions code/steps/source/basic/sparse_matrix_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,7 @@ void SPARSE_MATRIX_TEST::test_matrix_add()
// [0 2 4] + j [0 5 9]
// [1 0 1] [0 0 2]

STEPS_SPARSE_MATRIX* tempmatrix = matrix+matrix;
STEPS_SPARSE_MATRIX newmatrix = (*tempmatrix);
delete tempmatrix;
STEPS_SPARSE_MATRIX newmatrix = matrix+matrix;

TEST_ASSERT(newmatrix.get_matrix_size()==3);

Expand All @@ -481,9 +479,7 @@ void SPARSE_MATRIX_TEST::test_matrix_minus()
// [0 2 4] + j [0 5 9]
// [1 0 1] [0 0 2]

STEPS_SPARSE_MATRIX* tempmatrix = matrix-matrix;
STEPS_SPARSE_MATRIX newmatrix = (*tempmatrix);
delete tempmatrix;
STEPS_SPARSE_MATRIX newmatrix = matrix-matrix;

TEST_ASSERT(newmatrix.get_matrix_size()==3);

Expand All @@ -509,9 +505,7 @@ void SPARSE_MATRIX_TEST::test_matrix_multiply()
// [0 2 4] + j [0 5 9]
// [1 0 1] [0 0 2]

STEPS_SPARSE_MATRIX* tempmatrix = matrix*matrix;
STEPS_SPARSE_MATRIX newmatrix = (*tempmatrix);
delete tempmatrix;
STEPS_SPARSE_MATRIX newmatrix = matrix*matrix;

TEST_ASSERT(newmatrix.get_matrix_size()==3);

Expand All @@ -537,9 +531,7 @@ void SPARSE_MATRIX_TEST::test_matrix_inverse()
// [0 2 4] + j [0 5 9]
// [1 0 1] [0 0 2]

STEPS_SPARSE_MATRIX* tempmatrix = 1/matrix;
STEPS_SPARSE_MATRIX newmatrix = (*tempmatrix);
delete tempmatrix;
STEPS_SPARSE_MATRIX newmatrix = 1/matrix;

TEST_ASSERT(newmatrix.get_matrix_size()==3);

Expand Down

0 comments on commit 81d5ab4

Please sign in to comment.