引言
AMD开源了runtime library, 如果你有一张AMD的卡, 完全可以自己尝试编译一下他们的仓库,debug一下
步骤
克隆仓库: https://github.com/ROCm/rocm-systems.git和https://github.com/ROCm/llvm-project.git, 我自己编译的版本是rocm-7.2, 注意分支的一致性
编译LLVM
下面的路径可以按需替换
1 2 3 4 5 6 7 8 9 10
| cmake -S llvm -B build -DCMAKE_BUILD_TYPE=Release -G Ninja \ -DLLVM_ENABLE_PROJECTS="clang;lld" \ -DLLVM_EXTERNAL_PROJECTS="device-libs;comgr" \ -DLLVM_EXTERNAL_DEVICE_LIBS_SOURCE_DIR=/home/jing.zhou/rocm-llvm/amd/device-libs \ -DLLVM_EXTERNAL_COMGR_SOURCE_DIR=/home/jing.zhou/rocm-llvm/amd/comgr \ -DCMAKE_INSTALL_PREFIX=/home/jing.zhou/rocm-systems/projects/install \ -DLLVM_TARGETS_TO_BUILD="X86;AMDGPU" \ -DCMAKE_LINKER=/home/jing.zhou/tools/bin/mold
|
编译clr
cd到rocm-systems目录,
1 2 3 4 5 6 7 8 9 10 11
| export CLR_DIR="$(readlink -f projects/clr)" export HIP_DIR="$(readlink -f projects/hip)"
export PATH=/home/jing.zhou/rocm-systems/projects/install/bin:$PATH
cd "$CLR_DIR" mkdir -p build; cd build cmake -DHIP_COMMON_DIR=$HIP_DIR -DCMAKE_PREFIX_PATH="/home/jing.zhou/rocm-systems/projects" -DROCM_INSTALL_PATH="/home/jing.zhou/rocm-systems/projects/install" -DCMAKE_INSTALL_PREFIX=/home/jing.zhou/rocm-systems/projects/install -DCLR_BUILD_HIP=ON -DCLR_BUILD_OCL=OFF -DHIP_PLATFORM=amd .. make -j$(nproc) make install
|
demo
1 2 3 4
| export LD_LIBRARY_PATH=/home/jing.zhou/rocm-systems/projects/install/lib:$LD_LIBRARY_PATH export HIP_CLANG_PATH="/home/jing.zhou/rocm-systems/projects/install/bin" export HIP_PATH="/home/jing.zhou/rocm-systems/projects/install" export HIP_PLATFORM=amd
|
写一份简单的hello-world代码
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 <iostream>
#include <hip/hip_runtime.h>
__global__ void gpu_hello_world() { int gtid = blockIdx.x * blockDim.x + threadIdx.x;
printf("Hello World from GPU thread %d!\n", gtid); }
int main() { std::cout << "Hello World from CPU!" << std::endl;
dim3 grid(1); dim3 block(4);
hipLaunchKernelGGL(gpu_hello_world, grid, block, 0, 0);
hipDeviceSynchronize();
return 0; }
|
输出
1 2 3 4 5 6
| Hello World from CPU! Hello World from GPU thread 0! Hello World from GPU thread 1! Hello World from GPU thread 2! Hello World from GPU thread 3!
|