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

[Cmake] HIP/CUDA/OpenCL on AMD and NV support through CMake #323

Merged
merged 2 commits into from
Apr 16, 2020
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
14 changes: 5 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ endif()

# searches for CUDA if allowed
if (ALLOW_CUDA)
set(OCCA_CUDA_ENABLED 1)
set(WITH_CUDA 1)
enable_language(CUDA)
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
include(CUDA)
endif()

if (ALLOW_HIP)
include(HIP)
endif()

# searches for MPI if allowed
Expand All @@ -57,11 +58,6 @@ if (ALLOW_METAL)
include(Metal)
endif()

if (ALLOW_HIP)
# set(OCCA_HIP_ENABLED 1)
# set(WITH_HIP 1)
# include(ROCm) ... not available, TODO occa/cmake/FindROCm.cmake
endif()

add_definitions(-DUSE_CMAKE)

Expand Down
27 changes: 23 additions & 4 deletions cmake/CUDA.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
find_package(CUDA)

if (CUDA_FOUND)
set(OCCA_CUDA_ENABLED 1)
set(WITH_CUDA 1)
include_directories( ${CUDA_INCLUDE_DIRS} )
endif()
#find the shared library, rather than the static that find_package returns
find_library(
CUDART_LIB
NAMES cudart
PATHS
${CUDA_TOOLKIT_ROOT_DIR}
PATH_SUFFIXES lib64 lib
DOC "CUDA RT lib location"
)

set(CUDA_LIBRARIES "${CUDART_LIB};cuda")

set(WITH_CUDA 1)
set(OCCA_CUDA_ENABLED 1)
include_directories( ${CUDA_INCLUDE_DIRS} )

message(STATUS "CUDA version: ${CUDA_VERSION_STRING}")
message(STATUS "CUDA Include Paths: ${CUDA_INCLUDE_DIRS}")
message(STATUS "CUDA Libraries: ${CUDA_LIBRARIES}")
else (CUDA_FOUND)
set(WITH_CUDA 0)
set(OCCA_CUDA_ENABLED 0)
endif(CUDA_FOUND)
139 changes: 139 additions & 0 deletions cmake/FindHIP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
###############################################################################
# FIND: HIP and associated helper binaries
###############################################################################
# HIP is supported on Linux only
if(UNIX AND NOT APPLE AND NOT CYGWIN)
# Search for HIP installation
if(NOT HIP_ROOT_DIR)
# Search in user specified path first
find_path(
HIP_ROOT_DIR
NAMES hipconfig
PATHS
ENV ROCM_PATH
ENV HIP_PATH
PATH_SUFFIXES bin
DOC "HIP installed location"
NO_DEFAULT_PATH
)
# Now search in default path
find_path(
HIP_ROOT_DIR
NAMES hipconfig
PATHS
/opt/rocm
/opt/rocm/hip
PATH_SUFFIXES bin
DOC "HIP installed location"
)

# Check if we found HIP installation
if(HIP_ROOT_DIR)
# If so, fix the path
string(REGEX REPLACE "[/\\\\]?bin[64]*[/\\\\]?$" "" HIP_ROOT_DIR ${HIP_ROOT_DIR})
# And push it back to the cache
set(HIP_ROOT_DIR ${HIP_ROOT_DIR} CACHE PATH "HIP installed location" FORCE)
endif()

if(NOT EXISTS ${HIP_ROOT_DIR})
if(HIP_FIND_REQUIRED)
message(FATAL_ERROR "Specify HIP_ROOT_DIR")
elseif(NOT HIP_FIND_QUIETLY)
message("HIP_ROOT_DIR not found or specified")
endif()
endif()
endif()

# Find HIPCONFIG executable
find_program(
HIP_HIPCONFIG_EXECUTABLE
NAMES hipconfig
PATHS
"${HIP_ROOT_DIR}"
ENV ROCM_PATH
ENV HIP_PATH
/opt/rocm
/opt/rocm/hip
PATH_SUFFIXES bin
NO_DEFAULT_PATH
)
if(NOT HIP_HIPCONFIG_EXECUTABLE)
# Now search in default paths
find_program(HIP_HIPCONFIG_EXECUTABLE hipconfig)
endif()
mark_as_advanced(HIP_HIPCONFIG_EXECUTABLE)

if(HIP_HIPCONFIG_EXECUTABLE AND NOT HIP_VERSION)
# Compute the version
execute_process(
COMMAND ${HIP_HIPCONFIG_EXECUTABLE} --version
OUTPUT_VARIABLE _hip_version
ERROR_VARIABLE _hip_error
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE
)
if(NOT _hip_error)
set(HIP_VERSION ${_hip_version} CACHE STRING "Version of HIP as computed from hipcc")
else()
set(HIP_VERSION "0.0.0" CACHE STRING "Version of HIP as computed by FindHIP()")
endif()
mark_as_advanced(HIP_VERSION)
endif()
if(HIP_VERSION)
string(REPLACE "." ";" _hip_version_list "${HIP_VERSION}")
list(GET _hip_version_list 0 HIP_VERSION_MAJOR)
list(GET _hip_version_list 1 HIP_VERSION_MINOR)
list(GET _hip_version_list 2 HIP_VERSION_PATCH)
set(HIP_VERSION_STRING "${HIP_VERSION}")
endif()

