Skip to content

Commit

Permalink
Refactor matrix file I/O (#184)
Browse files Browse the repository at this point in the history
* Import proper CSR from Matrix Market file.
 
* Remove nnz_expanded_ sparse matrix member variable and supporting accessor methods (i.e. resolve document invariants for nnz_expanded_ or remove it entirely) resolves #166.

* Fix ReSolve::io::writeMatrixToFile calls Sparse::print, which writes out 0-indexed coordinates of matrix data, when the matrix market coordinate format is 1-indexed. Resolves #179.

* Resolve Sparse::updateData reads nnz from nnz_expanded_ if the is_expanded_ flag is set. Resolves #176.

* Update tests to verify the new matrix I/O code.

* Improve doxygen documentation in io.cpp.
  • Loading branch information
pelesh authored Sep 20, 2024
1 parent cc8e57a commit b326fbd
Show file tree
Hide file tree
Showing 54 changed files with 1,460 additions and 906 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Primary authors of this project are Kasia Świrydowicz and Slaven Peles.

ReSolve project would not be possible without significant contributions from (in alphabetic order):
- Maksudul Alam
- Kaleb Brunhoeber
- Ryan Danehy
- Nicholson Koukpaizan
- Jaelyn Litzinger
Expand Down
24 changes: 9 additions & 15 deletions examples/r_KLU_GLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ int main(int argc, char *argv[])
std::string matrixFileNameFull;
std::string rhsFileNameFull;

ReSolve::matrix::Coo* A_coo;
ReSolve::matrix::Csr* A;
ReSolve::LinAlgWorkspaceCUDA* workspace_CUDA = new ReSolve::LinAlgWorkspaceCUDA;
workspace_CUDA->initializeHandles();
Expand Down Expand Up @@ -82,39 +81,34 @@ int main(int argc, char *argv[])
std::cout << "Failed to open file " << rhsFileNameFull << "\n";
return -1;
}
bool is_expand_symmetric = true;
if (i == 0) {
A_coo = ReSolve::io::readMatrixFromFile(mat_file);
A = new ReSolve::matrix::Csr(A_coo->getNumRows(),
A_coo->getNumColumns(),
A_coo->getNnz(),
A_coo->symmetric(),
A_coo->expanded());

rhs = ReSolve::io::readRhsFromFile(rhs_file);
A = ReSolve::io::createCsrFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::createArrayFromFile(rhs_file);
x = new real_type[A->getNumRows()];
vec_rhs = new vector_type(A->getNumRows());
vec_x = new vector_type(A->getNumRows());
vec_x->allocate(ReSolve::memory::HOST);//for KLU
vec_x->allocate(ReSolve::memory::DEVICE);
vec_r = new vector_type(A->getNumRows());
} else {
ReSolve::io::readAndUpdateMatrix(mat_file, A_coo);
ReSolve::io::readAndUpdateRhs(rhs_file, &rhs);
ReSolve::io::updateMatrixFromFile(mat_file, A);
ReSolve::io::updateArrayFromFile(rhs_file, &rhs);
}
std::cout<<"Finished reading the matrix and rhs, size: "<<A->getNumRows()<<" x "<<A->getNumColumns()<< ", nnz: "<< A->getNnz()<< ", symmetric? "<<A->symmetric()<< ", Expanded? "<<A->expanded()<<std::endl;
mat_file.close();
rhs_file.close();

