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

Sparse Matrix Tests Refactoring #173

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
83 changes: 24 additions & 59 deletions tests/LinAlg/matrixTestsRajaSparseTriplet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,87 +64,40 @@

namespace hiop{ namespace tests {

/// Set `i`th element of vector `x`
void MatrixTestsRajaSparseTriplet::setLocalElement(
hiop::hiopVector* xvec,
const local_ordinal_type i,
const real_type val)
{
auto x = dynamic_cast<hiop::hiopVectorRajaPar*>(xvec);
if(x != nullptr)
{
x->copyFromDev();
real_type* data = x->local_data_host();
data[i] = val;
x->copyToDev();
}
else THROW_NULL_DEREF;
}

/// Returns element (i,j) of a dense matrix `A`.
/// First need to retrieve hiopMatrixDense from the abstract interface
real_type MatrixTestsRajaSparseTriplet::getLocalElement(
const hiop::hiopMatrix* A,
local_ordinal_type row,
local_ordinal_type col)
{
const auto* mat = dynamic_cast<const hiop::hiopMatrixRajaDense*>(A);

if (mat != nullptr)
{
auto* amat = const_cast<hiop::hiopMatrixRajaDense*>(mat);
amat->copyFromDev();
//double** M = amat->get_M_host();
//return M[row][col];
return amat->local_data_const()[row*amat->get_local_size_n() + col];
}
else THROW_NULL_DEREF;
}

/// Returns element _i_ of vector _x_.
/// First need to retrieve hiopVectorPar from the abstract interface
real_type MatrixTestsRajaSparseTriplet::getLocalElement(
const hiop::hiopVector* x,
local_ordinal_type i)
{
const auto* xvec = dynamic_cast<const hiop::hiopVectorRajaPar*>(x);
if(xvec != nullptr)
{
auto* axvec = const_cast<hiop::hiopVectorRajaPar*>(xvec);
axvec->copyFromDev();
return xvec->local_data_host_const()[i];
}
else THROW_NULL_DEREF;
}

real_type* MatrixTestsRajaSparseTriplet::getMatrixData(hiop::hiopMatrixSparse* A)
{
auto* mat = dynamic_cast<hiop::hiopMatrixRajaSparseTriplet*>(A);
if(mat == nullptr)
THROW_NULL_DEREF;
mat->copyFromDev();
return mat->M_host();
}

const local_ordinal_type* MatrixTestsRajaSparseTriplet::getRowIndices(const hiop::hiopMatrixSparse* A)
{
const auto* mat = dynamic_cast<const hiop::hiopMatrixRajaSparseTriplet*>(A);
if(mat == nullptr)
THROW_NULL_DEREF;
const_cast<hiop::hiopMatrixRajaSparseTriplet*>(mat)->copyFromDev(); // UB?
return mat->i_row_host();
}

const local_ordinal_type* MatrixTestsRajaSparseTriplet::getColumnIndices(const hiop::hiopMatrixSparse* A)
{
const auto* mat = dynamic_cast<const hiop::hiopMatrixRajaSparseTriplet*>(A);
if(mat == nullptr)
THROW_NULL_DEREF;
const_cast<hiop::hiopMatrixRajaSparseTriplet*>(mat)->copyFromDev(); // UB?
return mat->j_col_host();
}

/// Returns size of local data array for vector `x`
int MatrixTestsRajaSparseTriplet::getLocalSize(const hiop::hiopVector* x)
local_ordinal_type MatrixTestsRajaSparseTriplet::getLocalSize(const hiop::hiopVector* x)
{
const auto* xvec = dynamic_cast<const hiop::hiopVectorRajaPar*>(x);
if(xvec != nullptr)
return static_cast<int>(xvec->get_local_size());
else THROW_NULL_DEREF;
if(xvec == nullptr)
THROW_NULL_DEREF;
return static_cast<int>(xvec->get_local_size());
}

/**
Expand All @@ -156,9 +109,9 @@ int MatrixTestsRajaSparseTriplet::getLocalSize(const hiop::hiopVector* x)
[[nodiscard]]
int MatrixTestsRajaSparseTriplet::verifyAnswer(hiop::hiopMatrixSparse* A, const double answer)
{
if(A == nullptr)
return 1;
auto* mat = dynamic_cast<hiop::hiopMatrixRajaSparseTriplet*>(A);
if(mat == nullptr)
THROW_NULL_DEREF;
mat->copyFromDev();
const local_ordinal_type nnz = mat->numberOfNonzeros();
const real_type* values = mat->M_host();
Expand All @@ -184,6 +137,8 @@ int MatrixTestsRajaSparseTriplet::verifyAnswer(
std::function<real_type(local_ordinal_type, local_ordinal_type)> expect)
{
auto* A = dynamic_cast<hiop::hiopMatrixRajaDense*>(Amat);
if(A == nullptr)
THROW_NULL_DEREF;
assert(A->get_local_size_n() == A->n() && "Matrix should not be distributed");
const local_ordinal_type M = A->get_local_size_m();
const local_ordinal_type N = A->get_local_size_n();
Expand Down Expand Up @@ -211,6 +166,8 @@ int MatrixTestsRajaSparseTriplet::verifyAnswer(
int MatrixTestsRajaSparseTriplet::verifyAnswer(hiop::hiopVector* x, double answer)
{
auto* xvec = dynamic_cast<hiop::hiopVectorRajaPar*>(x);
if(xvec == nullptr)
THROW_NULL_DEREF;
const local_ordinal_type N = getLocalSize(x);
xvec->copyFromDev();
const auto* vec = xvec->local_data_host_const();
Expand All @@ -235,6 +192,8 @@ int MatrixTestsRajaSparseTriplet::verifyAnswer(
const local_ordinal_type N = getLocalSize(x);

auto* xvec = dynamic_cast<hiop::hiopVectorRajaPar*>(x);
if(xvec == nullptr)
THROW_NULL_DEREF;
xvec->copyFromDev();
const auto* vec = xvec->local_data_host_const();

Expand All @@ -255,6 +214,8 @@ int MatrixTestsRajaSparseTriplet::verifyAnswer(
local_ordinal_type* MatrixTestsRajaSparseTriplet::numNonzerosPerRow(hiop::hiopMatrixSparse* A)
{
auto* mat = dynamic_cast<hiop::hiopMatrixRajaSparseTriplet*>(A);
if(mat == nullptr)
THROW_NULL_DEREF;
mat->copyFromDev();
auto nnz = mat->numberOfNonzeros();
auto iRow = mat->i_row_host();
Expand All @@ -271,6 +232,8 @@ local_ordinal_type* MatrixTestsRajaSparseTriplet::numNonzerosPerRow(hiop::hiopMa
local_ordinal_type* MatrixTestsRajaSparseTriplet::numNonzerosPerCol(hiop::hiopMatrixSparse* A)
{
auto* mat = dynamic_cast<hiop::hiopMatrixRajaSparseTriplet*>(A);
if(mat == nullptr)
THROW_NULL_DEREF;
mat->copyFromDev();
auto nnz = mat->numberOfNonzeros();
auto jCol = mat->j_col_host();
Expand All @@ -289,6 +252,8 @@ void MatrixTestsRajaSparseTriplet::initializeMatrix(
local_ordinal_type entries_per_row)
{
auto* A = dynamic_cast<hiop::hiopMatrixRajaSparseTriplet*>(mat);
if(A == nullptr)
THROW_NULL_DEREF;
local_ordinal_type * iRow = A->i_row_host();
local_ordinal_type * jCol = A->j_col_host();
double * val = A->M_host();
Expand Down
9 changes: 1 addition & 8 deletions tests/LinAlg/matrixTestsRajaSparseTriplet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,10 @@ class MatrixTestsRajaSparseTriplet : public MatrixTestsSparse


private:
virtual void setLocalElement(
hiop::hiopVector *_x,
const local_ordinal_type i,
const real_type val);
virtual real_type getLocalElement(const hiop::hiopMatrix *a, local_ordinal_type i, local_ordinal_type j) override;
virtual real_type getLocalElement(const hiop::hiopVector *x, local_ordinal_type i) override;
virtual real_type* getMatrixData(hiop::hiopMatrixSparse* a) override;
virtual real_type getMatrixData(hiop::hiopMatrixSparse* a, local_ordinal_type i, local_ordinal_type j){assert(false);return 0;};
virtual const local_ordinal_type* getRowIndices(const hiop::hiopMatrixSparse* a);
virtual const local_ordinal_type* getColumnIndices(const hiop::hiopMatrixSparse* a);
virtual local_ordinal_type getLocalSize(const hiop::hiopVector *x) override;
local_ordinal_type getLocalSize(const hiop::hiopVector *x);
virtual int verifyAnswer(hiop::hiopMatrixSparse* A, real_type answer) override;
virtual int verifyAnswer(hiop::hiopMatrix* A, local_ordinal_type nnz_st, local_ordinal_type nnz_ed, const double answer) {assert(false);return 0;};
virtual int verifyAnswer(
Expand Down
Loading