Skip to content

Commit

Permalink
Method setValuesChanged in MatrixHandler now requires memory space as…
Browse files Browse the repository at this point in the history
… input.
  • Loading branch information
pelesh committed Oct 17, 2023
1 parent 6ad87aa commit 23edbf7
Show file tree
Hide file tree
Showing 20 changed files with 51 additions and 56 deletions.
2 changes: 1 addition & 1 deletion examples/r_KLU_GLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int main(int argc, char *argv[])
vec_r->update(rhs, "cpu", "cuda");


matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", "cuda");

printf("\t 2-Norm of the residual: %16.16e\n", sqrt(vector_handler->dot(vec_r, vec_r, "cuda")));
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 @@ -155,7 +155,7 @@ int main(int argc, char *argv[])
vec_r->update(rhs, "cpu", "cuda");


matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", "cuda");

printf("\t 2-Norm of the residual: %16.16e\n", sqrt(vector_handler->dot(vec_r, vec_r, "cuda")));
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 @@ -134,7 +134,7 @@ int main(int argc, char *argv[])
}
vec_r->update(rhs, "cpu", "cpu");

matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cpu");

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, "csr", "cpu");
real_type* test = vec_r->getData("cpu");
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 @@ -96,7 +96,7 @@ int main(int argc, char *argv[])
std::cout << "KLU solve status: " << status << std::endl;
vec_r->update(rhs, "cpu", "cpu");

matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cpu");

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, "csr", "cpu");
real_type* test = vec_r->getData("cpu");
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 @@ -158,7 +158,7 @@ int main(int argc, char *argv[] )
}
vec_r->update(rhs, "cpu", "cuda");

matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", "cuda");

