CUDA Cheatsheet
Basics
Use this CUDA reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
What is CUDA
CUDA (Compute Unified Device Architecture) is NVIDIA's parallel computing platform and API. Code runs on the host (CPU) and dispatches work to the device (GPU). A single binary contains both host and device code; nvcc splits and compiles them.
Use this CUDA cheatsheet when moving from C++ into GPU programming. Hack University's C++ online compiler is useful for quick host-side syntax checks in an online code editor; CUDA kernels still require NVIDIA hardware, drivers, and nvcc locally or in a GPU environment.
// Minimal CUDA program #include <cuda_runtime.h> #include <cstdio> __global__ void hello() { printf("Hello from thread %d\n", threadIdx.x); } int main() { hello<<<1, 4>>>(); // 1 block, 4 threads cudaDeviceSynchronize(); // wait for GPU to finish return 0; }
Function Qualifiers
| Qualifier | Runs on | Called from | Notes |
|---|---|---|---|
__global__ | device | host (or device, CC ≥ 3.5) | Kernel entry point; returns void |
__device__ | device | device only | Inline-able helper |
__host__ | host | host only | Default; explicit when combined |
__host__ __device__ | both | both | Compiled twice; useful for math helpers |
__noinline__ | device | device | Prevent inlining |
__forceinline__ | device | device | Force inlining |
__host__ __device__ float clamp(float v, float lo, float hi) { return fminf(fmaxf(v, lo), hi); // works on CPU and GPU }
Variable Qualifiers
| Qualifier | Location | Lifetime | Scope |
|---|---|---|---|
__device__ | global memory | application | all threads |
__constant__ | constant cache | application | all threads (read-only) |
__shared__ | shared memory | kernel | block |
__managed__ | unified memory | application | host + all threads |
__restrict__ | (pointer hint) | — | compiler alias hint |
__constant__ float kPi = 3.14159265f; // lives in constant cache __device__ int gCounter = 0; // global device variable __global__ void demo() { __shared__ float tile[256]; // block-local fast memory tile[threadIdx.x] = kPi * threadIdx.x; }
Execution Configuration Syntax
kernel<<<gridDim, blockDim, sharedBytes, stream>>>(args...);
| Argument | Type | Default | Meaning |
|---|---|---|---|
gridDim | dim3 or int | — | Number of blocks in the grid |
blockDim | dim3 or int | — | Number of threads per block |
sharedBytes | size_t | 0 | Extra dynamic shared memory bytes |
stream | cudaStream_t | 0 (default) | Stream to enqueue on |
dim3 grid(128, 1, 1); dim3 block(256, 1, 1); myKernel<<<grid, block, 0, myStream>>>(d_data, n);
Built-in Vector Types
| Type family | Variants | Example |
|---|---|---|
charN | 1,2,3,4 | char4 c = make_char4(1,2,3,4); |
intN | 1,2,3,4 | int2 idx = make_int2(x, y); |
floatN | 1,2,3,4 | float4 v = make_float4(1,0,0,0); |
doubleN | 1,2 | double2 z = make_double2(r, i); |
dim3 | — | dim3 g(32, 32, 1); |
Member access via .x, .y, .z, .w.
float4 a = make_float4(1.0f, 2.0f, 3.0f, 4.0f); float dot = a.x*a.x + a.y*a.y + a.z*a.z + a.w*a.w;
Device Properties Query
#include <cuda_runtime.h> int devCount; cudaGetDeviceCount(&devCount); cudaDeviceProp prop; cudaGetDeviceProperties(&prop, 0); // device 0 printf("Name: %s\n", prop.name); printf("SM count: %d\n", prop.multiProcessorCount); printf("Max threads/block: %d\n", prop.maxThreadsPerBlock); printf("Shared mem/block: %zu bytes\n", prop.sharedMemPerBlock); printf("Warp size: %d\n", prop.warpSize); // always 32 printf("Global mem: %zu MB\n", prop.totalGlobalMem >> 20); printf("Compute cap: %d.%d\n", prop.major, prop.minor);
Setting / Querying the Active Device
cudaSetDevice(0); // select GPU 0 for this host thread int dev; cudaGetDevice(&dev); // query current device // Peer access (multi-GPU) int canAccess; cudaDeviceCanAccessPeer(&canAccess, 0, 1); // can GPU 0 access GPU 1? if (canAccess) cudaDeviceEnablePeerAccess(1, 0);
Compute Capability Quick Reference
| CC | Key hardware feature |
|---|---|
| 3.5 | Dynamic parallelism (kernel launches from device) |
| 5.0 | Maxwell; unified memory improvements |
| 6.0 | Pascal; NVLink, 16-bit FP |
| 7.0 | Volta; Tensor Cores v1, independent thread scheduling |
| 7.5 | Turing; Tensor Cores v2, RT Cores |
| 8.0 | Ampere; Tensor Cores v3, async copy, __grid_constant__ |
| 8.9 | Ada Lovelace; FP8 Tensor Cores |
| 9.0 | Hopper; Thread Block Clusters, TMA |
| 10.0 | Blackwell data center (sm_100, B200/GB200); 5th-gen Tensor Cores, FP4 — CUDA ≥ 12.8 |
| 12.0 | Blackwell consumer/workstation (sm_120, RTX 50-series / RTX PRO) — CUDA ≥ 12.8 |
Gotcha:
__global__calling__global__(dynamic parallelism) requires CC ≥ 3.5 and linking with-lcudadevrt.
Common CUDA Headers
| Header | Contents |
|---|---|
<cuda_runtime.h> | Runtime API (cudaMalloc, cudaMemcpy, …) |
<cuda_runtime_api.h> | Subset of runtime API (no device code) |
<device_launch_parameters.h> | threadIdx, blockIdx, blockDim, gridDim |
<cooperative_groups.h> | Cooperative groups API |
<cuda_fp16.h> | Half-precision types (__half) |
<cuda_bf16.h> | __nv_bfloat16 |
<mma.h> | Warp-level matrix multiply (WMMA) |
<cub/cub.cuh> | CUB device/block/warp primitives |