Post

Setting up HIP on Fedora for AMD GPUs

Setting up HIP on Fedora for AMD GPUs

Basic environment setup

Install the compiler, HIP development headers, and GPU detection tool:

1
2
3
sudo dnf -y install hipcc
# AMD GPU specific
sudo dnf -y install rocm-clang rocm-lld rocm-hip-devel rocminfo

After successful installation, check the compiler version and confirm that ROCm detects the GPU:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ hipcc --version
HIP version: 7.2.53211-9999
clang version 22.0.0.rocm
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/lib64/rocm/llvm/bin

$ rocminfo | grep -E 'Name:'
  Name:                    AMD Ryzen 5 5600X 6-Core Processor
  Marketing Name:          AMD Ryzen 5 5600X 6-Core Processor
  Vendor Name:             CPU
  Name:                    gfx1031
  Marketing Name:          AMD Radeon RX 6700 XT
  Vendor Name:             AMD
      Name:                    amdgcn-amd-amdhsa--gfx1031
      Name:                    amdgcn-amd-amdhsa--gfx10-3-generic

Quick example

A test program test_hip_compile.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <hip/hip_runtime.h>
#include <cstdio>
#include <cstdlib>

void check(hipError_t status, const char* step) {
    if (status != hipSuccess) {
        std::fprintf(stderr, "%s: %s\n", step, hipGetErrorString(status));
        std::exit(EXIT_FAILURE);
    }
}

__global__ void write_value(int* output) {
    *output = 42;
}

int main() {
    hipDeviceProp_t gpu{};
    check(hipGetDeviceProperties(&gpu, 0), "Detect GPU");

    int* device_value = nullptr;
    check(hipMalloc(&device_value, sizeof(int)), "Allocate GPU memory");

    write_value<<<1, 1>>>(device_value);
    check(hipGetLastError(), "Launch kernel");
    check(hipDeviceSynchronize(), "Run kernel");

    int result = 0;
    check(hipMemcpy(&result, device_value, sizeof(result), hipMemcpyDeviceToHost),
          "Copy result to CPU");
    check(hipFree(device_value), "Free GPU memory");

    std::printf("%s on %s (%s): got %d, expected 42\n",
                result == 42 ? "PASS" : "FAIL", gpu.name, gpu.gcnArchName, result);
    return result == 42 ? EXIT_SUCCESS : EXIT_FAILURE;
}

Compiling it using hipcc:

1
2
3
$ hipcc test_hip_compile.cpp -lamdhip64 -o test_hip_compile.exe
$ ./test_hip_compile.exe
PASS on AMD Radeon RX 6700 XT (gfx1031): got 42, expected 42

If -lamdhip64 is not specified, the compilation threw ld.lld undefined symbol errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$ hipcc test_hip_compile.cpp -o test_hip_compile.exe
ld.lld: error: undefined symbol: hipGetErrorString
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(check(hipError_t, char const*))
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)
>>> referenced 4 more times

ld.lld: error: undefined symbol: __hipPopCallConfiguration
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(__device_stub__write_value(int*))
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)

ld.lld: error: undefined symbol: hipLaunchKernel
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(__device_stub__write_value(int*))
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)

ld.lld: error: undefined symbol: hipGetDevicePropertiesR0600
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)

ld.lld: error: undefined symbol: hipMalloc
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)

ld.lld: error: undefined symbol: __hipPushCallConfiguration
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)

ld.lld: error: undefined symbol: hipGetLastError
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)

ld.lld: error: undefined symbol: hipDeviceSynchronize
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)

ld.lld: error: undefined symbol: hipMemcpy
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)

ld.lld: error: undefined symbol: hipFree
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(main)

ld.lld: error: undefined symbol: __hipRegisterFatBinary
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(__hip_module_ctor)

ld.lld: error: undefined symbol: __hipRegisterFunction
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(__hip_module_ctor)

ld.lld: error: undefined symbol: __hipUnregisterFatBinary
>>> referenced by test_hip_compile.cpp
>>>               /tmp/test_hip_compile-db4a87.o:(__hip_module_dtor)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
failed to execute:/usr/lib64/rocm/llvm/bin/clang++  -O3 --driver-mode=g++ -O3 --hip-link  -x hip test_hip_compile.cpp -o "test_hip_compile.exe"

To avoid manually adding -lamdhip64 to every hipcc command, we can add it to environment varaible HIPCC_LINK_FLAGS_APPEND1:

1
export HIPCC_LINK_FLAGS_APPEND="-lamdhip64"

GPU linear algebra

Install hipBLAS and hipSOLVER for GPU linear algebra:

1
sudo dnf -y install hipblas-devel hipsolver-devel

dnf also installs the AMD backends rocblas and rocsolver.

hipblas example

Test matrix-matrix multiplication with test_hipblas_gemm.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <hip/hip_runtime.h>
#include <hipblas/hipblas.h>

#include <cstdio>

