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 all 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_allocated() << 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_allocated() << std::endl;

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

return 0;
}
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_memory_allocated() << std::endl;

// _sphinx_tag_tut_deallocate_start
allocator.deallocate(data);
// _sphinx_tag_tut_deallocate_end
Expand Down
4 changes: 4 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_allocators_by_name.push_back(name);
}

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

Expand Down
2 changes: 2 additions & 0 deletions src/umpire/ResourceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class ResourceManager {
util::AllocationMap m_allocations;

std::list<std::unique_ptr<strategy::AllocationStrategy>> m_allocators;
std::vector<std::string> m_shared_allocators_by_name;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
std::vector<std::string> m_shared_allocators_by_name;
std::vector<std::string> m_shared_allocator_names;

The by_name suggests it's a structure to access the allocators by their names (like a map). This isn't the case, so I suggest a slightly different name.


std::unordered_map<int, strategy::AllocationStrategy*> m_allocators_by_id;
std::unordered_map<std::string, strategy::AllocationStrategy*> m_allocators_by_name;
Expand All @@ -347,6 +348,7 @@ class ResourceManager {
friend std::vector<util::AllocationRecord> get_allocator_records(Allocator);
friend strategy::ZeroByteHandler;
friend strategy::mixins::AllocateNull;
friend std::size_t get_total_memory_allocated();
Copy link
Member

Choose a reason for hiding this comment

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

Add a method to the get the names, rather than having this be a friend and accessing the data structure directly

};

} // end namespace umpire
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_memory_allocated()
{
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();
}

for (auto s : rm.m_shared_allocators_by_name) {
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_allocated();

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