Skip to content
Draft
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b7b9267
Remove incorrect comment
mhucka Jun 29, 2025
4658564
Detect AVX & SSE and only build corresponding parts
mhucka Jun 29, 2025
e2c35ec
Consolidate common flags into the top-level CMakeLists.txt
mhucka Jun 29, 2025
1785a09
Silence warning about "using serial compilation"
mhucka Jun 29, 2025
d90c642
Use add_compile_options() instead of set()
mhucka Jun 29, 2025
7fbf19a
Fix cmake syntax
mhucka Jun 30, 2025
86c2ac8
Fix typo
mhucka Jun 30, 2025
f518df6
Add -march=native for the basic version
mhucka Jun 30, 2025
3941a3f
Use march=native with clang on MacOS
mhucka Jun 30, 2025
bb3a9f5
chore: merge branch
mhucka Jun 30, 2025
20c9dd8
Attempt to fix AVX compilation issues on MacOS
mhucka Jul 1, 2025
ba036d7
It seems -mbmi2 flag exists on Macos Intel after all
mhucka Jul 1, 2025
2490847
Simplify logic for setting flags
mhucka Jul 1, 2025
f00af25
Be more careful about SSE flags on Windows
mhucka Jul 1, 2025
9d1ca83
Fix determining AVX & SSE features of the host CPU
mhucka Jul 1, 2025
835db11
chore: merge branch
mhucka Jul 1, 2025
edbadb1
Fix missing NOT
mhucka Jul 2, 2025
12541ed
Merge branch 'master' into mh-consolidate-cmake-configs
mhucka Jul 3, 2025
c94360e
Move GetPybind11.cmake file to dev_tools/cmake
mhucka Jul 7, 2025
7f35ee2
Move and overhaul `GetPybind11.cmake`
mhucka Jul 7, 2025
640b572
Add new CMake macro for checking CPU vector instruction sets
mhucka Jul 7, 2025
b083ab7
Remove no-longer-needed pybind_interface/GetCUDAARCHS.cmake
mhucka Jul 7, 2025
28f52d9
Remove unnecessary CMake instructions
mhucka Jul 7, 2025
13e839c
Remove unnecessary CMake code & do some deduplication
mhucka Jul 7, 2025
eb93692
Rewrite the logic and remove unnecessary CMake code
mhucka Jul 7, 2025
a9e4991
Remove unnecessary CMake code and simply setting the arch
mhucka Jul 7, 2025
344cfb0
Overhaul the top-level CMakeLists.txt file (again)
mhucka Jul 7, 2025
9759a38
Don't bother setting number of threads for cibuildwheel
mhucka Jul 7, 2025
5bd427c
Try without CMP0179
mhucka Jul 7, 2025
4669af3
tmp
mhucka Jul 7, 2025
8d22fe8
tmp
mhucka Jul 7, 2025
c273d11
OpenMP is not required -- adjust CMake files accordingly
mhucka Jul 7, 2025
a08dbb3
Reduce & improve some of the excessive messages printed
mhucka Jul 7, 2025
f8f1ab1
Need to require Python3 Development.Module
mhucka Jul 7, 2025
a469ddd
Configure LTO in a portable way
mhucka Jul 7, 2025
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
78 changes: 66 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
cmake_minimum_required(VERSION 3.31)
project(qsim LANGUAGES CXX)

include(CheckLanguage)
check_language(CUDA)

# This text is prepended to messages printed by this config file so it's
# easier to figure out what came from where in the logs.
set(MSG_PREFIX "[qsim cmake configuration]")


# ~~~~~ Analyze the host's hardware & software features ~~~~~

include(CheckLanguage)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceRuns)

check_language(CUDA)

# CMake normally sets CMAKE_APPLE_SILICON_PROCESSOR on Apple Silicon; however,
# it doesn't happen when running builds using cibuildwheel, even on Apple
# Silicon. We have had better luck checking and seting it ourselves.
Expand Down Expand Up @@ -54,6 +60,30 @@ endif()

find_package(OpenMP REQUIRED)

cmake_host_system_information(RESULT HAVE_SSE2 QUERY HAS_SSE2)

if(WIN32)
check_cxx_compiler_flag("/arch:AVX2" HAVE_AVX2)
check_cxx_compiler_flag("/arch:AVX512" HAVE_AVX512)
else()
check_cxx_compiler_flag("-mavx2" HAVE_AVX2)
check_cxx_compiler_flag("-mavx512f" HAVE_AVX512)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really a good way to detect whether the architectural features are in fact available on the host computer? check_cxx_compiler_flag checks whether the C++ compiler supports a given flag. It seems the g++ compiler supports the -mavx512f flag even if the host CPU does not support AVX512 instructions.

endif()


# ~~~~~ Configure the build ~~~~~

