Skip to content

Commit 38d921c

Browse files
committed
Backport deactivation flags [for 0.2.1]
1 parent 6ef2543 commit 38d921c

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ option(CPPUDDLE_WITH_KOKKOS "Enable KOKKOS tests/examples" OFF)
2929
option(CPPUDDLE_WITH_CLANG_TIDY "Enable clang tidy warnings" OFF)
3030
option(CPPUDDLE_WITH_CLANG_FORMAT "Enable clang format target" OFF)
3131

32+
# backported flags for performance experiments
33+
option(CPPUDDLE_WITH_BUFFER_RECYCLING "Enables the default recycling behaviour! Turning this off will have a major negative performance impact and is only intended for testing!" ON)
34+
option(CPPUDDLE_WITH_AGGRESSIVE_CONTENT_RECYCLING "Allows the aggressive allocators variants to reuse contents from previous buffers (and thus skip initializations)" ON)
35+
3236
if (CPPUDDLE_WITH_CUDA)
3337
enable_language(CUDA)
3438
endif ()
@@ -113,6 +117,21 @@ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
113117
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>
114118
)
115119

120+
# Backported flags handling
121+
if(CPPUDDLE_WITH_BUFFER_RECYCLING)
122+
message(INFO " Using default buffer recycling behaviour!")
123+
else()
124+
message(WARNING " Slow Build: Buffer recycling is deactivated. This should only be used for performance tests!")
125+
target_compile_definitions(buffer_manager INTERFACE "CPPUDDLE_DEACTIVATE_BUFFER_RECYCLING")
126+
endif()
127+
128+
if(CPPUDDLE_WITH_AGGRESSIVE_CONTENT_RECYCLING)
129+
message(INFO " Using default behaviour for aggressive content reusage (only relevant for aggressive allocators)!")
130+
else()
131+
target_compile_definitions(buffer_manager INTERFACE "CPPUDDLE_DEACTIVATE_AGGRESSIVE_ALLOCATORS")
132+
message(WARNING " Slow Build: Aggressive allocators (and thus content recycling) is disabled. This should only be used for performance tests!")
133+
endif()
134+
116135
# install libs with the defitions:
117136
install(TARGETS buffer_manager EXPORT CPPuddle
118137
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
@@ -130,6 +149,9 @@ install(EXPORT CPPuddle NAMESPACE CPPuddle:: DESTINATION ${CMAKE_INSTALL_PREFIX}
130149

131150
## Add target for tests and tests definitions
132151
if (CPPUDDLE_WITH_TESTS)
152+
if(NOT CPPUDDLE_WITH_BUFFER_RECYCLING)
153+
message(FATAL_ERROR "The CPPuddle tests only work with CPPUDDLE_WITH_BUFFER_RECYCLING=ON. Turning off buffer recycling is not recommended in general!")
154+
endif()
133155
add_executable(allocator_test tests/allocator_test.cpp)
134156
target_link_libraries(allocator_test
135157
${Boost_LIBRARIES} Boost::boost Boost::program_options buffer_manager)

include/buffer_manager.hpp

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ void destroy_n(ForwardIt first, Size n) {
5252
class buffer_recycler {
5353
// Public interface
5454
public:
55+
#if defined(CPPUDDLE_DEACTIVATE_BUFFER_RECYCLING)
56+
57+
// Warn about suboptimal performance without recycling
58+
#pragma message \
59+
"Warning: Building without buffer recycling! Use only for performance testing! \
60+
For better performance configure CPPuddle with CPPUDDLE_WITH_BUFFER_RECYCLING=ON!"
61+
62+
template <typename T, typename Host_Allocator>
63+
static T *get(size_t number_elements, bool manage_content_lifetime = false,
64+
std::optional<size_t> location_hint = std::nullopt,
65+
std::optional<size_t> device_id = std::nullopt) {
66+
67+
return Host_Allocator{}.allocate(number_elements);
68+
}
69+
/// Marks an buffer as unused and fit for reusage
70+
template <typename T, typename Host_Allocator>
71+
static void mark_unused(T *p, size_t number_elements,
72+
std::optional<size_t> location_hint = std::nullopt,
73+
std::optional<size_t> device_id = std::nullopt) {
74+
return Host_Allocator{}.deallocate(p, number_elements);
75+
}
76+
/// Fail when internal reference counting is used but recylcing is turned off
77+
template <typename T, typename Host_Allocator>
78+
static void increase_usage_counter(T *p, size_t number_elements) noexcept {
79+
std::cerr << "ERROR: CPPuddle v0.2.1 does not support internal reference counting "
80+
"with CPPUDDLE_WITH_BUFFER_RECYCLING=OFF. Please re-enable buffer recycling! Aborting..."
81+
<< std::endl;
82+
abort();
83+
}
84+
#else
5585
/// Returns and allocated buffer of the requested size - this may be a reused
5686
/// buffer
5787
template <typename T, typename Host_Allocator>
@@ -79,13 +109,6 @@ class buffer_recycler {
79109
return buffer_manager<T, Host_Allocator>::mark_unused(p, number_elements);
80110
}
81111
}
82-
83-
template <typename T, typename Host_Allocator>
84-
static void register_allocator_counters_with_hpx(void) {
85-
std::cerr << "Warning: CPPuddle v0.2.1 does not yet support HPX counters "
86-
"-- this operation will be ignored!"
87-
<< std::endl;
88-
}
89112
/// Increase the reference coutner of a buffer
90113
template <typename T, typename Host_Allocator>
91114
static void increase_usage_counter(T *p, size_t number_elements) noexcept {
@@ -96,6 +119,14 @@ class buffer_recycler {
96119
p, number_elements);
97120
}
98121
}
122+
#endif
123+
124+
template <typename T, typename Host_Allocator>
125+
static void register_allocator_counters_with_hpx(void) {
126+
std::cerr << "Warning: CPPuddle v0.2.1 does not yet support HPX counters "
127+
"-- this operation will be ignored!"
128+
<< std::endl;
129+
}
99130
/// Deallocate all buffers, no matter whether they are marked as used or not
100131
static void clean_all() {
101132
std::lock_guard<std::mutex> guard(mut);
@@ -661,6 +692,11 @@ struct aggressive_recycle_allocator {
661692
void deallocate(T *p, std::size_t n) {
662693
buffer_recycler::mark_unused<T, Host_Allocator>(p, n);
663694
}
695+
void increase_usage_counter(T *p, size_t n) {
696+
buffer_recycler::increase_usage_counter<T, Host_Allocator>(p, n);
697+
}
698+
699+
#ifndef CPPUDDLE_DEACTIVATE_AGGRESSIVE_ALLOCATORS
664700
template <typename... Args>
665701
inline void construct(T *p, Args... args) noexcept {
666702
// Do nothing here - we reuse the content of the last owner
@@ -669,9 +705,17 @@ struct aggressive_recycle_allocator {
669705
// Do nothing here - Contents will be destroyed when the buffer manager is
670706
// destroyed, not before
671707
}
672-
void increase_usage_counter(T *p, size_t n) {
673-
buffer_recycler::increase_usage_counter<T, Host_Allocator>(p, n);
708+
#else
709+
// Warn about suboptimal performance without recycling
710+
#pragma message \
711+
"Warning: Building without content reusage for aggressive allocators! \
712+
For better performance configure with CPPUDDLE_WITH_AGGRESSIVE_CONTENT_RECYCLING=ON !"
713+
template <typename... Args>
714+
inline void construct(T *p, Args... args) noexcept {
715+
::new (static_cast<void *>(p)) T(std::forward<Args>(args)...);
674716
}
717+
void destroy(T *p) { p->~T(); }
718+
#endif
675719
};
676720
template <typename T, typename U, typename Host_Allocator>
677721
constexpr bool

0 commit comments

Comments
 (0)