Skip to content

Commit 5c93d72

Browse files
committed
Fetch CUDA if UR_BUILD_ADAPTER_CUDA is ON
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent ce0c110 commit 5c93d72

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

cmake/FetchCUDA.cmake

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
# See LICENSE.TXT
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
set(UR_CUDA_LIBRARY "" CACHE FILEPATH "Path of the CUDA library")
7+
set(UR_CUDA_INCLUDE_DIR "" CACHE FILEPATH "Directory containing the CUDA Headers")
8+
set(UR_CUDA_REPO "" CACHE STRING "Github repo to get the CUDA sources from")
9+
set(UR_CUDA_TAG "" CACHE STRING " GIT tag of the CUDA taken from github repo")
10+
11+
# Copy CUDA loader/headers locally to the build to avoid leaking their path.
12+
set(CUDA_COPY_DIR ${CMAKE_CURRENT_BINARY_DIR}/cuda_headers)
13+
if (NOT UR_CUDA_LIBRARY STREQUAL "")
14+
get_filename_component(CUDA_LIB_NAME "${UR_CUDA_LIBRARY}" NAME)
15+
set(CUDA_LIBRARY ${CUDA_COPY_DIR}/${CUDA_LIB_NAME})
16+
message(STATUS "CUDA Adapter: Copying CUDA loader to local build tree")
17+
file(COPY ${UR_CUDA_LIBRARY} DESTINATION ${CUDA_COPY_DIR} FOLLOW_SYMLINK_CHAIN)
18+
endif()
19+
if (NOT UR_CUDA_INCLUDE_DIR STREQUAL "")
20+
set(CUDA_INCLUDE_DIR ${CUDA_COPY_DIR})
21+
message(STATUS "CUDA Adapter: Copying CUDA headers to local build tree")
22+
file(COPY ${UR_CUDA_INCLUDE_DIR}/ DESTINATION ${CUDA_COPY_DIR})
23+
endif()
24+
25+
if (NOT DEFINED CUDA_LIBRARY OR NOT DEFINED CUDA_INCLUDE_DIR)
26+
message(STATUS "CUDA Adapter: Download CUDA loader and headers from github.com")
27+
28+
if (UR_CUDA_REPO STREQUAL "")
29+
set(UR_CUDA_REPO "https://gitlab.com/nvidia/headers/cuda-individual/cudart.git")
30+
endif()
31+
if (UR_CUDA_TAG STREQUAL "")
32+
set(UR_CUDA_TAG cuda-12.5.1)
33+
endif()
34+
35+
message(STATUS "CUDA Adapter: Will fetch CUDA ${UR_CUDA_TAG} from ${UR_CUDA_REPO}")
36+
include(FetchContent)
37+
FetchContent_Declare(cuda-content
38+
GIT_REPOSITORY ${UR_CUDA_REPO}
39+
GIT_TAG ${UR_CUDA_TAG}
40+
)
41+
FetchContent_MakeAvailable(cuda-content)
42+
43+
set(CUDA_LIBRARY cuda)
44+
set(CUDA_INCLUDE_DIR ${cuda-content_SOURCE_DIR} CACHE PATH "Path to CUDA headers")
45+
message(STATUS "CUDA include directory: ${CUDA_INCLUDE_DIR}")
46+
endif()
47+
48+
add_library(cuda INTERFACE)
49+
# The MSVC linker does not like / at the start of a path, so to work around this
50+
# we split it into a link library and a library path, where the path is allowed
51+
# to have leading /.
52+
get_filename_component(CUDA_LIBRARY_SRC "${CUDA_LIBRARY}" DIRECTORY)
53+
get_filename_component(CUDA_LIB_NAME "${CUDA_LIBRARY}" NAME)
54+
target_link_directories(cuda
55+
INTERFACE "$<BUILD_INTERFACE:${CUDA_LIBRARY_SRC}>"
56+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
57+
)
58+
target_link_libraries(cuda
59+
INTERFACE "${CUDA_LIB_NAME}"
60+
)
61+
62+
add_library(cuda-headers INTERFACE)
63+
target_include_directories(cuda-headers
64+
INTERFACE "$<BUILD_INTERFACE:${CUDA_INCLUDE_DIR}>"
65+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
66+
)

source/common/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ if (UR_BUILD_ADAPTER_L0 OR UR_BUILD_ADAPTER_L0_V2)
99
set(UMF_LEVEL_ZERO_INCLUDE_DIR "${LEVEL_ZERO_INCLUDE_DIR}" CACHE INTERNAL "Level Zero headers")
1010
endif()
1111

12+
if(UR_BUILD_ADAPTER_CUDA)
13+
include(FetchCUDA)
14+
set(UMF_BUILD_CUDA_PROVIDER ON CACHE INTERNAL "Build UMF CUDA provider")
15+
set(UMF_CUDA_INCLUDE_DIR "${CUDA_INCLUDE_DIR}" CACHE INTERNAL "CUDA headers")
16+
else()
17+
set(UMF_BUILD_CUDA_PROVIDER OFF CACHE INTERNAL "Build UMF CUDA provider")
18+
endif()
19+
1220
add_ur_library(ur_common STATIC
1321
ur_util.cpp
1422
ur_util.hpp
@@ -53,7 +61,6 @@ set(UMF_BUILD_TESTS OFF CACHE INTERNAL "Build UMF tests")
5361
set(UMF_BUILD_EXAMPLES OFF CACHE INTERNAL "Build UMF examples")
5462
set(UMF_BUILD_SHARED_LIBRARY ${UMF_BUILD_SHARED_LIBRARY} CACHE INTERNAL "Build UMF shared library")
5563
set(UMF_BUILD_LIBUMF_POOL_DISJOINT ON CACHE INTERNAL "Build Disjoint Pool")
56-
set(UMF_BUILD_CUDA_PROVIDER OFF CACHE INTERNAL "Build UMF CUDA provider")
5764

5865
FetchContent_MakeAvailable(unified-memory-framework)
5966
FetchContent_GetProperties(unified-memory-framework)

0 commit comments

Comments
 (0)