Skip to content

Commit

Permalink
Introduce rapids_cuda_patch_toolkit_targets
Browse files Browse the repository at this point in the history
Allows rapids-cmake patch CUDAToolkit to fix missing cublas
and cusolver dependencies.

https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7633
  • Loading branch information
robertmaynard committed Sep 2, 2022
1 parent 8ff1c5c commit 05c3175
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/command/rapids_cuda_patch_toolkit_targets.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. cmake-module:: ../../rapids-cmake/cuda/patch_toolkit_targets.cmake
52 changes: 52 additions & 0 deletions rapids-cmake/cuda/patch_toolkit_targets.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#=============================================================================
# Copyright (c) 2022, 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_patch_toolkit_targets
---------------------------------

.. versionadded:: v22.10.00

Corrects missing dependencies in the CUDA toolkit

.. code-block:: cmake

rapids_cuda_patch_toolkit_targets( )

For CMake versions 3.23.1-3, and 3.24.1 the dependencies
of cublas and cusolver targets are incorrect. This module must be called
from the same CMakeLists.txt as the first `find_project(CUDAToolkit)` to
patch the targets.

.. note::
:cmake:command:`rapids_cpm_find` will automatically call this module
when asked to find the CUDAToolkit.

#]=======================================================================]
function(rapids_cuda_patch_toolkit_targets)
list(APPEND CMAKE_MESSAGE_CONTEXT "rapids.cuda.patch_toolkit_targets")

get_directory_property(itargets IMPORTED_TARGETS)
if(CUDA::cublas_static IN_LIST itargets)
target_link_libraries(CUDA::cublas INTERFACE CUDA::cublasLt)
target_link_libraries(CUDA::cusparse INTERFACE CUDA::cublas)

target_link_libraries(CUDA::cublas_static INTERFACE CUDA::cublasLt_static)
target_link_libraries(CUDA::cusolver_static INTERFACE CUDA::cusolver_lapack_static)
target_link_libraries(CUDA::cusparse_static INTERFACE CUDA::cublas_static)
endif()
endfunction()
5 changes: 5 additions & 0 deletions rapids-cmake/find/package.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ macro(rapids_find_package name)
# OPTIONAL find packages
if(${${name}_FOUND})

if(${name} STREQUAL "CUDAToolkit")
include("${rapids-cmake-dir}/cuda/patch_toolkit_targets.cmake")
rapids_cuda_patch_toolkit_targets()
endif()

set(_rapids_extra_info)
if(_RAPIDS_GLOBAL_TARGETS)
list(APPEND _rapids_extra_info "GLOBAL_TARGETS" ${_RAPIDS_GLOBAL_TARGETS})
Expand Down
4 changes: 3 additions & 1 deletion testing/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ add_cmake_config_test( init_arch-native.cmake )
add_cmake_config_test( init_arch-native-via-empty-str )
add_cmake_config_test( init_arch-user.cmake )

add_cmake_config_test( patch_toolkit_targets.cmake )
add_cmake_config_test( patch_toolkit_targets-nested )

add_cmake_config_test( set_arch-all.cmake )
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 )

40 changes: 40 additions & 0 deletions testing/cuda/patch_toolkit_targets-nested/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#=============================================================================
# Copyright (c) 2022, 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.
#=============================================================================

cmake_minimum_required(VERSION 3.23.1)
project(rapids-project LANGUAGES CUDA)

include(${rapids-cmake-dir}/cuda/patch_toolkit_targets.cmake)

function(verify_links_to target library)
get_target_property(link_libs ${target} INTERFACE_LINK_LIBRARIES)
if(NOT ${library} IN_LIST link_libs)
message(FATAL_ERROR "${target} doesn't link to ${library}")
endif()
endfunction()

find_package(CUDAToolkit)
rapids_cuda_patch_toolkit_targets()

add_subdirectory(subdir)

if(TARGET CUDA::cublas_static)
verify_links_to(CUDA::cublas CUDA::cublasLt)
verify_links_to(CUDA::cusparse CUDA::cublas)
verify_links_to(CUDA::cublas_static CUDA::cublasLt_static)
verify_links_to(CUDA::cusparse_static CUDA::cublas_static)
verify_links_to(CUDA::cusolver_static CUDA::cusolver_lapack_static)
endif()
28 changes: 28 additions & 0 deletions testing/cuda/patch_toolkit_targets-nested/subdir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#=============================================================================
# Copyright (c) 2022, 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/patch_toolkit_targets.cmake)

rapids_cuda_patch_toolkit_targets()
find_package(CUDAToolkit)
rapids_cuda_patch_toolkit_targets()

if(TARGET CUDA::cublas_static)
verify_links_to(CUDA::cublas CUDA::cublasLt)
verify_links_to(CUDA::cusparse CUDA::cublas)
verify_links_to(CUDA::cublas_static CUDA::cublasLt_static)
verify_links_to(CUDA::cusparse_static CUDA::cublas_static)
verify_links_to(CUDA::cusolver_static CUDA::cusolver_lapack_static)
endif()
37 changes: 37 additions & 0 deletions testing/cuda/patch_toolkit_targets.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#=============================================================================
# Copyright (c) 2022, 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/patch_toolkit_targets.cmake)

function(verify_links_to target library)
get_target_property(link_libs ${target} INTERFACE_LINK_LIBRARIES)
if(NOT ${library} IN_LIST link_libs)
message(FATAL_ERROR "${target} doesn't link to ${library}")
endif()
endfunction()

# Verify we can call before find_package
rapids_cuda_patch_toolkit_targets()

find_package(CUDAToolkit)
rapids_cuda_patch_toolkit_targets()

if(TARGET CUDA::cublas_static)
verify_links_to(CUDA::cublas CUDA::cublasLt)
verify_links_to(CUDA::cusparse CUDA::cublas)
verify_links_to(CUDA::cublas_static CUDA::cublasLt_static)
verify_links_to(CUDA::cusparse_static CUDA::cublas_static)
verify_links_to(CUDA::cusolver_static CUDA::cusolver_lapack_static)
endif()
2 changes: 2 additions & 0 deletions testing/find/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ add_cmake_config_test( find_package-no-variable-leak.cmake )
add_cmake_config_test( find_package-build.cmake )
add_cmake_config_test( find_package-install.cmake )

add_cmake_config_test( find_package-cudatoolkit-patching.cmake )

# The inverse of `find_package-optional-failed` is the
# above two tests.
# The inverse of `find_package-required-found` is always
Expand Down
33 changes: 33 additions & 0 deletions testing/find/find_package-cudatoolkit-patching.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#=============================================================================
# Copyright (c) 2022, 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}/find/package.cmake)

function(verify_links_to target library)
get_target_property(link_libs ${target} INTERFACE_LINK_LIBRARIES)
if(NOT ${library} IN_LIST link_libs)
message(FATAL_ERROR "${target} doesn't link to ${library}")
endif()
endfunction()

rapids_find_package(CUDAToolkit)

if(TARGET CUDA::cublas_static)
verify_links_to(CUDA::cublas CUDA::cublasLt)
verify_links_to(CUDA::cusparse CUDA::cublas)
verify_links_to(CUDA::cublas_static CUDA::cublasLt_static)
verify_links_to(CUDA::cusparse_static CUDA::cublas_static)
verify_links_to(CUDA::cusolver_static CUDA::cusolver_lapack_static)
endif()

0 comments on commit 05c3175

Please sign in to comment.