From: Sebastian Kinnewig Date: Sun, 18 Feb 2024 17:37:51 +0000 (+0100) Subject: Add a comparison operator to LA::TpetraWrappers::Vector X-Git-Tag: relicensing~27^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=79b4356bc36fadeca73f07b8dad93a36041d2cec;p=dealii.git Add a comparison operator to LA::TpetraWrappers::Vector --- 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