if(HIP_HIPCONFIG_EXECUTABLE AND NOT HIP_PLATFORM)
# Compute the platform
execute_process(
COMMAND ${HIP_HIPCONFIG_EXECUTABLE} --platform
OUTPUT_VARIABLE _hip_platform
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(HIP_PLATFORM ${_hip_platform} CACHE STRING "HIP platform as computed by hipconfig")
mark_as_advanced(HIP_PLATFORM)
endif()

if(${HIP_PLATFORM} STREQUAL "hcc")
set(HIP_INCLUDE_DIRS "${HIP_ROOT_DIR}/include;${HIP_ROOT_DIR}/hcc/include")
set(HIP_LIBRARIES "${HIP_ROOT_DIR}/lib/libhip_hcc.so")
set(HIP_RUNTIME_DEFINE "__HIP_PLATFORM_HCC__")
elseif(${HIP_PLATFORM} STREQUAL "nvcc")
find_package(CUDA)

#find the shared library, rather than the static that find_package returns
find_library(
CUDART_LIB
NAMES cudart
PATHS
${CUDA_TOOLKIT_ROOT_DIR}
PATH_SUFFIXES lib64 lib
DOC "CUDA RT lib location"
)

set(HIP_INCLUDE_DIRS "${HIP_ROOT_DIR}/include;${CUDA_INCLUDE_DIRS}")
set(HIP_LIBRARIES "${CUDART_LIB};cuda")
set(HIP_RUNTIME_DEFINE "__HIP_PLATFORM_NVCC__")
endif()
mark_as_advanced(HIP_INCLUDE_DIRS)
mark_as_advanced(HIP_LIBRARIES)
mark_as_advanced(HIP_RUNTIME_DEFINE)

endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
HIP
REQUIRED_VARS
HIP_ROOT_DIR
HIP_INCLUDE_DIRS
HIP_LIBRARIES
HIP_RUNTIME_DEFINE
HIP_HIPCONFIG_EXECUTABLE
HIP_PLATFORM
VERSION_VAR HIP_VERSION
)
16 changes: 16 additions & 0 deletions cmake/HIP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
find_package(HIP)

if (HIP_FOUND)
set(WITH_HIP 1)
set(OCCA_HIP_ENABLED 1)
add_definitions(-D${HIP_RUNTIME_DEFINE})
include_directories( ${HIP_INCLUDE_DIRS} )

message(STATUS "HIP version: ${HIP_VERSION_STRING}")
message(STATUS "HIP platform: ${HIP_PLATFORM}")
message(STATUS "HIP Include Paths: ${HIP_INCLUDE_DIRS}")
message(STATUS "HIP Libraries: ${HIP_LIBRARIES}")
else (HIP_FOUND)
set(WITH_HIP 0)
set(OCCA_HIP_ENABLED 0)
endif(HIP_FOUND)
25 changes: 25 additions & 0 deletions cmake/OpenCL.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
#Look in some default places for OpenCL and set OPENCL_ROOT

# Search in user specified path first
find_path(
OPENCL_ROOT
NAMES include/CL/cl.h
PATHS
ENV OPENCL_PATH
DOC "OPENCL root location"
NO_DEFAULT_PATH
)
# Now search in default path
find_path(
OPENCL_ROOT
NAMES include/CL/cl.h
PATHS
/usr/
/opt/rocm/opencl
/usr/local/cuda
DOC "OPENCL root location"
)

#Trick cmake's default OpenCL module to look in our directory
set(ENV{AMDAPPSDKROOT} ${OPENCL_ROOT})

find_package(OpenCL)

if (OpenCL_FOUND)
Expand Down
12 changes: 6 additions & 6 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ macro(add_test_with_modes exe)
if (WITH_CUDA)
add_test_with_mode(${exe} cuda "mode: 'CUDA', device_id: 0")
endif()
if (WITH_HIP)
add_test_with_mode(${exe} hip "mode: 'HIP', device_id: 0")
endif()
if (WITH_METAL)
add_test_with_mode(${exe} metal "mode: 'Metal', device_id: 0")
endif()
Expand All @@ -26,26 +29,23 @@ endmacro()

macro(compile_c_example target file)
add_executable(examples_c_${target} ${file})
target_link_libraries(examples_c_${target} libocca ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(examples_c_${target} libocca)
if (ENABLE_TESTS)
add_test_with_modes(examples_c_${target})
endif()
endmacro()

macro(compile_cpp_example target file)
add_executable(examples_cpp_${target} ${file})
target_link_libraries(examples_cpp_${target} libocca ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(examples_cpp_${target} libocca)
if (ENABLE_TESTS)
add_test_without_mode(examples_cpp_${target})
endif()
endmacro()

macro(compile_cpp_example_with_modes target file)
add_executable(examples_cpp_${target} ${file})
target_link_libraries(examples_cpp_${target} libocca ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(examples_cpp_${target} libocca)
if (ENABLE_TESTS)
add_test_with_modes(examples_cpp_${target})
endif()
Expand Down
8 changes: 6 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ set_target_properties(libocca PROPERTIES OUTPUT_NAME occa LIBRARY_OUTPUT_DIRECTO
set(LIBOCCA_LIBRARIES ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS})

if (WITH_CUDA)
set(LIBOCCA_LIBRARIES cuda cudart ${LIBOCCA_LIBRARIES})
set(LIBOCCA_LIBRARIES ${CUDA_LIBRARIES} ${LIBOCCA_LIBRARIES})
endif()

if (WITH_HIP)
set(LIBOCCA_LIBRARIES ${HIP_LIBRARIES} ${LIBOCCA_LIBRARIES})
endif()

if (WITH_OPENCL)
set(LIBOCCA_LIBRARIES ${LIBOCCA_LIBRARIES} ${OpenCL_LIBRARIES} ${LIBOCCA_LIBRARIES})
set(LIBOCCA_LIBRARIES ${OpenCL_LIBRARIES} ${LIBOCCA_LIBRARIES})
endif()

if (WITH_MPI)
Expand Down