Skip to content

Commit

Permalink
Merge pull request #9748 from cgcgcg/xpetraEpetra
Browse files Browse the repository at this point in the history
Xpetra: Epetra changes
  • Loading branch information
cgcgcg committed Sep 30, 2021
2 parents d8cd12c + 49aeeb3 commit a846775
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,15 @@ namespace Xpetra {

virtual void randomize(bool bUseXpetraImplementation = false);

virtual void randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation = false);


//! Set multi-vector values to random numbers. XPetra implementation
virtual void Xpetra_randomize();

//! Set multi-vector values to random numbers. XPetra implementation
virtual void Xpetra_randomize(const Scalar& minVal, const Scalar& maxVal);


//@}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,18 @@ randomize(bool bUseXpetraImplementation)
}


template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
void
BlockedMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation)
{
for(size_t r = 0; r < map_->getNumMaps(); ++r)
{
getMultiVector(r)->randomize(minVal, maxVal, bUseXpetraImplementation);
}
}


template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
void
BlockedMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
Expand All @@ -830,6 +842,15 @@ Xpetra_randomize()
}


template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
void
BlockedMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
Xpetra_randomize(const Scalar& minVal, const Scalar& maxVal)
{
Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Xpetra_randomize(minVal, maxVal);
}


template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>>
BlockedMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,14 @@ class BlockedVector

virtual void randomize(bool bUseXpetraImplementation = false);

virtual void randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation = false);

//! Set vector values to random numbers. XPetra implementation
virtual void Xpetra_randomize();

//! Set vector values to random numbers. XPetra implementation
virtual void Xpetra_randomize(const Scalar& minVal, const Scalar& maxVal);

//@}


Expand Down
23 changes: 23 additions & 0 deletions packages/xpetra/src/BlockedVector/Xpetra_BlockedVector_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,18 @@ randomize(bool bUseXpetraImplementation)
}


template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
void
BlockedVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation)
{
for(size_t r = 0; r < this->getBlockedMap()->getNumMaps(); ++r)
{
getMultiVector(r)->randomize(minVal, maxVal, bUseXpetraImplementation);
}
}


template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
void
BlockedVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
Expand All @@ -598,6 +610,17 @@ Xpetra_randomize()
}
}


template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
void
BlockedVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
Xpetra_randomize(const Scalar& minVal, const Scalar& maxVal)
{
{
Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Xpetra_randomize(minVal, maxVal);
}
}

template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
Teuchos::RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> >
BlockedVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
Expand Down
94 changes: 61 additions & 33 deletions packages/xpetra/src/CrsMatrix/Xpetra_EpetraCrsMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,6 @@ class EpetraCrsMatrixT <int, EpetraNode>
this->fillComplete(domainMap, rowMap, params);
else
this->fillComplete(rowMap, rowMap, params);

