]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add a comparison operator to LA::TpetraWrappers::Vector 16664/head
authorSebastian Kinnewig <sebastian@kinnewig.org>
Sun, 18 Feb 2024 17:37:51 +0000 (18:37 +0100)
committerSebastian Kinnewig <sebastian@kinnewig.org>
Sun, 18 Feb 2024 17:37:51 +0000 (18:37 +0100)
include/deal.II/lac/trilinos_tpetra_vector.h
include/deal.II/lac/trilinos_tpetra_vector.templates.h

index 0b9a2d3c165e6574537e19c063fcc64b107678e6..8c4e5e3f357df627eff922b9a20d9c7a4957ee18 100644 (file)
@@ -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<Number, MemorySpace> &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<Number, MemorySpace> &v) const;
+
       /**
        * Return the global size of the vector, equal to the sum of the number of
        * locally owned indices among all processors.
index db6cc93ed54bd812ac3848509a218fcf51db28a8..3b0639e75ffe849c84dd6630777d1142f78713ac 100644 (file)
@@ -901,6 +901,53 @@ namespace LinearAlgebra
 
 
 
+    template <typename Number, typename MemorySpace>
+    bool
+    Vector<Number, MemorySpace>::operator==(
+      const Vector<Number, MemorySpace> &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<Kokkos::HostSpace>(
+        Tpetra::Access::ReadOnly);
+      auto other_vector_2d = v.vector->template getLocalView<Kokkos::HostSpace>(
+        Tpetra::Access::ReadOnly);
+
+#  else
+      vector->template sync<Kokkos::HostSpace>();
+      v.vector->template sync<Kokkos::HostSpace>();
+      auto this_vector_2d = vector->template getLocalView<Kokkos::HostSpace>();
+      auto other_vector_2d =
+        v.vector->template getLocalView<Kokkos::HostSpace>();
+#  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 <typename Number, typename MemorySpace>
+    bool
+    Vector<Number, MemorySpace>::operator!=(
+      const Vector<Number, MemorySpace> &v) const
+    {
+      return (!(*this == v));
+    }
+
+
+
     template <typename Number, typename MemorySpace>
     typename Vector<Number, MemorySpace>::size_type
     Vector<Number, MemorySpace>::size() const

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.