Skip to content

Instantly share code, notes, and snippets.

@kristerw
Last active July 18, 2024 05:12
Show Gist options
  • Save kristerw/4e9a735f2d755ffa73f9bf27edbf3c29 to your computer and use it in GitHub Desktop.
Save kristerw/4e9a735f2d755ffa73f9bf27edbf3c29 to your computer and use it in GitHub Desktop.
Build GCC with support for offloading to NVIDIA GPUs
#!/bin/sh
#
# Build GCC with support for offloading to NVIDIA GPUs.
#
work_dir=$HOME/offload/wrk
install_dir=$HOME/offload/install
# Location of the installed CUDA toolkit
cuda=/usr/local/cuda
# Build assembler and linking tools
mkdir -p $work_dir
cd $work_dir
git clone https://github.com/MentorEmbedded/nvptx-tools
cd nvptx-tools
./configure \
--with-cuda-driver-include=$cuda/include \
--with-cuda-driver-lib=$cuda/lib64 \
--prefix=$install_dir
make || exit 1
make install || exit 1
cd ..
# Set up the GCC source tree
git clone git://sourceware.org/git/newlib-cygwin.git nvptx-newlib
git clone --branch releases/gcc-11 git://gcc.gnu.org/git/gcc.git gcc
cd gcc
contrib/download_prerequisites
ln -s ../nvptx-newlib/newlib newlib
cd ..
target=$(gcc/config.guess)
# Build nvptx GCC
mkdir build-nvptx-gcc
cd build-nvptx-gcc
../gcc/configure \
--target=nvptx-none --with-build-time-tools=$install_dir/nvptx-none/bin \
--enable-as-accelerator-for=$target \
--disable-sjlj-exceptions \
--enable-newlib-io-long-long \
--enable-languages="c,c++,fortran,lto" \
--prefix=$install_dir
make -j`nproc` || exit 1
make install || exit 1
cd ..
# Build host GCC
mkdir build-host-gcc
cd build-host-gcc
../gcc/configure \
--enable-offload-targets=nvptx-none \
--with-cuda-driver-include=$cuda/include \
--with-cuda-driver-lib=$cuda/lib64 \
--disable-bootstrap \
--disable-multilib \
--enable-languages="c,c++,fortran,lto" \
--prefix=$install_dir
make -j`nproc` || exit 1
make install || exit 1
cd ..
@kristerw
Copy link
Author

I have not tried offloading since I wrote the blog post 5 years ago, so my knowledge is a bit rusty... But I think you need to add -foffload=-lm when linking nemolite2d.exe. I also thought you would need -foffload=nvptx-none when linking, but the error message mentions nvptx, so maybe that isn't necessary (but it would not hurt... :) )

@HyunChaeJung
Copy link

Thanks to your advice, the problem has been resolved.
Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment