Janus Troelsen
Janus Troelsen

Reputation: 21290

How to use Clang's CUDA compiler?

I am on Ubuntu 17.10. I installed the CUDA 9.1 SDK from NVIDIA.

This is what I tried:

~/GrinGoldMiner/src/Cudacka$ clang++-5.0 -Wl,--cuda-path=/usr/local/cuda-9.1 kernel.cu
clang: error: cannot find libdevice for sm_20. Provide path to different CUDA installation via --cuda-path, or pass -nocudalib to build without linking with libdevice.
clang: error: cannot find CUDA installation.  Provide its path via --cuda-path, or pass -nocudainc to build without CUDA includes.
clang: error: cannot find CUDA installation.  Provide its path via --cuda-path, or pass -nocudainc to build without CUDA includes.

Obviously it doesn't work. It seems like the linker flags are not getting passed. How can I pass them correctly?

Upvotes: 7

Views: 7151

Answers (2)

Hopobcn
Hopobcn

Reputation: 905

It seems clang++-5.0 does not support CUDA 9.X ...

clang++ is able to compile CUDA kernels with CUDA 8.0:

$ clang++-5.0 -O0 -g --cuda-gpu-arch=sm_50 --cuda-path=/usr/local/cuda-8.0 -o t1 t1.cu -L/usr/local/cuda-8.0/lib64 -lcudart

But when using CUDA 9.X I get the same error as you:

$ clang++-5.0 --cuda-gpu-arch=sm_50 --cuda-path=/usr/local/cuda-9.0 -o t1 t1.cu -L/usr/local/cuda-9.0/lib64 -lcudart
clang: error: cannot find libdevice for sm_50. Provide path to different CUDA installation via --cuda-path, or pass -nocudalib to build without linking with libdevice.

They added support for Volta (sm_70) and CUDA 9.0 in this commit: 6d4cb40. In 2017, this was only available on master branch, and you would have confirmed it like this:

$ git clone https://github.com/llvm-mirror/clang.git 
$ cd clang/ 
$ git branch --contains 6d4cb40
* master

$ git checkout release_50
Branch release_50 set up to track remote branch release_50 from origin.
Switched to a new branch 'release_50'
$ git log | grep 6d4cb40
$ (output was empty)

Note that clang (7.0.0, released September 2018) supports CUDA 7.0 through 9.2.

Upvotes: 7

Andriy Makukha
Andriy Makukha

Reputation: 8304

I tried to build the GrinGoldMiner's Cudacka under Ubuntu 17.10, and all I had to do was:

make

This generated two commands on my machine (after some cleanup):

/usr/local/cuda-9.1/bin/nvcc -ccbin g++ -m64 -Xcompiler -fpermissive -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_70,code=compute_70 -o cudacka.o -c kernel.cu
/usr/local/cuda-9.1/bin/nvcc -ccbin g++ -m64 -Xcompiler -fpermissive -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_70,code=compute_70 -o Cudacka.exe cudacka.o

And they finished successfully generating executable Cudacka.exe.

If you are interested specifically in clang:

When I tried to replace g++ with clang++-5.0, I got this error:

nvcc fatal   : The version ('50000') of the host compiler ('clang') is not supported

If I use -std=c++11 -ccbin clang++ instead of -ccbin g++, I get this error:

kernel.cu(397): error: explicit instantiation definition directive for __global__ functions with clang host compiler is not yet supported

So, I doubt that you can use clang to compile that code for Ubuntu.

Upvotes: 0

Related Questions