//Now convert to CSR.
// Update host and device data.
if (i < 1) {
A->updateFromCoo(A_coo, ReSolve::memory::HOST);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
vec_rhs->setDataUpdated(ReSolve::memory::HOST);
} else {
A->updateFromCoo(A_coo, ReSolve::memory::DEVICE);
A->copyData(ReSolve::memory::DEVICE);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE);
}
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnzExpanded()<<std::endl;
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnz()<<std::endl;
//Now call direct solver
int status;
if (i < 1) {
Expand Down
33 changes: 14 additions & 19 deletions examples/r_KLU_GLU_matrix_values_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ int main(int argc, char *argv[])
std::string matrixFileNameFull;
std::string rhsFileNameFull;

ReSolve::matrix::Coo* A_coo;
ReSolve::matrix::Coo* A_exp_coo;
ReSolve::matrix::Csr* A;
ReSolve::matrix::Csr* A_exp;
ReSolve::LinAlgWorkspaceCUDA* workspace_CUDA = new ReSolve::LinAlgWorkspaceCUDA;
workspace_CUDA->initializeHandles();
ReSolve::MatrixHandler* matrix_handler = new ReSolve::MatrixHandler(workspace_CUDA);
Expand Down Expand Up @@ -84,15 +83,11 @@ int main(int argc, char *argv[])
std::cout << "Failed to open file " << rhsFileNameFull << "\n";
return -1;
}
bool is_expand_symmetric = true;
if (i == 0) {
A_coo = ReSolve::io::readMatrixFromFile(mat_file);
A = new ReSolve::matrix::Csr(A_coo->getNumRows(),
A_coo->getNumColumns(),
A_coo->getNnz(),
A_coo->symmetric(),
A_coo->expanded());

rhs = ReSolve::io::readRhsFromFile(rhs_file);
A = ReSolve::io::createCsrFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::createArrayFromFile(rhs_file);
x = new real_type[A->getNumRows()];
vec_rhs = new vector_type(A->getNumRows());
vec_x = new vector_type(A->getNumRows());
Expand All @@ -101,29 +96,28 @@ int main(int argc, char *argv[])
vec_r = new vector_type(A->getNumRows());
} else {
if (i==1) {
A_exp_coo = ReSolve::io::readMatrixFromFile(mat_file);
A_exp = ReSolve::io::createCsrFromFile(mat_file, is_expand_symmetric);
} else {
ReSolve::io::readAndUpdateMatrix(mat_file, A_exp_coo);
ReSolve::io::updateMatrixFromFile(mat_file, A_exp);
}
std::cout<<"Updating values of A_coo!"<<std::endl;
A_coo->updateValues(A_exp_coo->getValues(ReSolve::memory::HOST), ReSolve::memory::HOST, ReSolve::memory::HOST);
//ReSolve::io::readAndUpdateMatrix(mat_file, A_coo);
ReSolve::io::readAndUpdateRhs(rhs_file, &rhs);
A->updateValues(A_exp->getValues(ReSolve::memory::HOST), ReSolve::memory::HOST, ReSolve::memory::HOST);
//ReSolve::io::updateMatrixFromFile(mat_file, A);
ReSolve::io::updateArrayFromFile(rhs_file, &rhs);
}
std::cout<<"Finished reading the matrix and rhs, size: "<<A->getNumRows()<<" x "<<A->getNumColumns()<< ", nnz: "<< A->getNnz()<< ", symmetric? "<<A->symmetric()<< ", Expanded? "<<A->expanded()<<std::endl;
mat_file.close();
rhs_file.close();

//Now convert to CSR.
// Update host and device data.
if (i < 1) {
A->updateFromCoo(A_coo, ReSolve::memory::HOST);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
vec_rhs->setDataUpdated(ReSolve::memory::HOST);
} else {
A->updateFromCoo(A_coo, ReSolve::memory::DEVICE);
A->copyData(ReSolve::memory::DEVICE);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE);
}
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnzExpanded()<<std::endl;
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnz()<<std::endl;
//Now call direct solver
int status;
if (i < 1){
Expand Down Expand Up @@ -165,6 +159,7 @@ int main(int argc, char *argv[])

//now DELETE
delete A;
delete A_exp;
delete KLU;
delete GLU;
delete [] x;
Expand Down
23 changes: 8 additions & 15 deletions examples/r_KLU_KLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ int main(int argc, char *argv[])
std::string matrixFileNameFull;
std::string rhsFileNameFull;

ReSolve::matrix::Coo* A_coo;
ReSolve::matrix::Csr* A;
ReSolve::LinAlgWorkspaceCpu* workspace = new ReSolve::LinAlgWorkspaceCpu();
ReSolve::MatrixHandler* matrix_handler = new ReSolve::MatrixHandler(workspace);
Expand Down Expand Up @@ -80,22 +79,18 @@ int main(int argc, char *argv[])
std::cout << "Failed to open file " << rhsFileNameFull << "\n";
return -1;
}
bool is_expand_symmetric = true;
if (i == 0) {
A_coo = ReSolve::io::readMatrixFromFile(mat_file);
A = new ReSolve::matrix::Csr(A_coo->getNumRows(),
A_coo->getNumColumns(),
A_coo->getNnz(),
A_coo->symmetric(),
A_coo->expanded());

rhs = ReSolve::io::readRhsFromFile(rhs_file);
A = ReSolve::io::createCsrFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::createArrayFromFile(rhs_file);
x = new real_type[A->getNumRows()];
vec_rhs = new vector_type(A->getNumRows());
vec_x = new vector_type(A->getNumRows());
vec_r = new vector_type(A->getNumRows());
} else {
ReSolve::io::readAndUpdateMatrix(mat_file, A_coo);
ReSolve::io::readAndUpdateRhs(rhs_file, &rhs);
ReSolve::io::updateMatrixFromFile(mat_file, A);
ReSolve::io::updateArrayFromFile(rhs_file, &rhs);
}
std::cout << "Finished reading the matrix and rhs, size: " << A->getNumRows()
<< " x " << A->getNumColumns()
Expand All @@ -105,16 +100,14 @@ int main(int argc, char *argv[])
mat_file.close();
rhs_file.close();