// AP (2015/10/22): Could probably be optimized using provided lclMatrix, but lets not worry about that
isInitializedLocalMatrix_ = false;
}
#endif
#endif
Expand Down Expand Up @@ -1262,9 +1259,6 @@ local_matrix_type getLocalMatrixDevice () const {


typename local_matrix_type::HostMirror getLocalMatrixHost () const {
if (isInitializedLocalMatrix_)
return localMatrix_;

RCP<Epetra_CrsMatrix> matrix = getEpetra_CrsMatrixNonConst();

const int numRows = matrix->NumMyRows();
Expand All @@ -1278,33 +1272,53 @@ typename local_matrix_type::HostMirror getLocalMatrixHost () const {
TEUCHOS_TEST_FOR_EXCEPTION(rv, std::runtime_error, "Xpetra::CrsMatrix<>::getLocalMatrix: failed in ExtractCrsDataPointers");

// Transform int* rowptr array to size_type* array
typename local_matrix_type::row_map_type::non_const_type kokkosRowPtr("local row map", numRows+1);
typename local_matrix_type::row_map_type::non_const_type kokkosRowPtr(Kokkos::ViewAllocateWithoutInitializing("local row map"), numRows+1);
for (size_t i = 0; i < kokkosRowPtr.size(); i++)
kokkosRowPtr(i) = Teuchos::asSafe<typename local_matrix_type::row_map_type::value_type>(rowptr[i]);

// create Kokkos::Views
typename local_matrix_type::index_type kokkosColind(colind, nnz);
typename local_matrix_type::values_type kokkosVals (vals, nnz);

localMatrix_ = local_matrix_type("LocalMatrix", numRows, numCols, nnz, kokkosVals, kokkosRowPtr, kokkosColind);
isInitializedLocalMatrix_ = true;
local_matrix_type localMatrix = local_matrix_type("LocalMatrix", numRows, numCols, nnz, kokkosVals, kokkosRowPtr, kokkosColind);

return localMatrix_;
return localMatrix;
}

void setAllValues (const typename local_matrix_type::row_map_type& /* ptr */,
const typename local_matrix_type::StaticCrsGraphType::entries_type::non_const_type& /* ind */,
const typename local_matrix_type::values_type& /* val */)
void setAllValues (const typename local_matrix_type::row_map_type& ptr,
const typename local_matrix_type::StaticCrsGraphType::entries_type::non_const_type& ind,
const typename local_matrix_type::values_type& val)
{
TEUCHOS_TEST_FOR_EXCEPTION(true, Xpetra::Exceptions::RuntimeError,
"Xpetra::EpetraCrsMatrix::setAllValues is not implemented");

// Check sizes
TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<size_t>(ptr.size()) != getNodeNumRows()+1, Xpetra::Exceptions::RuntimeError,
"An exception is thrown to let you know that the size of your rowptr array is incorrect.");
TEUCHOS_TEST_FOR_EXCEPTION(val.size() != ind.size(), Xpetra::Exceptions::RuntimeError,
"An exception is thrown to let you know that you mismatched your pointers.");

// Check pointers
if (val.size() > 0) {
std::cout << ind.data() << " " << mtx_->ExpertExtractIndices().Values() << std::endl;
TEUCHOS_TEST_FOR_EXCEPTION(ind.data() != mtx_->ExpertExtractIndices().Values(), Xpetra::Exceptions::RuntimeError,
"An exception is thrown to let you know that you mismatched your pointers.");
TEUCHOS_TEST_FOR_EXCEPTION(val.data() != mtx_->ExpertExtractValues(), Xpetra::Exceptions::RuntimeError,
"An exception is thrown to let you know that you mismatched your pointers.");
}

// We have to make a copy here, it is unavoidable
// See comments in allocateAllValues
const size_t N = getNodeNumRows();

Epetra_IntSerialDenseVector& myRowptr = mtx_->ExpertExtractIndexOffset();
myRowptr.Resize(N+1);
for (size_t i = 0; i < N+1; i++)
myRowptr[i] = Teuchos::as<int>(ptr(i));

}



private:
mutable local_matrix_type localMatrix_;
mutable bool isInitializedLocalMatrix_ = false; // It's OK to use C++11 when Tpetra is enabled
#else
#ifdef __GNUC__
#warning "Xpetra Kokkos interface for CrsMatrix is enabled (HAVE_XPETRA_KOKKOS_REFACTOR) but Tpetra is disabled. The Kokkos interface needs Tpetra to be enabled, too."
Expand Down Expand Up @@ -1578,9 +1592,6 @@ class EpetraCrsMatrixT <long long, EpetraNode>
this->fillComplete(domainMap, rowMap, params);
else
this->fillComplete(rowMap, rowMap, params);

// AP (2015/10/22): Could probably be optimized using provided lclMatrix, but lets not worry about that
isInitializedLocalMatrix_ = false;
}
#endif
#endif
Expand Down Expand Up @@ -2292,8 +2303,6 @@ class EpetraCrsMatrixT <long long, EpetraNode>
#ifdef HAVE_XPETRA_TPETRA
/// \brief Compatibility layer for accessing the matrix data through a Kokkos interface
local_matrix_type getLocalMatrix () const {
if (isInitializedLocalMatrix_)
return localMatrix_;

RCP<Epetra_CrsMatrix> matrix = getEpetra_CrsMatrixNonConst();

Expand All @@ -2308,32 +2317,51 @@ class EpetraCrsMatrixT <long long, EpetraNode>
TEUCHOS_TEST_FOR_EXCEPTION(rv, std::runtime_error, "Xpetra::CrsMatrix<>::getLocalMatrix: failed in ExtractCrsDataPointers");

// Transform int* rowptr array to size_type* array
typename local_matrix_type::row_map_type::non_const_type kokkosRowPtr("local row map", numRows+1);
typename local_matrix_type::row_map_type::non_const_type kokkosRowPtr(Kokkos::ViewAllocateWithoutInitializing("local row map"), numRows+1);
for (size_t i = 0; i < kokkosRowPtr.size(); i++)
kokkosRowPtr(i) = Teuchos::asSafe<typename local_matrix_type::row_map_type::value_type>(rowptr[i]);

// create Kokkos::Views
typename local_matrix_type::index_type kokkosColind(colind, nnz);
typename local_matrix_type::values_type kokkosVals (vals, nnz);

localMatrix_ = local_matrix_type("LocalMatrix", numRows, numCols, nnz, kokkosVals, kokkosRowPtr, kokkosColind);
isInitializedLocalMatrix_ = true;
local_matrix_type localMatrix = local_matrix_type("LocalMatrix", numRows, numCols, nnz, kokkosVals, kokkosRowPtr, kokkosColind);

return localMatrix_;
return localMatrix;
}

