Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ option(NATIVE "Build with -march=native" ON)
set(DEV_MODE OFF)

include(CheckFilesystem)
include(DetectArchitecture)

if (STATIC AND NOT APPLE)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
Expand Down Expand Up @@ -72,6 +73,11 @@ target_compile_options(build_type_flags INTERFACE
)
if (NATIVE)
target_compile_options(build_type_flags INTERFACE "-march=native")
else()
# Use portable flags based on detected architecture
if (ARCH_FLAGS)
target_compile_options(build_type_flags INTERFACE "${ARCH_FLAGS}")
endif()
endif()

add_library(headers INTERFACE)
Expand Down
42 changes: 42 additions & 0 deletions cmake/DetectArchitecture.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# DetectArchitecture.cmake
# Detects CPU architecture and sets appropriate compile definitions and flags

# Detect architecture based on CMAKE_SYSTEM_PROCESSOR
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|ARM64)")
set(ARCH_ARM64 ON CACHE BOOL "ARM64 architecture detected")
set(ARCH_X86_64 OFF CACHE BOOL "x86_64 architecture not detected")

# ARM64 guarantees NEON support (ARMv8-A specification)
add_compile_definitions(ARCH_ARM64=1)

# Portable ARM64 flags
set(ARCH_FLAGS "-march=armv8-a" CACHE STRING "Portable ARM64 architecture flags")

message(STATUS "Detected ARM64 architecture (${CMAKE_SYSTEM_PROCESSOR})")
message(STATUS "ARM NEON support: YES (guaranteed by ARMv8-A)")

elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64|AMD64|x64)")
set(ARCH_X86_64 ON CACHE BOOL "x86_64 architecture detected")
set(ARCH_ARM64 OFF CACHE BOOL "ARM64 architecture not detected")

# x86-64 guarantees SSE2 support
add_compile_definitions(ARCH_X86_64=1)

# Portable x86_64 flags
set(ARCH_FLAGS "-march=x86-64 -msse2" CACHE STRING "Portable x86_64 architecture flags")

message(STATUS "Detected x86_64 architecture (${CMAKE_SYSTEM_PROCESSOR})")
message(STATUS "SSE2 support: YES (guaranteed by x86-64)")

else()
set(ARCH_X86_64 OFF CACHE BOOL "x86_64 architecture not detected")
set(ARCH_ARM64 OFF CACHE BOOL "ARM64 architecture not detected")

message(WARNING "Unknown architecture: ${CMAKE_SYSTEM_PROCESSOR}")
message(WARNING "Building without architecture-specific optimizations")

set(ARCH_FLAGS "" CACHE STRING "No architecture-specific flags")
endif()

