Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rapids_cuda_set_runtime #429

Merged
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
5 changes: 5 additions & 0 deletions cmake-format-rapids-cmake.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@
"nargs": 1
}
},
"rapids_cuda_set_runtime": {
"pargs": {
"nargs": 3
}
},

"rapids_export_cpm": {
"pargs": {
Expand Down
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ require.
rapids_cuda_init_architectures </command/rapids_cuda_init_architectures>
rapids_cuda_init_runtime </command/rapids_cuda_init_runtime>
rapids_cuda_patch_toolkit </command/rapids_cuda_patch_toolkit>
rapids_cuda_set_runtime </command/rapids_cuda_set_runtime>
rapids_cuda_set_architectures [Advanced] </command/rapids_cuda_set_architectures>


Expand Down
1 change: 1 addition & 0 deletions docs/command/rapids_cuda_set_runtime.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. cmake-module:: ../../rapids-cmake/cuda/set_runtime.cmake
72 changes: 72 additions & 0 deletions rapids-cmake/cuda/set_runtime.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#=============================================================================
# Copyright (c) 2023, NVIDIA CORPORATION.
#
# 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
#
# http://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.
#=============================================================================
include_guard(GLOBAL)

#[=======================================================================[.rst:
rapids_cuda_set_runtime
-------------------------------

.. versionadded:: v23.08.00

Establish what CUDA runtime library should be used by a single target

.. code-block:: cmake

rapids_cuda_set_runtime( target USE_STATIC (TRUE|FALSE) )
robertmaynard marked this conversation as resolved.
Show resolved Hide resolved

Establishes what CUDA runtime will be used for a target, via
the :cmake:prop_tgt:`CUDA_RUNTIME_LIBRARY <cmake:prop_tgt:CUDA_RUNTIME_LIBRARY>`
and by linking to `CUDA::cudart` or `CUDA::cudart_static` if the :cmake:module:`find_package(CUDAToolkit)
<cmake:module:FindCUDAToolkit>` has been called.

The linking to the `CUDA::cudart` or `CUDA::cudart_static` will have the following
usage behavior:

- For `INTERFACE` targets the linking will be `INTERFACE`
- For all other targets the linking will be `PRIVATE`

.. note::
If using the deprecated `FindCUDA.cmake` you must use the
robertmaynard marked this conversation as resolved.
Show resolved Hide resolved
:cmake:command:`rapids_cuda_init_runtime` method to properly establish the default
mode.

When `USE_STATIC TRUE` is provided the target will link to a
statically-linked CUDA runtime library.

When `USE_STATIC FALSE` is provided the target will link to a
shared-linked CUDA runtime library.


#]=======================================================================]
function(rapids_cuda_set_runtime target use_static value)
list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cuda.set_runtime")

get_target_property(type ${target} TYPE)
if(type STREQUAL "INTERFACE_LIBRARY")
set(mode INTERFACE)
else()
set(mode PRIVATE)
endif()

if(${value})
set_target_properties(${target} PROPERTIES CUDA_RUNTIME_LIBRARY STATIC)
target_link_libraries(${target} ${mode} $<TARGET_NAME_IF_EXISTS:CUDA::cudart_static>)
vyasr marked this conversation as resolved.
Show resolved Hide resolved
else()
set_target_properties(${target} PROPERTIES CUDA_RUNTIME_LIBRARY SHARED)
target_link_libraries(${target} ${mode} $<TARGET_NAME_IF_EXISTS:CUDA::cudart>)
endif()

endfunction()
4 changes: 4 additions & 0 deletions testing/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ add_cmake_config_test( init_runtime-multiple.cmake )
add_cmake_config_test( init_runtime-shared.cmake )
add_cmake_config_test( init_runtime-static.cmake )

add_cmake_config_test( set_runtime-shared.cmake )
add_cmake_config_test( set_runtime-static.cmake )
add_cmake_config_test( set_runtime-on-interface-target.cmake )

add_cmake_config_test( init_arch-all-via-undef )
add_cmake_config_test( init_arch-existing-project-flags.cmake )
add_cmake_config_test( init_arch-native.cmake )
Expand Down
24 changes: 24 additions & 0 deletions testing/cuda/set_runtime-on-interface-target.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#=============================================================================
# Copyright (c) 2023, NVIDIA CORPORATION.
#
# 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
#
# http://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.
#=============================================================================
include(${rapids-cmake-dir}/cuda/set_runtime.cmake)

add_library(uses_cuda INTERFACE)
rapids_cuda_set_runtime(uses_cuda USE_STATIC TRUE)

get_target_property(linked_libs uses_cuda INTERFACE_LINK_LIBRARIES)
if(NOT "$<TARGET_NAME_IF_EXISTS:CUDA::cudart_static>" IN_LIST linked_libs)
message(FATAL_ERROR "rapids_cuda_set_runtime shouldn't set CUDA::cudart_static in target linked libraries correctly")
endif()
31 changes: 31 additions & 0 deletions testing/cuda/set_runtime-shared.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#=============================================================================
# Copyright (c) 2023, NVIDIA CORPORATION.
#
# 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
#
# http://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.
#=============================================================================
include(${rapids-cmake-dir}/cuda/set_runtime.cmake)

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp" " ")
add_library(uses_cuda SHARED ${CMAKE_CURRENT_BINARY_DIR}/empty.cpp)
rapids_cuda_set_runtime(uses_cuda USE_STATIC FALSE)


get_target_property(runtime_state uses_cuda CUDA_RUNTIME_LIBRARY)
if( NOT runtime_state STREQUAL "SHARED")
message(FATAL_ERROR "rapids_cuda_set_runtime didn't correctly set CUDA_RUNTIME_LIBRARY")
endif()

get_target_property(linked_libs uses_cuda LINK_LIBRARIES)
if(NOT "$<TARGET_NAME_IF_EXISTS:CUDA::cudart>" IN_LIST linked_libs)
message(FATAL_ERROR "rapids_cuda_set_runtime didn't privately link to CUDA::cudart_static")
endif()
30 changes: 30 additions & 0 deletions testing/cuda/set_runtime-static.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#=============================================================================
# Copyright (c) 2023, NVIDIA CORPORATION.
#
# 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
#
# http://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.
#=============================================================================
include(${rapids-cmake-dir}/cuda/set_runtime.cmake)

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp" " ")
add_library(uses_cuda SHARED ${CMAKE_CURRENT_BINARY_DIR}/empty.cpp)
rapids_cuda_set_runtime(uses_cuda USE_STATIC TRUE)

get_target_property(runtime_state uses_cuda CUDA_RUNTIME_LIBRARY)
if( NOT runtime_state STREQUAL "STATIC")
message(FATAL_ERROR "rapids_cuda_set_runtime didn't correctly set CUDA_RUNTIME_LIBRARY")
endif()

get_target_property(linked_libs uses_cuda LINK_LIBRARIES)
if(NOT "$<TARGET_NAME_IF_EXISTS:CUDA::cudart_static>" IN_LIST linked_libs)
message(FATAL_ERROR "rapids_cuda_set_runtime didn't privately link to CUDA::cudart_static")
endif()