Skip to content

Commit

Permalink
Change the single thread alloc macro name
Browse files Browse the repository at this point in the history
  • Loading branch information
KIwabuchi committed Oct 10, 2023
1 parent 293d4fd commit 7026a80
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 38 deletions.
9 changes: 0 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ set(INITIAL_SEGMENT_SIZE "0" CACHE STRING
"Set the initial segment size (use the internally defined value if 0 is specified)")

# ---------- Experimental options ---------- #
# This mode still uses multiple threads inside Metall.
# However, applications must not use metall with multiple threads.
option(SINGLE_THREAD_ALLOC "Optimize Metall kernel for single thread usage" OFF)

option(USE_ANONYMOUS_NEW_MAP "Use the anonymous map when creating a new map region" OFF)
set(UMAP_ROOT "" CACHE PATH "UMap installed root directory")

Expand Down Expand Up @@ -215,11 +211,6 @@ if (USE_ANONYMOUS_NEW_MAP)
message(STATUS "Use the anonymous map for new map region")
endif ()

if (SINGLE_THREAD_ALLOC)
list(APPEND METALL_DEFS "METALL_SINGLE_THREAD_ALLOC")
message(STATUS "Optimize Metall kernel for single thread usage")
endif ()

# Requirements for GCC
if (NOT RUN_BUILD_AND_TEST_WITH_CI)
if (("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU"))
Expand Down
11 changes: 11 additions & 0 deletions include/metall/basic_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
#ifndef METALL_BASIC_MANAGER_HPP
#define METALL_BASIC_MANAGER_HPP

#ifdef DOXYGEN_SKIP
/// \brief A macro to disable concurrency support.
/// \details
/// If this macro is defined, Metall disables concurrency support and optimizes
/// the internal behavior for single-thread usage. Applications must not call
/// any Metall functions concurrently if this macro is defined. On the other
/// hand, Metall still may use multi-threading for internal operations, such
/// as synchronizing data with files.
#define METALL_DISABLE_CONCURRENCY
#endif

#include <cstddef>
#include <memory>

Expand Down
10 changes: 6 additions & 4 deletions include/metall/kernel/manager_kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
#include <metall/kernel/segment_storage/mmap_segment_storage.hpp>
#endif

#define ENABLE_MUTEX_IN_METALL_MANAGER_KERNEL 1
#if ENABLE_MUTEX_IN_METALL_MANAGER_KERNEL
#ifndef METALL_DISABLE_CONCURRENCY
#define METALL_ENABLE_MUTEX_IN_MANAGER_KERNEL
#endif
#ifdef METALL_ENABLE_MUTEX_IN_MANAGER_KERNEL
#include <metall/detail/mutex.hpp>
#endif