# Export variables for use in other CMake files
mark_as_advanced(ARCH_X86_64 ARCH_ARM64 ARCH_FLAGS)
6 changes: 5 additions & 1 deletion include/kmtricks/bitmatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
#include <exception>
#include <fstream>
#include <cstring>
#include <emmintrin.h>
#ifdef ARCH_ARM64
#include <sse2neon.h>
#else
#include <emmintrin.h>
#endif
#include <iostream>
#include <cassert>
#include <iomanip>
Expand Down
3 changes: 3 additions & 0 deletions include/kmtricks/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#define CONDA_BUILD "@CONDA_BUILD@"
#define STATIC_BUILD "@STATIC@"
#define NATIVE_BUILD "@NATIVE@"
#cmakedefine ARCH_X86_64
#cmakedefine ARCH_ARM64
#define ARCH_FLAGS "@ARCH_FLAGS@"
#define MODULES_BUILD "@WITH_MODULES@"
#define SOCKS_ON "@WITH_SOCKS@"
#define HOWDE_BUILD "@WITH_HOWDE@"
Expand Down
5 changes: 5 additions & 0 deletions thirdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ target_include_directories(headers SYSTEM INTERFACE ${THIRD_DIR}/indicators/incl
target_include_directories(headers SYSTEM INTERFACE ${THIRD_DIR}/cfrcat/include)
target_include_directories(headers SYSTEM INTERFACE ${THIRD_DIR}/robin-hood-hashing/src/include)

# Add sse2neon header path for ARM64 builds
if(ARCH_ARM64)
target_include_directories(headers SYSTEM INTERFACE ${THIRD_DIR}/sse2neon)
endif()

ExternalProject_Add(FMT
PREFIX FMT
SOURCE_DIR ${PROJECT_SOURCE_DIR}/thirdparty/fmt
Expand Down
70 changes: 48 additions & 22 deletions thirdparty/gatb-core-stripped/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,37 +124,63 @@ if (NONCANONICAL)
endif()


# detect SSE for popcount
# detect SSE for popcount
# this was for emphf, maybe it's for something else also? otherwise this part can be removed.
#
# from https://github.com/rurban/smhasher/blob/master/CMakeLists.txt
# i do not see much performance gain for now, but let's keep that code here, might be useful later.
# list of performance gain observed:
# popcount in Graph::countNeighbors
IF(CMAKE_SYSTEM_NAME MATCHES "Linux")
EXEC_PROGRAM(cat ARGS "/proc/cpuinfo" OUTPUT_VARIABLE CPUINFO)
STRING(REGEX REPLACE "^.*(sse2).*$" "\\1" SSE_THERE ${CPUINFO})
STRING(COMPARE EQUAL "sse2" "${SSE_THERE}" SSE2_TRUE)
STRING(REGEX REPLACE "^.*(sse4_2).*$" "\\1" SSE_THERE ${CPUINFO})
STRING(COMPARE EQUAL "sse4_2" "${SSE_THERE}" SSE42_TRUE)
# Check if ARM64 - skip SSE detection
IF(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
message("-- ARM64 architecture detected - skipping SSE detection")
set(SSE2_FOUND false CACHE BOOL "SSE2 not available on ARM64")
set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 not available on ARM64")
ELSE()
# x86_64 - detect SSE support
EXEC_PROGRAM(cat ARGS "/proc/cpuinfo" OUTPUT_VARIABLE CPUINFO)
STRING(REGEX REPLACE "^.*(sse2).*$" "\\1" SSE_THERE ${CPUINFO})
STRING(COMPARE EQUAL "sse2" "${SSE_THERE}" SSE2_TRUE)
STRING(REGEX REPLACE "^.*(sse4_2).*$" "\\1" SSE_THERE ${CPUINFO})
STRING(COMPARE EQUAL "sse4_2" "${SSE_THERE}" SSE42_TRUE)
IF (SSE2_TRUE)
set(SSE2_FOUND true CACHE BOOL "SSE2 available")
ELSE (SSE2_TRUE)
set(SSE2_FOUND false CACHE BOOL "SSE2 not available")
ENDIF (SSE2_TRUE)
IF (SSE42_TRUE)
set(SSE4_2_FOUND true CACHE BOOL "SSE4.2 available")
ELSE (SSE42_TRUE)
set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 not available")
ENDIF (SSE42_TRUE)
ENDIF()
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin")
EXEC_PROGRAM("/usr/sbin/sysctl -n machdep.cpu.features" OUTPUT_VARIABLE
CPUINFO)
STRING(REGEX REPLACE "^.*[^S](SSE2).*$" "\\1" SSE_THERE ${CPUINFO})
STRING(COMPARE EQUAL "SSE2" "${SSE_THERE}" SSE2_TRUE)
STRING(REGEX REPLACE "^.*(SSE4.2).*$" "\\1" SSE_THERE ${CPUINFO})
STRING(COMPARE EQUAL "SSE4.2" "${SSE_THERE}" SSE42_TRUE)
# Check if Apple Silicon (ARM64) or Intel (x86_64)
IF(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
message("-- Apple Silicon (ARM64) detected - skipping SSE detection")
set(SSE2_FOUND false CACHE BOOL "SSE2 not available on ARM64")
set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 not available on ARM64")
ELSE()
# Intel Mac - detect SSE support
EXEC_PROGRAM("/usr/sbin/sysctl -n machdep.cpu.features" OUTPUT_VARIABLE
CPUINFO)
STRING(REGEX REPLACE "^.*[^S](SSE2).*$" "\\1" SSE_THERE ${CPUINFO})
STRING(COMPARE EQUAL "SSE2" "${SSE_THERE}" SSE2_TRUE)
STRING(REGEX REPLACE "^.*(SSE4.2).*$" "\\1" SSE_THERE ${CPUINFO})
STRING(COMPARE EQUAL "SSE4.2" "${SSE_THERE}" SSE42_TRUE)
IF (SSE2_TRUE)
set(SSE2_FOUND true CACHE BOOL "SSE2 available")
ELSE (SSE2_TRUE)
set(SSE2_FOUND false CACHE BOOL "SSE2 not available")
ENDIF (SSE2_TRUE)
IF (SSE42_TRUE)
set(SSE4_2_FOUND true CACHE BOOL "SSE4.2 available")
ELSE (SSE42_TRUE)
set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 not available")
ENDIF (SSE42_TRUE)
ENDIF()
ENDIF(CMAKE_SYSTEM_NAME MATCHES "Linux")
IF (SSE2_TRUE)
set(SSE2_FOUND true CACHE BOOL "SSE2 available")
ELSE (SSE2_TRUE)
set(SSE2_FOUND false CACHE BOOL "SSE2 not available")
ENDIF (SSE2_TRUE)
IF (SSE42_TRUE)
set(SSE4_2_FOUND true CACHE BOOL "SSE4.2 available")
ELSE (SSE42_TRUE)
set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 not available")
ENDIF (SSE42_TRUE)
# I could use LIBRARY_COMPILE_DEFINITIONS, but it's actually passed to "add_definitions", which isn't made for passing compilation flags, only -D ones.
IF(SSE4_2_FOUND AND (NOT NO_SSE))
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2 -msse4.2 -mpopcnt")
Expand Down
Loading