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.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