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 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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);
kab163 marked this conversation as resolved.
Show resolved Hide resolved
}

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;
kab163 marked this conversation as resolved.
Show resolved Hide resolved

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();
kab163 marked this conversation as resolved.
Show resolved Hide resolved
};

} // 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()
kab163 marked this conversation as resolved.
Show resolved Hide resolved
{
auto& rm = umpire::ResourceManager::getInstance();
std::size_t total_memory{0};

for (auto s : rm.getResourceNames()) {
kab163 marked this conversation as resolved.
Show resolved Hide resolved
umpire::Allocator alloc = rm.getAllocator(s);
total_memory += alloc.getActualSize();
}

for (auto s : rm.m_shared_allocators_by_name) {
kab163 marked this conversation as resolved.
Show resolved Hide resolved
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