Determining the Largest Block of Available Memory in PoolΒΆ

The umpire::strategy::DynamicPool provides a umpire::strategy::DynamicPool::getLargestAvailableBlock() that may be used to determine the size of the largest block currently available for allocation within the pool. To call this function, you must get the pointer to the umpire::strategy::AllocationStrategy from the umpire::Allocator:

  auto& rm = umpire::ResourceManager::getInstance();

  auto pool = rm.makeAllocator<umpire::strategy::DynamicPool>(
      "pool", rm.getAllocator("HOST"));

  auto dynamic_pool =
    umpire::util::unwrap_allocator<umpire::strategy::DynamicPool>(pool);

Once you have the pointer to the appropriate strategy, you can call the function:

  std::cout
    << "Largest available block in pool is "
    << dynamic_pool->getLargestAvailableBlock() << " bytes in size"
    << std::endl;

The complete example is included below:

//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016-19, Lawrence Livermore National Security, LLC and Umpire
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: (MIT)
//////////////////////////////////////////////////////////////////////////////
#include "umpire/strategy/DynamicPool.hpp"

#include "umpire/Allocator.hpp"
#include "umpire/ResourceManager.hpp"

#include "umpire/util/Exception.hpp"
#include "umpire/util/wrap_allocator.hpp"

#include <iostream>

int main(int, char**) {
  auto& rm = umpire::ResourceManager::getInstance();

  auto pool = rm.makeAllocator<umpire::strategy::DynamicPool>(
      "pool", rm.getAllocator("HOST"));

  auto dynamic_pool =
    umpire::util::unwrap_allocator<umpire::strategy::DynamicPool>(pool);

  if ( dynamic_pool == nullptr ) {
    UMPIRE_ERROR(pool.getName() << " is not a DynamicPool");
  }

  auto ptr = pool.allocate(1024);

  std::cout
    << "Largest available block in pool is "
    << dynamic_pool->getLargestAvailableBlock() << " bytes in size"
    << std::endl;

  pool.deallocate(ptr);

  return 0;
}