Skip to content

Commit 4658564

Browse files
committed
Detect AVX & SSE and only build corresponding parts
Previously, the CMake configuration was such that it would always build the AVX2, AVX512 & SSE2 Pybind11 modules without testing whether the current system supported those options. The changes here use CMake features to detect whether the architectural features are in fact available, and only attempt to build the appropriate modules. In addition, there is a minor bit of refactoring to group some of the code more logically, and to add some more informational message printouts.
1 parent b7b9267 commit 4658564

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

CMakeLists.txt

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515
cmake_minimum_required(VERSION 3.31)
1616
project(qsim LANGUAGES CXX)
1717

18-
include(CheckLanguage)
19-
check_language(CUDA)
20-
2118
# This text is prepended to messages printed by this config file so it's
2219
# easier to figure out what came from where in the logs.
2320
set(MSG_PREFIX "[qsim cmake configuration]")
2421

22+
23+
# ~~~~~ Analyze the host's hardware & software features ~~~~~
24+
25+
include(CheckLanguage)
26+
include(CheckCXXCompilerFlag)
27+
include(CheckCXXSourceRuns)
28+
29+
check_language(CUDA)
30+
2531
# CMake normally sets CMAKE_APPLE_SILICON_PROCESSOR on Apple Silicon; however,
2632
# it doesn't happen when running builds using cibuildwheel, even on Apple
2733
# Silicon. We have had better luck checking and seting it ourselves.
@@ -54,6 +60,23 @@ endif()
5460

5561
find_package(OpenMP REQUIRED)
5662

63+
cmake_host_system_information(RESULT HAVE_SSE2 QUERY HAS_SSE2)
64+
65+
if(WIN32)
66+
check_cxx_compiler_flag("/arch:AVX2" HAVE_AVX512)
67+
check_cxx_compiler_flag("/arch:AVX512" HAVE_AVX512)
68+
else()
69+
check_cxx_compiler_flag("-mavx2" HAVE_AVX2)
70+
check_cxx_compiler_flag("-mavx512f" HAVE_AVX512)
71+
endif()
72+
73+
74+
# ~~~~~ Configure the build ~~~~~
75+
76+
# The following settings mirror what is in our hand-written Makefiles.
77+
set(CMAKE_CXX_STANDARD 17)
78+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
79+
5780
# Always build the basic part.
5881
add_subdirectory(pybind_interface/basic)
5982
add_subdirectory(pybind_interface/decide)
@@ -69,16 +92,25 @@ if(NOT CMAKE_APPLE_SILICON_PROCESSOR)
6992
add_subdirectory(pybind_interface/hip)
7093
endif()
7194

72-
add_subdirectory(pybind_interface/sse)
73-
add_subdirectory(pybind_interface/avx512)
74-
add_subdirectory(pybind_interface/avx2)
95+
if(HAVE_SSE2)
96+
add_subdirectory(pybind_interface/sse)
97+
endif()
98+
99+
if(HAVE_AVX2)
100+
add_subdirectory(pybind_interface/avx2)
101+
endif()
102+
103+
if(HAVE_AVX512)
104+
add_subdirectory(pybind_interface/avx512)
105+
endif()
75106
endif()
76107

77-
# Additional miscellanous settings.
78-
# The following settings mirror what is in our hand-written Makefiles.
79-
set(CMAKE_CXX_STANDARD 17)
80-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
81108

82-
# Print additional useful info.
83-
message(STATUS "${MSG_PREFIX} OpenMP found = ${OPENMP_FOUND}")
109+
# ~~~~~ Print misc. info ~~~~~
110+
111+
message(STATUS "${MSG_PREFIX} host has SSE2 = ${HAVE_SSE2}")
112+
message(STATUS "${MSG_PREFIX} host has AVX2 = ${HAVE_AVX2}")
113+
message(STATUS "${MSG_PREFIX} host has AVX512 = ${HAVE_AVX512}")
114+
84115
message(STATUS "${MSG_PREFIX} shell $PATH = $ENV{PATH}")
116+
message(STATUS "${MSG_PREFIX} shell $CUQUANTUM_ROOT = $ENV{CUQUANTUM_ROOT}")

0 commit comments

Comments
 (0)