From 79b4356bc36fadeca73f07b8dad93a36041d2cec Mon Sep 17 00:00:00 2001 From: Sebastian Kinnewig Date: Sun, 18 Feb 2024 18:37:51 +0100 Subject: [PATCH] Add a comparison operator to LA::TpetraWrappers::Vector --- include/deal.II/lac/trilinos_tpetra_vector.h | 16 +++++++ .../lac/trilinos_tpetra_vector.templates.h | 47 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/include/deal.II/lac/trilinos_tpetra_vector.h b/include/deal.II/lac/trilinos_tpetra_vector.h index 0b9a2d3c16..8c4e5e3f35 100644 --- a/include/deal.II/lac/trilinos_tpetra_vector.h +++ b/include/deal.II/lac/trilinos_tpetra_vector.h @@ -709,6 +709,22 @@ namespace LinearAlgebra bool has_ghost_elements() const; + /** + * Test for equality. This function assumes that the present vector and + * the one to compare with have the same size already, since comparing + * vectors of different sizes makes not much sense anyway. + */ + bool + operator==(const Vector &v) const; + + /** + * Test for inequality. This function assumes that the present vector and + * the one to compare with have the same size already, since comparing + * vectors of different sizes makes not much sense anyway. + */ + bool + operator!=(const Vector &v) const; + /** * Return the global size of the vector, equal to the sum of the number of * locally owned indices among all processors. diff --git a/include/deal.II/lac/trilinos_tpetra_vector.templates.h b/include/deal.II/lac/trilinos_tpetra_vector.templates.h index db6cc93ed5..3b0639e75f 100644 --- a/include/deal.II/lac/trilinos_tpetra_vector.templates.h +++ b/include/deal.II/lac/trilinos_tpetra_vector.templates.h @@ -901,6 +901,53 @@ namespace LinearAlgebra + template + bool + Vector::operator==( + const Vector &v) const + { + Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size())); + + const size_t this_local_length = vector->getLocalLength(); + const size_t other_local_length = v.vector->getLocalLength(); + if (this_local_length != other_local_length) + return false; + +# if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0) + auto this_vector_2d = vector->template getLocalView( + Tpetra::Access::ReadOnly); + auto other_vector_2d = v.vector->template getLocalView( + Tpetra::Access::ReadOnly); + +# else + vector->template sync(); + v.vector->template sync(); + auto this_vector_2d = vector->template getLocalView(); + auto other_vector_2d = + v.vector->template getLocalView(); +# endif + auto this_vector_1d = Kokkos::subview(this_vector_2d, Kokkos::ALL(), 0); + auto other_vector_1d = Kokkos::subview(other_vector_2d, Kokkos::ALL(), 0); + + for (size_type i = 0; i < this_local_length; ++i) + if (this_vector_1d(i) != other_vector_1d(i)) + return false; + + return true; + } + + + + template + bool + Vector::operator!=( + const Vector &v) const + { + return (!(*this == v)); + } + + + template typename Vector::size_type Vector::size() const -- 2.39.5