void setAllValues (const typename local_matrix_type::row_map_type& /* ptr */,
const typename local_matrix_type::StaticCrsGraphType::entries_type::non_const_type& /* ind */,
const typename local_matrix_type::values_type& /* val */)
void setAllValues (const typename local_matrix_type::row_map_type& ptr,
const typename local_matrix_type::StaticCrsGraphType::entries_type::non_const_type& ind,
const typename local_matrix_type::values_type& val)
{
TEUCHOS_TEST_FOR_EXCEPTION(true, Xpetra::Exceptions::RuntimeError,
"Xpetra::EpetraCrsMatrix::setAllValues is not implemented");

// Check sizes
TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<size_t>(ptr.size()) != getNodeNumRows()+1, Xpetra::Exceptions::RuntimeError,
"An exception is thrown to let you know that the size of your rowptr array is incorrect.");
TEUCHOS_TEST_FOR_EXCEPTION(val.size() != ind.size(), Xpetra::Exceptions::RuntimeError,
"An exception is thrown to let you know that you mismatched your pointers.");

// Check pointers
if (val.size() > 0) {
TEUCHOS_TEST_FOR_EXCEPTION(ind.data() != mtx_->ExpertExtractIndices().Values(), Xpetra::Exceptions::RuntimeError,
"An exception is thrown to let you know that you mismatched your pointers.");
TEUCHOS_TEST_FOR_EXCEPTION(val.data() != mtx_->ExpertExtractValues(), Xpetra::Exceptions::RuntimeError,
"An exception is thrown to let you know that you mismatched your pointers.");
}

// We have to make a copy here, it is unavoidable
// See comments in allocateAllValues
const size_t N = getNodeNumRows();

Epetra_IntSerialDenseVector& myRowptr = mtx_->ExpertExtractIndexOffset();
myRowptr.Resize(N+1);
for (size_t i = 0; i < N+1; i++)
myRowptr[i] = Teuchos::as<int>(ptr(i));

}


private:
mutable local_matrix_type localMatrix_;
mutable bool isInitializedLocalMatrix_ = false;
#else
#ifdef __GNUC__
#warning "Xpetra Kokkos interface for CrsMatrix is enabled (HAVE_XPETRA_KOKKOS_REFACTOR) but Tpetra is disabled. The Kokkos interface needs Tpetra to be enabled, too."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ const Epetra_IntMultiVector & toEpetra(const MultiVector<int, int, GlobalOrdinal
//! Set multi-vector values to random numbers.
void randomize(bool bUseXpetraImplementation = true) { XPETRA_MONITOR("EpetraIntMultiVectorT::randomize"); TEUCHOS_TEST_FOR_EXCEPTION(1, Xpetra::Exceptions::NotImplemented, "Xpetra::EpetraIntMultiVectorT::randomize(): Functionnality not available in Epetra"); }

//! Set multi-vector values to random numbers.
void randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation = true) { XPETRA_MONITOR("EpetraIntMultiVectorT::randomize"); TEUCHOS_TEST_FOR_EXCEPTION(1, Xpetra::Exceptions::NotImplemented, "Xpetra::EpetraIntMultiVectorT::randomize(): Functionnality not available in Epetra"); }

//! Set seed for Random function.
/** Note: this method does not exist in Tpetra interface. Added for MueLu. */
Expand Down Expand Up @@ -400,6 +402,9 @@ const Epetra_IntMultiVector & toEpetra(const MultiVector<int, int, GlobalOrdinal
//! Set multi-vector values to random numbers.
void randomize(bool /* bUseXpetraImplementation */ = true) { XPETRA_MONITOR("EpetraIntMultiVectorT::randomize"); TEUCHOS_TEST_FOR_EXCEPTION(1, Xpetra::Exceptions::NotImplemented, "Xpetra::EpetraIntMultiVectorT::randomize(): Functionnality not available in Epetra"); }

//! Set multi-vector values to random numbers.
void randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation = true) { XPETRA_MONITOR("EpetraIntMultiVectorT::randomize"); TEUCHOS_TEST_FOR_EXCEPTION(1, Xpetra::Exceptions::NotImplemented, "Xpetra::EpetraIntMultiVectorT::randomize(): Functionnality not available in Epetra"); }


