From fe6cb122d43331c367db6910e65dbbeb9d3ae2fb Mon Sep 17 00:00:00 2001 From: Kristi Belcher Date: Fri, 17 Jan 2025 12:59:43 -0800 Subject: [PATCH 01/11] adding function to get total amount of umpire memory allocated --- src/umpire/Umpire.cpp | 13 +++++++++++++ src/umpire/Umpire.hpp | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/umpire/Umpire.cpp b/src/umpire/Umpire.cpp index ab84f87a9..a7160930d 100644 --- a/src/umpire/Umpire.cpp +++ b/src/umpire/Umpire.cpp @@ -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()) { + 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) diff --git a/src/umpire/Umpire.hpp b/src/umpire/Umpire.hpp index 2538f6fc4..f7056aa04 100644 --- a/src/umpire/Umpire.hpp +++ b/src/umpire/Umpire.hpp @@ -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. From fafc983fd70fea9222180a9ccae72ca63ec82f33 Mon Sep 17 00:00:00 2001 From: Kristi Belcher Date: Fri, 17 Jan 2025 13:01:45 -0800 Subject: [PATCH 02/11] adding example of using get_total_memory_usage func --- examples/size_limiter.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/examples/size_limiter.cpp b/examples/size_limiter.cpp index 4a4877bcd..fae064cda 100644 --- a/examples/size_limiter.cpp +++ b/examples/size_limiter.cpp @@ -10,6 +10,7 @@ #include "umpire/strategy/QuickPool.hpp" #include "umpire/strategy/SizeLimiter.hpp" #include "umpire/util/Macros.hpp" +#include "umpire/Umpire.hpp" int main(int, char**) { @@ -18,15 +19,30 @@ int main(int, char**) rm.makeAllocator("size_limited_alloc", rm.getAllocator("HOST"), 1024); auto pool = rm.makeAllocator("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; } From 77a662dda932b5f47309a8fa4aeee49b445da875 Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 17 Jan 2025 21:07:45 +0000 Subject: [PATCH 03/11] Apply style updates --- examples/size_limiter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/size_limiter.cpp b/examples/size_limiter.cpp index fae064cda..27acfb609 100644 --- a/examples/size_limiter.cpp +++ b/examples/size_limiter.cpp @@ -7,10 +7,10 @@ #include #include "umpire/ResourceManager.hpp" +#include "umpire/Umpire.hpp" #include "umpire/strategy/QuickPool.hpp" #include "umpire/strategy/SizeLimiter.hpp" #include "umpire/util/Macros.hpp" -#include "umpire/Umpire.hpp" int main(int, char**) { From 2c0ff4d1fe8db421a17eafe88c16ec65fcf902e1 Mon Sep 17 00:00:00 2001 From: Kristi Belcher Date: Fri, 17 Jan 2025 15:27:33 -0800 Subject: [PATCH 04/11] also collecting stats on shared memory allocators --- examples/size_limiter.cpp | 4 ++-- examples/tutorial/tut_allocator.cpp | 3 +++ src/umpire/ResourceManager.cpp | 4 ++++ src/umpire/ResourceManager.hpp | 2 ++ src/umpire/Umpire.cpp | 7 ++++++- src/umpire/Umpire.hpp | 2 +- 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/size_limiter.cpp b/examples/size_limiter.cpp index fae064cda..661adf428 100644 --- a/examples/size_limiter.cpp +++ b/examples/size_limiter.cpp @@ -29,7 +29,7 @@ int main(int, char**) } 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 << "The total amount of memory used was: " << umpire::get_total_memory_allocated() << std::endl; std::cout << "Attempting to allocate 512 bytes..." << std::endl; try { @@ -38,7 +38,7 @@ int main(int, char**) } 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 << "The total amount of memory used was: " << umpire::get_total_memory_allocated() << std::endl; if (data != nullptr) { pool.deallocate(data); diff --git a/examples/tutorial/tut_allocator.cpp b/examples/tutorial/tut_allocator.cpp index 1296eaf35..8c3a38678 100644 --- a/examples/tutorial/tut_allocator.cpp +++ b/examples/tutorial/tut_allocator.cpp @@ -6,6 +6,7 @@ ////////////////////////////////////////////////////////////////////////////// #include "umpire/Allocator.hpp" #include "umpire/ResourceManager.hpp" +#include "umpire/Umpire.hpp" int main(int, char**) { @@ -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 diff --git a/src/umpire/ResourceManager.cpp b/src/umpire/ResourceManager.cpp index ee58038a6..5987f7c70 100644 --- a/src/umpire/ResourceManager.cpp +++ b/src/umpire/ResourceManager.cpp @@ -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 allocator{registry.makeMemoryResource(name, getNextId(), traits)}; allocator->setTracking(traits.tracking); diff --git a/src/umpire/ResourceManager.hpp b/src/umpire/ResourceManager.hpp index f61d8853e..e39ce9fb2 100644 --- a/src/umpire/ResourceManager.hpp +++ b/src/umpire/ResourceManager.hpp @@ -328,6 +328,7 @@ class ResourceManager { util::AllocationMap m_allocations; std::list> m_allocators; + std::vector m_shared_allocators_by_name; std::unordered_map m_allocators_by_id; std::unordered_map m_allocators_by_name; @@ -347,6 +348,7 @@ class ResourceManager { friend std::vector get_allocator_records(Allocator); friend strategy::ZeroByteHandler; friend strategy::mixins::AllocateNull; + friend std::size_t get_total_memory_allocated(); }; } // end namespace umpire diff --git a/src/umpire/Umpire.cpp b/src/umpire/Umpire.cpp index a7160930d..d18d1ec20 100644 --- a/src/umpire/Umpire.cpp +++ b/src/umpire/Umpire.cpp @@ -173,7 +173,7 @@ 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() +std::size_t get_total_memory_allocated() { auto& rm = umpire::ResourceManager::getInstance(); std::size_t total_memory{0}; @@ -183,6 +183,11 @@ std::size_t get_total_memory_usage() 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; } diff --git a/src/umpire/Umpire.hpp b/src/umpire/Umpire.hpp index f7056aa04..ca688e385 100644 --- a/src/umpire/Umpire.hpp +++ b/src/umpire/Umpire.hpp @@ -165,7 +165,7 @@ 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(); +std::size_t get_total_memory_allocated(); /*! * \brief Get memory usage of device device_id, using appropriate underlying From 0e406b03b5d87fc63f47b86ccbde8ea2c7c886ca Mon Sep 17 00:00:00 2001 From: Kristi Date: Thu, 23 Jan 2025 10:23:04 -0800 Subject: [PATCH 05/11] Update src/umpire/ResourceManager.hpp Co-authored-by: David Beckingsale --- src/umpire/ResourceManager.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/umpire/ResourceManager.hpp b/src/umpire/ResourceManager.hpp index e39ce9fb2..dc5bd52c6 100644 --- a/src/umpire/ResourceManager.hpp +++ b/src/umpire/ResourceManager.hpp @@ -328,7 +328,7 @@ class ResourceManager { util::AllocationMap m_allocations; std::list> m_allocators; - std::vector m_shared_allocators_by_name; + std::vector m_shared_allocator_names; std::unordered_map m_allocators_by_id; std::unordered_map m_allocators_by_name; From fc8704cf11b2ef21c345f65b37c7169ace015a0d Mon Sep 17 00:00:00 2001 From: Kristi Belcher Date: Thu, 23 Jan 2025 10:26:08 -0800 Subject: [PATCH 06/11] reverting changes to size_limiter example --- examples/size_limiter.cpp | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/examples/size_limiter.cpp b/examples/size_limiter.cpp index bd9965cdb..4a4877bcd 100644 --- a/examples/size_limiter.cpp +++ b/examples/size_limiter.cpp @@ -7,7 +7,6 @@ #include #include "umpire/ResourceManager.hpp" -#include "umpire/Umpire.hpp" #include "umpire/strategy/QuickPool.hpp" #include "umpire/strategy/SizeLimiter.hpp" #include "umpire/util/Macros.hpp" @@ -19,30 +18,15 @@ int main(int, char**) rm.makeAllocator("size_limited_alloc", rm.getAllocator("HOST"), 1024); auto pool = rm.makeAllocator("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 { - data = pool.allocate(2048); + void* 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; } From 1008e531b02b3dded53e1ad98ae8c9d1505635f9 Mon Sep 17 00:00:00 2001 From: Kristi Belcher Date: Thu, 23 Jan 2025 10:50:02 -0800 Subject: [PATCH 07/11] updating var names --- src/umpire/ResourceManager.cpp | 12 +++++++++++- src/umpire/ResourceManager.hpp | 12 +++++++++++- src/umpire/Umpire.cpp | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/umpire/ResourceManager.cpp b/src/umpire/ResourceManager.cpp index 02253be91..4571dff6a 100644 --- a/src/umpire/ResourceManager.cpp +++ b/src/umpire/ResourceManager.cpp @@ -156,7 +156,7 @@ Allocator ResourceManager::makeResource(const std::string& name, MemoryResourceT } if (name.find("SHARED") != std::string::npos) { - m_shared_allocators_by_name.push_back(name); + m_shared_allocator_names.push_back(name); } std::unique_ptr allocator{registry.makeMemoryResource(name, getNextId(), traits)}; @@ -272,6 +272,16 @@ std::vector ResourceManager::getResourceNames() return registry.getResourceNames(); } +std::vector ResourceManager::getSharedAllocNames() +{ + if (m_shared_allocator_names.size() == 0) { + UMPIRE_LOG(Debug, "Called getSharedAllocNames, but there are none. Returning empty vector."); + return std::vector(); // Return an empty vector of strings + } + + return m_shared_allocator_names; +} + void ResourceManager::setDefaultAllocator(Allocator allocator) noexcept { UMPIRE_LOG(Debug, "(\"" << allocator.getName() << "\")"); diff --git a/src/umpire/ResourceManager.hpp b/src/umpire/ResourceManager.hpp index dc5bd52c6..298821310 100644 --- a/src/umpire/ResourceManager.hpp +++ b/src/umpire/ResourceManager.hpp @@ -100,6 +100,17 @@ class ResourceManager { */ std::vector 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 getSharedAllocNames(); + /*! * \brief Set the default Allocator. * @@ -348,7 +359,6 @@ class ResourceManager { friend std::vector get_allocator_records(Allocator); friend strategy::ZeroByteHandler; friend strategy::mixins::AllocateNull; - friend std::size_t get_total_memory_allocated(); }; } // end namespace umpire diff --git a/src/umpire/Umpire.cpp b/src/umpire/Umpire.cpp index d18d1ec20..fabd7cf08 100644 --- a/src/umpire/Umpire.cpp +++ b/src/umpire/Umpire.cpp @@ -183,7 +183,7 @@ std::size_t get_total_memory_allocated() total_memory += alloc.getActualSize(); } - for (auto s : rm.m_shared_allocators_by_name) { + for (auto s : rm.getSharedAllocNames()) { umpire::Allocator alloc = rm.getAllocator(s); total_memory += alloc.getActualSize(); } From aeb1243f7b538cf354686c3610b69cba39bb1ac3 Mon Sep 17 00:00:00 2001 From: Kristi Belcher Date: Thu, 23 Jan 2025 10:51:56 -0800 Subject: [PATCH 08/11] changing func name --- examples/tutorial/tut_allocator.cpp | 2 +- src/umpire/Umpire.cpp | 2 +- src/umpire/Umpire.hpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/tutorial/tut_allocator.cpp b/examples/tutorial/tut_allocator.cpp index 8c3a38678..8b45b9be9 100644 --- a/examples/tutorial/tut_allocator.cpp +++ b/examples/tutorial/tut_allocator.cpp @@ -32,7 +32,7 @@ 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; + std::cout << "The total amount of memory used was: " << umpire::get_total_bytes_allocated() << std::endl; // _sphinx_tag_tut_deallocate_start allocator.deallocate(data); diff --git a/src/umpire/Umpire.cpp b/src/umpire/Umpire.cpp index fabd7cf08..b142fad36 100644 --- a/src/umpire/Umpire.cpp +++ b/src/umpire/Umpire.cpp @@ -173,7 +173,7 @@ 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() +std::size_t get_total_bytes_allocated() { auto& rm = umpire::ResourceManager::getInstance(); std::size_t total_memory{0}; diff --git a/src/umpire/Umpire.hpp b/src/umpire/Umpire.hpp index ca688e385..1344fe7b1 100644 --- a/src/umpire/Umpire.hpp +++ b/src/umpire/Umpire.hpp @@ -165,7 +165,7 @@ 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(); +std::size_t get_total_bytes_allocated(); /*! * \brief Get memory usage of device device_id, using appropriate underlying From 816fa60bc7d12a6f9e3fb6466f67542236f5e90d Mon Sep 17 00:00:00 2001 From: github-actions Date: Thu, 23 Jan 2025 18:56:23 +0000 Subject: [PATCH 09/11] Apply style updates --- src/umpire/ResourceManager.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/umpire/ResourceManager.hpp b/src/umpire/ResourceManager.hpp index 298821310..ca0960533 100644 --- a/src/umpire/ResourceManager.hpp +++ b/src/umpire/ResourceManager.hpp @@ -104,7 +104,7 @@ class ResourceManager { * \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 + * 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. From 72576dd01367ead46ae8a74c337c9816d15ac963 Mon Sep 17 00:00:00 2001 From: Kristi Belcher Date: Thu, 23 Jan 2025 11:02:23 -0800 Subject: [PATCH 10/11] example of func with shared allocators --- examples/cookbook/recipe_naming_shim.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/cookbook/recipe_naming_shim.cpp b/examples/cookbook/recipe_naming_shim.cpp index bd24c6eb3..076502678 100644 --- a/examples/cookbook/recipe_naming_shim.cpp +++ b/examples/cookbook/recipe_naming_shim.cpp @@ -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); } From e953e95bf385ae37581307b289b712e7460b6915 Mon Sep 17 00:00:00 2001 From: Kristi Belcher Date: Thu, 23 Jan 2025 15:28:36 -0800 Subject: [PATCH 11/11] changing function name to appease David --- src/umpire/ResourceManager.cpp | 4 ++-- src/umpire/ResourceManager.hpp | 2 +- src/umpire/Umpire.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/umpire/ResourceManager.cpp b/src/umpire/ResourceManager.cpp index 4571dff6a..370c26b85 100644 --- a/src/umpire/ResourceManager.cpp +++ b/src/umpire/ResourceManager.cpp @@ -272,10 +272,10 @@ std::vector ResourceManager::getResourceNames() return registry.getResourceNames(); } -std::vector ResourceManager::getSharedAllocNames() +std::vector ResourceManager::getSharedAllocatorNames() { if (m_shared_allocator_names.size() == 0) { - UMPIRE_LOG(Debug, "Called getSharedAllocNames, but there are none. Returning empty vector."); + UMPIRE_LOG(Debug, "Called getSharedAllocatorNames, but there are none. Returning empty vector."); return std::vector(); // Return an empty vector of strings } diff --git a/src/umpire/ResourceManager.hpp b/src/umpire/ResourceManager.hpp index ca0960533..87ef68249 100644 --- a/src/umpire/ResourceManager.hpp +++ b/src/umpire/ResourceManager.hpp @@ -109,7 +109,7 @@ class ResourceManager { * * \return A vector of strings with the available SHARED allocator names. */ - std::vector getSharedAllocNames(); + std::vector getSharedAllocatorNames(); /*! * \brief Set the default Allocator. diff --git a/src/umpire/Umpire.cpp b/src/umpire/Umpire.cpp index b142fad36..9f4b222c5 100644 --- a/src/umpire/Umpire.cpp +++ b/src/umpire/Umpire.cpp @@ -183,7 +183,7 @@ std::size_t get_total_bytes_allocated() total_memory += alloc.getActualSize(); } - for (auto s : rm.getSharedAllocNames()) { + for (auto s : rm.getSharedAllocatorNames()) { umpire::Allocator alloc = rm.getAllocator(s); total_memory += alloc.getActualSize(); }