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

Add CMake HIP language support #740

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
64 changes: 37 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.21.0 AND CMAKE_VERSION VERSION_LESS 3.2
endif()
message(STATUS "Using CMake ${CMAKE_VERSION}")

project(rocsolver LANGUAGES CXX)
project(rocsolver LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_HIP_STANDARD 17)
set(CMAKE_HIP_EXTENSIONS OFF)
set(CMAKE_HIP_STANDARD_REQUIRED ON)

option(ROCSOLVER_EMBED_FMT "Hide libfmt symbols" ON)
option(OPTIMAL "Build specialized kernels for small matrix sizes" ON)
Expand Down Expand Up @@ -118,6 +121,7 @@ option(BUILD_SHARED_LIBS "Build rocSOLVER as a shared library" ON)

# Include helper functions and wrapper functions
include(util)
include(CheckLanguage)
include(CMakeDependentOption)

option(BUILD_TESTING "Build rocSOLVER tests" OFF)
Expand Down Expand Up @@ -153,33 +157,39 @@ message(STATUS "Tests: ${BUILD_CLIENTS_TESTS}")
message(STATUS "Benchmarks: ${BUILD_CLIENTS_BENCHMARKS}")
message(STATUS "Samples: ${BUILD_CLIENTS_SAMPLES}")

if(NOT DEFINED AMDGPU_TARGETS)
# Query for compiler support of GPU archs
rocm_check_target_ids(OPTIONAL_AMDGPU_TARGETS
TARGETS
gfx90a:xnack-
gfx90a:xnack+
gfx940
gfx941
gfx942
gfx1100
gfx1101
gfx1102
)
set(AMDGPU_TARGETS_INIT
gfx900
gfx906:xnack-
gfx908:xnack-
gfx1010
gfx1030
${OPTIONAL_AMDGPU_TARGETS}
)
endif()
check_language(HIP)
cmake_dependent_option(USE_HIPCXX "Use CMake HIP language support" OFF CMAKE_HIP_COMPILER OFF)
if(USE_HIPCXX)
enable_language(HIP)
else()
if(NOT DEFINED AMDGPU_TARGETS)
# Query for compiler support of GPU archs
rocm_check_target_ids(OPTIONAL_AMDGPU_TARGETS
TARGETS
gfx90a:xnack-
gfx90a:xnack+
gfx940
gfx941
gfx942
gfx1100
gfx1101
gfx1102
)
set(AMDGPU_TARGETS_INIT
gfx900
gfx906:xnack-
gfx908:xnack-
gfx1010
gfx1030
${OPTIONAL_AMDGPU_TARGETS}
)
endif()

# Set this before finding hip so that hip::device has the required arch flags
# added as usage requirements on its interface
set(AMDGPU_TARGETS "${AMDGPU_TARGETS_INIT}"
CACHE STRING "List of specific machine types for library to target")
# Set this before finding hip so that hip::device has the required arch flags
# added as usage requirements on its interface
set(AMDGPU_TARGETS "${AMDGPU_TARGETS_INIT}"
CACHE STRING "List of specific machine types for library to target")
endif()

# Find HIP dependencies
find_package(hip REQUIRED CONFIG PATHS ${ROCM_PATH} /opt/rocm)
Expand Down
4 changes: 1 addition & 3 deletions clients/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
# SUCH DAMAGE.
# ##########################################################################

project(rocsolver-clients LANGUAGES C CXX)
if(UNIX)
enable_language(Fortran)
endif()

# Specify where to put the client binaries
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/staging")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/staging")

# The rocsolver target will exist if the library is being built along with the clients,
# but if this is a clients-only build, we'll have to search for it.
Expand Down Expand Up @@ -165,7 +164,6 @@ if(BUILD_CLIENTS_BENCHMARKS OR BUILD_CLIENTS_TESTS)
)

prepend_path("${CMAKE_CURRENT_SOURCE_DIR}/" common_source_files common_source_paths)
target_sources(clients-common INTERFACE ${common_source_paths})

