View previous topic :: View next topic |
Author |
Message |
Benni12345678 n00b
Joined: 08 Dec 2024 Posts: 2
|
Posted: Sun Dec 08, 2024 5:25 pm Post subject: [HowTo] GPU offload with GCC and clang in gentoo. |
|
|
Hi@all, this is a short tutorial if somebody is interested in offloading to the gpu with the gcc or clang compiler.
Currently, for gcc, there is only offloading support for Nvidia gpu's in gentoo.
Clang currently supports more architectures.
offloading with gcc to Nvidia gpus
Offloading support for AMD is in the making for gcc, as far as I know: https://bugs.gentoo.org/945496
Even support for nvidia offloading was buggy until recently. After these bugs were removed, the process is now to
emerge a recent gcc https://wiki.gentoo.org/wiki/Upgrading_GCC
Then
Code: | emerge -u gcc-config | to get the latest patch for gcc-config as well,
Then, get the most recent, i.e. the 9999 version of crossdev.
https://wiki.gentoo.org/wiki/Crossdev
In order to do that, put the line
Code: | sys-devel/crossdev ** |
into Code: | /etc/portage/package.accept_keywords/packagelist |
in order to unmask the most recent version and then emerge it.
Then create a repository for crossdev.
Code: |
emerge -vn eselect-repository && eselect repository create crossdev |
and build the offload gcc for nvidia with
Code: | crossdev -t nvptx-none |
After these steps, typical examples, e.g. from
https://enccs.github.io/openmp-gpu/target/
will offload to gpu if compiled with the following flags
Code: | gcc -O3 -fopenmp -foffload=nvptx-none -fcf-protection=none -fno-stack-protector -no-pie ./main.cpp -lm -lstdc++ |
where main.cpp is just the program code file, -lm is the math library of c and -lstdc is the standard template library of c++.
Currently, it seems one can also use
Code: | -fcf-protection=check |
Thanks to Sam James from gentoo to make this possible.
building gcc with offloading support for Nvidia gpus from source
If somebody wants to build gcc entirely with make on your own, here is a tutorial in german language:
https://www.heise.de/hintergrund/Accelerator-Offloading-mit-GCC-3317330.html
For gcc, this makes actually not much sense in gentoo for nvidia gpu's.
offloading with clang to Nvidia and AMD gpus
For clang, one must unmask clang 20.0.0.9999, i.e. put this
Code: | dev-ml/llvm-ocaml **
dev-python/lit **
dev-debug/lldb **
dev-python/clang-python **
sys-devel/clang **
sys-devel/clang-common **
sys-devel/clang-runtime **
sys-devel/lld **
sys-devel/llvm **
sys-devel/llvm-common **
sys-libs/compiler-rt **
sys-libs/compiler-rt-sanitizers **
sys-libs/libcxx **
sys-libs/libcxxabi **
sys-libs/libcxxrt **
sys-libs/libomp **
sys-libs/llvm-libunwind **
dev-libs/libclc **
dev-libs/mlir **
sys-devel/llvmgold **
sys-devel/clang-toolchain-symlinks **
sys-devel/lld-toolchain-symlinks **
sys-devel/llvm-toolchain-symlinks **
sys-libs/llvm-offload ** |
into
Code: | /etc/portage/package.accept_keywords/packagelist |
Then, for nvidia-gpu's one must set NVPTX as an LLVM_TARGETS variable in Code: | /etc/portage/make.conf | , e.g if the host is an X86 or amd processor and the GPU is from Nvidia:
Code: | LLVM_TARGETS="X86 NVPTX" |
For AMD gpu's, one would have
Code: | LLVM_TARGETS="X86 AMDGPU" |
Finally, one can emerge clang and the new llvm-offload ebuild
Code: | emerge -u clang llvm-offload |
After emerging clang to a new version one needs to update the path in the console by typing
The typical open-mp examples, e.g. from https://enccs.github.io/openmp-gpu/target/
compile with
Code: | clang++ -O3 -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda ./main.cpp -lm -lstdc++
|
note that one has Code: | -fopenmp-targets=nvptx64-nvidia-cuda | for clang, instead of Code: | -foffload=nvptx-none
| for gcc.
Clang also supports various AMD targets that way.
E.g. for amd Code: | -fopenmp-targets=amdgcn-amd-amdhsa | ,
see https://openmp.llvm.org/CommandLineArgumentReference.html for more information.
Building the clang compiler with offloading support from source
Clang has an optimization framework like polly, which is currently not in gentoo. For clang, it makes more sense than for gcc to build it on your own.
At first, proceed like in the official manual https://clang.llvm.org/get_started.html
download a build:
Code: | git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build
cd build |
Theoretically, the following cmake parameters in the self created llvm-projects/build directory would build a configuration it with polly, openmp and offload:
Code: | cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="bolt;clang;clang-tools-extra;compiler-rt;libclc;lld;lldb;openmp;polly;pstl" -DLLVM_TARGETS_TO_BUILD="X86;NVPTX" -DLLVM_ENABLE_RUNTIMES="libc;libunwind;libcxxabi;libcxx;offload" - ../llvm |
gentoo, however, sets a specific path for the ffi.h header, so one has to include the option Code: | DFFI_INCLUDE_DIR=/usr/lib64/libffi/include |
Code: | cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="bolt;clang;clang-tools-extra;compiler-rt;libclc;lld;lldb;openmp;polly;pstl" -DLLVM_TARGETS_TO_BUILD="X86;NVPTX" -DLLVM_ENABLE_RUNTIMES="libc;libunwind;libcxxabi;libcxx;offload" -
DFFI_INCLUDE_DIR=/usr/lib64/libffi/include ../llvm |
One may then type to build and to install it.
If the build process stopps because 128 bit precision is not available on the target device, one may also have to set:
Code: | -DLIBOMP_USE_QUAD_PRECISION=OFF | in the cmake command, i.e.
Code: | cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="bolt;clang;clang-tools-extra;compiler-rt;libclc;lld;lldb;openmp;polly;pstl" -DLLVM_TARGETS_TO_BUILD="X86;NVPTX" -DLLVM_ENABLE_RUNTIMES="libc;libunwind;libcxxabi;libcxx;offload" -
DFFI_INCLUDE_DIR=/usr/lib64/libffi/include -DLIBOMP_USE_QUAD_PRECISION=OFF ../llvm |
If there are other problems, one may look at clang's cmakelists.txt https://github.com/llvm/llvm-project/blob/f0297ae552e1e5aacafc1ed43968041994dc8a6e/openmp/runtime/cmake/config-ix.cmake#L241
and adjust some variables of the build variables manually.
Additionally, clang apparently wants to link to a file called libomptarget.so.20.0git.
Currently, the llvm-offload ebuild has the following hack for this:
Code: | cmake_src_configure
if [[ -z ${gpus} ]]; then
# clang requires libomptarget.devicertl.a, but it can be empty
> "${BUILD_DIR}"/libomptarget.devicertl.a || die
fi
|
If one wants to build it from source on your own, then one has to create this empty file apparently by oneself...
As one can see, it is more easy to build with the recent ebuilds.
Clang's offloading support was buggy until recently.
Thanks to Michał Górny to make it possible that this works.
As you can see, Clang's documentation on its build system is also not very up to date and the build system itself is internally a bit complicated, so fixing clangs build system and providing these ebuilds was probably difficult work.
I hope this helps others who want to use the gpu.
Best regards,
Benni
Last edited by Benni12345678 on Mon Dec 09, 2024 8:45 am; edited 5 times in total |
|
Back to top |
|
|
Zucca Moderator
Joined: 14 Jun 2007 Posts: 3883 Location: Rasi, Finland
|
Posted: Sun Dec 08, 2024 9:07 pm Post subject: |
|
|
Moved from Portage & Programming to Documentation, Tips & Tricks.
Thanks. If you have energy and time you could contribute this information in to the Gentoo wiki.
Although a lot of packages still need to be in 9999 versions, so maybe it's better to wait some time before wiki... _________________ ..: Zucca :..
My gentoo installs: | init=/sbin/openrc-init
-systemd -logind -elogind seatd |
Quote: | I am NaN! I am a man! |
|
|
Back to top |
|
|
Benni12345678 n00b
Joined: 08 Dec 2024 Posts: 2
|
Posted: Mon Dec 09, 2024 5:56 am Post subject: |
|
|
I have updated the post a bit with some information for people who want to build gcc and clang from source on their own (e.g. when they want to include polly support in clang, which is not available currently in gentoo)...
I guess for a wiki it is a bit too early. Perhaps when the packages stabilize. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|