Skip to content
Merged
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
21 changes: 12 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set(CMAKE_CXX_STANDARD 17)
cmake_minimum_required(VERSION 3.31)
project(qsim LANGUAGES CXX)

Expand Down Expand Up @@ -48,21 +47,25 @@ endif()
find_package(OpenMP REQUIRED)

# Always build the basic part.
ADD_SUBDIRECTORY(pybind_interface/basic)
ADD_SUBDIRECTORY(pybind_interface/decide)
add_subdirectory(pybind_interface/basic)
add_subdirectory(pybind_interface/decide)

# Add subdirectories based on the architecture or available compilers.
if(NOT CMAKE_APPLE_SILICON_PROCESSOR)
if(CMAKE_CUDA_COMPILER)
ADD_SUBDIRECTORY(pybind_interface/cuda)
add_subdirectory(pybind_interface/cuda)
if(DEFINED ENV{CUQUANTUM_ROOT})
ADD_SUBDIRECTORY(pybind_interface/custatevec)
add_subdirectory(pybind_interface/custatevec)
endif()
elseif(has_hipcc)
ADD_SUBDIRECTORY(pybind_interface/hip)
add_subdirectory(pybind_interface/hip)
endif()

ADD_SUBDIRECTORY(pybind_interface/sse)
ADD_SUBDIRECTORY(pybind_interface/avx512)
ADD_SUBDIRECTORY(pybind_interface/avx2)
add_subdirectory(pybind_interface/sse)
add_subdirectory(pybind_interface/avx512)
add_subdirectory(pybind_interface/avx2)
endif()

# The following settings mirror what is in our hand-written Makefiles.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
49 changes: 49 additions & 0 deletions pybind_interface/GetCUDAARCHS.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2025 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# 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(DEFINED ENV{CUDAARCHS})
set(CMAKE_CUDA_ARCHITECTURES "$ENV{CUDAARCHS}")
message(STATUS "qsim: using CUDA archs string $ENV{CUDAARCHS}")
else()
find_program(NVIDIA_SMI nvidia-smi)
if(NVIDIA_SMI)
execute_process(
COMMAND ${NVIDIA_SMI} --query-gpu=compute_cap --format=csv,noheader
COMMAND tr -d .
OUTPUT_VARIABLE DETECTED_ARCHS
RESULT_VARIABLE NVIDIA_SMI_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NVIDIA_SMI_RESULT EQUAL 0 AND DETECTED_ARCHS)
# The command was successful. The output may contain
# multiple lines if there are multiple GPUs.
string(REPLACE "\n" ";" ARCHS_LIST "${DETECTED_ARCHS}")
set(CMAKE_CUDA_ARCHITECTURES "${ARCHS_LIST}")
else()
message(FATAL_ERROR "nvidia-smi failed or returned no GPU"
" architectures. Please check your"
" NVIDIA driver installation and your"
" command search PATH variable.")
endif()
else()
message(FATAL_ERROR "nvidia-smi not found. Cannot autodetect"
" the current GPU architecture(s). Please"
" set the environment variable CUDAARCHS"
" to an appropriate value and try again.")
endif()
endif()

6 changes: 4 additions & 2 deletions pybind_interface/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ if(APPLE)
)
endif()

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

find_package(Python 3.10 REQUIRED)

include_directories(${PYTHON_INCLUDE_DIRS})
Expand All @@ -48,7 +50,7 @@ endif()

add_library(qsim_cuda MODULE pybind_main_cuda.cpp)
set_target_properties(qsim_cuda PROPERTIES
CUDA_ARCHITECTURES "$ENV{CUDAARCHS}"
CUDA_ARCHITECTURES "${CMAKE_CUDA_ARCHITECTURES}"
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}"
)
Expand Down
3 changes: 2 additions & 1 deletion pybind_interface/decide/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ include(../GetPybind11.cmake)

# Configure based on the detected platform
if(CMAKE_CUDA_COMPILER)
include(../GetCUDAARCHS.cmake)
add_library(qsim_decide MODULE decide.cpp)
if(DEFINED ENV{CUQUANTUM_ROOT})
target_compile_options(qsim_decide PRIVATE
Expand All @@ -52,7 +53,7 @@ if(CMAKE_CUDA_COMPILER)
find_package(Python3 3.10 REQUIRED COMPONENTS Interpreter Development)
include_directories(${PYTHON_INCLUDE_DIRS} ${pybind11_SOURCE_DIR}/include)
set_target_properties(qsim_decide PROPERTIES
CUDA_ARCHITECTURES "$ENV{CUDAARCHS}"
CUDA_ARCHITECTURES "${CMAKE_CUDA_ARCHITECTURES}"
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}"
)
Expand Down
Loading