diff --git a/CMakeLists.txt b/CMakeLists.txt index b16004983..03c0b6a4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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) diff --git a/pybind_interface/GetCUDAARCHS.cmake b/pybind_interface/GetCUDAARCHS.cmake new file mode 100644 index 000000000..8530de9d1 --- /dev/null +++ b/pybind_interface/GetCUDAARCHS.cmake @@ -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() + diff --git a/pybind_interface/cuda/CMakeLists.txt b/pybind_interface/cuda/CMakeLists.txt index 51584c8f6..a695ab5b4 100644 --- a/pybind_interface/cuda/CMakeLists.txt +++ b/pybind_interface/cuda/CMakeLists.txt @@ -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}) @@ -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}" ) diff --git a/pybind_interface/decide/CMakeLists.txt b/pybind_interface/decide/CMakeLists.txt index 0ca186ed6..47b39ccc0 100644 --- a/pybind_interface/decide/CMakeLists.txt +++ b/pybind_interface/decide/CMakeLists.txt @@ -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 @@ -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}" )