Skip to content

Commit 9428bae

Browse files
committed
Fetch CUDA if UR_BUILD_ADAPTER_CUDA is ON
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 258a858 commit 9428bae

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

cmake/FetchCUDA.cmake

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
FetchContent_GetProperties(cuda-content)
43+
44+
set(CUDA_LIBRARY cuda)
45+
set(CUDA_INCLUDE_DIR ${cuda-content_SOURCE_DIR} CACHE PATH "Path to CUDA headers")
46+
message(STATUS "CUDA include directory: ${CUDA_INCLUDE_DIR}")
47+
endif()
48+
49+
add_library(cuda INTERFACE)
50+
# The MSVC linker does not like / at the start of a path, so to work around this
51+
# we split it into a link library and a library path, where the path is allowed
52+
# to have leading /.
53+
get_filename_component(CUDA_LIBRARY_SRC "${CUDA_LIBRARY}" DIRECTORY)
54+
get_filename_component(CUDA_LIB_NAME "${CUDA_LIBRARY}" NAME)
55+
target_link_directories(cuda
56+
INTERFACE "$<BUILD_INTERFACE:${CUDA_LIBRARY_SRC}>"
57+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
58+
)
59+
target_link_libraries(cuda
60+
INTERFACE "${CUDA_LIB_NAME}"
61+
)
62+
63+
add_library(cuda-headers INTERFACE)
64+
target_include_directories(cuda-headers
65+
INTERFACE "$<BUILD_INTERFACE:${CUDA_INCLUDE_DIR}>"
66+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
67+
)

source/common/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ 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+
endif()
17+
1218
add_ur_library(ur_common STATIC
1319
ur_util.cpp
1420
ur_util.hpp
@@ -53,7 +59,6 @@ set(UMF_BUILD_TESTS OFF CACHE INTERNAL "Build UMF tests")
5359
set(UMF_BUILD_EXAMPLES OFF CACHE INTERNAL "Build UMF examples")
5460
set(UMF_BUILD_SHARED_LIBRARY ${UMF_BUILD_SHARED_LIBRARY} CACHE INTERNAL "Build UMF shared library")
5561
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")
5762

5863
FetchContent_MakeAvailable(unified-memory-framework)
5964
FetchContent_GetProperties(unified-memory-framework)

0 commit comments

Comments
 (0)