Skip to content

Commit

Permalink
Enable HIP language
Browse files Browse the repository at this point in the history
If the HIP language can be used, the HIP_USECXX variable sets rocThrust to use the CMake HIP language rather than CXX.
  • Loading branch information
lawruble13 authored and umfranzw committed Nov 26, 2024
1 parent d935e55 commit 5e91399
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 57 deletions.
93 changes: 54 additions & 39 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@ endif()
# Note: C is required here for dependencies
project(rocthrust LANGUAGES CXX C)

# Set CXX flags
if (NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Set HIP flags
set(CMAKE_HIP_STANDARD 14)
set(CMAKE_HIP_STANDARD_REQUIRED ON)
set(CMAKE_HIP_EXTENSIONS OFF)

include(CheckLanguage)
include(CMakeDependentOption)

# Build options
# Disable -Werror
option(DISABLE_WERROR "Disable building with Werror" ON)
option(BUILD_TEST "Build tests" OFF)
option(BUILD_HIPSTDPAR_TEST "Build hipstdpar tests" OFF)
option(BUILD_EXAMPLES "Build examples" OFF)
option(BUILD_BENCHMARKS "Build benchmarks" OFF)
option(DOWNLOAD_ROCPRIM "Download rocPRIM and do not search for rocPRIM package" OFF)
option(BUILD_ADDRESS_SANITIZER "Build with address sanitizer enabled" OFF)
cmake_dependent_option(ENABLE_UPSTREAM_TESTS "Enable upstream (thrust) tests" ON BUILD_TEST OFF)
#Set the header wrapper OFF by default.
option(BUILD_FILE_REORG_BACKWARD_COMPATIBILITY "Build with file/folder reorg with backward compatibility enabled" OFF)

check_language(HIP)
cmake_dependent_option(USE_HIPCXX "Use CMake HIP language support" OFF CMAKE_HIP_COMPILER OFF)

#Adding CMAKE_PREFIX_PATH
list( APPEND CMAKE_PREFIX_PATH /opt/rocm/llvm /opt/rocm ${ROCM_PATH} )

Expand All @@ -34,7 +65,6 @@ endif()

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "Add paths to linker search and installed rpath")

include(CMakeDependentOption)
# rocm-cmake contains common cmake code for rocm projects to help
# setup and install
include( cmake/FindROCMCmake.cmake )
Expand All @@ -47,26 +77,30 @@ include( ROCMHeaderWrapper )
include( ROCMCheckTargetIds )
include( ROCMClients )

# Use target ID syntax if supported for GPU_TARGETS
if (NOT DEFINED AMDGPU_TARGETS)
set(GPU_TARGETS "all" CACHE STRING "GPU architectures to compile for")
if(USE_HIPCXX)
enable_language(HIP)
else()
set(GPU_TARGETS "${AMDGPU_TARGETS}" CACHE STRING "GPU architectures to compile for")
endif()
set_property(CACHE GPU_TARGETS PROPERTY STRINGS "all")

if(GPU_TARGETS STREQUAL "all")
if(BUILD_ADDRESS_SANITIZER)
# ASAN builds require xnack
rocm_check_target_ids(DEFAULT_AMDGPU_TARGETS
TARGETS "gfx908:xnack+;gfx90a:xnack+;gfx942:xnack+"
)
# Use target ID syntax if supported for GPU_TARGETS
if (NOT DEFINED AMDGPU_TARGETS)
set(GPU_TARGETS "all" CACHE STRING "GPU architectures to compile for")
else()
rocm_check_target_ids(DEFAULT_AMDGPU_TARGETS
TARGETS "gfx803;gfx900:xnack-;gfx906:xnack-;gfx908:xnack-;gfx90a:xnack-;gfx90a:xnack+;gfx942;gfx1030;gfx1100;gfx1101;gfx1102;gfx1151;gfx1200;gfx1201"
)
set(GPU_TARGETS "${AMDGPU_TARGETS}" CACHE STRING "GPU architectures to compile for")
endif()
set_property(CACHE GPU_TARGETS PROPERTY STRINGS "all")

if(GPU_TARGETS STREQUAL "all")
if(BUILD_ADDRESS_SANITIZER)
# ASAN builds require xnack
rocm_check_target_ids(DEFAULT_AMDGPU_TARGETS
TARGETS "gfx908:xnack+;gfx90a:xnack+;gfx942:xnack+"
)
else()
rocm_check_target_ids(DEFAULT_AMDGPU_TARGETS
TARGETS "gfx803;gfx900:xnack-;gfx906:xnack-;gfx908:xnack-;gfx90a:xnack-;gfx90a:xnack+;gfx942;gfx1030;gfx1100;gfx1101;gfx1102;gfx1151;gfx1200;gfx1201"
)
endif()
set(GPU_TARGETS "${DEFAULT_AMDGPU_TARGETS}" CACHE STRING "GPU architectures to compile for" FORCE)
endif()
set(GPU_TARGETS "${DEFAULT_AMDGPU_TARGETS}" CACHE STRING "GPU architectures to compile for" FORCE)
endif()

