Skip to content

Commit

Permalink
Pre-generate cacheline size
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed May 9, 2024
1 parent bd393ce commit cb26be8
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 164 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1690,9 +1690,13 @@ hpx_option(
include(HPX_SetupCUDA)
include(HPX_PerformCxxFeatureTests)
hpx_perform_cxx_feature_tests()

include(TargetArch)
target_architecture(__target_arch)

include(CacheLineSize)
cache_line_size(__cache_line_size)

# ##############################################################################
# Set configuration option to use Boost.Context or not. This depends on the
# platform.
Expand Down Expand Up @@ -2014,6 +2018,7 @@ if(HPX_WITH_COMPILER_WARNINGS_AS_ERRORS)
endif()

# Diagnostics
hpx_info("Cacheline size detected: ${__cache_line_size}")
if(MSVC)
# Display full paths in diagnostics
hpx_add_compile_flag(-FC)
Expand Down
63 changes: 63 additions & 0 deletions cmake/CacheLineSize.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright (c) 2024 Hartmut Kaiser
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

set(cache_line_size_detect_cpp_code
"
#include <iostream>
#include <new>
int main()
{
#if defined(HPX_HAVE_CXX17_HARDWARE_DESTRUCTIVE_INTERFERENCE_SIZE)
std::cout << std::hardware_destructive_interference_size;
#else
#if defined(__s390__) || defined(__s390x__)
std::cout << 256; // assume 256 byte cache-line size
#elif defined(powerpc) || defined(__powerpc__) || defined(__ppc__)
std::cout << 128; // assume 128 byte cache-line size
#else
std::cout << 64; // assume 64 byte cache-line size
#endif
#endif
}
"
)

function(cache_line_size output_var)
if(NOT HPX_INTERNAL_CACHE_LINE_SIZE_DETECT)
file(WRITE "${PROJECT_BINARY_DIR}/cache_line_size.cpp"
"${cache_line_size_detect_cpp_code}"
)

if(HPX_WITH_CXX17_HARDWARE_DESTRUCTIVE_INTERFERENCE_SIZE)
set(compile_definitions
"-DHPX_HAVE_CXX17_HARDWARE_DESTRUCTIVE_INTERFERENCE_SIZE"
)
endif()

try_run(
run_result_unused compile_result_unused "${PROJECT_BINARY_DIR}" SOURCES
"${PROJECT_BINARY_DIR}/cache_line_size.cpp"
COMPILE_DEFINITIONS ${compile_definitions}
CMAKE_FLAGS CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS FALSE
RUN_OUTPUT_VARIABLE CACHE_LINE_SIZE
)

if(NOT CACHE_LINE_SIZE)
set(CACHE_LINE_SIZE "64")
endif()
set(HPX_INTERNAL_CACHE_LINE_SIZE_DETECT
${CACHE_LINE_SIZE}
CACHE INTERNAL ""
)
else()
set(CACHE_LINE_SIZE ${HPX_INTERNAL_CACHE_LINE_SIZE_DETECT})
endif()

set(${output_var}
"${CACHE_LINE_SIZE}"
PARENT_SCOPE
)
endfunction()
12 changes: 12 additions & 0 deletions cmake/HPX_AddModule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ function(add_hpx_module libname modulename)
"${global_config_file}" @ONLY
)
set(generated_headers ${generated_headers} ${global_config_file})

# Global config defines file (different from the one for each module)
set(global_config_file
${CMAKE_CURRENT_BINARY_DIR}/include/hpx/config/defines.hpp
Expand All @@ -221,6 +222,17 @@ function(add_hpx_module libname modulename)
FILENAME "${global_config_file}"
)
set(generated_headers ${generated_headers} ${global_config_file})

# Cacheline size definition
set(cache_line_size_file
${CMAKE_CURRENT_BINARY_DIR}/include/hpx/config/cache_line_size.hpp
)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/templates/cache_line_size.hpp.in"
"${cache_line_size_file}" @ONLY
)
set(generated_headers ${generated_headers} ${cache_line_size_file})

endif()

# collect zombie generated headers
Expand Down
Loading

0 comments on commit cb26be8

Please sign in to comment.