Skip to content

Commit

Permalink
Matrix handler now checks in the background if the matrix is CSR. (#190)
Browse files Browse the repository at this point in the history
* Matrix handler checks if matrix is CSR. No other formats are supported by the handler.

* Sparse matrix base class has enum that indicates sparse format.

* Remove string input parameter specifying matrix format in matvec function.
  • Loading branch information
pelesh authored Sep 26, 2024
1 parent a1864d6 commit bd7b1c1
Show file tree
Hide file tree
Showing 39 changed files with 370 additions and 271 deletions.
2 changes: 1 addition & 1 deletion examples/r_KLU_GLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ int main(int argc, char *argv[])
vec_r->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE);
real_type bnorm = sqrt(vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE));
matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);


matrix_handler->matrixInfNorm(A, &norm_A, ReSolve::memory::DEVICE);
Expand Down
2 changes: 1 addition & 1 deletion examples/r_KLU_GLU_matrix_values_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int main(int argc, char *argv[])


matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);

std::cout << "\t 2-Norm of the residual: "
<< std::scientific << std::setprecision(16)
Expand Down
2 changes: 1 addition & 1 deletion examples/r_KLU_KLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ int main(int argc, char *argv[])

matrix_handler->setValuesChanged(true, ReSolve::memory::HOST);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, "csr", ReSolve::memory::HOST);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::HOST);
norm_r = vector_handler->infNorm(vec_r, ReSolve::memory::HOST);

std::cout << "\t2-Norm of the residual: "
Expand Down
2 changes: 1 addition & 1 deletion examples/r_KLU_KLU_standalone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int main(int argc, char *argv[])

matrix_handler->setValuesChanged(true, ReSolve::memory::HOST);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, "csr", ReSolve::memory::HOST);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::HOST);

std::cout << "\t 2-Norm of the residual: "
<< std::scientific << std::setprecision(16)
Expand Down
4 changes: 2 additions & 2 deletions examples/r_KLU_cusolverrf_redo_factorization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ int main(int argc, char *argv[] )

matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);
res_nrm = sqrt(vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE));
b_nrm = sqrt(vector_handler->dot(vec_rhs, vec_rhs, ReSolve::memory::DEVICE));
std::cout << "\t2-Norm of the residual: "
Expand All @@ -180,7 +180,7 @@ int main(int argc, char *argv[] )

matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);
res_nrm = sqrt(vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE));

std::cout <<"\t New residual norm: "
Expand Down
2 changes: 1 addition & 1 deletion examples/r_KLU_rf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int main(int argc, char *argv[] )

matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);

std::cout << "\t 2-Norm of the residual: "
<< std::scientific << std::setprecision(16)
Expand Down
4 changes: 2 additions & 2 deletions examples/r_KLU_rf_FGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int main(int argc, char *argv[])
norm_b = vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE);
norm_b = sqrt(norm_b);
matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);

matrix_handler->matrixInfNorm(A, &norm_A, ReSolve::memory::DEVICE);
norm_x = vector_handler->infNorm(vec_x, ReSolve::memory::DEVICE);
Expand Down Expand Up @@ -174,7 +174,7 @@ int main(int argc, char *argv[])
FGMRES->resetMatrix(A);
FGMRES->setupPreconditioner("LU", Rf);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);

matrix_handler->matrixInfNorm(A, &norm_A, ReSolve::memory::DEVICE);
norm_x = vector_handler->infNorm(vec_x, ReSolve::memory::DEVICE);
Expand Down
4 changes: 2 additions & 2 deletions examples/r_KLU_rf_FGMRES_reuse_factorization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int main(int argc, char *argv[])
norm_b = vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE);
norm_b = sqrt(norm_b);
matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);
std::cout << "\t 2-Norm of the residual : "
<< std::scientific << std::setprecision(16)
<< sqrt(vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE))/norm_b << "\n";
Expand Down Expand Up @@ -181,7 +181,7 @@ int main(int argc, char *argv[])
matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);
FGMRES->resetMatrix(A);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);

std::cout << "\t 2-Norm of the residual (before IR): "
<< std::scientific << std::setprecision(16)
Expand Down
4 changes: 2 additions & 2 deletions examples/r_KLU_rocSolverRf_FGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ int main(int argc, char *argv[])
norm_b = vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE);
norm_b = sqrt(norm_b);
matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);
std::cout << "\t2-Norm of the residual: "
<< std::scientific << std::setprecision(16)
<< sqrt(vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE))/norm_b << "\n";
Expand Down Expand Up @@ -176,7 +176,7 @@ int main(int argc, char *argv[])
FGMRES->resetMatrix(A);
FGMRES->setupPreconditioner("LU", Rf);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);
real_type rnrm = sqrt(vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE));
std::cout << "\t 2-Norm of the residual (before IR): "
<< std::scientific << std::setprecision(16)
Expand Down
2 changes: 1 addition & 1 deletion examples/r_KLU_rocsolverrf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ int main(int argc, char *argv[] )
vec_r->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE);
real_type bnorm = sqrt(vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE));
matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);