# Get dependencies
Expand All @@ -76,18 +110,6 @@ include(cmake/Dependencies.cmake)
if (NOT WIN32)
include(cmake/VerifyCompiler.cmake)
endif()
# Build options
# Disable -Werror
option(DISABLE_WERROR "Disable building with Werror" ON)
option(BUILD_TEST "Build tests" OFF)
option(BUILD_HIPSTDPAR_TEST "Build hipstdpar tests" OFF)
option(BUILD_EXAMPLES "Build examples" OFF)
option(BUILD_BENCHMARKS "Build benchmarks" OFF)
option(DOWNLOAD_ROCPRIM "Download rocPRIM and do not search for rocPRIM package" OFF)
option(BUILD_ADDRESS_SANITIZER "Build with address sanitizer enabled" OFF)
cmake_dependent_option(ENABLE_UPSTREAM_TESTS "Enable upstream (thrust) tests" ON BUILD_TEST OFF)
#Set the header wrapper OFF by default.
option(BUILD_FILE_REORG_BACKWARD_COMPATIBILITY "Build with file/folder reorg with backward compatibility enabled" OFF)

set(RNG_SEED_COUNT 0 CACHE STRING "Number of true random sequences to test each input size for")
set(PRNG_SEEDS 1 CACHE STRING "Seeds of pseudo random sequences to test each input size for")
Expand All @@ -106,17 +128,10 @@ if (NOT THRUST_HOST_SYSTEM IN_LIST THRUST_HOST_SYSTEM_OPTIONS)
)
endif ()

