Skip to content

Commit

Permalink
Merge pull request #721 from omarahmed1111/add-hip-build-to-ur
Browse files Browse the repository at this point in the history
[UR][HIP] Add ur hip target build
  • Loading branch information
omarahmed1111 committed Jul 14, 2023
2 parents df020c0 + 40c5086 commit 4dcc3e2
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ option(VAL_USE_LIBBACKTRACE_BACKTRACE "enable libbacktrace validation backtrace
option(UR_BUILD_TOOLS "build ur tools" ON)
option(UR_BUILD_ADAPTER_L0 "build level 0 adapter from SYCL" OFF)
option(UR_BUILD_ADAPTER_CUDA "build cuda adapter from SYCL" OFF)
option(UR_BUILD_ADAPTER_HIP "build hip adapter from SYCL" OFF)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ List of options provided by CMake:
| UR_BUILD_TOOLS | Build tools | ON/OFF | ON |
| UR_BUILD_ADAPTER_L0 | Fetch and use level-zero adapter from SYCL | ON/OFF | OFF |
| UR_BUILD_ADAPTER_CUDA | Fetch and use cuda adapter from SYCL | ON/OFF | OFF |
| UR_BUILD_ADAPTER_HIP | Fetch and use hip adapter from SYCL | ON/OFF | OFF |
| UR_HIP_PLATFORM | Build hip adapter for AMD or NVIDIA platform | AMD/NVIDIA | AMD |

**General**:

Expand Down
12 changes: 8 additions & 4 deletions source/adapters/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
add_subdirectory(null)


if(UR_BUILD_ADAPTER_L0 OR UR_BUILD_ADAPTER_CUDA)
if(UR_BUILD_ADAPTER_L0 OR UR_BUILD_ADAPTER_CUDA OR UR_BUILD_ADAPTER_HIP)
# fetch adapter sources from SYCL
set(SYCL_ADAPTER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external")
FetchSource(https://github.com/intel/llvm.git sycl-nightly/20230706 "sycl/plugins/unified_runtime/ur" ${SYCL_ADAPTER_DIR})
FetchSource(https://github.com/intel/llvm.git sycl-nightly/20230713 "sycl/plugins/unified_runtime/ur" ${SYCL_ADAPTER_DIR})

get_program_version_major_minor(git GIT_VERSION)
set(GIT_QUIET_OPTION "")
Expand All @@ -21,9 +21,13 @@ if(UR_BUILD_ADAPTER_L0 OR UR_BUILD_ADAPTER_CUDA)
endif()

if(UR_BUILD_ADAPTER_L0)
add_subdirectory(level_zero)
add_subdirectory(level_zero)
endif()

if(UR_BUILD_ADAPTER_CUDA)
add_subdirectory(cuda)
add_subdirectory(cuda)
endif()

if(UR_BUILD_ADAPTER_HIP)
add_subdirectory(hip)
endif()
144 changes: 144 additions & 0 deletions source/adapters/hip/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Copyright (C) 2022 Intel Corporation
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
# See LICENSE.TXT
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

set(HIP_DIR "${SYCL_ADAPTER_DIR}/sycl/plugins/unified_runtime/ur/adapters/hip")

set(TARGET_NAME ur_adapter_hip)

# Set default UR HIP platform to AMD
set(UR_HIP_PLATFORM "AMD" CACHE STRING "UR HIP platform, AMD or NVIDIA")

# Set default ROCm installation directory
set(UR_HIP_ROCM_DIR "/opt/rocm" CACHE STRING "ROCm installation dir")

set(UR_HIP_INCLUDE_DIR "${UR_HIP_ROCM_DIR}/include")

set(UR_HIP_HSA_INCLUDE_DIR "${UR_HIP_ROCM_DIR}/hsa/include")

# Set HIP lib dir
set(UR_HIP_LIB_DIR "${UR_HIP_ROCM_DIR}/hip/lib")

# Check if HIP library path exists (AMD platform only)
if("${UR_HIP_PLATFORM}" STREQUAL "AMD")
if(NOT EXISTS "${UR_HIP_LIB_DIR}")
message(FATAL_ERROR "Couldn't find the HIP library directory at '${UR_HIP_LIB_DIR}',"
" please check ROCm installation.")
endif()
# Check if HIP include path exists
if(NOT EXISTS "${UR_HIP_INCLUDE_DIR}")
message(FATAL_ERROR "Couldn't find the HIP include directory at '${UR_HIP_INCLUDE_DIR}',"
" please check ROCm installation.")
endif()

# Check if HSA include path exists
if(NOT EXISTS "${UR_HIP_HSA_INCLUDE_DIR}")
message(FATAL_ERROR "Couldn't find the HSA include directory at '${UR_HIP_HSA_INCLUDE_DIR}',"
" please check ROCm installation.")
endif()
endif()

# Set includes used in added library (rocmdrv)
set(HIP_HEADERS "${UR_HIP_INCLUDE_DIR};${UR_HIP_HSA_INCLUDE_DIR}")

add_library(${TARGET_NAME}
SHARED
${HIP_DIR}/ur_interface_loader.cpp
${HIP_DIR}/common.hpp
${HIP_DIR}/common.cpp
${HIP_DIR}/context.hpp
${HIP_DIR}/context.cpp
${HIP_DIR}/device.hpp
${HIP_DIR}/device.cpp
${HIP_DIR}/enqueue.cpp
${HIP_DIR}/event.hpp
${HIP_DIR}/event.cpp
${HIP_DIR}/kernel.hpp
${HIP_DIR}/kernel.cpp
${HIP_DIR}/memory.hpp
${HIP_DIR}/memory.cpp
${HIP_DIR}/platform.hpp
${HIP_DIR}/platform.cpp
${HIP_DIR}/program.hpp
${HIP_DIR}/program.cpp
${HIP_DIR}/queue.hpp
${HIP_DIR}/queue.cpp
${HIP_DIR}/sampler.hpp
${HIP_DIR}/sampler.cpp
${HIP_DIR}/usm.cpp
${HIP_DIR}/../../ur.cpp
${HIP_DIR}/../../ur.hpp
${HIP_DIR}/../../usm_allocator.cpp
${HIP_DIR}/../../usm_allocator.hpp
${HIP_DIR}/../../usm_allocator_config.cpp
${HIP_DIR}/../../usm_allocator_config.hpp
)

set_target_properties(${TARGET_NAME} PROPERTIES
VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}"
SOVERSION "${PROJECT_VERSION_MAJOR}"
)

if("${UR_HIP_PLATFORM}" STREQUAL "AMD")
# Import HIP runtime library
add_library(rocmdrv SHARED IMPORTED GLOBAL)

set_target_properties(
rocmdrv PROPERTIES
IMPORTED_LOCATION "${UR_HIP_LIB_DIR}/libamdhip64.so"
INTERFACE_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
)

target_link_libraries(${TARGET_NAME} PRIVATE
${PROJECT_NAME}::headers
${PROJECT_NAME}::common
rocmdrv
)

# Set HIP define to select AMD platform
target_compile_definitions(${TARGET_NAME} PRIVATE __HIP_PLATFORM_AMD__)
elseif("${UR_HIP_PLATFORM}" STREQUAL "NVIDIA")
# Import CUDA libraries
find_package(CUDA REQUIRED)
find_package(Threads REQUIRED)

list(APPEND HIP_HEADERS ${CUDA_INCLUDE_DIRS})

# cudadrv may be defined by the CUDA plugin
if(NOT TARGET cudadrv)
add_library(cudadrv SHARED IMPORTED GLOBAL)
set_target_properties(
cudadrv PROPERTIES
IMPORTED_LOCATION ${CUDA_CUDA_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
)
endif()

add_library(cudart SHARED IMPORTED GLOBAL)
set_target_properties(
cudart PROPERTIES
IMPORTED_LOCATION ${CUDA_CUDART_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${HIP_HEADERS}"
)

target_link_libraries(${TARGET_NAME} PRIVATE
${PROJECT_NAME}::headers
${PROJECT_NAME}::common
Threads::Threads
cudadrv
cudart
)

# Set HIP define to select NVIDIA platform
target_compile_definitions(${TARGET_NAME} PRIVATE __HIP_PLATFORM_NVIDIA__)
else()
message(FATAL_ERROR "Unspecified UR HIP platform please set UR_HIP_PLATFORM to 'AMD' or 'NVIDIA'")
endif()

target_include_directories(${TARGET_NAME} PRIVATE
${HIP_DIR}/../../../
)

0 comments on commit 4dcc3e2

Please sign in to comment.