Skip to content

Commit

Permalink
Eval CMAKE_CUDA_ARCHITECTURES before ENV{CUDAARCHS} (#624)
Browse files Browse the repository at this point in the history
Corrects a difference in behavior between rapids-cmake and cmake, where we would evaluate `ENV{CUDAARCHS}` over `CMAKE_CUDA_ARCHITECTURES` when both are set.

Authors:
  - Robert Maynard (https://github.com/robertmaynard)

Approvers:
  - Paul Taylor (https://github.com/trxcllnt)
  - Kyle Edwards (https://github.com/KyleFromNVIDIA)

URL: #624
  • Loading branch information
robertmaynard authored Jun 12, 2024
1 parent 5b05708 commit 116a5cc
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 142 deletions.
65 changes: 0 additions & 65 deletions rapids-cmake/cuda/detail/architectures_policy.cmake

This file was deleted.

21 changes: 5 additions & 16 deletions rapids-cmake/cuda/init_architectures.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -69,25 +69,14 @@ Example on how to properly use :cmake:command:`rapids_cuda_init_architectures`:
function(rapids_cuda_init_architectures project_name)
list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cuda.init_architectures")

include(${CMAKE_CURRENT_FUNCTION_LIST_DIR}/detail/architectures_policy.cmake)
# If `CMAKE_CUDA_ARCHITECTURES` is not defined, build for all supported architectures. If
# `CMAKE_CUDA_ARCHITECTURES` is set to an empty string (""), build for only the current
# architecture. If `CMAKE_CUDA_ARCHITECTURES` is specified by the user, use user setting.
if(DEFINED ENV{CUDAARCHS} AND ("$ENV{CUDAARCHS}" STREQUAL "RAPIDS" OR "$ENV{CUDAARCHS}" STREQUAL
"ALL"))
set(cuda_arch_mode "$ENV{CUDAARCHS}")
rapids_cuda_architectures_policy(FROM_INIT cuda_arch_mode)
elseif(DEFINED ENV{CUDAARCHS} AND "$ENV{CUDAARCHS}" STREQUAL "NATIVE")
set(cuda_arch_mode "NATIVE")
elseif(CMAKE_CUDA_ARCHITECTURES STREQUAL "RAPIDS" OR CMAKE_CUDA_ARCHITECTURES STREQUAL "ALL")
if(CMAKE_CUDA_ARCHITECTURES STREQUAL "RAPIDS" OR CMAKE_CUDA_ARCHITECTURES STREQUAL "NATIVE")
set(cuda_arch_mode "${CMAKE_CUDA_ARCHITECTURES}")
rapids_cuda_architectures_policy(FROM_INIT cuda_arch_mode)
elseif(CMAKE_CUDA_ARCHITECTURES STREQUAL "")
set(cuda_arch_mode "NATIVE")
set(deprecated_cuda_arch_mode "EMPTY_STR")
rapids_cuda_architectures_policy(FROM_INIT deprecated_cuda_arch_mode)
elseif(CMAKE_CUDA_ARCHITECTURES STREQUAL "NATIVE")
set(cuda_arch_mode "NATIVE")
elseif(DEFINED ENV{CUDAARCHS} AND ("$ENV{CUDAARCHS}" STREQUAL "RAPIDS" OR "$ENV{CUDAARCHS}"
STREQUAL "NATIVE"))
set(cuda_arch_mode "$ENV{CUDAARCHS}")
elseif(NOT (DEFINED ENV{CUDAARCHS} OR DEFINED CMAKE_CUDA_ARCHITECTURES))
set(cuda_arch_mode "RAPIDS")
endif()
Expand Down
16 changes: 12 additions & 4 deletions rapids-cmake/cuda/set_architectures.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ directly.
Result Variables
^^^^^^^^^^^^^^^^
``CMAKE_CUDA_ARCHITECTURES`` will exist and set to the list of architectures
``CMAKE_CUDA_ARCHITECTURES``
Will exist as a local variable and be set to the list of architectures
that should be compiled for. Will overwrite any existing values.
.. versionadded:: v24.08.00
Will be added as a cache variable if it isn't already one.
#]=======================================================================]
function(rapids_cuda_set_architectures mode)
list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cuda.set_architectures")
Expand All @@ -60,9 +65,6 @@ function(rapids_cuda_set_architectures mode)
list(REMOVE_ITEM supported_archs "90")
endif()

include(${CMAKE_CURRENT_FUNCTION_LIST_DIR}/detail/architectures_policy.cmake)
rapids_cuda_architectures_policy(FROM_SET mode)

if(${mode} STREQUAL "RAPIDS")

# CMake architecture list entry of "80" means to build compute and sm. What we want is for the
Expand Down Expand Up @@ -90,6 +92,12 @@ function(rapids_cuda_set_architectures mode)
)
endif()

# Need to set in the cache so we match CMake behavior of setting up as a cache variable. This
# ensures that on subsequent executions we use the value we computed from the first execution, and
# don't re-evaluate env vars which could have changed
set(CMAKE_CUDA_ARCHITECTURES "${CMAKE_CUDA_ARCHITECTURES}" CACHE STRING "CUDA architectures")

# Set as a local variable to maintain comp
set(CMAKE_CUDA_ARCHITECTURES ${CMAKE_CUDA_ARCHITECTURES} PARENT_SCOPE)

endfunction()
11 changes: 2 additions & 9 deletions testing/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,6 +32,7 @@ add_cmake_config_test( init_arch-rapids.cmake )
add_cmake_config_test( init_arch-rapids-via-env.cmake )
add_cmake_config_test( init_arch-user.cmake )
add_cmake_config_test( init_arch-user-via-env.cmake )
add_cmake_config_test( init_arch-var-over-env.cmake )

add_cmake_config_test( patch_toolkit.cmake )
add_cmake_config_test( patch_toolkit-nested )
Expand All @@ -40,11 +41,3 @@ add_cmake_config_test( set_arch-existing.cmake )
add_cmake_config_test( set_arch-invalid-mode.cmake )
add_cmake_config_test( set_arch-native.cmake )
add_cmake_config_test( set_arch-rapids.cmake )

set(deprecated_message [=[rapids-cmake policy [deprecated=23.02 removed=23.06]: Usage of `ALL`]=])
add_cmake_config_test( init_arch-all-via-env-deprecated.cmake SHOULD_FAIL "${deprecated_message}")
add_cmake_config_test( init_arch-all-deprecated.cmake SHOULD_FAIL "${deprecated_message}")
add_cmake_config_test( set_arch-all-deprecated.cmake SHOULD_FAIL "${deprecated_message}")

set(deprecated_message [=[rapids-cmake policy [deprecated=23.02 removed=23.06]: Usage of `""`]=])
add_cmake_config_test( init_arch-native-via-empty-str-deprecated SHOULD_FAIL "${deprecated_message}")
21 changes: 0 additions & 21 deletions testing/cuda/init_arch-all-deprecated.cmake

This file was deleted.

3 changes: 1 addition & 2 deletions testing/cuda/init_arch-native-via-env.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2022-2023, NVIDIA CORPORATION.
# Copyright (c) 2022-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
#=============================================================================
include(${rapids-cmake-dir}/cuda/init_architectures.cmake)

set(CMAKE_ERROR_DEPRECATED ON)
set(ENV{CUDAARCHS} "NATIVE")
rapids_cuda_init_architectures(rapids-project)
project(rapids-project LANGUAGES CUDA)
Expand Down
3 changes: 1 addition & 2 deletions testing/cuda/init_arch-native.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
#=============================================================================
include(${rapids-cmake-dir}/cuda/init_architectures.cmake)

set(CMAKE_ERROR_DEPRECATED ON)
set(CMAKE_CUDA_ARCHITECTURES "NATIVE")
rapids_cuda_init_architectures(rapids-project)
project(rapids-project LANGUAGES CUDA)
Expand Down
3 changes: 1 addition & 2 deletions testing/cuda/init_arch-rapids-via-env.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2022-2023, NVIDIA CORPORATION.
# Copyright (c) 2022-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
#=============================================================================
include(${rapids-cmake-dir}/cuda/init_architectures.cmake)

set(CMAKE_ERROR_DEPRECATED ON)
set(ENV{CUDAARCHS} "RAPIDS")

rapids_cuda_init_architectures(rapids-project)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2023, NVIDIA CORPORATION.
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,20 @@
#=============================================================================
include(${rapids-cmake-dir}/cuda/init_architectures.cmake)

cmake_minimum_required(VERSION 3.23.1)

set(ENV{CUDAARCHS} "ALL")
set(CMAKE_CUDA_ARCHITECTURES RAPIDS)
set(ENV{CUDAARCHS} "9000")

rapids_cuda_init_architectures(rapids-project)
project(rapids-project LANGUAGES CUDA)

if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
message(FATAL_ERROR "rapids_cuda_init_architectures didn't init CUDA_ARCHITECTURES")
endif()

if(CMAKE_CUDA_ARCHITECTURES STREQUAL "9000")
message(FATAL_ERROR "rapids_cuda_init_architectures didn't init CUDA_ARCHITECTURES")
endif()

include("${rapids-cmake-testing-dir}/cuda/validate-cuda-rapids.cmake")
17 changes: 0 additions & 17 deletions testing/cuda/set_arch-all-deprecated.cmake

This file was deleted.

6 changes: 5 additions & 1 deletion testing/cuda/validate-cuda-native.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -25,3 +25,7 @@ foreach(value IN LISTS CMAKE_CUDA_ARCHITECTURES)
endif()

endforeach()

if(NOT DEFINED CACHE{CMAKE_CUDA_ARCHITECTURES} )
message(FATAL_ERROR "rapids_cuda_set_architectures didn't make CMAKE_CUDA_ARCHITECTURES a cache variable")
endif()
6 changes: 5 additions & 1 deletion testing/cuda/validate-cuda-rapids.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#=============================================================================
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,3 +44,7 @@ if( last_value LESS previous_value )
endif()

list(APPEND CMAKE_CUDA_ARCHITECTURES ${last_value})

if(NOT DEFINED CACHE{CMAKE_CUDA_ARCHITECTURES} )
message(FATAL_ERROR "rapids_cuda_set_architectures didn't make CMAKE_CUDA_ARCHITECTURES a cache variable")
endif()

0 comments on commit 116a5cc

Please sign in to comment.