Expand Down
6 changes: 3 additions & 3 deletions examples/r_KLU_rf_FGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ int main(int argc, char *argv[])
real_type norm_b;
if (i < 2){
KLU->setup(A);
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
status = KLU->analyze();
std::cout<<"KLU analysis status: "<<status<<std::endl;
status = KLU->factorize();
Expand All @@ -135,7 +135,7 @@ int main(int argc, char *argv[])
vec_r->update(rhs, "cpu", "cuda");
norm_b = vector_handler->dot(vec_r, vec_r, "cuda");
norm_b = sqrt(norm_b);
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", "cuda");
printf("\t 2-Norm of the residual : %16.16e\n", sqrt(vector_handler->dot(vec_r, vec_r, "cuda"))/norm_b);
if (i == 1) {
Expand Down Expand Up @@ -165,7 +165,7 @@ int main(int argc, char *argv[])
norm_b = vector_handler->dot(vec_r, vec_r, "cuda");
norm_b = sqrt(norm_b);

//matrix_handler->setValuesChanged(true);
//matrix_handler->setValuesChanged(true, "cuda");
FGMRES->resetMatrix(A);
FGMRES->setupPreconditioner("CuSolverRf", Rf);

Expand Down
6 changes: 3 additions & 3 deletions examples/r_KLU_rf_FGMRES_reuse_factorization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ int main(int argc, char *argv[])
real_type norm_b;
if (i < 2){
KLU->setup(A);
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
status = KLU->analyze();
std::cout<<"KLU analysis status: "<<status<<std::endl;
status = KLU->factorize();
Expand All @@ -136,7 +136,7 @@ int main(int argc, char *argv[])
vec_r->update(rhs, "cpu", "cuda");
norm_b = vector_handler->dot(vec_r, vec_r, "cuda");
norm_b = sqrt(norm_b);
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", "cuda");
printf("\t 2-Norm of the residual : %16.16e\n", sqrt(vector_handler->dot(vec_r, vec_r, "cuda"))/norm_b);
if (i == 1) {
Expand Down Expand Up @@ -178,7 +178,7 @@ int main(int argc, char *argv[])
norm_b = vector_handler->dot(vec_r, vec_r, "cuda");
norm_b = sqrt(norm_b);

matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
FGMRES->resetMatrix(A);

matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", "cuda");
Expand Down
2 changes: 1 addition & 1 deletion resolve/LinSolverIterativeFGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ namespace ReSolve
int LinSolverIterativeFGMRES::resetMatrix(matrix::Sparse* new_matrix)
{
A_ = new_matrix;
matrix_handler_->setValuesChanged(true);
matrix_handler_->setValuesChanged(true, "cuda");
return 0;
}

Expand Down
12 changes: 8 additions & 4 deletions resolve/matrix/MatrixHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ namespace ReSolve {
MatrixHandler::MatrixHandler()
{
this->new_matrix_ = true;
this->values_changed_ = true;
cpuImpl_ = new MatrixHandlerCpu();
cudaImpl_ = new MatrixHandlerCuda();
}
Expand All @@ -66,10 +65,15 @@ namespace ReSolve {
cudaImpl_ = new MatrixHandlerCuda(new_workspace);
}

void MatrixHandler::setValuesChanged(bool toWhat)
void MatrixHandler::setValuesChanged(bool isValuesChanged, std::string memspace)
{
this->values_changed_ = toWhat;
cpuImpl_->setValuesChanged(values_changed_);
if (memspace == "cpu") {
cpuImpl_->setValuesChanged(isValuesChanged);
} else if (memspace == "cuda") {
cudaImpl_->setValuesChanged(isValuesChanged);
} else {
out::error() << "Unsupported device " << memspace << "\n";
}
}

int MatrixHandler::coo2csr(matrix::Coo* A_coo, matrix::Csr* A_csr, std::string memspace)
Expand Down
5 changes: 2 additions & 3 deletions resolve/matrix/MatrixHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ namespace ReSolve {
std::string matrix_type,
std::string memspace);
int Matrix1Norm(matrix::Sparse *A, real_type* norm);
void setValuesChanged(bool toWhat);
void setValuesChanged(bool toWhat, std::string memspace);

private:
LinAlgWorkspace* workspace_{nullptr};
bool new_matrix_{true}; ///< if the structure changed, you need a new handler.
bool values_changed_{true}; ///< needed for matvec

MemoryHandler mem_; ///< Device memory manager object
MatrixHandlerImpl* cpuImpl_{nullptr};
MatrixHandlerImpl* cpuImpl_{nullptr};
MatrixHandlerImpl* cudaImpl_{nullptr};
};

Expand Down
8 changes: 4 additions & 4 deletions resolve/matrix/MatrixHandlerCpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ namespace ReSolve {
// return this->values_changed_;
// }

// void MatrixHandlerCpu::setValuesChanged(bool toWhat)
// {
// this->values_changed_ = toWhat;
// }
void MatrixHandlerCpu::setValuesChanged(bool values_changed)
{
values_changed_ = values_changed;
}

// int MatrixHandlerCpu::coo2csr(matrix::Coo* A_coo, matrix::Csr* A_csr, std::string memspace)
// {
Expand Down
4 changes: 2 additions & 2 deletions resolve/matrix/MatrixHandlerCpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ namespace ReSolve {
const real_type* beta,
std::string matrix_type);
virtual int Matrix1Norm(matrix::Sparse *A, real_type* norm);
// void setValuesChanged(bool toWhat);
void setValuesChanged(bool isValuesChanged);

private:
LinAlgWorkspace* workspace_{nullptr};
// bool new_matrix_{true}; ///< if the structure changed, you need a new handler.
// bool values_changed_{true}; ///< needed for matvec
bool values_changed_{true}; ///< needed for matvec

MemoryHandler mem_; ///< Device memory manager object
};
Expand Down
8 changes: 4 additions & 4 deletions resolve/matrix/MatrixHandlerCuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ namespace ReSolve {
// return this->values_changed_;
// }

// void MatrixHandlerCuda::setValuesChanged(bool toWhat)
// {
// this->values_changed_ = toWhat;
// }
void MatrixHandlerCuda::setValuesChanged(bool values_changed)
{
values_changed_ = values_changed;
}

// int MatrixHandlerCuda::coo2csr(matrix::Coo* A_coo, matrix::Csr* A_csr, std::string memspace)
// {
Expand Down
4 changes: 2 additions & 2 deletions resolve/matrix/MatrixHandlerCuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ namespace ReSolve {
const real_type* beta,
std::string matrix_type);
virtual int Matrix1Norm(matrix::Sparse *A, real_type* norm);
// void setValuesChanged(bool toWhat);
void setValuesChanged(bool isValuesChanged);

private:
LinAlgWorkspace* workspace_{nullptr};
// bool new_matrix_{true}; ///< if the structure changed, you need a new handler.
// bool values_changed_{true}; ///< needed for matvec
bool values_changed_{true}; ///< needed for matvec

MemoryHandler mem_; ///< Device memory manager object
};
Expand Down
22 changes: 7 additions & 15 deletions resolve/matrix/MatrixHandlerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,19 @@ namespace ReSolve {

/// Should compute vec_result := alpha*A*vec_x + beta*vec_result, but at least on cpu alpha and beta are flipped
virtual int matvec(matrix::Sparse* A,
vector_type* vec_x,
vector_type* vec_result,
const real_type* alpha,
const real_type* beta,
std::string matrix_type) = 0;
vector_type* vec_x,
vector_type* vec_result,
const real_type* alpha,
const real_type* beta,
std::string matrix_type) = 0;
virtual int Matrix1Norm(matrix::Sparse* A, real_type* norm) = 0;

bool isValuesChanged()
{
return values_changed_;
}

void setValuesChanged(bool isValuesChanged)
{
values_changed_ = isValuesChanged;
}
virtual void setValuesChanged(bool isValuesChanged) = 0;

protected:
// LinAlgWorkspace* workspace_{nullptr};
// bool new_matrix_{true}; ///< if the structure changed, you need a new handler.
bool values_changed_{true}; ///< needed for matvec
// bool values_changed_{true}; ///< needed for matvec

// MemoryHandler mem_; ///< Device memory manager object
};
Expand Down
4 changes: 2 additions & 2 deletions tests/functionality/testKLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int main(int argc, char *argv[])
vec_diff->update(x_data, "cpu", "cpu");

// real_type normXmatrix1 = sqrt(vector_handler->dot(vec_test, vec_test, "cpu"));
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cpu");
status = matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr","cpu");
error_sum += status;

Expand Down Expand Up @@ -174,7 +174,7 @@ int main(int argc, char *argv[])
error_sum += status;

vec_r->update(rhs, "cpu", "cpu");
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cpu");

status = matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, "csr", "cpu");
error_sum += status;
Expand Down
4 changes: 2 additions & 2 deletions tests/functionality/testKLU_GLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int main(int argc, char *argv[])
vec_diff->update(x_data, "cpu", "cuda");

// real_type normXmatrix1 = sqrt(vector_handler->dot(vec_test, vec_test, "cuda"));
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
status = matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr","cuda");
error_sum += status;

Expand Down Expand Up @@ -198,7 +198,7 @@ int main(int argc, char *argv[])
error_sum += status;

vec_r->update(rhs, "cpu", "cuda");
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");

status = matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, "csr", "cuda");
error_sum += status;
Expand Down
6 changes: 3 additions & 3 deletions tests/functionality/testKLU_Rf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ int main(int argc, char *argv[])
vec_diff->update(x_data, "cpu", "cuda");

// real_type normXmatrix1 = sqrt(vector_handler->dot(vec_test, vec_test, "cuda"));
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
status = matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr","cuda");
error_sum += status;

Expand Down Expand Up @@ -194,8 +194,8 @@ int main(int argc, char *argv[])
status = Rf->solve(vec_rhs, vec_x);
error_sum += status;

vec_r->update(rhs, "cpu", "cuda");
matrix_handler->setValuesChanged(true);
vec_r->update(rhs, "cpu", "cuda");
matrix_handler->setValuesChanged(true, "cuda");

status = matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, "csr", "cuda");
error_sum += status;
Expand Down
4 changes: 2 additions & 2 deletions tests/functionality/testKLU_Rf_FGMRES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int main(int argc, char *argv[])
vec_diff->update(x_data, "cpu", "cuda");

// real_type normXmatrix1 = sqrt(vector_handler->dot(vec_test, vec_test, "cuda"));
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");
//evaluate the residual ||b-Ax||
status = matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr","cuda");
error_sum += status;
Expand Down Expand Up @@ -221,7 +221,7 @@ int main(int argc, char *argv[])
error_sum += status;

vec_r->update(rhs, "cpu", "cuda");
matrix_handler->setValuesChanged(true);
matrix_handler->setValuesChanged(true, "cuda");

//evaluate final residual
status = matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE, "csr", "cuda");
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/matrix/MatrixHandlerTests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MatrixHandlerTests : TestBase

real_type alpha = 2.0/30.0;
real_type beta = 2.0;
handler.setValuesChanged(true);
handler.setValuesChanged(true, memspace_);
handler.matvec(A, &x, &y, &alpha, &beta, "csr", memspace_);

status *= verifyAnswer(y, 4.0, memspace_);
Expand Down

0 comments on commit 23edbf7

Please sign in to comment.