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 compatibility with HIP #289

Merged
merged 105 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 104 commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
9eabddc
It does not make sense to keep the prehipified versions of files in t…
Mar 20, 2024
e1d2fa2
These are the results of conversions from running hipconvertinplace-p…
HannoSpreeuw Mar 20, 2024
114d9f1
I found this 'hack' for CMAKE_HIP_COMPILER_ENV_VAR on the web, not su…
HannoSpreeuw Mar 21, 2024
a0bedda
This file can be complete removed in this branch, since it is only ab…
HannoSpreeuw Mar 21, 2024
b33b261
These are fixes of loose ends appearing after running the hipconverti…
HannoSpreeuw Apr 30, 2024
04f278c
These are changes from running 'pre-commit run -a'
HannoSpreeuw May 8, 2024
ab0b3c1
These are also changes from running 'pre-commit run -a'
HannoSpreeuw May 8, 2024
56a8e91
There is no HIP equivalent of cuuint32_t, that is why we have added t…
HannoSpreeuw May 8, 2024
6e1b6a9
We commented out #include <cuda.h> because we want to build for HIP. …
HannoSpreeuw May 8, 2024
1b8533c
Trying to build using HIP. GH Action copied from https://github.com/m…
HannoSpreeuw May 8, 2024
5620e7e
Link tests to HIP libraries. test_hipfft still cannot find hipfft.h, …
loostrum May 8, 2024
8255a27
Add required memcpy direction parameter in device-to-device memcpy
loostrum May 8, 2024
4e607c6
Use HIP naming in vector add test, comment out tests related to other…
loostrum May 8, 2024
2155893
replace cuda api calls by hip in fft header and test
loostrum May 8, 2024
f3e7ad3
Delete constructors must be declared __host__ or hip will think they …
loostrum May 8, 2024
c0d5fa2
Try a new GH Actions supplier for ROCM setup. The previous one was fo…
HannoSpreeuw May 8, 2024
5da119d
Work around change of PACKAGE_PREFIX_DIR by find_package() when used …
loostrum May 31, 2024
9d98b3b
hiprtc code should be stored in a char vector instead of string
loostrum May 31, 2024
ab24c17
Use CUDAWRAPPERS_PACKAGE_PREFIX_DIR instead of overwriting PACKAGE_PR…
loostrum Jun 7, 2024
0d9fc88
Fix type used in ptx tests
loostrum Jun 18, 2024
acc69b9
Add macros header for CUDA to HIP translation of flags
loostrum Jun 18, 2024
ab147ad
Merge main into hipified (#282)
loostrum Jun 18, 2024
ba3c0f5
translate getuuid to HIP
loostrum Jun 18, 2024
a3052bc
fix HIP function called for occupancy check; disable NVML
loostrum Jun 18, 2024
e518459
Fix findNvrtcIncludePath for HIP
loostrum Jun 18, 2024
a27e3ef
Fix macro name
loostrum Jun 18, 2024
5e8dec4
Add macros as target so the header is installed
loostrum Jun 20, 2024
a404559
hipHostAlloc will be deprecated. hipHostMalloc should be used.
HannoSpreeuw Jun 27, 2024
30be6a4
__HIP__ is not defined when compiling host code with g++, but cmake does
loostrum Jun 28, 2024
ba3cef5
Changes towards compatibility with both CUDA and HIP
csbnw Jul 9, 2024
3859185
Some fixes
csbnw Jul 9, 2024
8fb2bc3
Extend macros
csbnw Jul 9, 2024
a6e6ee3
Extend and sort macros.hpp
csbnw Jul 9, 2024
2e48265
Exclude macros.hpp from clang-format
csbnw Jul 9, 2024
e430760
Revert cu.hpp to CUDA API wherever possible
csbnw Jul 9, 2024
289f46d
Add macros needed for cufft
csbnw Jul 9, 2024
26dff35
Revert cufft.hpp to CUDA API
csbnw Jul 9, 2024
217f13c
Add macros needed for nvrtc
csbnw Jul 9, 2024
ad8a8a9
Revert nvrtc.hpp to CUDA API
csbnw Jul 9, 2024
eb3023a
Update tests CMakeLists
csbnw Jul 10, 2024
b34eefb
Revert test_cufft
csbnw Jul 10, 2024
b21d1fd
Remove problematic code from cu::Context
csbnw Jul 10, 2024
15cec5c
Add cuComplex macros
csbnw Jul 10, 2024
834c9f8
Fix test_cufft
csbnw Jul 10, 2024
59d5c84
Add missing include of sstream
csbnw Jul 10, 2024
129df20
Change options to something compatible with both CUDA and HIP
csbnw Jul 10, 2024
82fa8f8
Remove commented include of cuda.h
csbnw Jul 10, 2024
02d6e25
Add missing checkCudaCall around CUDA api calls
csbnw Jul 10, 2024
be588e5
Fix test_nvrtc (the program is not maintained between SECTION's)
csbnw Jul 10, 2024
44aa754
Restore cu::DeviceMemory tests
csbnw Jul 10, 2024
7239520
Revert newline in test_nvrtc
csbnw Jul 10, 2024
8da7c3f
Restore managed memory tests
csbnw Jul 10, 2024
a02d818
Restore check for CU_DEVICE_ATTRIBUTE_INTEGRATED
csbnw Jul 10, 2024
e78a319
Restore CHECK from test_nvrtc
csbnw Jul 10, 2024
958edb9
Update comment
csbnw Jul 10, 2024
b238a55
Move comment
csbnw Jul 10, 2024
ec91dcf
Actually try to build cudawrappers in HIP mode in workflow
csbnw Jul 10, 2024
f81ab29
Change CUDAWRAPPERS_ENABLE_HIP to CUDAWRAPPERS_BACKEND
csbnw Jul 10, 2024
f090a9a
Update rocm-setup action v0.1.4-beta
csbnw Jul 10, 2024
a10490e
Update ROCm installation in github workflow
csbnw Jul 10, 2024
2a2e8e6
Add tag for rocm-setup
csbnw Jul 10, 2024
860877d
Change rocm-setup tag to hip-6.1.3
csbnw Jul 10, 2024
e5357f5
Change rocm-setup tag to hipfft-6.1.3
csbnw Jul 10, 2024
60a8e84
Set CMAKE_PREFIX_PATH for HIP in github workflow
csbnw Jul 10, 2024
f12691d
Don't set CMAKE_CXX_COMPILER in github workflow
csbnw Jul 10, 2024
e5afc90
Add prefix for hipcc in printing of tool version
csbnw Jul 10, 2024
0ffe751
Show cmake version
csbnw Jul 10, 2024
4ced610
Install HIP directly instead of using rocm-setup
csbnw Jul 10, 2024
6ed4cce
Use sudo when installing ROCm and HIP
csbnw Jul 10, 2024
a444e5c
Remove *.prehip from .gitignore
csbnw Jul 11, 2024
1fbdaac
Change TRUE to True
csbnw Jul 11, 2024
d4d5fd1
Add hip-version and amdgpu-version env variables
csbnw Jul 11, 2024
253c327
Revert changes to cudawrappers-config.cmake.in
csbnw Jul 11, 2024
eb48fd8
Make detecting of HIP dependencies more user friendly
csbnw Jul 12, 2024
62071f8
Fix formatting in HIP specific CMake code
csbnw Jul 16, 2024
1f5943b
Add cu::Device::getArch
csbnw Jul 16, 2024
debc1bf
Also set _ordinal for alternative Device constructor
csbnw Jul 16, 2024
1050a09
Add changed and removed items to changelog
csbnw Jul 16, 2024
0dcf3d2
Update changelog
csbnw Jul 16, 2024
d51152d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 16, 2024
724b19e
Remove __HIP_PLATFORM_AMD__
csbnw Jul 16, 2024
5cf75ae
Update tests/CMakeLists.txt
csbnw Jul 16, 2024
32542fc
Cleanup Device::getArch
csbnw Jul 16, 2024
980a65d
Update tests/test_cu.cpp
csbnw Jul 16, 2024
3191d77
Fix CMake
csbnw Jul 16, 2024
3ad1e8c
Fix ordinal
csbnw Jul 16, 2024
105a305
Add some missing instances of checkCudaCall
csbnw Jul 16, 2024
82eed06
Dont overwrite CUDAWRAPPERS_BACKEND if already defined
csbnw Jul 16, 2024
0acbc58
Change to CUDAWRAPPERS_BACKEND in CI and set AMDGPU_TARGETS
csbnw Jul 16, 2024
fe3adf1
Make Context::synchronize a no-op with HIP
csbnw Jul 16, 2024
3c37cc8
Define some cu::Context with no-op in HIP mode
csbnw Jul 16, 2024
777193d
Update changelog
csbnw Jul 16, 2024
304098b
Fix minor capability in Device::getArch
csbnw Jul 16, 2024
2e31b97
Add sm_ prefix in Device::getArch for CUDA
csbnw Jul 16, 2024
106d541
Update readme
csbnw Jul 16, 2024
609063d
Update readme
csbnw Jul 16, 2024
42c1df9
Make new text fit within 80 lines to satisfy Codacy
csbnw Jul 16, 2024
2aa18a3
More Codacy fixes in readme
csbnw Jul 16, 2024
05d3462
Fix supposedly dead links
csbnw Jul 16, 2024
bf9c87d
Merge branch 'main' into add-hip
loostrum Jul 18, 2024
95fbe1b
minor cleanup of hip-related includes and defines/typedefs
loostrum Jul 19, 2024
21ba350
Use gcnArchName only on AMD
loostrum Jul 19, 2024
047d940
Check minimum HIP version in cmake, version 6.1 required
loostrum Jul 19, 2024
eb74449
Merge branch 'add-hip' of github.com:nlesc-recruit/cudawrappers into …
loostrum Jul 19, 2024
901c140
Update CITATION.cff
csbnw Jul 19, 2024
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
27 changes: 20 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ on:
jobs:
linting:
name: run
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
env:
gcc-version: "12"
cuda-version: "12.2.0"
hip-version: "6.1.3"
amdgpu-version: "6.1.60103-1_all"
steps:

- name: Check out a copy of the repository
Expand All @@ -33,17 +34,29 @@ jobs:
non-cuda-sub-packages: '["libcufft-dev"]'
linux-local-args: '["--toolkit"]'

- name: Install ROCm and HIP
shell: bash -el {0}
run: |
wget https://repo.radeon.com/amdgpu-install/${{ env.hip-version }}/ubuntu/jammy/amdgpu-install_${{ env.amdgpu-version }}.deb
sudo apt -y install ./amdgpu-install_${{ env.amdgpu-version }}.deb
sudo apt update
sudo apt -y install rocm-hip-runtime-dev hipfft-dev

- name: Show tool versions
run: |
echo "nvcc: $(nvcc --version)"
echo "g++: $(g++-${{ env.gcc-version}} --version)"
echo "hipcc: $(/opt/rocm/bin/hipcc --version)"
echo "g++: $(g++ --version)"
echo "cmake: $(cmake --version)"

- name: Run CMake
- name: Build the code in CUDA mode
shell: bash -el {0}
run: |
cmake -DCMAKE_CXX_COMPILER=g++-${{ env.gcc-version }} -DCUDAWRAPPERS_BUILD_TESTING=True -S . -B build
cmake -DCUDAWRAPPERS_BUILD_TESTING=True -S . -B build
make -C build -j

- name: Build the code
- name: Build the code in HIP mode
shell: bash -el {0}
run: |
cd build && make -j
cmake -DCUDAWRAPPERS_BUILD_TESTING=True -DCUDAWRAPPERS_BACKEND=HIP -DAMDGPU_TARGETS=gfx1101 -S . -B build
make -C build -j
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ repos:
rev: v18.1.8
hooks:
- id: clang-format
exclude: macros.hpp
- repo: https://github.com/pocc/pre-commit-hooks
rev: v1.3.5
hooks:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Added compatibility with HIP
- Added `cu::Device::getArch()`

### Changed

- `cu::Context::{getCurrent, popCurrent, getDevice}` are no longer static
- `inline_local_includes` is now more robust: it properly handles commented
includes and respects the location of an include in the original source file

Expand Down
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ option(CUDAWRAPPERS_BUILD_TESTING "Build cudawrappers tests"
${CUDAWRAPPERS_TESTING_DEFAULT}
)

if(NOT DEFINED CUDAWRAPPERS_BACKEND)
set(CUDAWRAPPERS_BACKEND "CUDA")
endif()
set(CUDAWRAPPERS_BACKEND
${CUDAWRAPPERS_BACKEND}
CACHE STRING "Backend API to use in cudawrappers"
)
set_property(CACHE CUDAWRAPPERS_BACKEND PROPERTY STRINGS "CUDA" "HIP")

if(${CUDAWRAPPERS_BACKEND} STREQUAL "CUDA")
set(CUDAWRAPPERS_BACKEND_CUDA True)
elseif(${CUDAWRAPPERS_BACKEND} STREQUAL "HIP")
set(CUDAWRAPPERS_BACKEND_HIP True)
else()
message(
FATAL_ERROR
"Invalid value for CUDAWRAPPERS_BACKEND: ${CUDAWRAPPERS_BACKEND}"
)
endif()

if(${CUDAWRAPPERS_BACKEND_HIP})
enable_language(HIP)
set(CMAKE_HIP_COMPILER_ENV_VAR "HIPCXX")
endif()

# Enable Clang compiler warnings
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(
Expand Down
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![fair-software.eu](https://img.shields.io/badge/fair--software.eu-%E2%97%8F%20%20%E2%97%8F%20%20%E2%97%8F%20%20%E2%97%8F%20%20%E2%97%8F-green)](https://fair-software.eu)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/d38b0338fda24733ab41a64915af8248)](https://app.codacy.com/gh/nlesc-recruit/cudawrappers/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=nlesc-recruit/cudawrappers&amp;utm_campaign=Badge_Grade)
[![citation metadata](https://github.com/nlesc-recruit/cudawrappers/actions/workflows/cffconvert.yml/badge.svg)](https://github.com/nlesc-recruit/cudawrappers/actions/workflows/cffconvert.yml)
[![Documentation Status](https://readthedocs.org/projects/cudawrappers/badge/?version=latest)](https://cudawrappers.readthedocs.io/en/latest/?badge=latest)
[![Documentation Status](https://readthedocs.org/projects/cudawrappers/badge)](https://cudawrappers.readthedocs.io/en/latest/)


# cudawrappers
Expand All @@ -17,20 +17,29 @@ This library is a C++ wrapper for the Nvidia C libraries (e.g. CUDA driver, nvrt
2. _better fault handling_ (through exceptions);
3. _more compact user code_.

This library also supports AMD GPUs through the HIP: C++ Heterogeneous-Compute
Interface for Portability.

Originally, the API enforced RAII to even further reduce the risk of faulty code, but enforcing RAII and compatibility with (unmanaged) objects obtained outside this API are mutually exclusive.

## Requirements

| Software | Minimum version |
| ----------- | ----------- |
| CUDA | 10.0 or later |
| ROCM | 6.1.0 or later |
| CMake | 3.17 or later |
| gcc | 9.3 or later |
| OS | Linux distro (amd64) |

| Hardware | Type |
| ----------- | ----------- |
| GPU architecture | [NVIDIA PASCAL](https://www.nvidia.com/en-in/geforce/products/10series/architecture/) or newer|
| NVIDIA GPU |
[Pascal](https://www.nvidia.com/en-in/geforce/products/10series/architecture/)
or newer|
| AMD GPU | RDNA2 or newer, CDNA2 or newer |



## Usage

Expand All @@ -55,6 +64,18 @@ make -C build
make -C build install
```

## Usage (HIP)

To enable HIP, make sure to build cudawrappers with
`-DCUDAWRAPPERS_BACKEND=HIP`, or when using `FetchContent`, use
`set(CUDAWRAPPERS_BACKEND "HIP")`. In your project's `CMakeLists.txt`, add
`enable_language(HIP)`. Furthermore, every target that includes a cudawrappers
header file needs to be 'hipified', to this end, add
`set_source_files_properties(source.cpp PROPERTIES LANGUAGE HIP)` for every
relevant `source.cpp` file. Some CUDA specific features may not be available or
not work on non-NVIDIA GPUs, in those cases use `#ifdef(__HIP__)` guards to
patch your code wherever this is needed.

### Usage examples

You can include the cudawrappers library in your own projects in various ways. We have created a few repositories with example setups to get you started:
Expand Down
2 changes: 1 addition & 1 deletion cmake/cudawrappers-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

include(${PACKAGE_PREFIX_DIR}/${CMAKE_INSTALL_LIBDIR}/cmake/@PROJECT_NAME@/cudawrappers-dependencies.cmake)
include(${PACKAGE_PREFIX_DIR}/${CMAKE_INSTALL_LIBDIR}/cmake/@PROJECT_NAME@/cudawrappers-exported.cmake)
include(${PACKAGE_PREFIX_DIR}/${CMAKE_INSTALL_LIBDIR}/cmake/@PROJECT_NAME@/cudawrappers-helper.cmake)
include(${PACKAGE_PREFIX_DIR}/${CMAKE_INSTALL_LIBDIR}/cmake/@PROJECT_NAME@/cudawrappers-helper.cmake)
86 changes: 72 additions & 14 deletions cmake/cudawrappers-dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,19 +1,77 @@
# cudawrappers requires the CUDA Toolkit.If you include cudawrappers in your
# project, you need to include the toolkit yourself
set(CUDA_MIN_VERSION 10.0)
if(${CUDAWRAPPERS_INSTALLED})
find_package(CUDAToolkit ${CUDA_MIN_VERSION} REQUIRED)
if(${CUDAWRAPPERS_BACKEND_HIP})
# cmake-format: off
# This following code attempts to locate the HIP runtime library's root
# directory.
#
# 1. Checks if the ROCM_ROOT and ROCM_PATH variables are defined.
# - Assign them from corresponding environment variables if not defined.
# 2. Searches for the HIP runtime header file in the paths specified
# by ROCM_ROOT and ROCM_PATH.
# - If the path is set but HIP is not found, an error message is generated.
# 3. If HIP is still not found, the script searches in the default path.
# 4. Adding HIP_ROOT_DIR to CMAKE_PREFIX_PATH and Finding Packages
# - The HIP runtime directory is appended to CMAKE_PREFIX_PATH
# to ensure that CMake can find the HIP-related packages.
#
# Usage:
# Set the ROCM_ROOT or ROCM_PATH environment variables, or
# pass them as CMake options, e.g.:
# cmake -DROCM_ROOT=/path/to/rocm ..
# or
# ROCM_ROOT=/path/to/rocm cmake ..
# cmake-format: on

foreach(var IN ITEMS ROCM_ROOT ROCM_PATH)
# Step 1.
if(NOT DEFINED ${var})
set(${var} $ENV{${var}})
endif()

# Step 2.
find_path(
HIP_ROOT_DIR
NAMES include/hip/hip_runtime.h
PATHS ${${var}}
NO_DEFAULT_PATH
)

if(NOT HIP_ROOT_DIR AND ${var})
message(FATAL_ERROR "HIP not found in " ${${var}})
endif()
endforeach()

if(NOT HIP_ROOT_DIR)
# Step 3.
find_path(
HIP_ROOT_DIR
NAMES include/hip/hip_runtime.h
PATHS /opt/rocm
)
endif()

# Step 4.
list(APPEND CMAKE_PREFIX_PATH ${HIP_ROOT_DIR})
set(HIP_MIN_VERSION 6.1)
find_package(hip ${HIP_MIN_VERSION} REQUIRED)
find_package(hipfft REQUIRED)
else()
if(${CUDAToolkit_FOUND})
if(${CUDAToolkit_VERSION_MAJOR} LESS ${CUDA_MIN_VERSION})
message(FATAL_ERROR "Insufficient CUDA version: " ${CUDAToolkit_VERSION}
" < " ${CUDA_MIN_VERSION}
# cudawrappers requires the CUDA Toolkit.If you include cudawrappers in your
# project, you need to include the toolkit yourself
set(CUDA_MIN_VERSION 10.0)
if(${CUDAWRAPPERS_INSTALLED})
loostrum marked this conversation as resolved.
Show resolved Hide resolved
find_package(CUDAToolkit ${CUDA_MIN_VERSION} REQUIRED)
else()
if(${CUDAToolkit_FOUND})
if(${CUDAToolkit_VERSION_MAJOR} LESS ${CUDA_MIN_VERSION})
message(FATAL_ERROR "Insufficient CUDA version: "
${CUDAToolkit_VERSION} " < " ${CUDA_MIN_VERSION}
)
endif()
else()
message(
FATAL_ERROR
"CUDAToolkit not found, use find_package(CUDAToolkit REQUIRED)."
)
endif()
else()
message(
FATAL_ERROR
"CUDAToolkit not found, use find_package(CUDAToolkit REQUIRED)."
)
endif()
endif()
20 changes: 15 additions & 5 deletions cmake/cudawrappers-targets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ get_filename_component(

# Define all the individual components that cudawrappers provides
set(CUDAWRAPPERS_COMPONENTS cu cufft nvml nvrtc nvtx)
set(LINK_cu CUDA::cuda_driver)
set(LINK_cufft CUDA::cuda_driver CUDA::cufft)
set(LINK_nvml CUDA::cuda_driver CUDA::nvml)
set(LINK_nvrtc CUDA::cuda_driver CUDA::nvrtc)
set(LINK_nvtx CUDA::nvToolsExt)
if(${CUDAWRAPPERS_BACKEND_HIP})
list(APPEND CUDAWRAPPERS_COMPONENTS macros)
set(LINK_macros hip::host)
set(LINK_cu hip::host)
set(LINK_cufft hip::host hip::hipfft)
set(LINK_nvml hip::host)
set(LINK_nvrtc hip::host)
set(LINK_nvtx hip::host)
else()
set(LINK_cu CUDA::cuda_driver)
set(LINK_cufft CUDA::cuda_driver CUDA::cufft)
set(LINK_nvml CUDA::cuda_driver CUDA::nvml)
set(LINK_nvrtc CUDA::cuda_driver CUDA::nvrtc)
set(LINK_nvtx CUDA::nvToolsExt)
endif()

foreach(component ${CUDAWRAPPERS_COMPONENTS})
add_library(${component} INTERFACE)
Expand Down
Loading
Loading