# The following settings mirror what is in our hand-written Makefiles.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Options propagated to all sub-cmakefiles.
if(WIN32)
add_compile_options(/O2 /std:c++17 /openmp)
else()
add_compile_options(-O3 -std=c++17 -D_GLIBCXX_USE_CXX11_ABI=1 -flto=auto)
endif()

# Always build the basic part.
add_subdirectory(pybind_interface/basic)
add_subdirectory(pybind_interface/decide)
Expand All @@ -69,16 +99,40 @@ if(NOT CMAKE_APPLE_SILICON_PROCESSOR)
add_subdirectory(pybind_interface/hip)
endif()

add_subdirectory(pybind_interface/sse)
add_subdirectory(pybind_interface/avx512)
add_subdirectory(pybind_interface/avx2)
if(HAVE_SSE2)
add_subdirectory(pybind_interface/sse)
endif()

if(HAVE_AVX2)
add_subdirectory(pybind_interface/avx2)
endif()

if(HAVE_AVX512)
add_subdirectory(pybind_interface/avx512)
endif()
endif()

# Additional miscellanous settings.
# The following settings mirror what is in our hand-written Makefiles.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(APPLE)
include_directories(
"/usr/local/include"
"/usr/local/opt/llvm/include"
"/opt/homebrew/include"
"/opt/homebrew/opt/llvm@19/include"
)
link_directories(
"/usr/local/lib"
"/usr/local/opt/llvm/lib"
"/opt/homebrew/lib"
"/opt/homebrew/opt/llvm@19/lib"
)
endif()


# ~~~~~ Print misc. info ~~~~~

message(STATUS "${MSG_PREFIX} host has SSE2 = ${HAVE_SSE2}")
message(STATUS "${MSG_PREFIX} host has AVX2 = ${HAVE_AVX2}")
message(STATUS "${MSG_PREFIX} host has AVX512 = ${HAVE_AVX512}")

# Print additional useful info.
message(STATUS "${MSG_PREFIX} OpenMP found = ${OPENMP_FOUND}")
message(STATUS "${MSG_PREFIX} shell $PATH = $ENV{PATH}")
message(STATUS "${MSG_PREFIX} shell $CUQUANTUM_ROOT = $ENV{CUQUANTUM_ROOT}")
3 changes: 0 additions & 3 deletions pybind_interface/GetCUDAARCHS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


