From 097ebf6f274ebd15cca3fede08a4f6a49dc76a15 Mon Sep 17 00:00:00 2001 From: David Beckingsale Date: Wed, 31 May 2023 09:52:24 -0700 Subject: [PATCH 1/5] Add transfer function --- examples/CMakeLists.txt | 6 ++++++ examples/transfer.cpp | 25 +++++++++++++++++++++++++ src/umpire/ResourceManager.cpp | 11 +++++++++++ src/umpire/ResourceManager.hpp | 8 ++++++++ 4 files changed, 50 insertions(+) create mode 100644 examples/transfer.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 330a51a1b..3a9d4d2d3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -12,6 +12,12 @@ if (UMPIRE_ENABLE_OPENMP_TARGET) openmp) endif() +blt_add_executable( + NAME transfer + SOURCES transfer.cpp + DEPENDS_ON ${example_depends} +) + blt_add_executable( NAME aligned_allocator SOURCES aligned_allocator.cpp diff --git a/examples/transfer.cpp b/examples/transfer.cpp new file mode 100644 index 000000000..30ab7f713 --- /dev/null +++ b/examples/transfer.cpp @@ -0,0 +1,25 @@ +////////////////////////////////////////////////////////////////////////////// +// Copyright (c) 2016-23, Lawrence Livermore National Security, LLC and Umpire +// project contributors. See the COPYRIGHT file for details. +// +// SPDX-License-Identifier: (MIT) +////////////////////////////////////////////////////////////////////////////// +#include + +#include "umpire/ResourceManager.hpp" +#include "umpire/strategy/NamedAllocationStrategy.hpp" + +int main(int, char**) +{ + auto& rm = umpire::ResourceManager::getInstance(); + auto alloc_one = rm.makeAllocator("named_one", rm.getAllocator("HOST")); + auto alloc_two = rm.makeAllocator("named_two", rm.getAllocator("HOST")); + + void* test = alloc_one.allocate(100); + + rm.transfer(test, alloc_two); + + alloc_two.deallocate(test); + + return 0; +} diff --git a/src/umpire/ResourceManager.cpp b/src/umpire/ResourceManager.cpp index 95dad16d2..a0721f6d6 100644 --- a/src/umpire/ResourceManager.cpp +++ b/src/umpire/ResourceManager.cpp @@ -359,6 +359,17 @@ util::AllocationRecord ResourceManager::deregisterAllocation(void* ptr) return m_allocations.remove(ptr); } +void ResourceManager::transfer(void* ptr, Allocator to) +{ + auto alloc_record = findAllocationRecord(ptr); + Allocator from{alloc_record->strategy}; + + auto record = from.deregisterAllocation(ptr, alloc_record->strategy); + + record.strategy = to.getAllocationStrategy(); + to.registerAllocation(ptr, record.size, record.strategy); +} + const util::AllocationRecord* ResourceManager::findAllocationRecord(void* ptr) const { auto alloc_record = m_allocations.find(ptr); diff --git a/src/umpire/ResourceManager.hpp b/src/umpire/ResourceManager.hpp index 8a71f524a..707397d77 100644 --- a/src/umpire/ResourceManager.hpp +++ b/src/umpire/ResourceManager.hpp @@ -181,6 +181,14 @@ class ResourceManager { */ util::AllocationRecord deregisterAllocation(void* ptr); + /*! + * + * + * + * + */ + void transfer(void* ptr, Allocator to); + /*! * \brief Find the allocation record associated with an address ptr. * From 719277c517a47d4ee50e125c5590b232937d502e Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 31 May 2023 16:53:53 +0000 Subject: [PATCH 2/5] 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 707397d77..ac8bbde95 100644 --- a/src/umpire/ResourceManager.hpp +++ b/src/umpire/ResourceManager.hpp @@ -185,7 +185,7 @@ class ResourceManager { * * * - * + * */ void transfer(void* ptr, Allocator to); From 1d5de4e53a791bd6e38baae15cec66c83667377e Mon Sep 17 00:00:00 2001 From: David Beckingsale Date: Wed, 31 May 2023 10:21:24 -0700 Subject: [PATCH 3/5] Add transfer op --- src/umpire/ResourceManager.cpp | 16 +++++--- src/umpire/ResourceManager.hpp | 3 +- src/umpire/strategy/AllocationStrategy.cpp | 5 +++ src/umpire/strategy/AllocationStrategy.hpp | 2 + .../strategy/NamedAllocationStrategy.cpp | 5 +++ .../strategy/NamedAllocationStrategy.hpp | 2 + tests/integration/operation_tests.cpp | 39 +++++++++++++++++++ 7 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/umpire/ResourceManager.cpp b/src/umpire/ResourceManager.cpp index a0721f6d6..10f0f0b5b 100644 --- a/src/umpire/ResourceManager.cpp +++ b/src/umpire/ResourceManager.cpp @@ -359,15 +359,21 @@ util::AllocationRecord ResourceManager::deregisterAllocation(void* ptr) return m_allocations.remove(ptr); } -void ResourceManager::transfer(void* ptr, Allocator to) +void* ResourceManager::transfer(void* ptr, Allocator to) { auto alloc_record = findAllocationRecord(ptr); - Allocator from{alloc_record->strategy}; - auto record = from.deregisterAllocation(ptr, alloc_record->strategy); + if (! (alloc_record->strategy->treatAsPassthrough() && to.getAllocationStrategy()->treatAsPassthrough()) ) { + return move(ptr, to); + } else { + Allocator from{alloc_record->strategy}; + auto record = from.deregisterAllocation(ptr, alloc_record->strategy); + + record.strategy = to.getAllocationStrategy(); + to.registerAllocation(ptr, record.size, record.strategy); - record.strategy = to.getAllocationStrategy(); - to.registerAllocation(ptr, record.size, record.strategy); + return ptr; + } } const util::AllocationRecord* ResourceManager::findAllocationRecord(void* ptr) const diff --git a/src/umpire/ResourceManager.hpp b/src/umpire/ResourceManager.hpp index 707397d77..03db3aeb8 100644 --- a/src/umpire/ResourceManager.hpp +++ b/src/umpire/ResourceManager.hpp @@ -185,9 +185,8 @@ class ResourceManager { * * * - * */ - void transfer(void* ptr, Allocator to); + void* transfer(void* ptr, Allocator to); /*! * \brief Find the allocation record associated with an address ptr. diff --git a/src/umpire/strategy/AllocationStrategy.cpp b/src/umpire/strategy/AllocationStrategy.cpp index 719294d76..ce460bcea 100644 --- a/src/umpire/strategy/AllocationStrategy.cpp +++ b/src/umpire/strategy/AllocationStrategy.cpp @@ -109,6 +109,11 @@ bool AllocationStrategy::isTracked() const noexcept return m_tracked; } +bool AllocationStrategy::treatAsPassthrough() const noexcept +{ + return false; +} + std::ostream& operator<<(std::ostream& os, const AllocationStrategy& strategy) { os << "[" << strategy.m_name << "," << strategy.m_id << "]"; diff --git a/src/umpire/strategy/AllocationStrategy.hpp b/src/umpire/strategy/AllocationStrategy.hpp index 5d1a8de3c..99d6adf34 100644 --- a/src/umpire/strategy/AllocationStrategy.hpp +++ b/src/umpire/strategy/AllocationStrategy.hpp @@ -130,6 +130,8 @@ class AllocationStrategy { bool isTracked() const noexcept; + virtual bool treatAsPassthrough() const noexcept; + std::size_t m_current_size{0}; std::size_t m_high_watermark{0}; std::size_t m_allocation_count{0}; diff --git a/src/umpire/strategy/NamedAllocationStrategy.cpp b/src/umpire/strategy/NamedAllocationStrategy.cpp index e406cf67e..b1d7b3663 100644 --- a/src/umpire/strategy/NamedAllocationStrategy.cpp +++ b/src/umpire/strategy/NamedAllocationStrategy.cpp @@ -37,5 +37,10 @@ MemoryResourceTraits NamedAllocationStrategy::getTraits() const noexcept return m_allocator->getTraits(); } +bool NamedAllocationStrategy::treatAsPassthrough() const noexcept +{ + return true; +} + } // end of namespace strategy } // end of namespace umpire diff --git a/src/umpire/strategy/NamedAllocationStrategy.hpp b/src/umpire/strategy/NamedAllocationStrategy.hpp index 168a1b4e9..a28124ab9 100644 --- a/src/umpire/strategy/NamedAllocationStrategy.hpp +++ b/src/umpire/strategy/NamedAllocationStrategy.hpp @@ -24,6 +24,8 @@ class NamedAllocationStrategy : public AllocationStrategy { MemoryResourceTraits getTraits() const noexcept override; + bool treatAsPassthrough() const noexcept override; + protected: strategy::AllocationStrategy* m_allocator; }; diff --git a/tests/integration/operation_tests.cpp b/tests/integration/operation_tests.cpp index 7a177ee14..121b0b772 100644 --- a/tests/integration/operation_tests.cpp +++ b/tests/integration/operation_tests.cpp @@ -836,3 +836,42 @@ TEST(AsyncTest, Prefetch) alloc.deallocate(array); } #endif + + +TEST(Transfer, Transfer) +{ + auto& rm = umpire::ResourceManager::getInstance(); + + auto alloc_one = rm.makeAllocator("transfer_one", rm.getAllocator("HOST")); + auto alloc_two = rm.makeAllocator("transfer_two", rm.getAllocator("HOST")); + + void* data = alloc_one.allocate(100); + void* xfer_data{nullptr}; + + ASSERT_NO_THROW(xfer_data = rm.transfer(data, alloc_two)); + + ASSERT_EQ(data, xfer_data); + + ASSERT_NO_THROW(alloc_two.deallocate(xfer_data)); +} + +TEST(Transfer, Move) +{ + auto& rm = umpire::ResourceManager::getInstance(); + + auto alloc_one = rm.makeAllocator("transfer_three", rm.getAllocator("HOST")); + auto alloc_two = rm.makeAllocator("transfer_four", rm.getAllocator("HOST")); + + void* xfer_data{nullptr}; + + void* data = alloc_one.allocate(100); + ASSERT_NO_THROW(xfer_data = rm.transfer(data, alloc_two)); + ASSERT_NE(xfer_data, data); + ASSERT_NO_THROW(alloc_two.deallocate(xfer_data)); + + xfer_data = nullptr; + data = alloc_two.allocate(100); + ASSERT_NO_THROW(xfer_data = rm.transfer(data, alloc_one)); + ASSERT_NE(xfer_data, data); + ASSERT_NO_THROW(alloc_one.deallocate(xfer_data)); +} \ No newline at end of file From f24708c5dba89f47bd54eb232ca52433273ee337 Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 31 May 2023 17:23:39 +0000 Subject: [PATCH 4/5] Apply style updates --- src/umpire/ResourceManager.cpp | 2 +- tests/integration/operation_tests.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/umpire/ResourceManager.cpp b/src/umpire/ResourceManager.cpp index 10f0f0b5b..88d8f91e2 100644 --- a/src/umpire/ResourceManager.cpp +++ b/src/umpire/ResourceManager.cpp @@ -363,7 +363,7 @@ void* ResourceManager::transfer(void* ptr, Allocator to) { auto alloc_record = findAllocationRecord(ptr); - if (! (alloc_record->strategy->treatAsPassthrough() && to.getAllocationStrategy()->treatAsPassthrough()) ) { + if (!(alloc_record->strategy->treatAsPassthrough() && to.getAllocationStrategy()->treatAsPassthrough())) { return move(ptr, to); } else { Allocator from{alloc_record->strategy}; diff --git a/tests/integration/operation_tests.cpp b/tests/integration/operation_tests.cpp index 121b0b772..725fcfde3 100644 --- a/tests/integration/operation_tests.cpp +++ b/tests/integration/operation_tests.cpp @@ -837,7 +837,6 @@ TEST(AsyncTest, Prefetch) } #endif - TEST(Transfer, Transfer) { auto& rm = umpire::ResourceManager::getInstance(); @@ -860,7 +859,8 @@ TEST(Transfer, Move) auto& rm = umpire::ResourceManager::getInstance(); auto alloc_one = rm.makeAllocator("transfer_three", rm.getAllocator("HOST")); - auto alloc_two = rm.makeAllocator("transfer_four", rm.getAllocator("HOST")); + auto alloc_two = + rm.makeAllocator("transfer_four", rm.getAllocator("HOST")); void* xfer_data{nullptr}; From 7e24c5683e25edbb11e07afcdc955642b9af5818 Mon Sep 17 00:00:00 2001 From: David Beckingsale Date: Wed, 31 May 2023 15:21:41 -0700 Subject: [PATCH 5/5] Attempt to resolve error --- src/umpire/event/event.hpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/umpire/event/event.hpp b/src/umpire/event/event.hpp index 03f0d3777..272936d23 100644 --- a/src/umpire/event/event.hpp +++ b/src/umpire/event/event.hpp @@ -15,10 +15,45 @@ #include #include "camp/camp.hpp" +#include "camp/resource.hpp" #include "umpire/event/recorder_factory.hpp" #include "umpire/util/Macros.hpp" +namespace camp { +namespace resources { +inline namespace v1 { + +inline std::string to_string(camp::resources::Resource& r) +{ + switch (r.get_platform()) { + case camp::resources::Platform::cuda: + return "camp::resource::Cuda"; + break; + case camp::resources::Platform::hip: + return "camp::resource::Hip"; + break; + case camp::resources::Platform::host: + return "camp::resource::Host"; + break; + case camp::resources::Platform::omp_target: + return "camp::resource::OmpTarget"; + break; + case camp::resources::Platform::sycl: + return "camp::resource::Sycl"; + break; + case camp::resources::Platform::undefined: + return "camp::resource::Undefined"; + } + + return "unkown resource"; +} + +} // namespace v1 +} // namespace resources +} // namespace camp + namespace umpire { + namespace event { namespace {