Type something to search...
CLion with multiple CUDA SDKs

CLion with multiple CUDA SDKs

There are several ways to set up multiple toolchains with different CUDA versions when using CLion. The easiest way I found was to use micromamba, a lightweight (mini|ana)conda variant, and then use the environment script to configure the toolchains in CLion. The idea is to be able to quickly switch from one sdk to the other, like the following

CLion Toolchains

Micromamba (conda) environments

You can install micromamba by following the instructions here

Terminal window
1
ice@kube:~$ "${SHELL}" <(curl -L micro.mamba.pm/install.sh)
2
% Total % Received % Xferd Average Speed Time Time Time Current
3
Dload Upload Total Spent Left Speed
4
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
5
100 3069 100 3069 0 0 7525 0 --:--:-- --:--:-- --:--:-- 149k
6
Micromamba binary folder? [~/.local/bin] y
7
Init shell (bash)? [Y/n] Y
8
Configure conda-forge? [Y/n] Y
9
Prefix location? [~/micromamba]
10
Modifying RC file "/home/ice/.bashrc"
11
Generating config for root prefix "/home/ice/micromamba"
12
Setting mamba executable to: "/home/ice/y/micromamba"
13
Adding (or replacing) the following in your "/home/ice/.bashrc" file
14
15
# >>> mamba initialize >>>
16
# !! Contents within this block are managed by 'mamba init' !!
17
export MAMBA_EXE='/home/ice/y/micromamba';
18
export MAMBA_ROOT_PREFIX='/home/ice/micromamba';
19
__mamba_setup="$("$MAMBA_EXE" shell hook --shell bash --root-prefix "$MAMBA_ROOT_PREFIX" 2> /dev/null)"
20
if [ $? -eq 0 ]; then
21
eval "$__mamba_setup"
22
else
23
alias micromamba="$MAMBA_EXE" # Fallback on help from mamba activate
24
fi
25
unset __mamba_setup
26
# <<< mamba initialize <<<
27
28
Please restart your shell to activate micromamba or run the following:\n
29
source ~/.bashrc (or ~/.zshrc, ~/.xonshrc, ~/.config/fish/config.fish, ...)

Once installed, please take note of the code between

Terminal window
1
>>> mamba initialize >>>
2
....
3
<<< mamba initialize <<<

It will be needed later when we create the environment script for the toolchains. Once you installed micromamba, you can start to install the CUDA sdks, like for example 11.8.0

Terminal window
1
# create and activate the env
2
micromamba create --name cuda-11.8.0
3
micromamba activate cuda-11.8.0
4
5
# install the needed packages for cuda development
6
micromamba install -c "nvidia/label/cuda-11.8.0" cuda-libraries-dev cuda-nvcc cuda-cudart-static
7
8
# here I install gcc and g++ using a compatible version
9
micromamba install gcc=11.4.0 gxx=11.4.0

For 12.3.0 and 12.3.1

Terminal window
1
# create and activate the env
2
micromamba create --name cuda-12.3.0
3
micromamba activate cuda-12.3.0
4
5
# install the needed packages for cuda development
6
micromamba install -c "nvidia/label/cuda-12.3.0" cuda-libraries-dev cuda-nvcc cuda-cudart-static
7
8
# install gcc and g++, packages coming from conda-forge
9
micromamba install gcc=12.3.0 gxx=12.3.0
10
11
# create and activate the env
12
micromamba create --name cuda-12.3.1
13
micromamba activate cuda-12.3.1
14
15
# install the needed packages for cuda development
16
micromamba install -c "nvidia/label/cuda-12.3.1" cuda-libraries-dev cuda-nvcc cuda-cudart-static
17
18
# install gcc and g++, packages coming from conda-forge
19
micromamba install gcc=12.3.0 gxx=12.3.0

Now you have the environments ready, with all the files installed in ~/micromamba/envs/… and not polluting the systems. So it is easy to get rid of them or update them.

CLion Toolchains

Now we need to create a shell script for each of the conda environments we created, to be used to configure the toolchain in CLion. One script could be called load_cuda_11.8.0.sh with the following content

Terminal window
1
# >>> mamba initialize >>>
2
# !! Contents within this block are managed by 'mamba init' !!
3
export MAMBA_EXE='/home/ice/y/micromamba';
4
export MAMBA_ROOT_PREFIX='/home/ice/micromamba';
5
__mamba_setup="$("$MAMBA_EXE" shell hook --shell bash --root-prefix "$MAMBA_ROOT_PREFIX" 2> /dev/null)"
6
if [ $? -eq 0 ]; then
7
eval "$__mamba_setup"
8
else
9
alias micromamba="$MAMBA_EXE" # Fallback on help from mamba activate
10
fi
11
unset __mamba_setup
12
# <<< mamba initialize <<<
13
14
micromamba activate cuda-11.8.0

Now we can add a toolchain in CLion, by using the above script as an environment file. Here I show my toolchains

CLion Toolchains

You add a toolchain for every environment you want to use. Once you defined the toolchains, you need to create a Debug|Release configuration based on each toolchain, like here

Build Configuration

Now when you set up your project, three configurations will be used, as I can show with these three screenshots

Configuration 1

Configuration 2

Configuration 3

Troubleshooting

The amount of things that can go wrong is huge :-) For example, for the cuda sdk 11.8.0, I found that the libcudadevrt.a from the conda channel was not the correct one (at least for the version of the Nvidia drivers I had) so I had to take that library from the Nvidia sdk distribution and copy to my environment, like this

Terminal window
1
cp nvidia-cuda-11.8/libcudadevrt.a /home/ice/micromamba/envs/cuda-11.8.0/lib

The nice thing is that I am not messing with my system libraries, just with a local conda environment.

Well, I hope it helps.

Related Posts

Dali operator for multi-page TIFF's

Dali operator for multi-page TIFF's

This article explains how I wrote a simple C++ Dali operator to load a multichannel TIFF...

Read More
Homography for tensorflow

Homography for tensorflow

An article about how to implement an homography function in Tensorflow 1.x...

Read More