# Set CXX flags
if (NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(DISABLE_WERROR)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra ")
add_compile_options(-Wall -Wextra)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
add_compile_options(-Wall -Wextra -Werror)
endif()

if (CMAKE_CXX_STANDARD EQUAL 14)
Expand Down
15 changes: 11 additions & 4 deletions cmake/Benchmarks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ endfunction()
# Registers a .cu as C++ rocThrust benchmark
function(add_thrust_benchmark BENCHMARK_NAME BENCHMARK_SOURCE NOT_INTERNAL)
set(BENCHMARK_TARGET "benchmark_thrust_${BENCHMARK_NAME}")
set_source_files_properties(${BENCHMARK_SOURCE}
PROPERTIES
LANGUAGE CXX
)
if(USE_HIPCXX)
set_source_files_properties(${BENCHMARK_SOURCE}
PROPERTIES
LANGUAGE HIP
)
else()
set_source_files_properties(${BENCHMARK_SOURCE}
PROPERTIES
LANGUAGE CXX
)
endif()
add_executable(${BENCHMARK_TARGET} ${BENCHMARK_SOURCE})

target_link_libraries(${BENCHMARK_TARGET}
Expand Down
11 changes: 11 additions & 0 deletions cmake/Summary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ function (print_configuration_summary)
message(STATUS "General:")
message(STATUS " System : ${CMAKE_SYSTEM_NAME}")
message(STATUS " HIP ROOT : ${HIP_ROOT_DIR}")
if(USE_HIPCXX)
message(STATUS " HIP compiler : ${CMAKE_HIP_COMPILER}")
message(STATUS " HIP compiler version : ${CMAKE_HIP_COMPILER_VERSION}")
string(STRIP "${CMAKE_HIP_FLAGS}" CMAKE_HIP_FLAGS_STRIP)
message(STATUS " HIP flags : ${CMAKE_HIP_FLAGS_STRIP}")
else()
message(STATUS " C++ compiler : ${CMAKE_CXX_COMPILER}")
message(STATUS " C++ compiler version : ${CMAKE_CXX_COMPILER_VERSION}")
string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS_STRIP)
message(STATUS " CXX flags : ${CMAKE_CXX_FLAGS_STRIP}")
endif()
message(STATUS " Build type : ${CMAKE_BUILD_TYPE}")
message(STATUS " Install prefix : ${CMAKE_INSTALL_PREFIX}")
if(HIP_COMPILER STREQUAL "clang")
if(USE_HIPCXX)
message(STATUS " Device targets : ${CMAKE_HIP_ARCHITECTURES}")
else()
message(STATUS " Device targets : ${GPU_TARGETS}")
endif()
endif()
message(STATUS "")
message(STATUS " DISABLE_WERROR : ${DISABLE_WERROR}")
Expand Down
10 changes: 8 additions & 2 deletions cmake/VerifyCompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ find_package(hip REQUIRED CONFIG PATHS /opt/rocm)
if(HIP_COMPILER STREQUAL "nvcc")
message(FATAL_ERROR "rocThrust does not support the CUDA backend.")
elseif(HIP_COMPILER STREQUAL "clang")
if(NOT (CMAKE_CXX_COMPILER MATCHES ".*hipcc$" OR CMAKE_CXX_COMPILER MATCHES ".*clang\\+\\+"))
message(FATAL_ERROR "On ROCm platform 'hipcc' or HIP-aware Clang must be used as C++ compiler.")
if(USE_HIPCXX)
if(NOT (CMAKE_HIP_COMPILER MATCHES ".*hipcc$" OR CMAKE_HIP_COMPILER MATCHES ".*clang\\+\\+"))
message(FATAL_ERROR "On ROCm platform 'hipcc' or HIP-aware Clang must be used as HIP compiler.")
endif()
else()
if(NOT (CMAKE_CXX_COMPILER MATCHES ".*hipcc$" OR CMAKE_CXX_COMPILER MATCHES ".*clang\\+\\+"))
message(FATAL_ERROR "On ROCm platform 'hipcc' or HIP-aware Clang must be used as C++ compiler.")
endif()
endif()
else()
message(FATAL_ERROR "HIP_COMPILER must be `clang` (AMD ROCm platform)")
Expand Down
15 changes: 11 additions & 4 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ endif()
function(add_thrust_example EXAMPLE)
set(EXAMPLE_SOURCE "${EXAMPLE}.cu")
set(EXAMPLE_TARGET "example_thrust_${EXAMPLE}")
set_source_files_properties(${EXAMPLE_SOURCE}
if(USE_HIPCXX)
set_source_files_properties(${EXAMPLE_SOURCE}
PROPERTIES
LANGUAGE CXX
)
LANGUAGE HIP
)
else()
set_source_files_properties(${EXAMPLE_SOURCE}
PROPERTIES
LANGUAGE CXX
)
endif()
add_executable(${EXAMPLE_TARGET} ${EXAMPLE_SOURCE})

target_link_libraries(${EXAMPLE_TARGET}
Expand Down Expand Up @@ -45,7 +52,7 @@ function(add_thrust_example EXAMPLE)
foreach( file_i ${third_party_dlls})
add_custom_command( TARGET ${EXAMPLE_TARGET} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${file_i} ${PROJECT_BINARY_DIR}/examples )
endforeach( file_i )
endif()
endif()
endfunction()


Expand Down
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ endfunction()

function(add_rocthrust_test TEST)
set(TEST_SOURCE "test_${TEST}.cpp")
if(USE_HIPCXX)
set_source_files_properties(${TEST_SOURCE} PROPERTIES LANGUAGE HIP)
endif()

set(TEST_TARGET "${TEST}.hip")
add_executable(${TEST_TARGET} ${TEST_SOURCE})
target_include_directories(${TEST_TARGET} SYSTEM BEFORE
Expand Down
3 changes: 3 additions & 0 deletions test/hipstdpar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

function(add_hipstdpar_test TEST TEST_TYPE INTERPOSE_ALLOC)
set(TEST_SOURCE "test_${TEST}.cpp")
if(USE_HIPCXX)
set_source_files_properties(${TEST_SOURCE} PROPERTIES LANGUAGE HIP)
endif()
set(TEST_TARGET "${TEST}_${TEST_TYPE}.hip")

add_executable(${TEST_TARGET} ${TEST_SOURCE})
Expand Down
30 changes: 22 additions & 8 deletions testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ if(HIP_COMPILER STREQUAL "nvcc")
)
target_sources(testframework PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/unittest/cuda/testframework.cu)
elseif(HIP_COMPILER STREQUAL "clang")
set_source_files_properties(unittest/hip/testframework.cu
PROPERTIES
LANGUAGE CXX
)
if(USE_HIPCXX)
set_source_files_properties(unittest/hip/testframework.cu
PROPERTIES
LANGUAGE HIP
)
else()
set_source_files_properties(unittest/hip/testframework.cu
PROPERTIES
LANGUAGE CXX
)
endif()
target_sources(testframework PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/unittest/hip/testframework.cu)
endif()

Expand All @@ -76,10 +83,17 @@ function(add_thrust_test TEST)
# This code path used to add "-x c++" to the compiler flags too, but that was
# superfluous as "-x hip" was already present on the command-line due to linking to
# hip::device transitively from roc::rocprim_hip.
set_source_files_properties(${TEST_SOURCE}
PROPERTIES
LANGUAGE CXX
)
if(USE_HIPCXX)
set_source_files_properties(${TEST_SOURCE}
PROPERTIES
LANGUAGE HIP
)
else()
set_source_files_properties(${TEST_SOURCE}
PROPERTIES
LANGUAGE CXX
)
endif()
if(NOT CMAKE_VERSION VERSION_LESS 3.13)
add_executable(${TEST_TARGET} ${TEST_SOURCE} $<TARGET_OBJECTS:testframework>)
target_link_libraries(${TEST_TARGET}
Expand Down

0 comments on commit 5e91399

Please sign in to comment.