Skip to content

Commit

Permalink
set CMAKE_CUDA_ARCHITECTURES from CAS
Browse files Browse the repository at this point in the history
  • Loading branch information
upsj committed Aug 9, 2023
1 parent d97f479 commit 100da3f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 27 deletions.
3 changes: 1 addition & 2 deletions cmake/GinkgoConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ set(GINKGO_IWYU_PATH @GINKGO_IWYU_PATH@)

set(GINKGO_JACOBI_FULL_OPTIMIZATIONS @GINKGO_JACOBI_FULL_OPTIMIZATIONS@)

set(GINKGO_CUDA_ARCHITECTURES "@GINKGO_CUDA_ARCHITECTURES@")
set(GINKGO_CUDA_ARCHITECTURES "@CMAKE_CUDA_ARCHITECTURES@")
set(GINKGO_CUDA_HOST_COMPILER "@CMAKE_CUDA_HOST_COMPILER@")
set(GINKGO_CUDA_ARCH_FLAGS "@GINKGO_CUDA_ARCH_FLAGS@")

set(GINKGO_HIP_COMPILER_FLAGS "@GINKGO_HIP_COMPILER_FLAGS@")
set(GINKGO_HIP_HCC_COMPILER_FLAGS "@GINKGO_HIP_HCC_COMPILER_FLAGS@")
Expand Down
36 changes: 36 additions & 0 deletions cmake/Modules/CudaArchitectureSelector.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@
# The command has the same result as ``cas_target_cuda_architectures``. It does
# not add the compiler flags to the target, but stores the compiler flags in
# the variable (string).
#
# cas_variable_cmake_cuda_architectures(
# [<variable>] # variable for storing architecture list
# [<spec>] # list of architecture specifications
# )
#
# The command prepares an architecture list supported by the CMake
# ``CUDA_ARCHITECTURES`` target property and ``CMAKE_CUDA_ARCHITECTURES``
# variable. The architecture specification
#
#
# ``ARCHITECTURES`` specification list
Expand Down Expand Up @@ -404,3 +413,30 @@ function(cas_variable_cuda_architectures variable)
cas_get_compiler_flags(flags ${ARGN})
set(${variable} "${flags}" PARENT_SCOPE)
endfunction()


