CUDA Compiler (nvcc)

Need to first understand how a normal compiler works…

NVIDIA cares a lot about CUDA, and so CUDA uses Clang and uses LLVM for graphics and GPU.

Resources: https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html

The CUDA platform ships with the NVIDIA CUDA Compiler nvcc, which can compile CUDA accelerated applications, both the host, and the device code they contain.

nvcc will be very familiar to experienced gcc users. Compiling, for example, a some-CUDA.cu file, is simply:

nvcc -std=c++11 arch=sm_70 -o out some-CUDA.cu -run
  • nvcc is the command line command for using the nvcc compiler.
  • some-CUDA.cu is passed as the file to compile.
  • The o flag is used to specify the output file for the compiled program.
  • -std specifies the C++ version
  • The arch flag indicates for which architecture the files must be compiled. But for those interested in a deeper dive, please refer to the docs about the arch flag, virtual architecture features and GPU features.
  • As a matter of convenience, providing the run flag will execute the successfully compiled binary.

The .cu file extension just specifies that it is a CUDA file.

If you want to include CUDA code in regular .cpp file, just write a header file for the .cu file, and then include it.