# Copy and point to sparse test data
file(COPY
Expand Down
24 changes: 22 additions & 2 deletions clients/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,37 @@
# SUCH DAMAGE.
# ##########################################################################

add_executable(rocsolver-bench client.cpp)
set(rocsolver_bench_common_source
${PROJECT_SOURCE_DIR}/common/src/common_host_helpers.cpp
)

add_executable(rocsolver-bench
client.cpp
${common_source_paths}
${rocsolver_bench_common_source}
)
if(USE_HIPCXX)
set_source_files_properties(
client.cpp
${common_source_paths}
${rocsolver_bench_common_source}
PROPERTIES
LANGUAGE HIP
)
endif()

add_armor_flags(rocsolver-bench "${ARMOR_LEVEL}")

target_link_libraries(rocsolver-bench PRIVATE
Threads::Threads
hip::device
hip::host
rocsolver-common
clients-common
roc::rocsolver
)
if(NOT USE_HIPCXX)
target_link_libraries(rocsolver-test PRIVATE hip::device)
endif()

# Turn on f16c intrinsics
target_compile_options(rocsolver-bench PRIVATE -mf16c)
Expand Down
29 changes: 26 additions & 3 deletions clients/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,27 @@ set(rocsolver_test_source
rocsolver_gtest_main.cpp
)

set(rocsolver_test_common_source
${PROJECT_SOURCE_DIR}/common/src/common_host_helpers.cpp
)

if(USE_HIPCXX)
set_source_files_properties(
${common_source_paths}
${rocsolver_test_common_source}
${roclapack_test_source}
${rocauxiliary_test_source}
${rocrefact_test_source}
${others_test_source}
${rocsolver_test_source}
PROPERTIES
LANGUAGE HIP
)
endif()

add_executable(rocsolver-test
${common_source_paths}
${rocsolver_test_common_source}
${roclapack_test_source}
${rocauxiliary_test_source}
${rocrefact_test_source}
Expand All @@ -165,26 +185,29 @@ if(WIN32)
add_custom_command(TARGET rocsolver-test
POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${file_i} ${PROJECT_BINARY_DIR}/staging/
ARGS -E copy ${file_i} $<TARGET_FILE_DIR:rocsolver-test>
)
endforeach()
add_custom_command(TARGET rocsolver-test
POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy_directory $ENV{rocblas_DIR}/bin/rocblas/library ${PROJECT_BINARY_DIR}/staging/library
ARGS -E copy_directory $ENV{rocblas_DIR}/bin/rocblas/library $<TARGET_FILE_DIR:rocsolver-test>/library
)
endif()

target_link_libraries(rocsolver-test PRIVATE
GTest::GTest
hip::device
hip::host
rocsolver-common
clients-common
$<$<PLATFORM_ID:Linux>:stdc++fs>
$<$<PLATFORM_ID:Linux>:m>
roc::rocsolver
roc::rocblas
)
if(NOT USE_HIPCXX)
target_link_libraries(rocsolver-test PRIVATE hip::device)
endif()

# Turn on f16c intrinsics
target_compile_options(rocsolver-test PRIVATE -mf16c)
Expand Down
5 changes: 0 additions & 5 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ target_include_directories(rocsolver-common INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/include
)

set(source_files
common_host_helpers.cpp
)
prepend_path("${CMAKE_CURRENT_SOURCE_DIR}/src/" source_files source_paths)
target_sources(rocsolver-common INTERFACE ${source_paths})
target_compile_definitions(rocsolver-common INTERFACE __HIP_HCC_COMPAT_MODE__=1)

if(WIN32)
Expand Down
36 changes: 33 additions & 3 deletions library/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,25 @@ set(auxiliaries
common/rocsolver_logger.cpp
)

set(common_source
${PROJECT_SOURCE_DIR}/common/src/common_host_helpers.cpp
)

if(USE_HIPCXX)
set_source_files_properties(
${common_source}
${auxiliaries}
${rocsolver_specialized_source}
${rocsolver_auxiliary_source}
${rocsolver_lapack_source}
${rocsolver_refact_source}
PROPERTIES
LANGUAGE HIP
)
endif()

add_library(rocsolver
${common_source}
${auxiliaries}
${rocsolver_specialized_source}
${rocsolver_auxiliary_source}
Expand All @@ -351,10 +369,12 @@ target_link_libraries(rocsolver
PRIVATE
$<BUILD_INTERFACE:rocsolver-common> # https://gitlab.kitware.com/cmake/cmake/-/issues/15415
roc::rocprim
hip::device
$<$<PLATFORM_ID:Linux>:--rtlib=compiler-rt>
$<$<PLATFORM_ID:Linux>:--unwindlib=libgcc>
)
if(NOT USE_HIPCXX)
target_link_libraries(rocsolver PRIVATE hip::device)
endif()

set(static_depends)

Expand All @@ -375,14 +395,24 @@ else()
endif()

# In ROCm 4.0 and earlier, the default maximum threads per block is 256
target_compile_options(rocsolver PRIVATE --gpu-max-threads-per-block=1024)
if(USE_HIPCXX)
target_compile_options(rocsolver PRIVATE
$<$<COMPILE_LANGUAGE:HIP>:--gpu-max-threads-per-block=1024>)
else()
target_compile_options(rocsolver PRIVATE --gpu-max-threads-per-block=1024)
endif()

# Ignore loop unrolling failures
target_compile_options(rocsolver PRIVATE -Wno-pass-failed)

# hipcc adds these options automatically. These options might have an effect
# on performance, so add them when building with clang directly, just in case.
if(CMAKE_CXX_COMPILER MATCHES ".*clang\\+\\+.*")
if(USE_HIPCXX)
target_compile_options(rocsolver PRIVATE
"$<$<COMPILE_LANGUAGE:HIP>:SHELL:-mllvm -amdgpu-early-inline-all=true>"
"$<$<COMPILE_LANGUAGE:HIP>:SHELL:-mllvm -amdgpu-function-calls=false>"
)
elseif(CMAKE_CXX_COMPILER MATCHES ".*clang\\+\\+.*")
target_compile_options(rocsolver PRIVATE
"SHELL:-mllvm -amdgpu-early-inline-all=true"
"SHELL:-mllvm -amdgpu-function-calls=false"
Expand Down