From: Maximilian Bergbauer Date: Tue, 12 Sep 2023 14:39:48 +0000 (+0200) Subject: Don't allow call to shrink_to_fit after replicate X-Git-Tag: relicensing~487^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb17bfbc711111b7b72aeefa8c8ace2b4fce0e04;p=dealii.git Don't allow call to shrink_to_fit after replicate --- diff --git a/include/deal.II/base/aligned_vector.h b/include/deal.II/base/aligned_vector.h index 9a13dfea1c..0a61377b53 100644 --- a/include/deal.II/base/aligned_vector.h +++ b/include/deal.II/base/aligned_vector.h @@ -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::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::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::AlignedVector(const AlignedVector &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 inline void AlignedVector::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::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 packed_data = Utilities::pack(*this); const int hash = std::accumulate(packed_data.begin(), packed_data.end(), int(0)); diff --git a/tests/base/aligned_vector_01.output b/tests/base/aligned_vector_01.output index 3139875e46..17920c6f64 100644 --- a/tests/base/aligned_vector_01.output +++ b/tests/base/aligned_vector_01.output @@ -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