#define CHECK(call)                                                       \
    do {                                                                  \
        const int status = static_cast<int>(call);                        \
        if (status != 0) {                                                \
            std::fprintf(stderr, "%s failed (status %d)\n", #call, status); \
            return 1;                                                     \
        }                                                                 \
    } while (false)

int main() {
    // Column-major storage for A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]].
    const float a[] = {1, 3, 2, 4};
    const float b[] = {5, 7, 6, 8};
    float c[4]{};
    constexpr int n = 2;
    const float alpha = 1.0f;
    const float beta = 0.0f;

    float *d_a = nullptr, *d_b = nullptr, *d_c = nullptr;
    CHECK(hipMalloc(&d_a, sizeof(a)));
    CHECK(hipMalloc(&d_b, sizeof(b)));
    CHECK(hipMalloc(&d_c, sizeof(c)));
    CHECK(hipMemcpy(d_a, a, sizeof(a), hipMemcpyHostToDevice));
    CHECK(hipMemcpy(d_b, b, sizeof(b), hipMemcpyHostToDevice));

    hipblasHandle_t handle;
    CHECK(hipblasCreate(&handle));
    CHECK(hipblasSgemm(handle, HIPBLAS_OP_N, HIPBLAS_OP_N, n, n, n,
                       &alpha, d_a, n, d_b, n, &beta, d_c, n));
    CHECK(hipMemcpy(c, d_c, sizeof(c), hipMemcpyDeviceToHost));
    CHECK(hipblasDestroy(handle));
    CHECK(hipFree(d_c));
    CHECK(hipFree(d_b));
    CHECK(hipFree(d_a));

    const bool passed = c[0] == 19 && c[1] == 43 && c[2] == 22 && c[3] == 50;
    std::printf("%s: C = [[%g, %g], [%g, %g]]\n",
                passed ? "PASS" : "FAIL", c[0], c[2], c[1], c[3]);
    return passed ? 0 : 1;
}

Build and run the example:

1
2
3
$ hipcc test_hipblas_gemm.cpp -lamdhip64 -lhipblas -o test_hipblas_gemm.exe
$ ./test_hipblas_gemm.exe
PASS: C = [[19, 22], [43, 50]]

hipsolver example

Use test_hipsolver_gesv.cpp to solv the linear system Ax=b

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <hip/hip_runtime.h>
#include <hipsolver/hipsolver.h>

#include <cmath>
#include <cstddef>
#include <cstdio>

#define CHECK(call)                                                       \
    do {                                                                  \
        const int status = static_cast<int>(call);                        \
        if (status != 0) {                                                \
            std::fprintf(stderr, "%s failed (status %d)\n", #call, status); \
            return 1;                                                     \
        }                                                                 \
    } while (false)

int main() {
    // Solve A*x = b with A = [[3, 1], [2, 4]], stored column-major.
    const float a[] = {3, 2, 1, 4};
    const float b[] = {5, 10};
    float x[2]{};
    constexpr int n = 2;
    constexpr int nrhs = 1;

    float *d_a = nullptr, *d_b = nullptr, *d_x = nullptr;
    int *d_pivots = nullptr, *d_info = nullptr;
    CHECK(hipMalloc(&d_a, sizeof(a)));
    CHECK(hipMalloc(&d_b, sizeof(b)));
    CHECK(hipMalloc(&d_x, sizeof(x)));
    CHECK(hipMalloc(&d_pivots, n * sizeof(int)));
    CHECK(hipMalloc(&d_info, sizeof(int)));
    CHECK(hipMemcpy(d_a, a, sizeof(a), hipMemcpyHostToDevice));
    CHECK(hipMemcpy(d_b, b, sizeof(b), hipMemcpyHostToDevice));

    hipsolverHandle_t handle;
    CHECK(hipsolverCreate(&handle));
    std::size_t workspace_bytes = 0;
    CHECK(hipsolverSSgesv_bufferSize(handle, n, nrhs, d_a, n, d_pivots,
                                     d_b, n, d_x, n, &workspace_bytes));
    void* workspace = nullptr;
    if (workspace_bytes != 0) {
        CHECK(hipMalloc(&workspace, workspace_bytes));
    }

    int iterations = 0;
    CHECK(hipsolverSSgesv(handle, n, nrhs, d_a, n, d_pivots, d_b, n, d_x, n,
                         workspace, workspace_bytes, &iterations, d_info));
    int info = 0;
    CHECK(hipMemcpy(&info, d_info, sizeof(info), hipMemcpyDeviceToHost));
    if (info != 0) {
        std::fprintf(stderr, "hipsolverSSgesv reported info = %d\n", info);
        return 1;
    }
    CHECK(hipMemcpy(x, d_x, sizeof(x), hipMemcpyDeviceToHost));
    CHECK(hipsolverDestroy(handle));
    CHECK(hipFree(workspace));
    CHECK(hipFree(d_info));
    CHECK(hipFree(d_pivots));
    CHECK(hipFree(d_x));
    CHECK(hipFree(d_b));
    CHECK(hipFree(d_a));

    const bool passed = std::fabs(x[0] - 1.0f) < 1e-5f
                     && std::fabs(x[1] - 2.0f) < 1e-5f;
    std::printf("%s: x = [%g, %g]\n", passed ? "PASS" : "FAIL", x[0], x[1]);
    return passed ? 0 : 1;
}

Build and run the example:

1
2
3
$ hipcc test_hipsolver_gesv.cpp -lamdhip64 -lhipsolver -o test_hipsolver_gesv.exe
$ ./test_hipsolver_gesv.exe
PASS: x = [1, 2]

This post is licensed under CC BY 4.0 by the author.