Skip to content

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.

Terminal window
cmake -B build -DMTL5_WITH_HIP=ON -DCMAKE_PREFIX_PATH=/opt/rocm

The 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.

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.

SpaceHIP allocationHost access
memory_space::device<runtime::hip>hipMallocnone: only the GPU touches it
memory_space::shared<runtime::hip>hipMallocManagedyes
memory_space::pinned<runtime::hip>hipHostMallocyes: 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 build
mtl::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_dgemm
std::vector<std::size_t> pivot;
mtl::lu_factor(dA, pivot); // rocsolver_dgetrf
auto C = mtl::to_host(dC);
CallOperandsRuns as
mult(A, B, C)dense2D, all in HIP memoryrocblas_<t>gemm
mult(A, x, y)dense2D and dense_vector, all in HIP memoryrocblas_<t>gemv
lu_factor(A, pivot)dense2D in HIP memoryrocsolver_<t>getrf
lu_solve(LU, pivot, x, b)the factor and dense_vectors, all in HIP memoryrocsolver_<t>getrs
cholesky_factor(A)dense2D in HIP memoryrocsolver_<t>potrf
cholesky_solve(L, x, b)the factor and dense_vectors, all in HIP memoryrocsolver_<t>potrs
  • float and double, 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-default Accumulator (#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_hip and potrs_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 C is computed as C^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 factor L^T lies exactly where L lies.
  • 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 (hipSetDevice picks the device). Operands live on the current device (hipSetDevice picks 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 x and b as the same vector, and solves in place; an x that overlaps part of b, or the factor, goes through a temporary.

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 i was interchanged with row pivot[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 of A match the host exactly. The shared mechanism is include/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 throws std::length_error. A non-square matrix in device memory throws std::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.

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.

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:

Terminal window
cmake -B build -DMTL5_WITH_HIP=ON -DCMAKE_PREFIX_PATH=/opt/rocm
cmake --build build --target hip_test_hip
MTL5_REQUIRE_HIP=1 ctest --test-dir build -R '^hip_' --output-on-failure

A 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.

  • 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::hip targets AMD GPUs; NVIDIA GPUs use the CUDA backend directly, and HIP-allocated memory stays runtime::hip memory whatever the hardware (design doc).