Skip to content

Commit

Permalink
Get the Total Amount of Umpire Memory Allocated (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
kab163 authored Jan 27, 2025
1 parent fc4dd26 commit 02df2c1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/cookbook/recipe_naming_shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ int main(int, char**)

void* ptr = shim.allocate(1024);
std::cout << "Ptr = " << ptr << std::endl;
std::cout << "Total Memory Allocated: " << umpire::get_total_bytes_allocated() << std::endl;
shim.deallocate(ptr);
}
3 changes: 3 additions & 0 deletions examples/tutorial/tut_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//////////////////////////////////////////////////////////////////////////////
#include "umpire/Allocator.hpp"
#include "umpire/ResourceManager.hpp"
#include "umpire/Umpire.hpp"

int main(int, char**)
{
Expand All @@ -31,6 +32,8 @@ int main(int, char**)
std::cout << "Created an add-on allocator of size " << addon_allocator.getCurrentSize() << " using the "
<< allocator.getName() << " allocator." << std::endl;

std::cout << "The total amount of memory used was: " << umpire::get_total_bytes_allocated() << std::endl;

// _sphinx_tag_tut_deallocate_start
allocator.deallocate(data);
// _sphinx_tag_tut_deallocate_end
Expand Down
14 changes: 14 additions & 0 deletions src/umpire/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ Allocator ResourceManager::makeResource(const std::string& name, MemoryResourceT
traits.granularity = MemoryResourceTraits::granularity_type::fine_grained;
}

if (name.find("SHARED") != std::string::npos) {
m_shared_allocator_names.push_back(name);
}

std::unique_ptr<strategy::AllocationStrategy> allocator{registry.makeMemoryResource(name, getNextId(), traits)};
allocator->setTracking(traits.tracking);

Expand Down Expand Up @@ -268,6 +272,16 @@ std::vector<std::string> ResourceManager::getResourceNames()
return registry.getResourceNames();
}

std::vector<std::string> ResourceManager::getSharedAllocatorNames()
{
if (m_shared_allocator_names.size() == 0) {
UMPIRE_LOG(Debug, "Called getSharedAllocatorNames, but there are none. Returning empty vector.");
return std::vector<std::string>(); // Return an empty vector of strings
}

return m_shared_allocator_names;
}

void ResourceManager::setDefaultAllocator(Allocator allocator) noexcept
{
UMPIRE_LOG(Debug, "(\"" << allocator.getName() << "\")");
Expand Down
12 changes: 12 additions & 0 deletions src/umpire/ResourceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ class ResourceManager {
*/
std::vector<std::string> getResourceNames();

/*!
* \brief Get the names for existing SHARED Allocator names, if any.
*
* The SHARED memory resource only indicates whether or not these SHARED allocators
* exist. Since SHARED allocators are made at runtime, this function will actually
* find the specific name of each SHARED allocator and return it.
*
* \return A vector of strings with the available SHARED allocator names.
*/
std::vector<std::string> getSharedAllocatorNames();

/*!
* \brief Set the default Allocator.
*
Expand Down Expand Up @@ -328,6 +339,7 @@ class ResourceManager {
util::AllocationMap m_allocations;

std::list<std::unique_ptr<strategy::AllocationStrategy>> m_allocators;
std::vector<std::string> m_shared_allocator_names;

std::unordered_map<int, strategy::AllocationStrategy*> m_allocators_by_id;
std::unordered_map<std::string, strategy::AllocationStrategy*> m_allocators_by_name;
Expand Down
18 changes: 18 additions & 0 deletions src/umpire/Umpire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ void mark_event(const std::string& event)
[&](auto& e) { e.name("event").category(event::category::metadata).arg("name", event).tag("replay", "true"); });
}

std::size_t get_total_bytes_allocated()
{
auto& rm = umpire::ResourceManager::getInstance();
std::size_t total_memory{0};

for (auto s : rm.getResourceNames()) {
umpire::Allocator alloc = rm.getAllocator(s);
total_memory += alloc.getActualSize();
}

for (auto s : rm.getSharedAllocatorNames()) {
umpire::Allocator alloc = rm.getAllocator(s);
total_memory += alloc.getActualSize();
}

return total_memory;
}

std::size_t get_device_memory_usage(int device_id)
{
#if defined(UMPIRE_ENABLE_CUDA)
Expand Down
5 changes: 5 additions & 0 deletions src/umpire/Umpire.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ std::size_t get_process_memory_usage_hwm();
*/
void mark_event(const std::string& event);

/*!
* \brief Get the total umpire memory usage in bytes across all memory resources
*/
std::size_t get_total_bytes_allocated();

/*!
* \brief Get memory usage of device device_id, using appropriate underlying
* vendor API.
Expand Down

0 comments on commit 02df2c1

Please sign in to comment.