Skip to content

Commit

Permalink
Merge pull request #1928 from DARMA-tasking/1913-add-option-to-optimi…
Browse files Browse the repository at this point in the history
…ze-memory-pool-when-a-header-is-not-needed

1913 Add option of headerless pool
  • Loading branch information
PhilMiller authored Sep 14, 2022
2 parents cedbe4e + 11a134f commit 7bb2b1a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
46 changes: 26 additions & 20 deletions src/vt/pool/static_sized/memory_pool_equal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@

namespace vt { namespace pool {

template <int64_t num_bytes_t>
MemoryPoolEqual<num_bytes_t>::MemoryPoolEqual(SlotType const in_pool_size)
template <int64_t num_bytes_t, bool use_header>
MemoryPoolEqual<num_bytes_t, use_header>::MemoryPoolEqual(SlotType const in_pool_size)
: pool_size_(in_pool_size)
{
resizePool();
}

template <int64_t num_bytes_t>
/*virtual*/ MemoryPoolEqual<num_bytes_t>::~MemoryPoolEqual() {
template <int64_t num_bytes_t, bool use_header>
/*virtual*/ MemoryPoolEqual<num_bytes_t, use_header>::~MemoryPoolEqual() {
vt_debug_print(
normal, pool,
"cur_slot_={}\n", cur_slot_
Expand All @@ -77,8 +77,8 @@ template <int64_t num_bytes_t>
}
}

template <int64_t num_bytes_t>
void* MemoryPoolEqual<num_bytes_t>::alloc(
template <int64_t num_bytes_t, bool use_header>
void* MemoryPoolEqual<num_bytes_t, use_header>::alloc(
size_t const& sz, size_t const& oversize
) {
if (static_cast<size_t>(cur_slot_ + 1) >= holder_.size()) {
Expand All @@ -92,9 +92,12 @@ void* MemoryPoolEqual<num_bytes_t>::alloc(

auto const& slot = cur_slot_;
void* const ptr = holder_[slot];
void* const ptr_ret = HeaderManagerType::setHeader(
sz, oversize, static_cast<char*>(ptr)
);
void* ptr_ret = ptr;
if (use_header) {
ptr_ret = HeaderManagerType::setHeader(
sz, oversize, static_cast<char*>(ptr)
);
}

vt_debug_print(
normal, pool,
Expand All @@ -107,8 +110,8 @@ void* MemoryPoolEqual<num_bytes_t>::alloc(
return ptr_ret;
}

template <int64_t num_bytes_t>
void MemoryPoolEqual<num_bytes_t>::dealloc(void* const t) {
template <int64_t num_bytes_t, bool use_header>
void MemoryPoolEqual<num_bytes_t, use_header>::dealloc(void* const t) {
vt_debug_print(
normal, pool,
"dealloc t={}, cur_slot={}\n", t, cur_slot_
Expand All @@ -119,30 +122,33 @@ void MemoryPoolEqual<num_bytes_t>::dealloc(void* const t) {
);

auto t_char = static_cast<char*>(t);
void* const ptr_actual = HeaderManagerType::getHeaderPtr(t_char);
void* ptr_actual = t;
if (use_header) {
ptr_actual = HeaderManagerType::getHeaderPtr(t_char);
}

holder_[--cur_slot_] = ptr_actual;
}

template <int64_t num_bytes_t>
void MemoryPoolEqual<num_bytes_t>::resizePool() {
template <int64_t num_bytes_t, bool use_header>
void MemoryPoolEqual<num_bytes_t, use_header>::resizePool() {
SlotType const cur_size = holder_.size();
SlotType const new_size = cur_size == 0 ? pool_size_ : cur_size * 2;

holder_.resize(new_size);

for (auto i = cur_size; i < new_size; i++) {
holder_[i] = static_cast<void*>(malloc(num_full_bytes_));
holder_[i] = static_cast<void*>(malloc(use_header ? num_full_bytes_ : num_bytes_t));
}
}

template <int64_t num_bytes_t>
typename MemoryPoolEqual<num_bytes_t>::SlotType
MemoryPoolEqual<num_bytes_t>::getNumBytes() {
template <int64_t num_bytes_t, bool use_header>
typename MemoryPoolEqual<num_bytes_t, use_header>::SlotType
MemoryPoolEqual<num_bytes_t, use_header>::getNumBytes() {
return num_bytes_;
}

template struct MemoryPoolEqual<memory_size_small>;
template struct MemoryPoolEqual<memory_size_medium>;
template struct MemoryPoolEqual<memory_size_small, true>;
template struct MemoryPoolEqual<memory_size_medium, true>;

}} //end namespace vt::pool
18 changes: 16 additions & 2 deletions src/vt/pool/static_sized/memory_pool_equal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ static constexpr size_t const medium_msg_size_buf =
static constexpr size_t const memory_size_medium =
sizeof(EpochTagEnvelope) + medium_msg_size_buf;

template <int64_t num_bytes_t>



/**
* \struct MemoryPoolEqual
*
* \brief Memory pool based on size and whether to use the header.
* The header contains additional information about messages
* that may not always be needed.
*/
template <int64_t num_bytes_t, bool use_header = true>
struct MemoryPoolEqual {
using ContainerType = std::vector<void*>;
using SlotType = int64_t;
Expand All @@ -73,7 +83,11 @@ struct MemoryPoolEqual {

static constexpr SlotType const fst_pool_slot = 0;
static constexpr SlotType const default_pool_size = 1024;

/**
* \brief Construct a memory pool
*
* \param[in] in_pool_size the size of the pool in bytes
*/
MemoryPoolEqual(SlotType const in_pool_size = default_pool_size);

virtual ~MemoryPoolEqual();
Expand Down

0 comments on commit 7bb2b1a

Please sign in to comment.