Update GPU programming authored by Boman Romain's avatar Boman Romain
GPU programming can be done on Lucia with the CECI account opened during the [INFO0939 course](https://people.montefiore.uliege.be/geuzaine/INFO0939/notes/gpu/).
## GPU programming using your own computer?
If you own a laptop with a nvidia graphics card, you can use OpenMP to offload some parts of your program to your local GPU. This would allow you to test and debug your OpenMP instructions locally before running them on Lucia.
GPU programming with OpenMP is only possible on computers running Linux or the Windows Subsystem for Linux (WSL) equipped with a nvidia graphics card.
### Windows: WSL
First, install WSL following [these instructions](https://learn.microsoft.com/en-us/windows/wsl/install), then follow the Linux instructions in a WSL terminal.
### Linux
#### install CUDA
The following commands are copied from the [CUDA Installation Guide for Linux](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/)
```
# Install the newcuda-keyring package
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
# install cuda
sudo apt-get update
sudo apt-get install cuda
```
Then, add these lines to your `~/.bashrc`:
```
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64\
${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
```
and restart the terminal.
Install these additional packages:
```
sudo apt-get install g++ freeglut3-dev build-essential libx11-dev \
libxmu-dev libxi-dev libglu1-mesa-dev libfreeimage-dev libglfw3-dev
sudo apt install libffi-dev zlib1g-dev cmake
```
Clone the clang repository and build it with gcc:
```
git clone --depth=1 https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build
cd build
cmake ../llvm/ -DCMAKE_BUILD_TYPE=Release \
-DLLVM_TARGETS_TO_BUILD="X86;NVPTX" \
-DLLVM_ENABLE_PROJECTS="clang;openmp" \
2>&1 | tee cmake-clang-stage1.log
make -j 8
```
Check that your nvidia card is detected:
```
./bin/llvm-omp-device-info
[...]
Device (4):
CUDA Driver Version 12030
CUDA OpenMP Device Number 0
Device Name NVIDIA GeForce GTX 1660 Ti
[...]
Compute Capabilities sm_75
```
Rebuild clang with clang:
```
cd ..
mkdir build2
cd build2
CC=../build/bin/clang CXX=../build/bin/clang++ \
cmake ../llvm/ -DCMAKE_BUILD_TYPE=Release \
-DLLVM_TARGETS_TO_BUILD="X86;NVPTX" \
-DLLVM_ENABLE_PROJECTS="clang;openmp" \
-DCMAKE_INSTALL_PREFIX=$HOME/.local/clang-gpu \
2>&1 | tee cmake-clang-stage2.log
make -j 8 install
```
In order to use the compiler, you must add these lines in your `~/.bashrc`:
```
export PATH=~/.local/clang-gpu/bin:$PATH
export LD_LIBRARY_PATH=~/.local/clang-gpu/lib:$LD_LIBRARY_PATH
export CC=clang
export CXX=clang++
```