//Now convert to CSR.
// Update data.
if (i < 2) {
A->updateFromCoo(A_coo, ReSolve::memory::HOST);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
vec_rhs->setDataUpdated(ReSolve::memory::HOST);
} else {
A->updateFromCoo(A_coo, ReSolve::memory::HOST);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
}
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnzExpanded()<<std::endl;
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnz()<<std::endl;
//Now call direct solver
int status;

Expand Down
17 changes: 5 additions & 12 deletions examples/r_KLU_KLU_standalone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ int main(int argc, char *argv[])
std::string fileId;
std::string rhsId;

ReSolve::matrix::Coo* A_coo;
ReSolve::matrix::Csr* A;
ReSolve::LinAlgWorkspaceCpu* workspace = new ReSolve::LinAlgWorkspaceCpu();
ReSolve::MatrixHandler* matrix_handler = new ReSolve::MatrixHandler(workspace);
Expand Down Expand Up @@ -65,14 +64,10 @@ int main(int argc, char *argv[])
std::cout << "Failed to open file " << rhsFileName << "\n";
return -1;
}
A_coo = ReSolve::io::readMatrixFromFile(mat_file);
A = new ReSolve::matrix::Csr(A_coo->getNumRows(),
A_coo->getNumColumns(),
A_coo->getNnz(),
A_coo->symmetric(),
A_coo->expanded());

rhs = ReSolve::io::readRhsFromFile(rhs_file);
bool is_expand_symmetric = true;
A = ReSolve::io::createCsrFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::createArrayFromFile(rhs_file);
x = new real_type[A->getNumRows()];
vec_rhs = new vector_type(A->getNumRows());
vec_x = new vector_type(A->getNumRows());
Expand All @@ -81,11 +76,9 @@ int main(int argc, char *argv[])
mat_file.close();
rhs_file.close();

//Now convert to CSR.
A->updateFromCoo(A_coo, ReSolve::memory::HOST);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
vec_rhs->setDataUpdated(ReSolve::memory::HOST);
std::cout << "COO to CSR completed. Expanded NNZ: " << A->getNnzExpanded() << std::endl;
std::cout << "COO to CSR completed. Expanded NNZ: " << A->getNnz() << std::endl;
//Now call direct solver
int status;
KLU->setup(A);
Expand Down
25 changes: 9 additions & 16 deletions examples/r_KLU_cusolverrf_redo_factorization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ int main(int argc, char *argv[] )
std::string matrixFileNameFull;
std::string rhsFileNameFull;

ReSolve::matrix::Coo* A_coo;
ReSolve::matrix::Csr* A;