# Check whether the user has provided info about the GPU(s) installed
# on their system. If not, try to determine what it is automaticaly.
if(CMAKE_CUDA_ARCHITECTURES)
# CMake 3.18+ sets this variable from $CUDAARCHS automatically.
message(STATUS "qsim: using CUDA architectures "
Expand Down
21 changes: 3 additions & 18 deletions pybind_interface/avx2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,11 @@ cmake_minimum_required(VERSION 3.31)
project(qsim)

IF (WIN32)
set(CMAKE_CXX_FLAGS "/arch:AVX2 /O2 /openmp")
ELSE()
set(CMAKE_CXX_FLAGS "-mavx2 -mfma -O3 -flto=auto")
add_compile_options(/arch:AVX2)
ELSEIF(NOT CMAKE_APPLE_SILICON_PROCESSOR)
add_compile_options(-mavx2 -mfma)
ENDIF()

if(APPLE)
include_directories(
"/usr/local/include"
"/usr/local/opt/llvm/include"
"/opt/homebrew/include"
"/opt/homebrew/opt/llvm@19/include"
)
link_directories(
"/usr/local/lib"
"/usr/local/opt/llvm/lib"
"/opt/homebrew/lib"
"/opt/homebrew/opt/llvm@19/lib"
)
endif()

INCLUDE(../GetPybind11.cmake)
pybind11_add_module(qsim_avx2 pybind_main_avx2.cpp)

Expand Down
21 changes: 3 additions & 18 deletions pybind_interface/avx512/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,11 @@ cmake_minimum_required(VERSION 3.31)
project(qsim)

IF (WIN32)
set(CMAKE_CXX_FLAGS "/arch:AVX512 /O2 /openmp")
ELSE()
set(CMAKE_CXX_FLAGS "-mavx512f -mbmi2 -O3 -flto=auto")
add_compile_options(/arch:AVX512)
ELSEIF(NOT CMAKE_APPLE_SILICON_PROCESSOR)
add_compile_options(-mavx512f -mbmi2)
ENDIF()

if(APPLE)
include_directories(
"/usr/local/include"
"/usr/local/opt/llvm/include"
"/opt/homebrew/include"
"/opt/homebrew/opt/llvm@19/include"
)
link_directories(
"/usr/local/lib"
"/usr/local/opt/llvm/lib"
"/opt/homebrew/lib"
"/opt/homebrew/opt/llvm@19/lib"
)
endif()

INCLUDE(../GetPybind11.cmake)
pybind11_add_module(qsim_avx512 pybind_main_avx512.cpp)

Expand Down
21 changes: 2 additions & 19 deletions pybind_interface/basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,8 @@
cmake_minimum_required(VERSION 3.31)
project(qsim)

if(WIN32)
set(CMAKE_CXX_FLAGS "/O2 /openmp")
else()
set(CMAKE_CXX_FLAGS "-O3 -flto=auto")
endif()

if(APPLE)
include_directories(
"/usr/local/include"
"/usr/local/opt/llvm/include"
"/opt/homebrew/include"
"/opt/homebrew/opt/llvm@19/include"
)
link_directories(
"/usr/local/lib"
"/usr/local/opt/llvm/lib"
"/opt/homebrew/lib"
"/opt/homebrew/opt/llvm@19/lib"
)
if(NOT WIN32)
add_compile_options(-march=native)
endif()

INCLUDE(../GetPybind11.cmake)
Expand Down
21 changes: 0 additions & 21 deletions pybind_interface/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,6 @@
cmake_minimum_required(VERSION 3.31)
project(qsim LANGUAGES CXX CUDA)

if(WIN32)
set(CMAKE_CXX_FLAGS "/O2 /openmp")
else()
set(CMAKE_CXX_FLAGS "-O3 -flto=auto")
endif()

if(APPLE)
include_directories(
"/usr/local/include"
"/usr/local/opt/llvm/include"
"/opt/homebrew/include"
"/opt/homebrew/opt/llvm@19/include"
)
link_directories(
"/usr/local/lib"
"/usr/local/opt/llvm/lib"
"/opt/homebrew/lib"
"/opt/homebrew/opt/llvm@19/lib"
)
endif()

include(../GetPybind11.cmake)
include(../GetCUDAARCHS.cmake)

Expand Down
21 changes: 0 additions & 21 deletions pybind_interface/custatevec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,6 @@
cmake_minimum_required(VERSION 3.31)
project(qsim LANGUAGES CXX CUDA)

if(WIN32)
set(CMAKE_CXX_FLAGS "/O2 /openmp")
else()
set(CMAKE_CXX_FLAGS "-O3 -flto=auto")
endif()

if(APPLE)
include_directories(
"/usr/local/include"
"/usr/local/opt/llvm/include"
"/opt/homebrew/include"
"/opt/homebrew/opt/llvm@19/include"
)
link_directories(
"/usr/local/lib"
"/usr/local/opt/llvm/lib"
"/opt/homebrew/lib"
"/opt/homebrew/opt/llvm@19/lib"
)
endif()

INCLUDE(../GetPybind11.cmake)
find_package(Python3 3.10 REQUIRED)

Expand Down
21 changes: 0 additions & 21 deletions pybind_interface/decide/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,6 @@ project(qsim LANGUAGES CXX)
include(CheckLanguage)
check_language(CUDA)

if(WIN32)
set(CMAKE_CXX_FLAGS "/O2 /openmp")
else()
set(CMAKE_CXX_FLAGS "-O3 -flto=auto")
endif()

if(APPLE)
include_directories(
"/usr/local/include"
"/usr/local/opt/llvm/include"
"/opt/homebrew/include"
"/opt/homebrew/opt/llvm@19/include"
)
link_directories(
"/usr/local/lib"
"/usr/local/opt/llvm/lib"
"/opt/homebrew/lib"
"/opt/homebrew/opt/llvm@19/lib"
)
endif()

include(../GetPybind11.cmake)

# Configure based on the detected platform
Expand Down
6 changes: 0 additions & 6 deletions pybind_interface/hip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
cmake_minimum_required(VERSION 3.31)
project(qsim LANGUAGES CXX HIP)

if(WIN32)
set(CMAKE_CXX_FLAGS "/O2 /openmp")
else()
set(CMAKE_CXX_FLAGS "-O3 -flto=auto")
endif()

INCLUDE(../GetPybind11.cmake)
find_package(PythonLibs 3.10 REQUIRED)

Expand Down
21 changes: 2 additions & 19 deletions pybind_interface/sse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,8 @@
cmake_minimum_required(VERSION 3.31)
project(qsim)

IF (WIN32)
set(CMAKE_CXX_FLAGS "/O2 /openmp")
ELSE()
set(CMAKE_CXX_FLAGS "-msse4.1 -O3 -flto=auto")
ENDIF()

if(APPLE)
include_directories(
"/usr/local/include"
"/usr/local/opt/llvm/include"
"/opt/homebrew/include"
"/opt/homebrew/opt/llvm@19/include"
)
link_directories(
"/usr/local/lib"
"/usr/local/opt/llvm/lib"
"/opt/homebrew/lib"
"/opt/homebrew/opt/llvm@19/lib"
)
if(NOT WIN32)
add_compile_options(-msse4.1)
endif()

INCLUDE(../GetPybind11.cmake)
Expand Down
Loading