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

QualifierRuns onCalled fromNotes
__global__devicehost (or device, CC ≥ 3.5)Kernel entry point; returns void
__device__devicedevice onlyInline-able helper
__host__hosthost onlyDefault; explicit when combined
__host__ __device__bothbothCompiled twice; useful for math helpers
__noinline__devicedevicePrevent inlining
__forceinline__devicedeviceForce inlining
__host__ __device__ float clamp(float v, float lo, float hi) {
    return fminf(fmaxf(v, lo), hi);   // works on CPU and GPU
}

Variable Qualifiers

QualifierLocationLifetimeScope
__device__global memoryapplicationall threads
__constant__constant cacheapplicationall threads (read-only)
__shared__shared memorykernelblock
__managed__unified memoryapplicationhost + 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...);
ArgumentTypeDefaultMeaning
gridDimdim3 or intNumber of blocks in the grid
blockDimdim3 or intNumber of threads per block
sharedBytessize_t0Extra dynamic shared memory bytes
streamcudaStream_t0 (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 familyVariantsExample
charN1,2,3,4char4 c = make_char4(1,2,3,4);
intN1,2,3,4int2 idx = make_int2(x, y);
floatN1,2,3,4float4 v = make_float4(1,0,0,0);
doubleN1,2double2 z = make_double2(r, i);
dim3dim3 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

CCKey hardware feature
3.5Dynamic parallelism (kernel launches from device)
5.0Maxwell; unified memory improvements
6.0Pascal; NVLink, 16-bit FP
7.0Volta; Tensor Cores v1, independent thread scheduling
7.5Turing; Tensor Cores v2, RT Cores
8.0Ampere; Tensor Cores v3, async copy, __grid_constant__
8.9Ada Lovelace; FP8 Tensor Cores
9.0Hopper; Thread Block Clusters, TMA
10.0Blackwell data center (sm_100, B200/GB200); 5th-gen Tensor Cores, FP4 — CUDA ≥ 12.8
12.0Blackwell 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

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