LLVM 编译器/安装
在 Ubuntu 上
- sudo apt-get install clang llvm
硬件/操作系统
- Azure VM | 标准 NC6_Promo (6 个 vcpu,56 GiB 内存)
- Tesla K80,具有 3.7 的计算能力。您可以通过 lshw -C display 找出您获得的卡;您可以找到您卡的计算能力 https://developer.nvidia.com/cuda-gpus。
- Linux (ubuntu 18.04)
sudo apt update sudo apt install build-essential sudo apt install cmake sudo apt install -y libelf-dev libffi-dev sudo apt install -y pkg-config
安装 CUDA 10.2,使用说明 https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1804&target_type=debnetwork
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" sudo apt-get update sudo apt-get -y install cuda
安装后,导出两个环境变量,vim ~/.bashrc 添加两行
- export PATH=$PATH:/usr/local/cuda-10.2/bin/
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.2/lib/
假设您位于您的主目录中
cd
git clone https://github.com/llvm/llvm-project.git
mkdir build cd build/ cmake -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;libcxx;libcxxabi;lld;openmp" \ -DCMAKE_BUILD_TYPE=Release \ -DLLVM_TARGETS_TO_BUILD="X86;NVPTX" \ -DCMAKE_INSTALL_PREFIX=$(pwd)/../llvm \ -DCLANG_OPENMP_NVPTX_DEFAULT_ARCH=sm_37 \ -DLIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES=35,37,50,52,60,61,70,75 \ -DCMAKE_C_COMPILER=gcc \ -DCMAKE_CXX_COMPILER=g++ \ -G "Unix Makefiles" ../llvm-project/llvm time make -j time make -j install
配置选项说明
- -DCMAKE_C_COMPILER=gcc // 用于编译 clang/llvm 的 C 编译器,GCC
- -DCMAKE_CXX_COMPILER=g++ // 用于编译 clang/llvm 的 C++ 编译器:G++
- -DLLVM_TARGETS_TO_BUILD=X86;PowerPC;NVPTX;AMDGPU // 明确指定要支持的目标设备,英特尔、英伟达、IBM 和 AMD CPU 或 GPU
- -DCLANG_OPENMP_NVPTX_DEFAULT_ARCH=sm_70 // 要支持的默认 GPU 计算能力版本,https://developer.nvidia.com/cuda-gpus 列出了此类信息。
- -DLIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES=37,60,70 // libomptarget 中要构建的所有 GPU 计算能力版本
其他可选选项
- -DGCC_INSTALL_PREFIX=${GCC_PATH}
- -DCMAKE_C_COMPILER=${GCC_PATH}/bin/gcc
- -DCMAKE_CXX_COMPILER=${GCC_PATH}/bin/g++
- -DCMAKE_Fortran_COMPILER=${GCC_PATH}/bin/gfortran
- -DCUDA_PATH= // 如果 cuda 在您的搜索路径中,此选项应自动设置
- -DCUDA_TOOLKIT_ROOT_DIR= // 如果 cuda 在您的搜索路径中,此选项应自动设置
- -DOPENMP_ENABLE_LIBOMPTARGET=ON // 此选项应默认打开
- -DLIBOMP_FORTRAN_MODULES=ON
- -DBUILD_SHARED_LIBS=OFF // 关闭共享库
更多示例
- cmake -G Ninja -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;libcxx;libcxxabi;lld;openmp" -DCMAKE_INSTALL_PREFIX=/Users/abc/llvm-research/inst-10.0.1 -DCMAKE_BUILD_TYPE=Debug ../llvm/
- ninja -j8
- ninja install
将路径添加到已安装的 clang,vim ~/.bashrc
export PATH=~/llvm/bin:$PATH export LD_LIBRARY_PATH=~/llvm/lib:$LD_LIBRARY_PATH cd build-openmp cd build-openmp/ cmake -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;libcxx;libcxxabi;lld;openmp" \ -DCMAKE_BUILD_TYPE=Release \ -DLLVM_TARGETS_TO_BUILD="X86;NVPTX" \ -DCMAKE_INSTALL_PREFIX=$(pwd)/../llvm \ -DCLANG_OPENMP_NVPTX_DEFAULT_ARCH=sm_37 \ -DLIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES=35,37,50,52,60,61,70,75 \ -DCMAKE_C_COMPILER=clang \ -DCMAKE_CXX_COMPILER=clang++ \ -G "Unix Makefiles" ../llvm-project/llvm make -j make -j install
可选选项,明确打开 bitcode 库,以及编译器/链接器来构建它
- -DLIBOMPTARGET_NVPTX_ENABLE_BCLIB=true
- -DLIBOMPTARGET_NVPTX_CUDA_COMPILER=${PREFIX}/bin/clang
- -DLIBOMPTARGET_NVPTX_BC_LINKER=${PREFIX}/bin/llvm-link
硬件/操作系统
- Azure VM | 标准 NC6_Promo (6 个 vcpu,56 GiB 内存)
- Tesla K80,具有 3.7 的计算能力。您可以通过 lshw -C display 找出您获得的卡;您可以在这里找到您卡的计算能力。
- Linux (ubuntu 18.04)
sudo apt update sudo apt install build-essential sudo apt install cmake sudo apt install -y libelf-dev libffi-dev sudo apt install -y pkg-config
安装 CUDA 10.2,使用说明 https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1804&target_type=debnetwork
假设您当前的路径是
- /home/ubuntu/omp5-gpu-llvm
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" sudo apt-get update sudo apt-get -y install cuda
安装后,导出两个环境变量,vim ~/.bashrc 添加两行
- export PATH=$PATH:/usr/local/cuda-10.2/bin/
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.2/lib/
三个步骤来下载、解压并将其放入正确的位置。
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/llvm-10.0.0.src.tar.xz wget https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/clang-10.0.0.src.tar.xz wget https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/openmp-10.0.0.src.tar.xz wget https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/compiler-rt-10.0.0.src.tar.xz tar xf llvm-10.0.0.src.tar.xz tar xf clang-10.0.0.src.tar.xz tar xf openmp-10.0.0.src.tar.xz tar xf compiler-rt-10.0.0.src.tar.xz mv clang-10.0.0.src llvm-10.0.0.src/tools/clang mv openmp-10.0.0.src llvm-10.0.0.src/projects/openmp mv compiler-rt-10.0.0.src llvm-10.0.0.src/projects/compiler-rt
最后,目录布局应该如下所示
- llvm-10.0.0.src
- tools/clang
- projects/openmp
- projects/compiler-rt
您需要知道 GPU 的计算能力版本。 https://developer.nvidia.com/cuda-gpus 列出了此类信息。例如,一些典型的 GPU 及其 CC 版本是
- Tesla K80 3.7,sm_37
- Tesla P100 6.0,sm_37
- Tesla V100 7.0,sm_37
mkdir build cd build # this step is to generate a make file using cmake. picking gcc/g++ as the compiler cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$(pwd)/../install \ -DCLANG_OPENMP_NVPTX_DEFAULT_ARCH=sm_37 \ -DCMAKE_C_COMPILER=gcc \ -DCMAKE_CXX_COMPILER=g++ \ -DLIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES=37,60,70 ../llvm-10.0.0.src # the screen output of the step above should show the following info: # -- Found LIBOMPTARGET_DEP_CUDA_DRIVER: /usr/lib/x86_64-linux-gnu/libcuda.so # -- LIBOMPTARGET: Building offloading runtime library libomptarget. # -- LIBOMPTARGET: Building CUDA offloading plugin. # -- LIBOMPTARGET: Building x86_64 offloading plugin. make -j6 make install -j6
安装后,您应该再次扩展您的 PATH 和 LD_LIBRARY_PATH
- export PATH=$PATH:/home/ubuntu/omp5-gpu-llvm/install/bin
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/ubuntu/omp5-gpu-llvm/install/lib
您应该使用新安装的 clang 来重建 OpenMP 运行时库
cd /home/ubuntu/omp5-gpu-llvm mkdir build-openmp cd build-openmp cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$(pwd)/../install \ -DCMAKE_C_COMPILER=$(pwd)/../install/bin/clang \ -DCMAKE_CXX_COMPILER=$(pwd)/../install/bin/clang++ \ -DLIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES=37,60,70 \ ../llvm-10.0.0.src/projects/openmp make -j6 make install -j6
将以下代码保存到名为 ongpu.c 的文件中
#include <stdio.h> #include <omp.h> int main() { int runningOnGPU = 0; /* Test if GPU is available using OpenMP4.5 */ #pragma omp target map(from:runningOnGPU) { if (omp_is_initial_device() == 0) runningOnGPU = 1; } /* If still running on CPU, GPU must not be available */ if (runningOnGPU) printf("### Able to use the GPU! ### \n"); else printf("### Unable to use the GPU, using CPU! ###\n"); return 0; }
编译并运行它
clang -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda ongpu.c ./a.out ### Able to use the GPU! ###
./a.out: 错误 while loading shared libraries: libomp.so: 无法打开共享对象文件: 没有此文件或目录 解决方法
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/pathTo/installed-llvm/lib
- OpenMP 5 GPU offloading 指令 [Fabio]: https://github.com/devitocodes/devito/wiki/OpenMP-5-GPU-offloading-instructions--%5BFabio%5D
- Clang_with_OpenMP_Offloading_to_NVIDIA_GPUs: https://hpc-wiki.info/hpc/Building_LLVM/Clang_with_OpenMP_Offloading_to_NVIDIA_GPUs
- OpenMP 5 GPU offloading 指令与 Clang 11 [Italo] https://github.com/devitocodes/devito/wiki/OpenMP-5-GPU-offloading-instructions-with-Clang-11-%5BItalo%5D
- 一个展示 LLVM 安装的 docker 文件: https://github.com/freeCompilerCamp/play-with-compiler/blob/master/dockerfiles/dind/Dockerfile.llvm10