Skip to content

Commit

Permalink
Add CMake HIP language support (#188)
Browse files Browse the repository at this point in the history
Add USE_HIPCXX option to build using CMake's support for the
HIP language. This option requires CMake 3.21.3 or newer.
  • Loading branch information
cgmb authored Jul 7, 2023
1 parent dae71f1 commit 5bd014f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
22 changes: 16 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ project(rocalution LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_HIP_STANDARD 14)
set(CMAKE_HIP_STANDARD_REQUIRED ON)
set(CMAKE_HIP_EXTENSIONS OFF)

# Build options
option(BUILD_SHARED_LIBS "Build rocALUTION as a shared library" ON)
Expand Down Expand Up @@ -91,17 +94,24 @@ set(DEFAULT_AMDGPU_TARGETS
)
set(AMDGPU_TARGETS "${DEFAULT_AMDGPU_TARGETS}" CACHE STRING "List of specific machine types for library to target")

# Find HIP package
find_package(HIP)

if (NOT HIP_FOUND)
message("-- HIP not found. Compiling WITHOUT HIP support.")
include(CheckLanguage)
include(CMakeDependentOption)
check_language(HIP)
cmake_dependent_option(USE_HIPCXX "Use CMake HIP language support" ON CMAKE_HIP_COMPILER OFF)
if(USE_HIPCXX)
enable_language(HIP)
else()
option(SUPPORT_HIP "Compile WITH HIP support." ON)
find_package(HIP MODULE) # hip_add_library is only provided by the find module
if(NOT HIP_FOUND)
message("-- HIP not found. Compiling WITHOUT HIP support.")
endif()
endif()

cmake_dependent_option(SUPPORT_HIP "Compile WITH HIP support" ON "USE_HIPCXX OR HIP_FOUND" OFF)

# HIP related library dependencies
if(SUPPORT_HIP)
find_package(hip REQUIRED)
find_package(rocblas REQUIRED)
find_package(rocsparse REQUIRED)
find_package(rocprim REQUIRED)
Expand Down
46 changes: 28 additions & 18 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,37 @@ target_compile_options(rocalution PRIVATE ${CMAKE_HOST_FLAGS})
# Create rocALUTION hip library
if(SUPPORT_HIP)
# Flag source file as a hip source file
foreach(i ${HIP_SOURCES})
set_source_files_properties(${i} PROPERTIES HIP_SOURCE_PROPERTY_FORMAT TRUE)
endforeach()

# HIP flags workaround while target_compile_options do not work

# If CPU optimizations for local machine is actived
if(BUILD_OPTCPU)
list(APPEND HIP_HIPCC_FLAGS "-march=native")
if(USE_HIPCXX)
set_source_files_properties(${HIP_SOURCES} PROPERTIES LANGUAGE HIP)
else()
set_source_files_properties(${HIP_SOURCES} PROPERTIES HIP_SOURCE_PROPERTY_FORMAT TRUE)
endif()

list(APPEND HIP_HIPCC_FLAGS "-O3 -fPIC -std=c++14")
foreach(target ${AMDGPU_TARGETS})
list(APPEND HIP_HIPCC_FLAGS "--offload-arch=${target}")
endforeach()

# Create rocALUTION HIP library
if(WIN32)
add_library(rocalution_hip OBJECT ${HIP_SOURCES})
if(USE_HIPCXX)
add_library(rocalution_hip ${HIP_SOURCES})
# If CPU optimizations for local machine is actived
if(BUILD_OPTCPU)
target_compile_options(rocalution_hip PRIVATE "-march=native")
endif()
else()
hip_add_library(rocalution_hip ${HIP_SOURCES})
# HIP flags workaround while target_compile_options do not work

# If CPU optimizations for local machine is actived
if(BUILD_OPTCPU)
list(APPEND HIP_HIPCC_FLAGS "-march=native")
endif()

list(APPEND HIP_HIPCC_FLAGS "-O3 -fPIC -std=c++14")
foreach(target ${AMDGPU_TARGETS})
list(APPEND HIP_HIPCC_FLAGS "--offload-arch=${target}")
endforeach()

# Create rocALUTION HIP library
if(WIN32)
add_library(rocalution_hip OBJECT ${HIP_SOURCES})
else()
hip_add_library(rocalution_hip ${HIP_SOURCES})
endif()
endif()

if(SUPPORT_MPI)
Expand Down

0 comments on commit 5bd014f

Please sign in to comment.