ReSolve::LinAlgWorkspaceCUDA* workspace_CUDA = new ReSolve::LinAlgWorkspaceCUDA;
Expand Down Expand Up @@ -93,37 +92,32 @@ int main(int argc, char *argv[] )
std::cout << "Failed to open file " << rhsFileNameFull << "\n";
return -1;
}
bool is_expand_symmetric = true;
if (i == 0) {
A_coo = ReSolve::io::readMatrixFromFile(mat_file);
A = new ReSolve::matrix::Csr(A_coo->getNumRows(),
A_coo->getNumColumns(),
A_coo->getNnz(),
A_coo->symmetric(),
A_coo->expanded());

rhs = ReSolve::io::readRhsFromFile(rhs_file);
A = ReSolve::io::createCsrFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::createArrayFromFile(rhs_file);
x = new real_type[A->getNumRows()];
vec_rhs = new vector_type(A->getNumRows());
vec_x = new vector_type(A->getNumRows());
vec_r = new vector_type(A->getNumRows());
} else {
ReSolve::io::readAndUpdateMatrix(mat_file, A_coo);
ReSolve::io::readAndUpdateRhs(rhs_file, &rhs);
ReSolve::io::updateMatrixFromFile(mat_file, A);
ReSolve::io::updateArrayFromFile(rhs_file, &rhs);
}
std::cout<<"Finished reading the matrix and rhs, size: "<<A->getNumRows()<<" x "<<A->getNumColumns()<< ", nnz: "<< A->getNnz()<< ", symmetric? "<<A->symmetric()<< ", Expanded? "<<A->expanded()<<std::endl;
mat_file.close();
rhs_file.close();

//Now convert to CSR.
// Update host and device data.
if (i < 2) {
matrix_handler->coo2csr(A_coo, A, ReSolve::memory::HOST);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
vec_rhs->setDataUpdated(ReSolve::memory::HOST);
} else {
matrix_handler->coo2csr(A_coo, A, ReSolve::memory::DEVICE);
A->copyData(ReSolve::memory::DEVICE);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE);
}
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnzExpanded()<<std::endl;
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnz()<<std::endl;
//Now call direct solver
if (i < 2) {
KLU->setup(A);
Expand Down Expand Up @@ -215,7 +209,6 @@ int main(int argc, char *argv[] )

//now DELETE
delete A;
delete A_coo;
delete KLU;
delete Rf;
delete [] x;
Expand Down
24 changes: 9 additions & 15 deletions examples/r_KLU_rf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ int main(int argc, char *argv[] )
std::string matrixFileNameFull;
std::string rhsFileNameFull;

ReSolve::matrix::Coo* A_coo;
ReSolve::matrix::Csr* A;

ReSolve::LinAlgWorkspaceCUDA* workspace_CUDA = new ReSolve::LinAlgWorkspaceCUDA;
Expand Down Expand Up @@ -82,38 +81,33 @@ int main(int argc, char *argv[] )
std::cout << "Failed to open file " << rhsFileNameFull << "\n";
return -1;
}
bool is_expand_symmetric = true;
if (i == 0) {
A_coo = ReSolve::io::readMatrixFromFile(mat_file);
A = new ReSolve::matrix::Csr(A_coo->getNumRows(),
A_coo->getNumColumns(),
A_coo->getNnz(),
A_coo->symmetric(),
A_coo->expanded());

rhs = ReSolve::io::readRhsFromFile(rhs_file);
A = ReSolve::io::createCsrFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::createArrayFromFile(rhs_file);
x = new real_type[A->getNumRows()];
vec_rhs = new vector_type(A->getNumRows());
vec_x = new vector_type(A->getNumRows());
vec_r = new vector_type(A->getNumRows());
}
else {
ReSolve::io::readAndUpdateMatrix(mat_file, A_coo);
ReSolve::io::readAndUpdateRhs(rhs_file, &rhs);
ReSolve::io::updateMatrixFromFile(mat_file, A);
ReSolve::io::updateArrayFromFile(rhs_file, &rhs);
}
std::cout<<"Finished reading the matrix and rhs, size: "<<A->getNumRows()<<" x "<<A->getNumColumns()<< ", nnz: "<< A->getNnz()<< ", symmetric? "<<A->symmetric()<< ", Expanded? "<<A->expanded()<<std::endl;
mat_file.close();
rhs_file.close();

//Now convert to CSR.
// Update host and device data.
if (i < 2) {
A->updateFromCoo(A_coo, ReSolve::memory::HOST);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST);
vec_rhs->setDataUpdated(ReSolve::memory::HOST);
} else {
A->updateFromCoo(A_coo, ReSolve::memory::DEVICE);
A->copyData(ReSolve::memory::DEVICE);
vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE);
}
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnzExpanded()<<std::endl;
std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnz()<<std::endl;
//Now call direct solver
int status;
if (i < 2){
Expand Down
Loading

0 comments on commit b326fbd

Please sign in to comment.