]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Don't allow call to shrink_to_fit after replicate
authorMaximilian Bergbauer <maximilian.bergbauer@tum.de>
Tue, 12 Sep 2023 14:39:48 +0000 (16:39 +0200)
committerMaximilian Bergbauer <maximilian.bergbauer@tum.de>
Wed, 13 Sep 2023 08:52:52 +0000 (10:52 +0200)
include/deal.II/base/aligned_vector.h
tests/base/aligned_vector_01.output

index 9a13dfea1ccfdc2ee0d849f7a9b6b789fe2b9da1..0a61377b53253324844bb46939db6408743416ec 100644 (file)
@@ -469,6 +469,16 @@ public:
   BOOST_SERIALIZATION_SPLIT_MEMBER()
 #endif
 
+  /**
+   * Exception message for changing the vector after a call to
+   * replicate_across_communicator().
+   *
+   * @ingroup Exceptions
+   */
+  DeclExceptionMsg(ExcAlignedVectorChangeAfterReplication,
+                   "Changing the vector after a call to "
+                   "replicate_across_communicator() is not allowed.");
+
 private:
   /**
    * Allocates a new vector and moves the data from the old vector to the new
@@ -699,6 +709,11 @@ private:
    * Pointer to the end of the allocated memory.
    */
   T *allocated_elements_end;
+
+  /**
+   * Flag indicating if replicate_across_communicator() has been called.
+   */
+  bool replicated_across_communicator;
 };
 
 
@@ -1172,6 +1187,9 @@ inline AlignedVector<T>::AlignedVector()
   : elements(nullptr, Deleter(this))
   , used_elements_end(nullptr)
   , allocated_elements_end(nullptr)
+#  ifdef DEBUG
+  , replicated_across_communicator(false)
+#  endif
 {}
 
 
@@ -1181,6 +1199,9 @@ inline AlignedVector<T>::AlignedVector(const size_type size, const T &init)
   : elements(nullptr, Deleter(this))
   , used_elements_end(nullptr)
   , allocated_elements_end(nullptr)
+#  ifdef DEBUG
+  , replicated_across_communicator(false)
+#  endif
 {
   if (size > 0)
     resize(size, init);
@@ -1193,6 +1214,9 @@ inline AlignedVector<T>::AlignedVector(const AlignedVector<T> &vec)
   : elements(nullptr, Deleter(this))
   , used_elements_end(nullptr)
   , allocated_elements_end(nullptr)
+#  ifdef DEBUG
+  , replicated_across_communicator(false)
+#  endif
 {
   // copy the data from vec
   reserve(vec.size());
@@ -1444,6 +1468,10 @@ template <class T>
 inline void
 AlignedVector<T>::shrink_to_fit()
 {
+#  ifdef DEBUG
+  Assert(replicated_across_communicator == false,
+         ExcAlignedVectorChangeAfterReplication());
+#  endif
   const size_type used_size      = used_elements_end - elements.get();
   const size_type allocated_size = allocated_elements_end - elements.get();
   if (allocated_size > used_size)
@@ -1917,6 +1945,7 @@ AlignedVector<T>::replicate_across_communicator(const MPI_Comm     communicator,
   // At this point, each process should have a copy of the data.
   // Verify this in some sort of round-about way
 #    ifdef DEBUG
+  replicated_across_communicator      = true;
   const std::vector<char> packed_data = Utilities::pack(*this);
   const int               hash =
     std::accumulate(packed_data.begin(), packed_data.end(), int(0));
index 3139875e46bd53029e9b23d54bd4994667ca65cb..17920c6f64dd51a9a6e997e8804ab3a3f3318eb6 100644 (file)
@@ -1,7 +1,7 @@
 
 DEAL::Constructor: 0 0 0 0 
 DEAL::Insertion: 0 0 1 0 5 42 0 0 1 0 5 42 27 
-DEAL::Memory Shrinking: 104 to 56
+DEAL::Memory Shrinking: 112 to 64
 DEAL::Shrinking: 0 0 1 0 
 DEAL::Reserve: 0 0 1 0 
 DEAL::Assignment: 0 0 1 0 5 42 27 

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.