Skip to content

Commit

Permalink
enable if both compiler and hardware support this
Browse files Browse the repository at this point in the history
  • Loading branch information
azimafroozeh committed Nov 19, 2024
1 parent 513facb commit 5d48078
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ add_compile_definitions(ALP_CMAKE_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
include_directories(include)

# Src: -----------------------------------------------------------------------------------------------------------------
add_subdirectory(src)
if (TRUE)
message("---------------------------------------------------------------------------------------------------------")
message("-- ALP: build library:")
add_subdirectory(src)
endif ()

# Data: ----------------------------------------------------------------------------------------------------------------
if (ALP_BUILD_TESTING OR ALP_BUILD_PUBLICATION OR ALP_BUILD_BENCHMARKING)
Expand Down
39 changes: 29 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
# Detect AVX512 support
# Detect hardware AVX-512 support
include(CheckCXXCompilerFlag)

# Step 1: Check compiler support for AVX-512
check_cxx_compiler_flag("-mavx512dq" COMPILER_SUPPORTS_AVX512)

if(COMPILER_SUPPORTS_AVX512)
message(STATUS "AVX512 support detected. Adding AVX512 flag for ALP library.")
set(AVX512_FLAG "-mavx512dq")
else()
message(STATUS "AVX512 support not detected.")
endif()
if (COMPILER_SUPPORTS_AVX512)
message(STATUS "Compiler supports AVX512.")

# Step 2: Create a test program for AVX-512
file(WRITE "${CMAKE_BINARY_DIR}/check_avx512.cpp"
"#include <immintrin.h>
int main() {
__m512i x = _mm512_set1_epi32(1); // AVX-512 instruction
return _mm512_reduce_add_epi32(x);
}")

# Step 3: Try to compile and run the program
try_compile(HAS_AVX512_SUPPORT "${CMAKE_BINARY_DIR}" "${CMAKE_BINARY_DIR}/check_avx512.cpp"
CMAKE_FLAGS "-DCMAKE_CXX_FLAGS=-mavx512dq")

if (HAS_AVX512_SUPPORT)
message(STATUS "Hardware supports AVX-512. Adding AVX-512 flags.")
set(AVX512_FLAG "-mavx512dq")
else ()
message(STATUS "Hardware does not support AVX-512.")
endif ()
else ()
message(STATUS "Compiler does not support AVX-512.")
endif ()

# Define the library
add_library(ALP SHARED
Expand All @@ -19,7 +38,7 @@ add_library(ALP SHARED
fastlanes_unffor.cpp
)

# Add AVX512 flag if supported
if(AVX512_FLAG)
# Add AVX-512 flag if supported
if (AVX512_FLAG)
target_compile_options(ALP PUBLIC ${AVX512_FLAG})
endif()
endif ()

0 comments on commit 5d48078

Please sign in to comment.