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

Add NamingShim to generate unique names for allocations #909

Open
wants to merge 9 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
14 changes: 11 additions & 3 deletions examples/cookbook/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ if (UMPIRE_ENABLE_NUMA)
endif ()

if (UMPIRE_ENABLE_IPC_SHARED_MEMORY)
if (UMPIRE_ENABLE_MPI)
blt_add_executable(
NAME recipe_shared_memory
SOURCES recipe_shared_memory.cpp
DEPENDS_ON ${cookbook_depends})
list(APPEND umpire_cookbooks recipe_shared_memory)
endif()

blt_add_executable(
NAME recipe_shared_memory
SOURCES recipe_shared_memory.cpp
NAME recipe_naming_shim
SOURCES recipe_naming_shim.cpp
DEPENDS_ON ${cookbook_depends})
list(APPEND umpire_cookbooks recipe_shared_memory)
list(APPEND umpire_cookbooks recipe_naming_shim)
endif()

if (UMPIRE_ENABLE_CUDA)
Expand Down
30 changes: 30 additions & 0 deletions examples/cookbook/recipe_naming_shim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016-24, Lawrence Livermore National Security, LLC and Umpire
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: (MIT)
//////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include "umpire/Allocator.hpp"
#include "umpire/ResourceManager.hpp"
#include "umpire/Umpire.hpp"
#include "umpire/config.hpp"
#include "umpire/resource/HostSharedMemoryResource.hpp"
#include "umpire/strategy/NamingShim.hpp"
#include "umpire/util/MemoryResourceTraits.hpp"

int main(int, char**)
{
auto& rm = umpire::ResourceManager::getInstance();
auto traits{umpire::get_default_resource_traits("SHARED")};
traits.size = 1 * 1024 * 1024; // Maximum size of this Allocator
traits.scope = umpire::MemoryResourceTraits::shared_scope::node; // default
auto node_allocator{rm.makeResource("SHARED::node_allocator", traits)};
auto shim{rm.makeAllocator<umpire::strategy::NamingShim>("shim", node_allocator)};

void* ptr = shim.allocate(1024);
std::cout << "Ptr = " << ptr << std::endl;
shim.deallocate(ptr);
}
12 changes: 12 additions & 0 deletions src/umpire/strategy/AllocationStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ void* AllocationStrategy::allocate_named(const std::string& UMPIRE_UNUSED_ARG(na
return allocate(bytes);
}

void* AllocationStrategy::allocate_named_internal(const std::string& name, std::size_t bytes)
{
m_current_size += bytes;
m_allocation_count++;

if (m_current_size > m_high_watermark) {
m_high_watermark = m_current_size;
}

return allocate_named(name, bytes);
}

void AllocationStrategy::deallocate_internal(void* ptr, std::size_t size)
{
m_current_size -= size;
Expand Down
2 changes: 2 additions & 0 deletions src/umpire/strategy/AllocationStrategy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class AllocationStrategy {

void* allocate_internal(std::size_t bytes);

void* allocate_named_internal(const std::string& name, std::size_t bytes);

void deallocate_internal(void* ptr, std::size_t size = 0);

/*!
Expand Down
2 changes: 2 additions & 0 deletions src/umpire/strategy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set (umpire_strategy_headers
MixedPool.hpp
MonotonicAllocationStrategy.hpp
NamedAllocationStrategy.hpp
NamingShim.hpp
PoolCoalesceHeuristic.hpp
QuickPool.hpp
SizeLimiter.hpp
Expand Down Expand Up @@ -48,6 +49,7 @@ set (umpire_strategy_sources
mixins/Inspector.cpp
MonotonicAllocationStrategy.cpp
NamedAllocationStrategy.cpp
NamingShim.cpp
QuickPool.cpp
SizeLimiter.cpp
SlotPool.cpp
Expand Down
49 changes: 49 additions & 0 deletions src/umpire/strategy/NamingShim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016-24, Lawrence Livermore National Security, LLC and Umpire
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: (MIT)
//////////////////////////////////////////////////////////////////////////////

#include "umpire/strategy/NamingShim.hpp"

#include "umpire/util/Macros.hpp"
#include "umpire/util/error.hpp"

namespace umpire {
namespace strategy {

NamingShim::NamingShim(const std::string& name, int id, Allocator allocator)
: AllocationStrategy{name, id, allocator.getAllocationStrategy(), "NamingShim"},
m_counter{0},
m_allocator(allocator.getAllocationStrategy())
{
}

NamingShim::~NamingShim()
{
}

void* NamingShim::allocate(std::size_t bytes)
{
std::string name{m_name + "_alloc_" + std::to_string(m_counter++)};
return m_allocator->allocate_named_internal(name, bytes);
}

void NamingShim::deallocate(void* ptr, std::size_t UMPIRE_UNUSED_ARG(size))
{
m_allocator->deallocate_internal(ptr);
}

Platform NamingShim::getPlatform() noexcept
{
return m_allocator->getPlatform();
}

MemoryResourceTraits NamingShim::getTraits() const noexcept
{
return m_allocator->getTraits();
}

} // end of namespace strategy
} // end of namespace umpire
37 changes: 37 additions & 0 deletions src/umpire/strategy/NamingShim.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016-24, Lawrence Livermore National Security, LLC and Umpire
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: (MIT)
//////////////////////////////////////////////////////////////////////////////
#ifndef UMPIRE_NamingShim_HPP
#define UMPIRE_NamingShim_HPP

#include "umpire/Allocator.hpp"
#include "umpire/strategy/AllocationStrategy.hpp"

namespace umpire {
namespace strategy {

class NamingShim : public AllocationStrategy {
public:
NamingShim(const std::string& name, int id, Allocator allocator);

~NamingShim();

void* allocate(std::size_t bytes) override;
void deallocate(void* ptr, std::size_t size) override;

Platform getPlatform() noexcept override;

MemoryResourceTraits getTraits() const noexcept override;

private:
std::size_t m_counter;
strategy::AllocationStrategy* m_allocator;
};

} // end of namespace strategy
} // end namespace umpire

#endif // UMPIRE_NamingShim_HPP
21 changes: 21 additions & 0 deletions tests/integration/strategy_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "umpire/strategy/MixedPool.hpp"
#include "umpire/strategy/MonotonicAllocationStrategy.hpp"
#include "umpire/strategy/NamedAllocationStrategy.hpp"
#include "umpire/strategy/NamingShim.hpp"
#include "umpire/strategy/QuickPool.hpp"
#include "umpire/strategy/SizeLimiter.hpp"
#include "umpire/strategy/SlotPool.hpp"
Expand Down Expand Up @@ -743,3 +744,23 @@ TEST(AlignedAllocator, BadAlignment)
},
umpire::runtime_error);
}

#if defined(UMPIRE_ENABLE_IPC_SHARED_MEMORY)
TEST(NamingShimTests, TestAllocateDeallocate)
{
auto& rm = umpire::ResourceManager::getInstance();

auto traits{umpire::get_default_resource_traits("SHARED")};
traits.size = 1 * 1024 * 1024;
traits.scope = umpire::MemoryResourceTraits::shared_scope::node;

auto node_allocator{rm.makeResource("SHARED::shim_allocator", traits)};

auto shim{rm.makeAllocator<umpire::strategy::NamingShim>("shim", node_allocator)};
{
void* ptr = shim.allocate(1024);
EXPECT_NE(ptr, nullptr);
shim.deallocate(ptr);
}
}
#endif
Loading