Skip to content

Commit

Permalink
Backport deactivation flags [for 0.2.1]
Browse files Browse the repository at this point in the history
  • Loading branch information
G-071 committed Aug 24, 2023
1 parent 6ef2543 commit 38d921c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ option(CPPUDDLE_WITH_KOKKOS "Enable KOKKOS tests/examples" OFF)
option(CPPUDDLE_WITH_CLANG_TIDY "Enable clang tidy warnings" OFF)
option(CPPUDDLE_WITH_CLANG_FORMAT "Enable clang format target" OFF)

# backported flags for performance experiments
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)
option(CPPUDDLE_WITH_AGGRESSIVE_CONTENT_RECYCLING "Allows the aggressive allocators variants to reuse contents from previous buffers (and thus skip initializations)" ON)

if (CPPUDDLE_WITH_CUDA)
enable_language(CUDA)
endif ()
Expand Down Expand Up @@ -113,6 +117,21 @@ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>
)

# Backported flags handling
if(CPPUDDLE_WITH_BUFFER_RECYCLING)
message(INFO " Using default buffer recycling behaviour!")
else()
message(WARNING " Slow Build: Buffer recycling is deactivated. This should only be used for performance tests!")
target_compile_definitions(buffer_manager INTERFACE "CPPUDDLE_DEACTIVATE_BUFFER_RECYCLING")
endif()

if(CPPUDDLE_WITH_AGGRESSIVE_CONTENT_RECYCLING)
message(INFO " Using default behaviour for aggressive content reusage (only relevant for aggressive allocators)!")
else()
target_compile_definitions(buffer_manager INTERFACE "CPPUDDLE_DEACTIVATE_AGGRESSIVE_ALLOCATORS")
message(WARNING " Slow Build: Aggressive allocators (and thus content recycling) is disabled. This should only be used for performance tests!")
endif()

# install libs with the defitions:
install(TARGETS buffer_manager EXPORT CPPuddle
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
Expand All @@ -130,6 +149,9 @@ install(EXPORT CPPuddle NAMESPACE CPPuddle:: DESTINATION ${CMAKE_INSTALL_PREFIX}

## Add target for tests and tests definitions
if (CPPUDDLE_WITH_TESTS)
if(NOT CPPUDDLE_WITH_BUFFER_RECYCLING)
message(FATAL_ERROR "The CPPuddle tests only work with CPPUDDLE_WITH_BUFFER_RECYCLING=ON. Turning off buffer recycling is not recommended in general!")
endif()
add_executable(allocator_test tests/allocator_test.cpp)
target_link_libraries(allocator_test
${Boost_LIBRARIES} Boost::boost Boost::program_options buffer_manager)
Expand Down
62 changes: 53 additions & 9 deletions include/buffer_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ void destroy_n(ForwardIt first, Size n) {
class buffer_recycler {
// Public interface
public:
#if defined(CPPUDDLE_DEACTIVATE_BUFFER_RECYCLING)

// Warn about suboptimal performance without recycling
#pragma message \
"Warning: Building without buffer recycling! Use only for performance testing! \
For better performance configure CPPuddle with CPPUDDLE_WITH_BUFFER_RECYCLING=ON!"

template <typename T, typename Host_Allocator>
static T *get(size_t number_elements, bool manage_content_lifetime = false,
std::optional<size_t> location_hint = std::nullopt,
std::optional<size_t> device_id = std::nullopt) {

return Host_Allocator{}.allocate(number_elements);
}
/// Marks an buffer as unused and fit for reusage
template <typename T, typename Host_Allocator>
static void mark_unused(T *p, size_t number_elements,
std::optional<size_t> location_hint = std::nullopt,
std::optional<size_t> device_id = std::nullopt) {
return Host_Allocator{}.deallocate(p, number_elements);
}
/// Fail when internal reference counting is used but recylcing is turned off
template <typename T, typename Host_Allocator>
static void increase_usage_counter(T *p, size_t number_elements) noexcept {
std::cerr << "ERROR: CPPuddle v0.2.1 does not support internal reference counting "
"with CPPUDDLE_WITH_BUFFER_RECYCLING=OFF. Please re-enable buffer recycling! Aborting..."
<< std::endl;
abort();
}
#else
/// Returns and allocated buffer of the requested size - this may be a reused
/// buffer
template <typename T, typename Host_Allocator>
Expand Down Expand Up @@ -79,13 +109,6 @@ class buffer_recycler {
return buffer_manager<T, Host_Allocator>::mark_unused(p, number_elements);
}
}

template <typename T, typename Host_Allocator>
static void register_allocator_counters_with_hpx(void) {
std::cerr << "Warning: CPPuddle v0.2.1 does not yet support HPX counters "
"-- this operation will be ignored!"
<< std::endl;
}
/// Increase the reference coutner of a buffer
template <typename T, typename Host_Allocator>
static void increase_usage_counter(T *p, size_t number_elements) noexcept {
Expand All @@ -96,6 +119,14 @@ class buffer_recycler {
p, number_elements);
}
}
#endif

template <typename T, typename Host_Allocator>
static void register_allocator_counters_with_hpx(void) {
std::cerr << "Warning: CPPuddle v0.2.1 does not yet support HPX counters "
"-- this operation will be ignored!"
<< std::endl;
}
/// Deallocate all buffers, no matter whether they are marked as used or not
static void clean_all() {
std::lock_guard<std::mutex> guard(mut);
Expand Down Expand Up @@ -661,6 +692,11 @@ struct aggressive_recycle_allocator {
void deallocate(T *p, std::size_t n) {
buffer_recycler::mark_unused<T, Host_Allocator>(p, n);
}
void increase_usage_counter(T *p, size_t n) {
buffer_recycler::increase_usage_counter<T, Host_Allocator>(p, n);
}

#ifndef CPPUDDLE_DEACTIVATE_AGGRESSIVE_ALLOCATORS
template <typename... Args>
inline void construct(T *p, Args... args) noexcept {
// Do nothing here - we reuse the content of the last owner
Expand All @@ -669,9 +705,17 @@ struct aggressive_recycle_allocator {
// Do nothing here - Contents will be destroyed when the buffer manager is
// destroyed, not before
}
void increase_usage_counter(T *p, size_t n) {
buffer_recycler::increase_usage_counter<T, Host_Allocator>(p, n);
#else
// Warn about suboptimal performance without recycling
#pragma message \
"Warning: Building without content reusage for aggressive allocators! \
For better performance configure with CPPUDDLE_WITH_AGGRESSIVE_CONTENT_RECYCLING=ON !"
template <typename... Args>
inline void construct(T *p, Args... args) noexcept {
::new (static_cast<void *>(p)) T(std::forward<Args>(args)...);
}
void destroy(T *p) { p->~T(); }
#endif
};
template <typename T, typename U, typename Host_Allocator>
constexpr bool
Expand Down

0 comments on commit 38d921c

Please sign in to comment.