Skip to content

Commit

Permalink
Don't allow call to shrink_to_fit after replicate
Browse files Browse the repository at this point in the history
  • Loading branch information
bergbauer committed Sep 12, 2023
1 parent 5843d24 commit dbe653f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions include/deal.II/base/aligned_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,11 @@ class AlignedVector
const AlignedVector<T> *owning_aligned_vector;
};

/**
* Flag indicating if replicate_across_communicator() has been called.
*/
bool replicated_across_communicator;

/**
* Pointer to actual data array, using the custom deleter class above.
*/
Expand Down Expand Up @@ -1169,7 +1174,8 @@ AlignedVector<T>::Deleter::MPISharedMemDeleterAction::delete_array(

template <class T>
inline AlignedVector<T>::AlignedVector()
: elements(nullptr, Deleter(this))
: replicated_across_communicator(false)
, elements(nullptr, Deleter(this))
, used_elements_end(nullptr)
, allocated_elements_end(nullptr)
{}
Expand All @@ -1178,7 +1184,8 @@ inline AlignedVector<T>::AlignedVector()

template <class T>
inline AlignedVector<T>::AlignedVector(const size_type size, const T &init)
: elements(nullptr, Deleter(this))
: replicated_across_communicator(false)
, elements(nullptr, Deleter(this))
, used_elements_end(nullptr)
, allocated_elements_end(nullptr)
{
Expand All @@ -1190,7 +1197,8 @@ inline AlignedVector<T>::AlignedVector(const size_type size, const T &init)

template <class T>
inline AlignedVector<T>::AlignedVector(const AlignedVector<T> &vec)
: elements(nullptr, Deleter(this))
: replicated_across_communicator(false)
, elements(nullptr, Deleter(this))
, used_elements_end(nullptr)
, allocated_elements_end(nullptr)
{
Expand Down Expand Up @@ -1444,6 +1452,9 @@ template <class T>
inline void
AlignedVector<T>::shrink_to_fit()
{
Assert(replicated_across_communicator == false,
ExcMessage("Changing the vector after a call to"
" replicate_across_communicator() is not allowed."));
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)
Expand Down Expand Up @@ -1917,6 +1928,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));
Expand Down
2 changes: 1 addition & 1 deletion tests/base/aligned_vector_01.output
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit dbe653f

Please sign in to comment.