Introduction
A .cu file holds source code in CUDA C++, an extension of
C++ from
NVIDIA for running code on the
company’s graphics cards and accelerators. One file holds both the code that runs on the
main processor and the code that runs on the GPU. .cuh is the usual name
for CUDA headers.
NVIDIA’s compiler, nvcc, splits a file in two. It compiles the GPU part
itself and hands the rest to the system’s C++ compiler. Clang can also compile CUDA.
Structure
A CUDA file is ordinary C++ text, with some additions:
- Function specifiers say where a function runs.
__global__marks a kernel, started from the host and run on the GPU,__device__a function called on the GPU, and__host__a function on the CPU. - A kernel is launched with the triple angle brackets
kernel<<<blocks, threads>>>(arguments), which give the grid size and the number of threads in each block. - Inside a kernel, built-in variables such as
blockIdx,threadIdx,blockDimandgridDimtell each thread which part of the work is its own. - Memory qualifiers such as
__shared__place data in memory shared by a block of threads, and functions from the CUDA runtime, such ascudaMalloc, manage memory on the GPU.
#include <cuda_runtime.h>
__global__ void scale(float *data, int n, float factor) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) {
data[i] *= factor;
}
}
int main() {
const int n = 1024;
float *data;
cudaMalloc(&data, n * sizeof(float));
scale<<<4, 256>>>(data, n, 2.0f);
cudaDeviceSynchronize();
cudaFree(data);
return 0;
}
main.Not described
Known, not yet covered
- Reference: this overview has no sub-pages and does not describe the C++ it builds on (see C++). It does not cover the memory model and the other memory spaces (global, constant, local and managed memory, unified memory), synchronisation and warp-level functions (
__syncthreads, shuffles), streams and events, the vector types (float4,dim3), the attributes and launch bounds (__launch_bounds__), cooperative groups, dynamic parallelism, or the arithmetic of the supported compute capabilities. - Reference: nvcc’s options and phases, PTX and the inline
asmthat writes it, the CUDA runtime and driver APIs beyondcudaMalloc, and the libraries (cuBLAS, Thrust and others) are not described. HIP and SYCL, which share much of the syntax, are not mentioned. - Viewer: a CUDA file is coloured with the C++ rules plus nine extra keywords (
__global__,__device__,__host__,__shared__,__constant__,__restrict__,__managed__,__noinline__,__forceinline__) and four extra types (dim3,cudaError_t,cudaStream_t,cudaEvent_t). The built-in variables (threadIdx,blockIdx,blockDim,gridDim), the vector types (float4,int2),__halfand the<<< >>>of a launch are plain, and__syncthreadsand__launch_bounds__are coloured like any function call. - Viewer: the PTX inside
asmis a string, so it is not coloured as assembly. Nothing is compiled or checked: a call to a__device__function from the wrong side, a bad launch configuration or a missing#includeis not reported. Only the first 1 MiB of a file is read and coloured.
Links
Software
- CUDA Toolkit – NVIDIA’s
nvcccompiler, libraries and tools (Windows, Linux) - Clang – C and C++ compiler that also compiles CUDA (Windows, Linux)
- Visual Studio Code – free code editor with NVIDIA’s CUDA extension (Windows, Linux)
Developers
- tree-sitter-cuda – CUDA grammar for the Tree-sitter parser used by editors
Reference
- CUDA Programming Guide – NVIDIA’s guide, with the C++ language extensions and
nvcc - Compiling CUDA with clang and LLVM – how to build CUDA without
nvcc