Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor matrix file I/O #184

Merged
merged 27 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
87a1f19
First draft of refactored coo2csr function.
pelesh Jul 4, 2024
563940c
Clean up code.
pelesh Jul 5, 2024
ee813ce
Add test for old coo2csr.
pelesh Jul 5, 2024
ff34b12
coo2csr_new moves data to selected memory space.
pelesh Sep 12, 2024
931ab6f
Refactor file I/O matrix functions.
pelesh Sep 14, 2024
7bf788b
Reading matrix from file removes duplicates.
pelesh Sep 16, 2024
36b008e
Improve MatrixIoTests.
pelesh Sep 16, 2024
564e266
Refactor matrix read from file functions.
pelesh Sep 16, 2024
dfc58fd
Matrix import has option to expand symmetric matrices.
pelesh Sep 16, 2024
ed13611
Matrix handler also uses coo2csr_simple.
pelesh Sep 16, 2024
63a2727
Fix memory leak in coo2csr_simple.
pelesh Sep 16, 2024
b440734
Remove deprecated code.
pelesh Sep 17, 2024
8dce980
Remove nnz_expanded_ sparse matrix member variable.
pelesh Sep 17, 2024
2e1a3e5
Fix compiler warnings.
pelesh Sep 17, 2024
9b720c8
Matrix file import now can create CSR matrices.
pelesh Sep 17, 2024
a4e4e15
Fix bug in matrix import from file.
pelesh Sep 18, 2024
e317c75
Tests use direct CSR matrix import from file.
pelesh Sep 18, 2024
c108341
Examples use direct CSR matrix import from file.
pelesh Sep 18, 2024
bff1a76
Remove deprecated code.
pelesh Sep 18, 2024
d3b0aae
Remove deprecated coo2csr code.
pelesh Sep 18, 2024
21f31cc
[skip CI] Add Kaleb as contributor.
pelesh Sep 18, 2024
f09193a
Address PR review comments.
pelesh Sep 18, 2024
9bf95a7
Remove dead code and document MatrixElementTriplet.
pelesh Sep 19, 2024
88cb867
Improve doxygen documentation in io.cpp.
pelesh Sep 19, 2024
792e68b
Minor tweaks to io.cpp.
pelesh Sep 19, 2024
ff91c46
Separate COO and CSR matrix I/O tests.
pelesh Sep 20, 2024
27dd3f1
Addressing comments by @maksud.
pelesh Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 6 additions & 12 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,13 +81,9 @@ 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());
A = ReSolve::io::readCsrMatrixFromFile(mat_file, is_expand_symmetric);
maksud marked this conversation as resolved.
Show resolved Hide resolved

rhs = ReSolve::io::readRhsFromFile(rhs_file);
x = new real_type[A->getNumRows()];
Expand All @@ -98,23 +93,22 @@ int main(int argc, char *argv[])
vec_x->allocate(ReSolve::memory::DEVICE);
vec_r = new vector_type(A->getNumRows());
} else {
ReSolve::io::readAndUpdateMatrix(mat_file, A_coo);
ReSolve::io::readAndUpdateMatrix(mat_file, A);
ReSolve::io::readAndUpdateRhs(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);
pelesh marked this conversation as resolved.
Show resolved Hide resolved
} 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
27 changes: 11 additions & 16 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,13 +83,9 @@ 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());
A = ReSolve::io::readCsrMatrixFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::readRhsFromFile(rhs_file);
x = new real_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::readCsrMatrixFromFile(mat_file, is_expand_symmetric);
} else {
ReSolve::io::readAndUpdateMatrix(mat_file, A_exp_coo);
ReSolve::io::readAndUpdateMatrix(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);
A->updateValues(A_exp->getValues(ReSolve::memory::HOST), ReSolve::memory::HOST, ReSolve::memory::HOST);
//ReSolve::io::readAndUpdateMatrix(mat_file, A);
ReSolve::io::readAndUpdateRhs(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);
pelesh marked this conversation as resolved.
Show resolved Hide resolved
} 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
17 changes: 5 additions & 12 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,21 +79,17 @@ 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());
A = ReSolve::io::readCsrMatrixFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::readRhsFromFile(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::readAndUpdateMatrix(mat_file, A);
ReSolve::io::readAndUpdateRhs(rhs_file, &rhs);
}
std::cout << "Finished reading the matrix and rhs, size: " << A->getNumRows()
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
13 changes: 3 additions & 10 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,12 +64,8 @@ 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());
bool is_expand_symmetric = true;
A = ReSolve::io::readCsrMatrixFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::readRhsFromFile(rhs_file);
x = new real_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
19 changes: 6 additions & 13 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());
A = ReSolve::io::readCsrMatrixFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::readRhsFromFile(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::readAndUpdateMatrix(mat_file, A);
ReSolve::io::readAndUpdateRhs(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
18 changes: 6 additions & 12 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,13 +81,9 @@ 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());
A = ReSolve::io::readCsrMatrixFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::readRhsFromFile(rhs_file);
x = new real_type[A->getNumRows()];
Expand All @@ -97,23 +92,22 @@ int main(int argc, char *argv[] )
vec_r = new vector_type(A->getNumRows());
}
else {
ReSolve::io::readAndUpdateMatrix(mat_file, A_coo);
ReSolve::io::readAndUpdateMatrix(mat_file, A);
ReSolve::io::readAndUpdateRhs(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
23 changes: 8 additions & 15 deletions examples/r_KLU_rf_FGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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 @@ -85,13 +84,9 @@ 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());
A = ReSolve::io::readCsrMatrixFromFile(mat_file, is_expand_symmetric);

rhs = ReSolve::io::readRhsFromFile(rhs_file);
x = new real_type[A->getNumRows()];
Expand All @@ -100,29 +95,27 @@ int main(int argc, char *argv[])
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);
} else {
ReSolve::io::readAndUpdateMatrix(mat_file, A);
ReSolve::io::readAndUpdateRhs(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;
real_type norm_b;
if (i < 2){
if (i < 2) {
KLU->setup(A);
matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);
status = KLU->analyze();
Expand Down
Loading