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 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:

            << dynamic_pool->getLargestAvailableBlock() << " bytes in size"
            << std::endl;

  pool.deallocate(ptr);

The complete example is included below:

//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016-20, Lawrence Livermore National Security, LLC and Umpire
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: (MIT)
//////////////////////////////////////////////////////////////////////////////
#include <iostream>

#include "umpire/Allocator.hpp"
#include "umpire/ResourceManager.hpp"
#include "umpire/strategy/DynamicPool.hpp"
#include "umpire/util/Exception.hpp"
#include "umpire/util/wrap_allocator.hpp"

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;
}