Skip to content

Commit

Permalink
PODVector Updates
Browse files Browse the repository at this point in the history
Remove deprecated and unused PolymorphicAllocator. It has been replaced by
PolymorphicArenaAllocator.

Restrict PODVector's Allocator to std::allocator and AMReX's various Arena
based allocators. This simplifies the implementation of PODVector, because
std::allocator is stateless and Arena based allocators are simple even when
it's polymorphic.

Fix a few issues of PODVectors with a PolymorphicArenaAllocator. For
example, copy assignment operator should copy the Allocator. Copy
constructor should consider the possibility that other PODVector has a
different type of Arena.

Add placeholders for potentially growing and shrinking memory allocation
in-place that will be implemented in a follow-up PR.

Update PODVector's growth strategy. Hopefully this helps to reduce the
memory consumption.

  * Always try to grow in-place.

  * For assign, operator=, resize & reserve, allocate the specified size
    without capacity.

  * For push_back & emplace_back, grow the capacity by a factor that is 1.5
    by default.

  * For insert, the capacity grows either by a factor that is 1.5 by default
    or to the new size, whichever is greater.
  • Loading branch information
WeiqunZhang committed Jul 17, 2023
1 parent 01b750d commit 4e795fc
Show file tree
Hide file tree
Showing 7 changed files with 626 additions and 561 deletions.
2 changes: 1 addition & 1 deletion Src/AmrCore/AMReX_TagBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ TagBoxArray::local_collate_gpu (Gpu::PinnedVector<IntVect>& v) const
Gpu::dtoh_memcpy(hv_ntags.data(), dv_ntags.data(), ntotblocks*sizeof(int));

Gpu::PinnedVector<int> hv_tags_offset(ntotblocks+1);
hv_tags_offset[0] = 0;
if (! hv_tags_offset.empty()) { hv_tags_offset[0] = 0; }
std::partial_sum(hv_ntags.begin(), hv_ntags.end(), hv_tags_offset.begin()+1);
int ntotaltags = hv_tags_offset.back();

Expand Down
21 changes: 21 additions & 0 deletions Src/Base/AMReX_Arena.H
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstddef>
#include <cstdlib>
#include <limits>
#include <utility>

namespace amrex {

Expand Down Expand Up @@ -100,6 +101,26 @@ public:
* \return a pointer to the allocated memory
*/
[[nodiscard]] virtual void* alloc (std::size_t sz) = 0;

/**
* Try to allocate in-place by extending the capacity of given pointer.
*/
[[nodiscard]] virtual std::pair<void*,std::size_t>
alloc_in_place (void* /*pt*/, std::size_t /*szmin*/, std::size_t szmax)
{
auto* p = alloc(szmax);
return std::make_pair(p, szmax);
}

/**
* Try to shrink in-place
*/
[[nodiscard]] virtual void*
shrink_in_place (void* /*pt*/, std::size_t sz)
{
return alloc(sz);
}

/**
* \brief A pure virtual function for deleting the arena pointed to by pt
*/
Expand Down
Loading

0 comments on commit 4e795fc

Please sign in to comment.