Expand Down Expand Up @@ -140,7 +142,7 @@ class manager_kernel {

using json_store = mdtl::ptree::node_type;

#if ENABLE_MUTEX_IN_METALL_MANAGER_KERNEL
#ifdef METALL_ENABLE_MUTEX_IN_MANAGER_KERNEL
using mutex_type = mdtl::mutex;
using lock_guard_type = mdtl::mutex_lock_guard;
#endif
Expand Down Expand Up @@ -596,7 +598,7 @@ class manager_kernel {
segment_memory_allocator m_segment_memory_allocator{nullptr};
std::unique_ptr<json_store> m_manager_metadata{nullptr};

#if ENABLE_MUTEX_IN_METALL_MANAGER_KERNEL
#ifdef METALL_ENABLE_MUTEX_IN_MANAGER_KERNEL
std::unique_ptr<mutex_type> m_object_directories_mutex{nullptr};
#endif
};
Expand Down
10 changes: 5 additions & 5 deletions include/metall/kernel/manager_kernel_impl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ manager_kernel<chnk_no, chnk_sz>::manager_kernel()
if (!m_manager_metadata) {
return;
}
#if ENABLE_MUTEX_IN_METALL_MANAGER_KERNEL
#ifdef METALL_ENABLE_MUTEX_IN_MANAGER_KERNEL
m_object_directories_mutex = std::make_unique<mutex_type>();
if (!m_object_directories_mutex) {
return;
Expand Down Expand Up @@ -177,7 +177,7 @@ bool manager_kernel<chnk_no, chnk_sz>::destroy(char_ptr_holder_type name) {
size_type length = 0;

{
#if ENABLE_MUTEX_IN_METALL_MANAGER_KERNEL
#ifdef METALL_ENABLE_MUTEX_IN_MANAGER_KERNEL
lock_guard_type guard(*m_object_directories_mutex);
#endif

Expand All @@ -204,7 +204,7 @@ bool manager_kernel<chnk_no, chnk_sz>::destroy_ptr(const T *ptr) {

size_type length = 0;
{
#if ENABLE_MUTEX_IN_METALL_MANAGER_KERNEL
#ifdef METALL_ENABLE_MUTEX_IN_MANAGER_KERNEL
lock_guard_type guard(*m_object_directories_mutex);
#endif

Expand Down Expand Up @@ -820,7 +820,7 @@ T *manager_kernel<chnk_no, chnk_sz>::priv_generic_construct(

void *ptr = nullptr;
try {
#if ENABLE_MUTEX_IN_METALL_MANAGER_KERNEL
#ifdef METALL_ENABLE_MUTEX_IN_MANAGER_KERNEL
lock_guard_type guard(*m_object_directories_mutex);
#endif

Expand Down Expand Up @@ -856,7 +856,7 @@ T *manager_kernel<chnk_no, chnk_sz>::priv_generic_construct(
ptr, [this](void *const ptr) {
try {
{
#if ENABLE_MUTEX_IN_METALL_MANAGER_KERNEL
#ifdef METALL_ENABLE_MUTEX_IN_MANAGER_KERNEL
lock_guard_type guard(*m_object_directories_mutex);
#endif
priv_remove_attr_object_no_mutex(priv_to_offset(ptr));
Expand Down
12 changes: 6 additions & 6 deletions include/metall/kernel/object_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
#include <metall/detail/proc.hpp>
#include <metall/detail/hash.hpp>

#ifndef METALL_SINGLE_THREAD_ALLOC
#ifndef METALL_DISABLE_CONCURRENCY
#define METALL_ENABLE_MUTEX_IN_OBJECT_CACHE
#endif
#ifdef METALL_ENABLE_MUTEX_IN_OBJECT_CACHE
#include <metall/detail/mutex.hpp>
#endif
#endif

namespace metall::kernel {

Expand Down Expand Up @@ -58,7 +58,7 @@ class object_cache {
// -------------------- //

static constexpr std::size_t k_num_cache_per_core =
#ifdef METALL_SINGLE_THREAD_ALLOC
#ifdef METALL_DISABLE_CONCURRENCY
1;
#else
4;
Expand Down Expand Up @@ -232,7 +232,7 @@ class object_cache {
}

std::size_t priv_comp_cache_no() const {
#ifdef METALL_SINGLE_THREAD_ALLOC
#ifdef METALL_DISABLE_CONCURRENCY
return 0;
#endif
#if SUPPORT_GET_CPU_CORE_NO
Expand All @@ -252,7 +252,7 @@ class object_cache {
/// \brief Get CPU core number.
/// This function does not call the system call every time as it is slow.
static std::size_t priv_get_core_no() {
#ifdef METALL_SINGLE_THREAD_ALLOC
#ifdef METALL_DISABLE_CONCURRENCY
return 0;
#endif
thread_local static int cached_core_no = 0;
Expand All @@ -265,7 +265,7 @@ class object_cache {
}

static std::size_t get_num_cores() {
#ifdef METALL_SINGLE_THREAD_ALLOC
#ifdef METALL_DISABLE_CONCURRENCY
return 1;
#else
return std::thread::hardware_concurrency();
Expand Down
29 changes: 16 additions & 13 deletions include/metall/kernel/segment_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
#include <metall/detail/utilities.hpp>
#include <metall/logger.hpp>

#define ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR 1
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifndef METALL_DISABLE_CONCURRENCY
#define METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
#endif

#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
#include <metall/detail/mutex.hpp>
#endif

Expand Down Expand Up @@ -94,7 +97,7 @@ class segment_allocator {
myself>;
#endif

#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
using mutex_type = mdtl::mutex;
using lock_guard_type = mdtl::mutex_lock_guard;
#endif
Expand All @@ -114,13 +117,13 @@ class segment_allocator {
,
m_object_cache()
#endif
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
,
m_chunk_mutex(nullptr),
m_bin_mutex(nullptr)
#endif
{
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
m_chunk_mutex = std::make_unique<mutex_type>();
m_bin_mutex = std::make_unique<std::array<mutex_type, k_num_small_bins>>();
#endif
Expand Down Expand Up @@ -217,7 +220,7 @@ class segment_allocator {
/// This function is not cheap if many objects are allocated.
/// \return Returns true if all memory is deallocated.
bool all_memory_deallocated() const {
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
lock_guard_type chunk_guard(*m_chunk_mutex);
#endif

Expand Down Expand Up @@ -384,7 +387,7 @@ class segment_allocator {
void priv_allocate_small_objects_from_global(
const bin_no_type bin_no, const size_type num_allocates,
difference_type *const allocated_offsets) {
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
lock_guard_type bin_guard(m_bin_mutex->at(bin_no));
#endif

Expand Down Expand Up @@ -470,7 +473,7 @@ class segment_allocator {

bool priv_insert_new_small_object_chunk(const bin_no_type bin_no) {
chunk_no_type new_chunk_no;
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
lock_guard_type chunk_guard(*m_chunk_mutex);
#endif
new_chunk_no = m_chunk_directory.insert(bin_no);
Expand All @@ -482,7 +485,7 @@ class segment_allocator {
}

difference_type priv_allocate_large_object(const bin_no_type bin_no) {
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
lock_guard_type chunk_guard(*m_chunk_mutex);
#endif
const chunk_no_type new_chunk_no = m_chunk_directory.insert(bin_no);
Expand Down Expand Up @@ -535,7 +538,7 @@ class segment_allocator {
void priv_deallocate_small_objects_from_global(
const bin_no_type bin_no, const size_type num_deallocates,
const difference_type offsets[]) {
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
lock_guard_type bin_guard(m_bin_mutex->at(bin_no));
#endif
for (size_type i = 0; i < num_deallocates; ++i) {
Expand All @@ -559,7 +562,7 @@ class segment_allocator {
} else if (m_chunk_directory.all_slots_unmarked(chunk_no)) {
// All slots in the chunk are not used, deallocate it
{
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
lock_guard_type chunk_guard(*m_chunk_mutex);
#endif
m_chunk_directory.erase(chunk_no);
Expand Down Expand Up @@ -639,7 +642,7 @@ class segment_allocator {

void priv_deallocate_large_object(const chunk_no_type chunk_no,
const bin_no_type bin_no) {
#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
lock_guard_type chunk_guard(*m_chunk_mutex);
#endif
m_chunk_directory.erase(chunk_no);
Expand Down Expand Up @@ -717,7 +720,7 @@ class segment_allocator {
small_object_cache_type m_object_cache;
#endif

#if ENABLE_MUTEX_IN_METALL_SEGMENT_ALLOCATOR
#ifdef METALL_ENABLE_MUTEX_IN_SEGMENT_ALLOCATOR
std::unique_ptr<mutex_type> m_chunk_mutex{nullptr};
std::unique_ptr<std::array<mutex_type, k_num_small_bins>> m_bin_mutex{
nullptr};
Expand Down
2 changes: 1 addition & 1 deletion test/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_metall_test_executable(chunk_directory_test chunk_directory_test.cpp)
add_metall_test_executable(manager_test manager_test.cpp)

add_metall_test_executable(manager_test_single_thread manager_test.cpp)
target_compile_definitions(manager_test_single_thread PRIVATE METALL_SINGLE_THREAD_ALLOC)
target_compile_definitions(manager_test_single_thread PRIVATE METALL_DISABLE_CONCURRENCY)

add_metall_test_executable(snapshot_test snapshot_test.cpp)

Expand Down

0 comments on commit 7026a80

Please sign in to comment.