//! Set seed for Random function.
/** Note: this method does not exist in Tpetra interface. Added for MueLu. */
Expand Down Expand Up @@ -786,6 +791,9 @@ const Epetra_IntMultiVector & toEpetra(const MultiVector<int, int, GlobalOrdinal
//! Set multi-vector values to random numbers.
void randomize(bool /* bUseXpetraImplementation */ = true) { XPETRA_MONITOR("EpetraIntMultiVectorT::randomize"); TEUCHOS_TEST_FOR_EXCEPTION(1, Xpetra::Exceptions::NotImplemented, "Xpetra::EpetraIntMultiVectorT::randomize(): Functionnality not available in Epetra"); }

//! Set multi-vector values to random numbers.
void randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation = true) { XPETRA_MONITOR("EpetraIntMultiVectorT::randomize"); TEUCHOS_TEST_FOR_EXCEPTION(1, Xpetra::Exceptions::NotImplemented, "Xpetra::EpetraIntMultiVectorT::randomize(): Functionnality not available in Epetra"); }


//! Set seed for Random function.
/** Note: this method does not exist in Tpetra interface. Added for MueLu. */
Expand Down
55 changes: 48 additions & 7 deletions packages/xpetra/src/MultiVector/Xpetra_EpetraMultiVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ namespace Xpetra {
//! Set multi-vector values to random numbers.
void randomize(bool bUseXpetraImplementation = false) { }

//! Set multi-vector values to random numbers.
void randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation = false) { }

//! Implements DistObject interface
//{@

Expand Down Expand Up @@ -524,6 +527,28 @@ namespace Xpetra {
vec_->Random();
}

//! Set multi-vector values to random numbers.
void randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation = false) {
XPETRA_MONITOR("EpetraMultiVectorT::randomize");

if (bUseXpetraImplementation)
Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node >::Xpetra_randomize(minVal, maxVal);
else {
vec_->Random();
const size_t numVectors = getNumVectors();
for(size_t i = 0; i < numVectors; i++)
{
Teuchos::ArrayRCP<Scalar> datai = getDataNonConst(i);

const size_t myLength = getLocalLength();
for(size_t j = 0; j < myLength; j++)
{
datai[ j ] = 0.5*(maxVal-minVal)*datai[ j ]+0.5*(maxVal+minVal);
}
}
}
}

//! Implements DistObject interface
//{@

Expand Down Expand Up @@ -645,13 +670,7 @@ namespace Xpetra {
return ret;
}

typename dual_view_type::t_dev_um getDeviceLocalView(Access::ReadWriteStruct) const override {
throw std::runtime_error("Epetra does not support device views! in "+std::string(__FILE__)+":"+std::to_string(__LINE__));
#ifndef __NVCC__ //prevent nvcc warning
typename dual_view_type::t_dev_um ret;
#endif
TEUCHOS_UNREACHABLE_RETURN(ret);
}
typename dual_view_type::t_dev_um getDeviceLocalView(Access::ReadWriteStruct) const override { return getHostLocalView(Access::ReadWrite); }

#endif

Expand Down Expand Up @@ -925,6 +944,28 @@ namespace Xpetra {
vec_->Random();
}

//! Set multi-vector values to random numbers.
void randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation = false) {
XPETRA_MONITOR("EpetraMultiVectorT::randomize");

if (bUseXpetraImplementation)
Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node >::Xpetra_randomize(minVal, maxVal);
else {
vec_->Random();
const size_t numVectors = getNumVectors();
for(size_t i = 0; i < numVectors; i++)
{
Teuchos::ArrayRCP<Scalar> datai = getDataNonConst(i);

const size_t myLength = getLocalLength();
for(size_t j = 0; j < myLength; j++)
{
datai[ j ] = 0.5*(maxVal-minVal)*datai[ j ]+0.5*(maxVal+minVal);
}
}
}
}

//! Implements DistObject interface
//{@

Expand Down
5 changes: 5 additions & 0 deletions packages/xpetra/src/MultiVector/Xpetra_MultiVector_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,15 @@ class MultiVector
//! Set multi-vector values to random numbers.
virtual void randomize(bool bUseXpetraImplementation = false) = 0;

//! Set multi-vector values to random numbers.
virtual void randomize(const Scalar& minVal, const Scalar& maxVal, bool bUseXpetraImplementation = false) = 0;

//! Set multi-vector values to random numbers. XPetra implementation
virtual void Xpetra_randomize();

//! Set multi-vector values to random numbers. XPetra implementation
virtual void Xpetra_randomize(const Scalar& minVal, const Scalar& maxVal);


#ifdef HAVE_XPETRA_KOKKOS_REFACTOR

Expand Down
Loading

0 comments on commit a846775

Please sign in to comment.