std::cout << "\t 2-Norm of the residual: "
<< std::scientific << std::setprecision(16)
Expand Down
4 changes: 2 additions & 2 deletions examples/r_KLU_rocsolverrf_redo_factorization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ int main(int argc, char *argv[] )

matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);
res_nrm = sqrt(vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE));
b_nrm = sqrt(vector_handler->dot(vec_rhs, vec_rhs, ReSolve::memory::DEVICE));
std::cout << "\t 2-Norm of the residual: "
Expand All @@ -163,7 +163,7 @@ int main(int argc, char *argv[] )

matrix_handler->setValuesChanged(true, ReSolve::memory::DEVICE);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", ReSolve::memory::DEVICE);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::DEVICE);
res_nrm = sqrt(vector_handler->dot(vec_r, vec_r, ReSolve::memory::DEVICE));

std::cout<<"\t New residual norm: "
Expand Down
2 changes: 1 addition & 1 deletion examples/r_SysSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ int main(int argc, char *argv[])

matrix_handler->setValuesChanged(true, ReSolve::memory::HOST);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, "csr", ReSolve::memory::HOST);
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, ReSolve::memory::HOST);

std::cout << "\t 2-Norm of the residual: "
<< std::scientific << std::setprecision(16)
Expand Down
6 changes: 3 additions & 3 deletions resolve/LinSolverIterativeFGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace ReSolve
vec_V_->setToZero(memspace_);

rhs->deepCopyVectorData(vec_V_->getData(memspace_), 0, memspace_);
matrix_handler_->matvec(A_, x, vec_V_, &MINUSONE, &ONE, "csr", memspace_);
matrix_handler_->matvec(A_, x, vec_V_, &MINUSONE, &ONE, memspace_);
rnorm = 0.0;
bnorm = vector_handler_->dot(rhs, rhs, memspace_);
rnorm = vector_handler_->dot(vec_V_, vec_V_, memspace_);
Expand Down Expand Up @@ -189,7 +189,7 @@ namespace ReSolve

vec_v->setData( vec_V_->getVectorData(i + 1, memspace_), memspace_);

matrix_handler_->matvec(A_, vec_z, vec_v, &ONE, &ZERO,"csr", memspace_);
matrix_handler_->matvec(A_, vec_z, vec_v, &ONE, &ZERO, memspace_);

// orthogonalize V[i+1], form a column of h_H_

Expand Down Expand Up @@ -274,7 +274,7 @@ namespace ReSolve
}

rhs->deepCopyVectorData(vec_V_->getData(memspace_), 0, memspace_);
matrix_handler_->matvec(A_, x, vec_V_, &MINUSONE, &ONE,"csr", memspace_);
matrix_handler_->matvec(A_, x, vec_V_, &MINUSONE, &ONE, memspace_);
rnorm = vector_handler_->dot(vec_V_, vec_V_, memspace_);
// rnorm = ||V_1||
rnorm = std::sqrt(rnorm);
Expand Down
6 changes: 3 additions & 3 deletions resolve/LinSolverIterativeRandFGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ namespace ReSolve
vec_V_->setToZero(memspace_);

rhs->deepCopyVectorData(vec_V_->getData(memspace_), 0, memspace_);
matrix_handler_->matvec(A_, x, vec_V_, &MINUSONE, &ONE, "csr", memspace_);
matrix_handler_->matvec(A_, x, vec_V_, &MINUSONE, &ONE, memspace_);

vec_v->setData(vec_V_->getVectorData(0, memspace_), memspace_);
vec_s->setData(vec_S_->getVectorData(0, memspace_), memspace_);
Expand Down Expand Up @@ -228,7 +228,7 @@ namespace ReSolve
// V_{i+1}=A*Z_i
vec_v->setData(vec_V_->getVectorData(i + 1, memspace_), memspace_);

matrix_handler_->matvec(A_, vec_z, vec_v, &ONE, &ZERO, "csr", memspace_);
matrix_handler_->matvec(A_, vec_z, vec_v, &ONE, &ZERO, memspace_);

// orthogonalize V[i+1], form a column of h_H_
// this is where it differs from normal solver GS
Expand Down Expand Up @@ -337,7 +337,7 @@ namespace ReSolve
}

rhs->deepCopyVectorData(vec_V_->getData(memspace_), 0, memspace_);
matrix_handler_->matvec(A_, x, vec_V_, &MINUSONE, &ONE,"csr", memspace_);
matrix_handler_->matvec(A_, x, vec_V_, &MINUSONE, &ONE, memspace_);
if (outer_flag) {

sketching_handler_->reset();
Expand Down
4 changes: 2 additions & 2 deletions resolve/SystemSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ namespace ReSolve
return -1.0;
}
matrixHandler_->setValuesChanged(true, ms);
matrixHandler_->matvec(A_, x, resVector_, &ONE, &MINUSONE, "csr", ms);
matrixHandler_->matvec(A_, x, resVector_, &ONE, &MINUSONE, ms);
resnorm = std::sqrt(vectorHandler_->dot(resVector_, resVector_, ms));
return resnorm/norm_b;
}
Expand Down Expand Up @@ -711,7 +711,7 @@ namespace ReSolve
return -1.0;
}
matrixHandler_->setValuesChanged(true, ms);
matrixHandler_->matvec(A_, x, resVector_, &ONE, &MINUSONE, "csr", ms);
matrixHandler_->matvec(A_, x, resVector_, &ONE, &MINUSONE, ms);
resnorm = vectorHandler_->infNorm(resVector_, ms);
norm_x = vectorHandler_->infNorm(x, ms);
matrixHandler_->matrixInfNorm(A_, &norm_A, ms);
Expand Down
6 changes: 5 additions & 1 deletion resolve/matrix/Coo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ namespace ReSolve

