The HIP backend: AMD GPUs
MTL5 runs dense float and double products, LU and Cholesky factorizations, and their solves on AMD GPUs through ROCm: HIP, rocBLAS and rocSOLVER. It is the third backend on the accelerator core, after Metal and CUDA, mirrors the CUDA backend operation for operation, and follows the accelerator backend architecture. Tracked in issue 1019, under the umbrella #225.
Enabling it
Section titled “Enabling it”cmake -B build -DMTL5_WITH_HIP=ON -DCMAKE_PREFIX_PATH=/opt/rocmThe backend is header-only C++ over the C APIs of the HIP runtime, rocBLAS and rocSOLVER, so the host compiler (gcc or clang) builds it: hipcc is not involved, and no MTL5 code is compiled for the GPU. MTL5::hip is an interface target that links ROCm’s hip::host (which defines __HIP_PLATFORM_AMD__), roc::rocblas and roc::rocsolver, and defines MTL5_HAS_HIP. Code built without it is unchanged. ROCm’s hip package configuration looks for hipcc and the device libraries (AMDDeviceLibs), though the build never runs them. A full ROCm installation has both; a minimal one needs hip-dev, rocblas-dev, rocsolver-dev and rocm-device-libs.
target_link_libraries(my_app PRIVATE MTL5::hip)An installed MTL5 built with the option exports MTL5::hip, and its package configuration finds hip, rocblas and rocsolver for the consumer.
Memory
Section titled “Memory”runtime::hip names memory by pointers. HIP’s unified virtual addressing makes every pointer unique across the host and the devices, so one hipMemcpy copies in any direction.
| Space | HIP allocation | Host access |
|---|---|---|
memory_space::device<runtime::hip> | hipMalloc | none: only the GPU touches it |
memory_space::shared<runtime::hip> | hipMallocManaged | yes |
memory_space::pinned<runtime::hip> | hipHostMalloc | yes: page-locked host memory |
Every kind allocates zero-filled memory. Since device memory cannot run a constructor, it holds only element types whose value is all-zero bytes: arithmetic types, enumerations, pointers and floating-point std::complex. A program opts another such type in by specializing mtl::zero_fill_is_value_init (include/mtl/memory/zero_fill.hpp). Shared memory is the natural space on an APU such as the Radeon 780M in the Ryzen 8845HS and 8945HS, whose GPU shares the CPU’s physical memory; on a discrete card the driver migrates its pages. Where runtime::hip::concurrent_managed_access() is false, the host must not touch shared memory while a kernel may be running. MTL5’s calls wait for the device before they return, but GPU work started by another thread is not covered. Pinned memory is host memory that copies quickly; operations on it run on the host.
#include <mtl/mtl.hpp>#include <mtl/interface/hip.hpp>
using dev = mtl::memory_space::device<mtl::runtime::hip>; // or device<> in a HIP buildmtl::mat::dense2D<double> A = ..., B = ...;auto dA = mtl::copy_to<dev>(A);auto dB = mtl::copy_to<dev>(B);auto dC = mtl::copy_to<dev>(mtl::mat::dense2D<double>(A.num_rows(), B.num_cols()));mtl::mult(dA, dB, dC); // rocblas_dgemmstd::vector<std::size_t> pivot;mtl::lu_factor(dA, pivot); // rocsolver_dgetrfauto C = mtl::to_host(dC);Operations
Section titled “Operations”| Call | Operands | Runs as |
|---|---|---|
mult(A, B, C) | dense2D, all in HIP memory | rocblas_<t>gemm |
mult(A, x, y) | dense2D and dense_vector, all in HIP memory | rocblas_<t>gemv |
lu_factor(A, pivot) | dense2D in HIP memory | rocsolver_<t>getrf |
lu_solve(LU, pivot, x, b) | the factor and dense_vectors, all in HIP memory | rocsolver_<t>getrs |
cholesky_factor(A) | dense2D in HIP memory | rocsolver_<t>potrf |
cholesky_solve(L, x, b) | the factor and dense_vectors, all in HIP memory | rocsolver_<t>potrs |
floatanddouble, one value type across the operands of a call. Other element types, and custom number types, take the host routines in shared memory and do not compile in device memory. A non-defaultAccumulator(#585) never reaches the GPU either.- Placement follows the operands. Every operand in HIP device or shared memory, mixed freely, runs on the GPU. A pinned operand keeps the call off the GPU: with shared and pinned operands it runs on the host, and with a device-memory operand it does not compile, since the host cannot read that operand. Copy it to device or shared memory first. The dispatch trace counts
gemm_hip,gemv_hip,getrf_hip,getrs_hip,potrf_hipandpotrs_hip. - Orientation is free. rocBLAS and rocSOLVER read column-major storage, so a row-major matrix is its stored transpose. Products pass the transpose flags that give the right result, and a row-major
Cis computed asC^T = B^T A^T. A row-major LU is transposed into column-major scratch (rocblas_<t>geam) and back, as is the factor in each row-major solve. A row-major Cholesky needs no transpose: its stored transpose is the same symmetric matrix, whose upper factorL^Tlies exactly whereLlies. - Synchronous. Each call waits for the device, as phase 1 of the design promises, and reports a HIP, rocBLAS or rocSOLVER error as
std::runtime_error. Each thread has its own rocBLAS handle (rocSOLVER uses the same one) for each device it uses (hipSetDevicepicks the device). Operands live on the current device (hipSetDevicepicks it): phase 1 does not move work between GPUs, and an operand allocated on another device is not detected. Streams come later. - Aliasing. A product whose result shares memory with an operand goes through a temporary. A solve may take
xandbas the same vector, and solves in place; anxthat overlaps part ofb, or the factor, goes through a temporary.
Factorizations
Section titled “Factorizations”The factorizations return what the host routines return, and leave the same factor in A, as on Metal:
- Pivots are the host’s convention: row
iwas interchanged with rowpivot[i], 0-based. rocSOLVER’s 1-based pivots are converted, so a factor from the GPU solves on the host, and the reverse. - Failures are the host’s. rocSOLVER reports the first zero pivot, but a blocked factorization leaves a different partial factor from the host’s, and a NaN pivot is not reported. The backend saves a device copy of
A, factors in place, and tests the factor’s diagonal as the host does (zero or NaN for LU, not positive for Cholesky). On a failure the host routine factors the copy instead, so the return code and the contents ofAmatch the host exactly. The shared mechanism isinclude/mtl/interface/device_fallback.hpp. - The other triangle of a Cholesky factor is left as it was, as rocSOLVER and the host routine both leave it.
- Size. rocBLAS and rocSOLVER take dimensions as
rocblas_int(32-bit unless ROCm is built for ILP64), so a larger matrix throwsstd::length_error. A non-square matrix in device memory throwsstd::invalid_argument; one in shared memory first meets the host routine’s assertion, as a host matrix does. The solves check the factor’s shape themselves, in every build.
Precision and supported GPUs
Section titled “Precision and supported GPUs”Every ROCm GPU has FP64, but consumer RDNA parts run it at a small fraction of FP32. double operands run on the GPU anyway; a benchmark should state which precision it ran in.
ROCm’s official support list is narrower than the GPUs that run it. The Radeon 780M (gfx1103) in the Ryzen NUCs is not an official target, and rocBLAS ships no tuned kernels for it; HSA_OVERRIDE_GFX_VERSION=11.0.0 makes it pose as a gfx1100 card, which works widely but is unsupported (see issue 1019 for the findings). A discrete card on ROCm’s list is the dependable test target.
Testing
Section titled “Testing”tests/unit/hip/ checks the allocations, the copies, every orientation of mult, and LU and Cholesky (factors, pivots, solves, failures and edges) against MTL5’s host routines, in float and double. A test skips itself on a machine without a HIP device, and fails instead when MTL5_REQUIRE_HIP is set.
GitHub’s hosted runners have no AMD GPU. CI’s Linux x64 HIP lane installs HIP, rocBLAS and rocSOLVER from AMD’s apt repository, builds the tests with the host compiler and runs them, so each one compiles, links and loads, then skips. The GPU runs happen on a machine with an AMD GPU:
cmake -B build -DMTL5_WITH_HIP=ON -DCMAKE_PREFIX_PATH=/opt/rocmcmake --build build --target hip_test_hipMTL5_REQUIRE_HIP=1 ctest --test-dir build -R '^hip_' --output-on-failureA self-hosted runner on such a machine (a cloud GPU instance, or a NUC with the override) would turn the lane into a GPU lane by setting MTL5_REQUIRE_HIP.
Not done yet
Section titled “Not done yet”- A GPU run. The tests have run against a host emulation of the HIP, rocBLAS and rocSOLVER APIs and in CI to the skip, not yet on an AMD GPU.
- Eigenvalues and the SVD through rocSOLVER (
syevd,gesvd), and complex element types. - Streams (
stream<runtime::hip>), several right-hand sides per solve, and rocSPARSE for sparse matrices. - HIP on NVIDIA.
runtime::hiptargets AMD GPUs; NVIDIA GPUs use the CUDA backend directly, and HIP-allocated memory staysruntime::hipmemory whatever the hardware (design doc).