diff --git a/tests/LinAlg/matrixTestsRajaSparseTriplet.cpp b/tests/LinAlg/matrixTestsRajaSparseTriplet.cpp index 93fcdd3ee..998eb4d4e 100644 --- a/tests/LinAlg/matrixTestsRajaSparseTriplet.cpp +++ b/tests/LinAlg/matrixTestsRajaSparseTriplet.cpp @@ -64,62 +64,11 @@ 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(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(A); - - if (mat != nullptr) - { - auto* amat = const_cast(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(x); - if(xvec != nullptr) - { - auto* axvec = const_cast(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(A); + if(mat == nullptr) + THROW_NULL_DEREF; mat->copyFromDev(); return mat->M_host(); } @@ -127,6 +76,8 @@ real_type* MatrixTestsRajaSparseTriplet::getMatrixData(hiop::hiopMatrixSparse* A const local_ordinal_type* MatrixTestsRajaSparseTriplet::getRowIndices(const hiop::hiopMatrixSparse* A) { const auto* mat = dynamic_cast(A); + if(mat == nullptr) + THROW_NULL_DEREF; const_cast(mat)->copyFromDev(); // UB? return mat->i_row_host(); } @@ -134,17 +85,19 @@ const local_ordinal_type* MatrixTestsRajaSparseTriplet::getRowIndices(const hiop const local_ordinal_type* MatrixTestsRajaSparseTriplet::getColumnIndices(const hiop::hiopMatrixSparse* A) { const auto* mat = dynamic_cast(A); + if(mat == nullptr) + THROW_NULL_DEREF; const_cast(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(x); - if(xvec != nullptr) - return static_cast(xvec->get_local_size()); - else THROW_NULL_DEREF; + if(xvec == nullptr) + THROW_NULL_DEREF; + return static_cast(xvec->get_local_size()); } /** @@ -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(A); + if(mat == nullptr) + THROW_NULL_DEREF; mat->copyFromDev(); const local_ordinal_type nnz = mat->numberOfNonzeros(); const real_type* values = mat->M_host(); @@ -184,6 +137,8 @@ int MatrixTestsRajaSparseTriplet::verifyAnswer( std::function expect) { auto* A = dynamic_cast(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(); @@ -211,6 +166,8 @@ int MatrixTestsRajaSparseTriplet::verifyAnswer( int MatrixTestsRajaSparseTriplet::verifyAnswer(hiop::hiopVector* x, double answer) { auto* xvec = dynamic_cast(x); + if(xvec == nullptr) + THROW_NULL_DEREF; const local_ordinal_type N = getLocalSize(x); xvec->copyFromDev(); const auto* vec = xvec->local_data_host_const(); @@ -235,6 +192,8 @@ int MatrixTestsRajaSparseTriplet::verifyAnswer( const local_ordinal_type N = getLocalSize(x); auto* xvec = dynamic_cast(x); + if(xvec == nullptr) + THROW_NULL_DEREF; xvec->copyFromDev(); const auto* vec = xvec->local_data_host_const(); @@ -255,6 +214,8 @@ int MatrixTestsRajaSparseTriplet::verifyAnswer( local_ordinal_type* MatrixTestsRajaSparseTriplet::numNonzerosPerRow(hiop::hiopMatrixSparse* A) { auto* mat = dynamic_cast(A); + if(mat == nullptr) + THROW_NULL_DEREF; mat->copyFromDev(); auto nnz = mat->numberOfNonzeros(); auto iRow = mat->i_row_host(); @@ -271,6 +232,8 @@ local_ordinal_type* MatrixTestsRajaSparseTriplet::numNonzerosPerRow(hiop::hiopMa local_ordinal_type* MatrixTestsRajaSparseTriplet::numNonzerosPerCol(hiop::hiopMatrixSparse* A) { auto* mat = dynamic_cast(A); + if(mat == nullptr) + THROW_NULL_DEREF; mat->copyFromDev(); auto nnz = mat->numberOfNonzeros(); auto jCol = mat->j_col_host(); @@ -289,6 +252,8 @@ void MatrixTestsRajaSparseTriplet::initializeMatrix( local_ordinal_type entries_per_row) { auto* A = dynamic_cast(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(); diff --git a/tests/LinAlg/matrixTestsRajaSparseTriplet.hpp b/tests/LinAlg/matrixTestsRajaSparseTriplet.hpp index 6bf7a1236..71d4ad116 100644 --- a/tests/LinAlg/matrixTestsRajaSparseTriplet.hpp +++ b/tests/LinAlg/matrixTestsRajaSparseTriplet.hpp @@ -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( diff --git a/tests/LinAlg/matrixTestsSparse.hpp b/tests/LinAlg/matrixTestsSparse.hpp index 6daf46814..20d12dd2d 100644 --- a/tests/LinAlg/matrixTestsSparse.hpp +++ b/tests/LinAlg/matrixTestsSparse.hpp @@ -430,169 +430,6 @@ class MatrixTestsSparse : public TestBase return fail; } - // /** - // * Block of W += alpha*A - // * - // * Precondition: W is square - // * - // * @todo Change parameter _A_ to be of abstract class hiopMatrixSymSparse - // * as soon as this interface exists. - // */ - // bool symAddToSymDenseMatrixUpperTriangle( - // hiop::hiopMatrixDense& W, - // hiop::hiopMatrixSparse& A, // sym sparse matrix - // const int rank=0) - // { - // const local_ordinal_type N_loc = W.get_local_size_n(); - // const local_ordinal_type A_M = A.m(); - // const local_ordinal_type A_N_loc = A.n(); - // assert(W.m() == W.n()); - // assert(W.m() >= A.m()); - // assert(W.n() >= A.n()); - - // const local_ordinal_type start_idx_row = 0; - // const local_ordinal_type start_idx_col = N_loc - A_N_loc; - // const real_type alpha = half, - // A_val = half, - // W_val = one; - // int fail = 0; - - // // Check with non-1 alpha - // A.setToConstant(A_val); - // W.setToConstant(W_val); - // A.addToSymDenseMatrixUpperTriangle(start_idx_row, start_idx_col, alpha, W); - - // // get sparsity pattern - // const auto* iRow = getRowIndices(&A); - // const auto* jCol = getColumnIndices(&A); - // auto nnz = A.numberOfNonzeros(); - // fail += verifyAnswer(&W, - // [=] (local_ordinal_type i, local_ordinal_type j) -> real_type - // { - // // check if (i, j) within bounds of A - // // then check if (i, j) within upper triangle of W - // const bool isUpperTriangle = ( - // i>=start_idx_row && i=start_idx_col && j= i); - - // // only nonzero entries in A will be added - // const bool indexExists = find_unsorted_pair(i, j, iRow, jCol, nnz); - // real_type ans = (isUpperTriangle && indexExists) ? W_val + A_val*alpha : W_val; // 1 + .5 * .5 = 1.25 - // return ans; - // }); - - // printMessage(fail, __func__, rank); - // return fail; - // } - - /** - * Block of W += alpha*A - * - * Block of W summed with A is in the trasposed - * location of the same call to addToSymDenseMatrixUpperTriangle - * - * Precondition: W is square - * - * @todo Remove implementations specific code from this test!!! - * @todo Format documentation correctly - */ - bool symTransAddToSymDenseMatrixUpperTriangle( - hiop::hiopMatrixDense& W, - hiop::hiopMatrixSparse& A, - const int rank=0) - { - const local_ordinal_type N_loc = W.get_local_size_n(); - const local_ordinal_type A_M = A.m(); - const local_ordinal_type A_N_loc = A.n(); - assert(W.m() == W.n()); - assert(W.m() >= A.m()); - assert(W.n() >= A.n()); - - const local_ordinal_type start_idx_row = 0; - const local_ordinal_type start_idx_col = N_loc - A_M; - const real_type alpha = half, - A_val = half, - W_val = one; - - A.setToConstant(A_val); - W.setToConstant(W_val); - - A.transAddToSymDenseMatrixUpperTriangle(start_idx_row, start_idx_col, alpha, W); - - // get sparsity pattern - const auto* iRow = getRowIndices(&A); - const auto* jCol = getColumnIndices(&A); - auto nnz = A.numberOfNonzeros(); - const int fail = verifyAnswer(&W, - [=] (local_ordinal_type i, local_ordinal_type j) -> real_type - { - const bool isTransUpperTriangle = ( - i>=start_idx_row && i=start_idx_col && j= A.m()); - // assert(W.n() >= A.n()); - - // const local_ordinal_type start_idx_row = 0; - // const local_ordinal_type start_idx_col = N_loc - A_N_loc; - // const real_type alpha = half, - // A_val = half, - // W_val = one; - - // A.setToConstant(A_val); - // W.setToConstant(W_val); - - // A.addToSymDenseMatrixUpperTriangle(start_idx_row, start_idx_col, alpha, W); - - // // get sparsity pattern - // const local_ordinal_type* iRow = getRowIndices(&A); - // const local_ordinal_type* jCol = getColumnIndices(&A); - // //const auto* iRow = A.i_row(); - // //const auto* jCol = A.j_col(); - // auto nnz = A.numberOfNonzeros(); - // const int fail = verifyAnswer(&W, - // [=] (local_ordinal_type i, local_ordinal_type j) -> real_type - // { - // const bool isUpperTriangle = ( - // i>=start_idx_row && i=start_idx_col && j= A.n()); - assert(W.n() >= A.m()); - //auto W = dynamic_cast(&_W); - // Map the upper triangle of A to W starting - // at W's upper left corner - const local_ordinal_type diag_start = 0; - int fail = 0; - const real_type alpha = half, - A_val = half, - W_val = one; - - A.setToConstant(A_val); - W.setToConstant(W_val); - - A.addUpperTriangleToSymDenseMatrixUpperTriangle(diag_start, alpha, W); - - const auto* iRow = getRowIndices(&A); - const auto* jCol = getColumnIndices(&A); - auto nnz = A.numberOfNonzeros(); - - fail += verifyAnswer(&W, - [=] (local_ordinal_type i, local_ordinal_type j) -> real_type - { - bool isUpperTriangle = (i>=diag_start && i=i && j(Amat); - assert(W.get_size() == A.m()); // A is square - - const auto start_src_idx = 0; - const auto start_dest_idx = 0; - const auto num_elems = W.get_size(); - const auto A_val = half; - const auto W_val = one; - const auto alpha = half; - - A.setToConstant(A_val); - W.setToConstant(W_val); - A.startingAtAddSubDiagonalToStartingAt(start_src_idx, alpha, W, start_dest_idx, num_elems); - - const auto* iRow = getRowIndices(&A); - const auto* jCol = getColumnIndices(&A); - auto nnz = A.numberOfNonzeros(); - const auto fail = verifyAnswer(&W, - [=](local_ordinal_type i) -> real_type - { - const bool indexExists = find_unsorted_pair(i, i, iRow, jCol, nnz); - return (indexExists) ? (W_val + A_val * alpha) : W_val; - }); - - printMessage(fail, __func__, rank); - return fail; - } - /** * @brief Copy 'n_rows' rows from matrix 'A', started from 'A_rows_st', to the rows started from 'B_rows_st' in 'B'. * The non-zero elements start from 'B_nnz_st' will be replaced by the new elements. @@ -799,19 +548,10 @@ class MatrixTestsSparse : public TestBase } private: - /// TODO: The sparse matrix is not distributed - all is local. - // Rename functions to remove redundant "local" from their names? - virtual void setLocalElement( - hiop::hiopVector* x, - const local_ordinal_type i, - const real_type val) = 0; - virtual real_type getLocalElement(const hiop::hiopMatrix* a, local_ordinal_type i, local_ordinal_type j) = 0; - virtual real_type getLocalElement(const hiop::hiopVector* x, local_ordinal_type i) = 0; + // Implementation specific sprase matrix testing methods virtual real_type* getMatrixData(hiop::hiopMatrixSparse* a) = 0; - virtual real_type getMatrixData(hiop::hiopMatrixSparse* a, local_ordinal_type i, local_ordinal_type j) = 0; virtual const local_ordinal_type* getRowIndices(const hiop::hiopMatrixSparse* a) = 0; virtual const local_ordinal_type* getColumnIndices(const hiop::hiopMatrixSparse* a) = 0; - virtual local_ordinal_type getLocalSize(const hiop::hiopVector* x) = 0; virtual int verifyAnswer(hiop::hiopMatrixSparse* A, real_type answer) = 0; virtual int verifyAnswer(hiop::hiopMatrix* A, local_ordinal_type nnz_st, local_ordinal_type nnz_ed, const double answer) = 0; virtual int verifyAnswer( diff --git a/tests/LinAlg/matrixTestsSparseTriplet.cpp b/tests/LinAlg/matrixTestsSparseTriplet.cpp index 7dbc5e44a..90272f945 100644 --- a/tests/LinAlg/matrixTestsSparseTriplet.cpp +++ b/tests/LinAlg/matrixTestsSparseTriplet.cpp @@ -62,97 +62,65 @@ namespace hiop{ namespace tests { -/// Set `i`th element of vector `x` -void MatrixTestsSparseTriplet::setLocalElement( - hiop::hiopVector* xvec, - const local_ordinal_type i, - const real_type val) -{ - auto x = dynamic_cast(xvec); - if(x != nullptr) - { - real_type* data = x->local_data(); - data[i] = val; - } - else THROW_NULL_DEREF; -} - /// Returns element (i,j) of a dense matrix `A`. /// First need to retrieve hiopMatrixDense from the abstract interface -real_type MatrixTestsSparseTriplet::getLocalElement( +real_type MatrixTestsSparseTriplet::getElement( const hiop::hiopMatrix* A, local_ordinal_type row, local_ordinal_type col) { auto mat = dynamic_cast(A); - - if (mat != nullptr) - { - const double* M = mat->local_data_const(); - //return M[row][col]; - return M[row*mat->get_local_size_n()+col]; - } - - else THROW_NULL_DEREF; + if (mat == nullptr) + THROW_NULL_DEREF; + const double* M = mat->local_data_const(); + //return M[row][col]; + return M[row*mat->get_local_size_n()+col]; + } /// Returns element _i_ of vector _x_. /// First need to retrieve hiopVectorPar from the abstract interface -real_type MatrixTestsSparseTriplet::getLocalElement( +real_type MatrixTestsSparseTriplet::getElement( const hiop::hiopVector* x, local_ordinal_type i) { const hiop::hiopVectorPar* xvec = dynamic_cast(x); - if(xvec != nullptr) - return xvec->local_data_const()[i]; - else THROW_NULL_DEREF; + if(xvec == nullptr) + THROW_NULL_DEREF; + return xvec->local_data_const()[i]; } real_type* MatrixTestsSparseTriplet::getMatrixData(hiop::hiopMatrixSparse* A) { auto* mat = dynamic_cast(A); + if(mat == nullptr) + THROW_NULL_DEREF; return mat->M(); } -real_type MatrixTestsSparseTriplet::getMatrixData(hiop::hiopMatrixSparse* A, local_ordinal_type i, local_ordinal_type j) -{ - auto* mat = dynamic_cast(A); - auto* val = mat->M(); - auto* iRow = mat->i_row(); - auto* jCol = mat->j_col(); - auto nnz = mat->numberOfNonzeros(); - - for (auto k=0; k< nnz; i++) - { - if(iRow[k]==i && jCol[k]==j){ - return val[k]; - } - // assume elements are row-major ordered. - if(iRow[k]>=i) - break; - } - return zero; -} - const local_ordinal_type* MatrixTestsSparseTriplet::getRowIndices(const hiop::hiopMatrixSparse* A) { const auto* mat = dynamic_cast(A); + if(mat == nullptr) + THROW_NULL_DEREF; return mat->i_row(); } const local_ordinal_type* MatrixTestsSparseTriplet::getColumnIndices(const hiop::hiopMatrixSparse* A) { const auto* mat = dynamic_cast(A); + if(mat == nullptr) + THROW_NULL_DEREF; return mat->j_col(); } /// Returns size of local data array for vector `x` -int MatrixTestsSparseTriplet::getLocalSize(const hiop::hiopVector* x) +local_ordinal_type MatrixTestsSparseTriplet::getLocalSize(const hiop::hiopVector* x) { const hiop::hiopVectorPar* xvec = dynamic_cast(x); - if(xvec != nullptr) - return static_cast(xvec->get_local_size()); - else THROW_NULL_DEREF; + if(xvec == nullptr) + THROW_NULL_DEREF; + return static_cast(xvec->get_local_size()); } /** @@ -164,9 +132,9 @@ int MatrixTestsSparseTriplet::getLocalSize(const hiop::hiopVector* x) [[nodiscard]] int MatrixTestsSparseTriplet::verifyAnswer(hiop::hiopMatrixSparse* A, const double answer) { - if(A == nullptr) - return 1; auto mat = dynamic_cast(A); + if(mat == nullptr) + THROW_NULL_DEREF; const local_ordinal_type nnz = mat->numberOfNonzeros(); const real_type* values = mat->M(); int fail = 0; @@ -189,9 +157,9 @@ int MatrixTestsSparseTriplet::verifyAnswer(hiop::hiopMatrixSparse* A, const doub [[nodiscard]] int MatrixTestsSparseTriplet::verifyAnswer(hiop::hiopMatrix* A, local_ordinal_type nnz_st, local_ordinal_type nnz_ed, const double answer) { - if(A == nullptr) - return 1; auto mat = dynamic_cast(A); + if(mat == nullptr) + THROW_NULL_DEREF; const local_ordinal_type nnz = mat->numberOfNonzeros(); const real_type* values = mat->M(); int fail = 0; @@ -223,7 +191,7 @@ int MatrixTestsSparseTriplet::verifyAnswer( { for (local_ordinal_type j=0; j(x)) - { - return xvec->local_data_const(); - } - else - { - assert(false && "Wrong type of vector passed into `VectorTestsPar::getLocalDataConst`!"); + auto* xvec = dynamic_cast(x); + if(xvec == nullptr) THROW_NULL_DEREF; - } + return xvec->local_data_const(); } /// Method to set vector _x_ element _i_ to _value_. void VectorTestsPar::setLocalElement(hiop::hiopVector* x, local_ordinal_type i, real_type val) { - if(auto* xvec = dynamic_cast(x)) - { - real_type *xdat = xvec->local_data(); - xdat[i] = val; - } - else - { - assert(false && "Wrong type of vector passed into `VectorTestsPar::setLocalElement`!"); + auto* xvec = dynamic_cast(x); + if(xvec == nullptr) THROW_NULL_DEREF; - } + real_type *xdat = xvec->local_data(); + xdat[i] = val; } /// Get communicator MPI_Comm VectorTestsPar::getMPIComm(hiop::hiopVector* x) { - if(auto* xvec = dynamic_cast(x)) - { - return xvec->get_mpi_comm(); - } - else - { - assert(false && "Wrong type of vector passed into `VectorTestsPar::getMPIComm`!"); + auto* xvec = dynamic_cast(x); + if(xvec == nullptr) THROW_NULL_DEREF; - } + return xvec->get_mpi_comm(); } /// Wrap new command diff --git a/tests/LinAlg/vectorTestsRajaPar.cpp b/tests/LinAlg/vectorTestsRajaPar.cpp index bb1e24a3e..8046f6465 100644 --- a/tests/LinAlg/vectorTestsRajaPar.cpp +++ b/tests/LinAlg/vectorTestsRajaPar.cpp @@ -67,47 +67,32 @@ namespace hiop { namespace tests { /// Returns const pointer to local vector data const real_type* VectorTestsRajaPar::getLocalDataConst(const hiop::hiopVector* x_in) { - if(auto* x = dynamic_cast(x_in)) - { - x->copyFromDev(); - return x->local_data_host_const(); - } - else - { - assert(false && "Wrong type of vector passed into `VectorTestsRajaPar::getLocalDataConst`!"); + auto* x = dynamic_cast(x_in); + if(x == nullptr) THROW_NULL_DEREF; - } + x->copyFromDev(); + return x->local_data_host_const(); } /// Method to set vector _x_ element _i_ to _value_. void VectorTestsRajaPar::setLocalElement(hiop::hiopVector* x_in, local_ordinal_type i, real_type val) { - if(auto* x = dynamic_cast(x_in)) - { - x->copyFromDev(); - real_type *xdat = x->local_data_host(); - xdat[i] = val; - x->copyToDev(); - } - else - { - assert(false && "Wrong type of vector passed into `VectorTestsRajaPar::setLocalElement`!"); + auto* x = dynamic_cast(x_in); + if(x == nullptr) THROW_NULL_DEREF; - } + x->copyFromDev(); + real_type *xdat = x->local_data_host(); + xdat[i] = val; + x->copyToDev(); } /// Get communicator MPI_Comm VectorTestsRajaPar::getMPIComm(hiop::hiopVector* x) { - if(auto* xvec = dynamic_cast(x)) - { - return xvec->get_mpi_comm(); - } - else - { - assert(false && "Wrong type of vector passed into `VectorTestsRajaPar::getMPIComm`!"); + auto* xvec = dynamic_cast(x); + if(xvec == nullptr) THROW_NULL_DEREF; - } + return xvec->get_mpi_comm(); } /// Wrap new command diff --git a/tests/testMatrixDense.cpp b/tests/testMatrixDense.cpp index 03ac15e60..56924c1fa 100644 --- a/tests/testMatrixDense.cpp +++ b/tests/testMatrixDense.cpp @@ -76,7 +76,6 @@ static int runTests(const char* mem_space, MPI_Comm comm); int main(int argc, char** argv) { using namespace hiop::tests; - using hiop::tests::global_ordinal_type; int rank = 0; MPI_Comm comm = MPI_COMM_SELF; diff --git a/tests/testMatrixSparse.cpp b/tests/testMatrixSparse.cpp index 31ce8ea9c..c42d2ffa6 100644 --- a/tests/testMatrixSparse.cpp +++ b/tests/testMatrixSparse.cpp @@ -62,174 +62,140 @@ #include #include #include -#include +#include #include "LinAlg/matrixTestsSparseTriplet.hpp" #ifdef HIOP_USE_RAJA -#include -#include #include "LinAlg/matrixTestsRajaSparseTriplet.hpp" #endif -using namespace hiop::tests; +template +static int runTests(const char* mem_space); int main(int argc, char** argv) { + using namespace hiop::tests; + using hiop::tests::global_ordinal_type; + if(argc > 1) std::cout << "Executable " << argv[0] << " doesn't take any input."; hiop::hiopOptions options; + int fail = 0; + + // + // Test HiOp Sparse Matrices + // + std::cout << "\nTesting HiOp default sparse matrix implementation:\n"; + fail += runTests("default"); +#ifdef HIOP_USE_RAJA +#ifdef HIOP_USE_GPU + std::cout << "\nTesting HiOp RAJA sparse matrix implementation:\n"; + std::cout << " ... using device memory space:\n"; + fail += runTests("device"); + std::cout << "\nTesting HiOp RAJA sparse matrix implementation:\n"; + std::cout << " ... using unified virtual memory space:\n"; + fail += runTests("um"); +#else + std::cout << "\nTesting HiOp RAJA sparse matrix implementation:\n"; + std::cout << " ... unified host memory space:\n"; + fail += runTests("host"); +#endif // GPU +#endif // RAJA + + if(fail) + { + std::cout << "\n" << fail << " sparse matrix tests failed\n\n"; + } + else + { + std::cout << "\nAll sparse matrix tests passed\n\n"; + } + + return fail; +} + +/// Driver for all sparse matrix tests +template +static int runTests(const char* mem_space) +{ + using namespace hiop; + using hiop::tests::local_ordinal_type; + using hiop::tests::global_ordinal_type; + + T test; + + hiopOptions options; + options.SetStringValue("mem_space", mem_space); + LinearAlgebraFactory::set_mem_space(mem_space); + + int fail = 0; + local_ordinal_type M_local = 5; - local_ordinal_type N_local = 10*M_local; + local_ordinal_type N_local = 10 * M_local; + + // Establishing sparsity pattern and initializing Matrix + local_ordinal_type entries_per_row = 5; + local_ordinal_type nnz = M_local * entries_per_row; // Sparse matrix is not distributed global_ordinal_type M_global = M_local; global_ordinal_type N_global = N_local; - int fail = 0; + hiopMatrixSparse* mxn_sparse = LinearAlgebraFactory::createMatrixSparse(M_local, N_local, nnz); + test.initializeMatrix(mxn_sparse, entries_per_row); - // Test sparse matrix - { - std::cout << "\nTesting hiopMatrixSparseTriplet\n"; - hiop::tests::MatrixTestsSparseTriplet test; + hiopVector* vec_m = LinearAlgebraFactory::createVector(M_global); + hiopVector* vec_n = LinearAlgebraFactory::createVector(N_global); - // Establishing sparsity pattern and initializing Matrix - local_ordinal_type entries_per_row = 5; - local_ordinal_type nnz = M_local * entries_per_row; - - hiop::hiopMatrixSparse* mxn_sparse = - hiop::LinearAlgebraFactory::createMatrixSparse(M_local, N_local, nnz); - test.initializeMatrix(mxn_sparse, entries_per_row); - - hiop::hiopVectorPar vec_m(M_global); - hiop::hiopVectorPar vec_n(N_global); - - /// @see LinAlg/matrixTestsSparseTriplet.hpp for reasons why some tests are implemented/not implemented - fail += test.matrixNumRows(*mxn_sparse, M_global); - fail += test.matrixNumCols(*mxn_sparse, N_global); - fail += test.matrixSetToZero(*mxn_sparse); - fail += test.matrixSetToConstant(*mxn_sparse); - fail += test.matrixTimesVec(*mxn_sparse, vec_m, vec_n); - fail += test.matrixTransTimesVec(*mxn_sparse, vec_m, vec_n); - fail += test.matrixMaxAbsValue(*mxn_sparse); - fail += test.matrixIsFinite(*mxn_sparse); - - // Need a dense matrix to store the output of the following tests - global_ordinal_type W_delta = M_global * 10; - hiop::hiopMatrixDenseRowMajor W_dense(N_global + W_delta, N_global + W_delta); - - // local_ordinal_type test_offset = 10; - local_ordinal_type test_offset = 4; - fail += test.matrixAddMDinvMtransToDiagBlockOfSymDeMatUTri(*mxn_sparse, vec_n, W_dense, test_offset); - - // Need a dense matrix that is big enough for the sparse matrix to map inside the upper triangular part of it - //hiop::hiopMatrixDenseRowMajor n2xn2_dense(2 * N_global, 2 * N_global); - //fail += test.addToSymDenseMatrixUpperTriangle(W_dense, *mxn_sparse); - fail += test.transAddToSymDenseMatrixUpperTriangle(W_dense, *mxn_sparse); - - // Initialise another sparse Matrix - local_ordinal_type M2 = M_global * 2; - nnz = M2 * (entries_per_row); - - hiop::hiopMatrixSparse* m2xn_sparse = - hiop::LinearAlgebraFactory::createMatrixSparse(M2, N_global, nnz); - test.initializeMatrix(m2xn_sparse, entries_per_row); - - // Set offsets where to insert sparse matrix - local_ordinal_type i_offset = 1; - local_ordinal_type j_offset = M2 + 1; - - fail += test.matrixAddMDinvNtransToSymDeMatUTri(*mxn_sparse, *m2xn_sparse, vec_n, W_dense, i_offset, j_offset); - - // copy the 1st row of mxn_sparse to the last row. - // replace the nonzero index from "nnz-entries_per_row" - fail += test.copyRowsBlockFrom(*mxn_sparse, *m2xn_sparse,0, 1, M_global-1, mxn_sparse->numberOfNonzeros()-entries_per_row); + /// @see LinAlg/matrixTestsSparseTriplet.hpp for reasons why some tests are implemented/not implemented + fail += test.matrixNumRows(*mxn_sparse, M_global); + fail += test.matrixNumCols(*mxn_sparse, N_global); + fail += test.matrixSetToZero(*mxn_sparse); + fail += test.matrixSetToConstant(*mxn_sparse); + fail += test.matrixMaxAbsValue(*mxn_sparse); + fail += test.matrixIsFinite(*mxn_sparse); + fail += test.matrixTimesVec(*mxn_sparse, *vec_m, *vec_n); + fail += test.matrixTransTimesVec(*mxn_sparse, *vec_m, *vec_n); - // Remove testing objects - delete mxn_sparse; - delete m2xn_sparse; - - } + // Need a dense matrix to store the output of the following tests + global_ordinal_type W_delta = M_global * 10; + hiopMatrixDense* W_dense = LinearAlgebraFactory::createMatrixDense(N_global + W_delta, N_global + W_delta); -#ifdef HIOP_USE_RAJA - // Test RAJA sparse matrix - { - std::cout << "\nTesting hiopMatrixRajaSparseTriplet\n"; + local_ordinal_type test_offset = 4; + fail += test.matrixAddMDinvMtransToDiagBlockOfSymDeMatUTri(*mxn_sparse, *vec_n, *W_dense, test_offset); - options.SetStringValue("mem_space", "device"); - hiop::LinearAlgebraFactory::set_mem_space(options.GetString("mem_space")); - std::string mem_space = hiop::LinearAlgebraFactory::get_mem_space(); + // testing adding sparse matrix to the upper triangular area of a symmetric dense matrix + fail += test.transAddToSymDenseMatrixUpperTriangle(*W_dense, *mxn_sparse); - hiop::tests::MatrixTestsRajaSparseTriplet test; - - // Establishing sparsity pattern and initializing Matrix - local_ordinal_type entries_per_row = 5; - local_ordinal_type nnz = M_local * entries_per_row; + // Initialise another sparse Matrix + local_ordinal_type M2 = M_global * 2; + nnz = M2 * (entries_per_row); - hiop::hiopMatrixSparse* mxn_sparse = - hiop::LinearAlgebraFactory::createMatrixSparse(M_local, N_local, nnz); + hiopMatrixSparse* m2xn_sparse = LinearAlgebraFactory::createMatrixSparse(M2, N_global, nnz); + test.initializeMatrix(m2xn_sparse, entries_per_row); - test.initializeMatrix(mxn_sparse, entries_per_row); - - hiop::hiopVectorRajaPar vec_m(M_global, mem_space); - hiop::hiopVectorRajaPar vec_m_2(M_global, mem_space); - hiop::hiopVectorRajaPar vec_n(N_global, mem_space); - - /// @see LinAlg/matrixTestsSparseTriplet.hpp for reasons why some tests are implemented/not implemented - fail += test.matrixNumRows(*mxn_sparse, M_global); - fail += test.matrixNumCols(*mxn_sparse, N_global); - fail += test.matrixSetToZero(*mxn_sparse); - fail += test.matrixSetToConstant(*mxn_sparse); - fail += test.matrixMaxAbsValue(*mxn_sparse); - fail += test.matrixIsFinite(*mxn_sparse); - fail += test.matrixTimesVec(*mxn_sparse, vec_m, vec_n); - fail += test.matrixTransTimesVec(*mxn_sparse, vec_m, vec_n); - - // Need a dense matrix to store the output of the following tests - global_ordinal_type W_delta = M_global * 10; - /// @todo use linear algebra factory for these - hiop::hiopMatrixRajaDense W_dense(N_global + W_delta, N_global + W_delta, mem_space); - - // local_ordinal_type test_offset = 10; - local_ordinal_type test_offset = 4; - fail += test.matrixAddMDinvMtransToDiagBlockOfSymDeMatUTri(*mxn_sparse, vec_n, W_dense, test_offset); - - // testing adding sparse matrix to the upper triangular area of a symmetric dense matrix - //fail += test.addToSymDenseMatrixUpperTriangle(W_dense, *mxn_sparse); - fail += test.transAddToSymDenseMatrixUpperTriangle(W_dense, *mxn_sparse); - - // Initialise another sparse Matrix - local_ordinal_type M2 = M_global * 2; - nnz = M2 * (entries_per_row); - - /// @todo: use linear algebra factory for this - hiop::hiopMatrixRajaSparseTriplet m2xn_sparse(M2, N_global, nnz, mem_space); - test.initializeMatrix(&m2xn_sparse, entries_per_row); - - // Set offsets where to insert sparse matrix - local_ordinal_type i_offset = 1; - local_ordinal_type j_offset = M2 + 1; - - fail += test.matrixAddMDinvNtransToSymDeMatUTri(*mxn_sparse, m2xn_sparse, vec_n, W_dense, i_offset, j_offset); - - // Remove testing objects - delete mxn_sparse; - - // Set memory space back to default value - options.SetStringValue("mem_space", "default"); - } -#endif + // Set offsets where to insert sparse matrix + local_ordinal_type i_offset = 1; + local_ordinal_type j_offset = M2 + 1; + fail += test.matrixAddMDinvNtransToSymDeMatUTri(*mxn_sparse, *m2xn_sparse, *vec_n, *W_dense, i_offset, j_offset); - if(fail) - { - std::cout << "\n" << fail << " sparse matrix tests failed!\n\n"; - } - else + // + // Perform hipoMatrixSparseTriplet specific tests + // + //auto * test_triplet = dynamic_cast(&test); + if (dynamic_cast(&test)) { - std::cout << "\nAll sparse matrix tests passed!\n\n"; + fail += test.copyRowsBlockFrom(*mxn_sparse, *m2xn_sparse,0, 1, M_global-1, mxn_sparse->numberOfNonzeros()-entries_per_row); } + delete mxn_sparse; + delete m2xn_sparse; + delete W_dense; + delete vec_m; + delete vec_n; + return fail; -} +} \ No newline at end of file