matrix::Coo::Coo()
{
sparse_format_ = TRIPLET;
}

matrix::Coo::Coo(index_type n, index_type m, index_type nnz) : Sparse(n, m, nnz)
{
sparse_format_ = TRIPLET;
}

matrix::Coo::Coo(index_type n,
Expand All @@ -24,6 +26,7 @@ namespace ReSolve
bool symmetric,
bool expanded) : Sparse(n, m, nnz, symmetric, expanded)
{
sparse_format_ = TRIPLET;
}

/**
Expand All @@ -41,7 +44,8 @@ namespace ReSolve
memory::MemorySpace memspaceDst)
: Sparse(n, m, nnz, symmetric, expanded)
{
using namespace memory;
sparse_format_ = TRIPLET;

int control = -1;
if ((memspaceSrc == memory::HOST) && (memspaceDst == memory::HOST)) { control = 0;}
if ((memspaceSrc == memory::HOST) && (memspaceDst == memory::DEVICE)){ control = 1;}
Expand Down
2 changes: 2 additions & 0 deletions resolve/matrix/Csc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace ReSolve

matrix::Csc::Csc()
{
sparse_format_ = COMPRESSED_SPARSE_COLUMN;
}

matrix::Csc::Csc(index_type n, index_type m, index_type nnz) : Sparse(n, m, nnz)
Expand All @@ -22,6 +23,7 @@ namespace ReSolve
bool symmetric,
bool expanded) : Sparse(n, m, nnz, symmetric, expanded)
{
sparse_format_ = COMPRESSED_SPARSE_COLUMN;
}

matrix::Csc::~Csc()
Expand Down
6 changes: 5 additions & 1 deletion resolve/matrix/Csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ namespace ReSolve

matrix::Csr::Csr()
{
sparse_format_ = COMPRESSED_SPARSE_ROW;
}

matrix::Csr::Csr(index_type n, index_type m, index_type nnz) : Sparse(n, m, nnz)
{
sparse_format_ = COMPRESSED_SPARSE_ROW;
}

matrix::Csr::Csr(index_type n,
Expand All @@ -25,6 +27,7 @@ namespace ReSolve
bool symmetric,
bool expanded) : Sparse(n, m, nnz, symmetric, expanded)
{
sparse_format_ = COMPRESSED_SPARSE_ROW;
}

/**
Expand Down Expand Up @@ -53,7 +56,8 @@ namespace ReSolve
memory::MemorySpace memspaceDst)
: Sparse(n, m, nnz, symmetric, expanded)
{
using namespace memory;
sparse_format_ = COMPRESSED_SPARSE_ROW;

int control = -1;
if ((memspaceSrc == memory::HOST) && (memspaceDst == memory::HOST)) { control = 0;}
if ((memspaceSrc == memory::HOST) && (memspaceDst == memory::DEVICE)){ control = 1;}
Expand Down
6 changes: 2 additions & 4 deletions resolve/matrix/MatrixHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ namespace ReSolve {
* @param[out] vec_result - Vector where the result is stored
* @param[in] alpha - scalar parameter
* @param[in] beta - scalar parameter
* @param[in] matrixFormat - Only CSR format is supported at this time
* @param[in] memspace - Device where the product is computed
* @return result := alpha * A * x + beta * result
*/
Expand All @@ -125,16 +124,15 @@ namespace ReSolve {
vector_type* vec_result,
const real_type* alpha,
const real_type* beta,
std::string matrixFormat,
memory::MemorySpace memspace)
{
using namespace ReSolve::memory;
switch (memspace) {
case HOST:
return cpuImpl_->matvec(A, vec_x, vec_result, alpha, beta, matrixFormat);
return cpuImpl_->matvec(A, vec_x, vec_result, alpha, beta);
break;
case DEVICE:
return devImpl_->matvec(A, vec_x, vec_result, alpha, beta, matrixFormat);
return devImpl_->matvec(A, vec_x, vec_result, alpha, beta);
break;
}
return 1;
Expand Down
1 change: 0 additions & 1 deletion resolve/matrix/MatrixHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ namespace ReSolve {
vector_type* vec_result,
const real_type* alpha,
const real_type* beta,
std::string matrix_type,
memory::MemorySpace memspace);
int matrixInfNorm(matrix::Sparse *A, real_type* norm, memory::MemorySpace memspace);
void setValuesChanged(bool toWhat, memory::MemorySpace memspace);
Expand Down
Loading

0 comments on commit bd7b1c1

Please sign in to comment.