function(cas_variable_cmake_cuda_architectures variable)
cas_get_onboard_architectures(onboard_archs)
cas_get_supported_architectures(supported_archs)
if(("${ARGN}" STREQUAL "All") OR ("${ARGN}" STREQUAL "Auto" AND (NOT onboard_archs)))
set(archs "${supported_archs}")
elseif("${ARGN}" STREQUAL "Auto")
set(archs "${onboard_archs}")
else()
set(archs)
foreach(arch IN LISTS ARGN)
if(arch MATCHES "${cas_spec_regex}")
if(CMAKE_MATCH_1)
list(APPEND archs ${CMAKE_MATCH_1}-real)
endif()
if(CMAKE_MATCH_3)
list(APPEND archs ${CMAKE_MATCH_3}-virtual)
endif()
else()
cas_get_architectures_by_name("${arch}" arch)
list(APPEND archs ${arch})
endif()
endforeach()
endif()
set("${variable}" "${archs}" PARENT_SCOPE)
endfunction()
1 change: 0 additions & 1 deletion cmake/create_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ function(ginkgo_create_cuda_test_internal test_name filename test_target_name)
target_compile_definitions(${test_target_name} PRIVATE GKO_COMPILING_CUDA)
target_compile_options(${test_target_name}
PRIVATE
$<$<COMPILE_LANGUAGE:CUDA>:${GINKGO_CUDA_ARCH_FLAGS}>
$<$<COMPILE_LANGUAGE:CUDA>:${GINKGO_CUDA_COMPILER_FLAGS}>)
if(MSVC)
target_compile_options(${test_target_name}
Expand Down
9 changes: 5 additions & 4 deletions cmake/cuda.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ find_package(CUDAToolkit REQUIRED)

include(cmake/Modules/CudaArchitectureSelector.cmake)

# Detect the CUDA architecture flags and propagate to all the project
cas_variable_cuda_architectures(GINKGO_CUDA_ARCH_FLAGS
ARCHITECTURES ${GINKGO_CUDA_ARCHITECTURES}
UNSUPPORTED "20" "21")
# Detect the CUDA architecture and propagate to all the project
cas_variable_cmake_cuda_architectures(cuda_detected_archs ${GINKGO_CUDA_ARCHITECTURES})
if(NOT CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES "${cuda_detected_archs}")
endif()

find_package(NVTX REQUIRED)

Expand Down
3 changes: 1 addition & 2 deletions cuda/get_info.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
ginkgo_print_module_header(${detailed_log} "CUDA")
ginkgo_print_variable(${detailed_log} "GINKGO_CUDA_ARCHITECTURES")
ginkgo_print_variable(${detailed_log} "CMAKE_CUDA_ARCHITECTURES")
ginkgo_print_variable(${detailed_log} "GINKGO_CUDA_COMPILER_FLAGS")
ginkgo_print_variable(${detailed_log} "GINKGO_CUDA_ARCH_FLAGS")
ginkgo_print_module_footer(${detailed_log} "CUDA variables:")
ginkgo_print_variable(${detailed_log} "CMAKE_CUDA_COMPILER")
ginkgo_print_variable(${detailed_log} "CMAKE_CUDA_COMPILER_VERSION")
Expand Down
8 changes: 0 additions & 8 deletions examples/custom-matrix-format/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,5 @@ set(CMAKE_CUDA_STANDARD_REQUIRED ON)
add_executable(custom-matrix-format custom-matrix-format.cpp stencil_kernel.cu)
target_link_libraries(custom-matrix-format Ginkgo::ginkgo OpenMP::OpenMP_CXX)

# inherit CUDA architecture flags from Ginkgo
target_compile_options(custom-matrix-format
PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:${GINKGO_CUDA_ARCH_FLAGS}>")
# we handle CUDA architecture flags for now, disable CMake handling
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.18)
set_target_properties(custom-matrix-format PROPERTIES CUDA_ARCHITECTURES OFF)
endif()

# workaround for clang-cuda/g++ interaction
set_target_properties(custom-matrix-format PROPERTIES POSITION_INDEPENDENT_CODE ON)
11 changes: 1 addition & 10 deletions test/test_install/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ if(GINKGO_BUILD_CUDA)
enable_language(CUDA)
configure_file(test_install.cpp test_install.cu COPYONLY)
add_executable(test_install_cuda ${CMAKE_CURRENT_BINARY_DIR}/test_install.cu)
target_compile_options(test_install_cuda
PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:${GINKGO_CUDA_ARCH_FLAGS}>")
# we handle CUDA architecture flags for now, disable CMake handling
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.18)
set_target_properties(test_install_cuda PROPERTIES CUDA_ARCHITECTURES OFF)
endif()
target_compile_definitions(test_install_cuda PRIVATE HAS_CUDA=1)
target_compile_definitions(test_install_cuda PRIVATE HAS_REFERENCE=${HAS_REFERENCE})
target_link_libraries(test_install_cuda PRIVATE Ginkgo::ginkgo)
Expand All @@ -60,16 +54,13 @@ if(GINKGO_BUILD_HIP)
else()
set (GINKGO_PIC_OPTION "$<$<CONFIG:Debug>:-fPIC>")
endif()
if (CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
set(TESTINSTALL_CUDA_ARCH_FLAGS "${GINKGO_CUDA_ARCH_FLAGS}")
endif()
if (CMAKE_CUDA_HOST_COMPILER)
set(TESTINSTALL_CUDA_HOST_COMPILER "-ccbin=${CMAKE_CUDA_HOST_COMPILER}")
endif()
hip_add_executable(test_install_hip test_install.cpp
HIPCC_OPTIONS "-std=c++14"
CLANG_OPTIONS "${GINKGO_PIC_OPTION}"
NVCC_OPTIONS "${GINKGO_CUDA_PIC_OPTION}" "${TESTINSTALL_CUDA_ARCH_FLAGS}" "${TESTINSTALL_CUDA_HOST_COMPILER}")
NVCC_OPTIONS "${GINKGO_CUDA_PIC_OPTION}" "${TESTINSTALL_CUDA_HOST_COMPILER}")

target_link_libraries(test_install_hip PRIVATE Ginkgo::ginkgo)
target_compile_definitions(test_install_hip PRIVATE HAS_HIP=1)
Expand Down

0 comments on commit 100da3f

Please sign in to comment.