Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the Total Amount of Umpire Memory Allocated #929

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion examples/size_limiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <iostream>

#include "umpire/ResourceManager.hpp"
#include "umpire/Umpire.hpp"
#include "umpire/strategy/QuickPool.hpp"
#include "umpire/strategy/SizeLimiter.hpp"
#include "umpire/util/Macros.hpp"
Expand All @@ -18,15 +19,30 @@ int main(int, char**)
rm.makeAllocator<umpire::strategy::SizeLimiter>("size_limited_alloc", rm.getAllocator("HOST"), 1024);

auto pool = rm.makeAllocator<umpire::strategy::QuickPool>("pool", size_limited_alloc, 64, 64);
void* data;

// This will throw an exception because the pool is limited to 1024 bytes.
std::cout << "Attempting to allocate 2098 bytes..." << std::endl;
try {
void* data = pool.allocate(2048);
data = pool.allocate(2048);
UMPIRE_USE_VAR(data);
} catch (...) {
std::cout << "Exception caught! Pool is limited to 1024 bytes." << std::endl;
}
std::cout << "The total amount of memory used was: " << umpire::get_total_memory_usage() << std::endl;

std::cout << "Attempting to allocate 512 bytes..." << std::endl;
try {
data = pool.allocate(512);
UMPIRE_USE_VAR(data);
} catch (...) {
std::cout << "Exception caught! Pool is limited to 1024 bytes." << std::endl;
}
std::cout << "The total amount of memory used was: " << umpire::get_total_memory_usage() << std::endl;

if (data != nullptr) {
pool.deallocate(data);
}

return 0;
}
13 changes: 13 additions & 0 deletions src/umpire/Umpire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ 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_memory_usage()
{
auto& rm = umpire::ResourceManager::getInstance();
std::size_t total_memory{0};

for (auto s : rm.getResourceNames()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this limited to the base resources (PINNED, UNIFIED_MEMORY, DEVICE, HOST, etc...)? Or does it include the pools built on top of them? Just want to make sure we're not double counting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just resources - this is the correct way to get everything (including any alignment adjustments) without double counting.

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_memory_usage();

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