CUDA examples ported to HIP. The same sources build for AMD GPUs with
ROCm/HIP and for NVIDIA GPUs with HIP's CUDA backend (hipcc plus the CUDA
Toolkit).
- AMD GPU: ROCm 5.4 or newer. See ROCm Documentation.
- NVIDIA GPU: the CUDA Toolkit plus a
hipccbuild with the NVIDIA backend. See the CUDA Toolkit.
On Debian/Ubuntu, the distribution package includes both backends:
sudo apt install hipccBuild all examples with platform-aware flags:
./Scripts/build-examples.sh
./Scripts/build-examples.sh --runThe script auto-detects AMD vs NVIDIA. Force a backend with
HIP_PLATFORM=amd or HIP_PLATFORM=nvidia.
AMD cross-compilation without a local GPU:
HIP_PLATFORM=amd EXTRA_HIPCC_FLAGS=--offload-arch=gfx90a \
./Scripts/build-examples.shManual build:
cd 01-Hello
hipcc -std=c++17 -o 01-Hello.out 01-Hello.hip \
-I ../Common ../Common/*.cpp ../Common/*.hip
./01-Hello.outCommon headers:
Common/hipCommon.hiphreplacescuda_common.cuh.Common/hipHelpers.hiphcontains optional RAII helpers.Common/hipPlatform.hiphdetects AMD vs NVIDIA and setsHIP_WARP_SIZEto 64 on AMD and 32 on NVIDIA.Common/CUDA/contains a testing-only HIP-to-CUDA compatibility header.
32-DynamicParallelism and 33-Reduction use device-side dynamic parallelism
on NVIDIA and a host-driven fallback on AMD. On NVIDIA they need relocatable
device code, which the build script adds automatically:
hipcc -std=c++17 -rdc=true -o example.out example.hip \
-I ../Common ../Common/*.cpp ../Common/*.hipOn AMD, no -rdc flag is required.
ROCm 5.7's NVIDIA-backend headers do not compile against CUDA 13.2. The build
script detects this and adds Common/CUDA/ automatically. For a manual build:
hipcc -std=c++17 -I ../Common/CUDA -o 01-Hello.out 01-Hello.hip \
-I ../Common ../Common/*.cpp ../Common/*.hipThis compatibility header is not used on AMD/ROCm builds.
Profiling: use rocprof on ROCm and ncu/Nsight on CUDA.
- NVIDIA RTX A6000, CUDA 13.2,
hipcc5.7.1: 66/66 examples built and ran successfully. - AMD cross-compile for
gfx90a: 66/66 examples built successfully. Runtime validation still requires an AMD GPU/ROCm host.
- AMD wavefronts are 64 lanes; NVIDIA warps are 32. Reduction examples use
HIP_WARP_SIZE, so AMD uses 64-lane wavefronts and NVIDIA uses 32-lane warps. - Explicit
gpuErrorCheckcalls are intentional; they are part of the error handling lesson.hipHelpers.hiphhides checks only for RAII cleanup. - Examples
32and33use dynamic parallelism on NVIDIA and a host-driven fallback on AMD.
Pull requests and feedback are welcome.
- Author: Karthik Kumar Viswanathan
- Web: https://karthikkumar.org
- Email: me@karthikkumar.org