Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix A - CUDA API Reference

“Every primitive of the CUDA programming model, gathered in one place. When a chapter says ‘the primitive’, this is the definition it means.”

This appendix is the book’s vocabulary list: every type, built-in variable, function and flag used in the main text, with its meaning and where it is discussed. It is intended as a lookup table, not as a tutorial - the tutorials are the chapters.

A.1 Execution Configuration

SyntaxMeaningChapter
kernel<<<gridDim, blockDim>>>(args...)Launch kernel with a grid of gridDim blocks of blockDim threads each3
kernel<<<gridDim, blockDim, sharedBytes, stream>>>As above, with dynamic shared memory (bytes) and an explicit stream6, 7
dim3Three-unsigned vector type; fields .x, .y, .z3
threadIdxThe thread’s position within its block (a dim3)3
blockIdxThe block’s position within the grid (a dim3)3
blockDimThreads per block, as launched (a dim3)3
gridDimBlocks per grid, as launched (a dim3)3
__launch_bounds__(maxThreads, minBlocks)Compiler directive: register budget for occupancy9

The universal 1-D global index: blockIdx.x * blockDim.x + threadIdx.x. The 2-D composition is in Chapter 3, §3.5.

A.2 Function Qualifiers

QualifierRuns onCalled fromChapter
__global__DeviceHost3
__device__DeviceDevice only3
__host__ (default)HostHost3
__host__ __device__BothBoth10

extern "C" __global__ keeps the kernel symbol unmangled for driver-API / NVRTC lookup (Chapters 12, 13).

A.3 Vector Types

TypeBytesAlignmentNotes
uchar331RGB pixels; no padding (Chapter 15)
float2, float48, 164, 16Vectorised loads (Chapter 7, §7.6)
int2, int4, double2, uint48/16/16/16as sizeSame vectorisation rules
size_tplatform-Byte sizes; use instead of int (Chapter 3)

Vectorised accesses require alignment to the vector size.

A.4 Memory Management

FunctionBehaviourChapter
cudaMalloc(void** p, size_t n)Allocate n bytes in device global memory3
cudaFree(void* p)Free a device allocation3
cudaMemcpy(dst, src, n, kind)Synchronous copy; kind = HostToDevice, DeviceToHost, DeviceToDevice, HostToHost3
cudaMemcpyAsync(dst, src, n, kind, stream)Asynchronous copy, queued on stream; requires pinned host memory4, 6
cudaMallocHost(void** p, size_t n)Allocate pinned (page-locked) host memory4
cudaHostAlloc(void** p, size_t n, flags)Pinned host memory; cudaHostAllocMapped adds zero-copy mapping4
cudaHostGetDevicePointer(void** dp, void* hp, 0)Device pointer for zero-copy mapped host memory4
cudaFreeHost(void* p)Free pinned host memory4
cudaMallocManaged(void** p, size_t n)Unified memory (host + device address space)4
cudaMemPrefetchAsync(p, n, device, stream)Migrate unified-memory pages now4
cudaMemcpyToSymbol(sym, src, n)Copy into __constant__ memory7

Memory kinds and when to use each: Chapter 4, §4.7.

A.5 Synchronisation and Memory Ordering

PrimitiveMeaningChapter
__syncthreads()Block-wide barrier; must be uniformly reachable5
__threadfence()Order my device-scope global accesses5
__threadfence_block()Order my block-scope accesses5
__threadfence_system()Order host+device accesses5
volatileDisable register caching of a location5
atomicAdd/Sub/Exch/CAS/Min/Max/And/Or/XorHardware read-modify-write; return old value5
cuda::atomic<T, scope>C++20-style atomic with memory orders (CUDA 12)10

The atomics table with semantics: Chapter 5, §5.5.

A.6 Streams and Events

FunctionBehaviourChapter
cudaStreamCreate(&s)Create a stream6
cudaStreamDestroy(s)Destroy a stream6
cudaStreamCreateWithFlags(&s, flag)cudaStreamNonBlocking disables default-stream sync6
cudaStreamCreateWithPriority(&s, flag, prio)Priority stream; range from cudaDeviceGetStreamPriorityRange6
cudaStreamSynchronize(s)Wait for all work in s6
cudaEventCreate(&e) / cudaEventDestroy(e)Create/destroy an event6
cudaEventRecord(e, s)Mark the stream position6
cudaEventSynchronize(e)Wait until the device reaches e6
cudaEventElapsedTime(&ms, e0, e1)Time between two events6, 16
cudaStreamWaitEvent(s, e)Make s wait for e (cross-stream dependency)6
cudaGraphCreate/Instantiate/Launch/DestroyCapture and replay device work6
cudaDeviceSynchronize()Wait for all device work3, 6

The synchronisation cheat sheet: Chapter 6, §6.8.

A.7 Error Handling

PrimitiveBehaviourChapter
cudaError_tEnum; cudaSuccess == 0, everything else is an error3
cudaGetLastError()Return and clear the last asynchronous error3
cudaGetErrorString(e)Human-readable error text3
cudaDeviceSynchronize()Also surfaces async kernel errors3, 6
cublasStatus_t, nvrtcResult, CUresultLibrary error types (cuBLAS, NVRTC, driver API)11, 12

Two error modes (synchronous vs asynchronous) and the CHECK discipline: Chapter 3, §3.8.

A.8 Device Math Library (selected)

fminf, fmaxf, sqrtf, fabsf, sinf, cosf, expf, logf, powf, fmaf (fused multiply-add). The __host__ __device__ versions work on both sides (Chapter 10, §10.4). Fast approximations live in the __ prefixed forms (__expf, __sinf); enable globally with --use_fast_math - but measure before trusting (Chapter 16, §16.7).

A.9 Warp-Level Primitives

PrimitiveMeaningChapter
__shfl_down_sync(mask, val, delta)Move val from lane lane+delta to lane8
__shfl_sync, __shfl_up_sync, __shfl_xor_syncOther shuffle directions8
0xffffffffuThe 32-lane mask for _sync primitives8
__activemask()Mask of currently active lanes (use with care)8

All _sync primitives require all named lanes to execute them.

A.10 Driver API and NVRTC (Chapter 12)

PrimitiveMeaning
cuInit, cuDeviceGet, cuCtxCreate, cuCtxDestroyContext lifecycle
cuModuleLoadData, cuModuleGetFunction, cuModuleUnloadLoad PTX/cubin, fetch kernel by name
cuLaunchKernel(f, gx,gy,gz, bx,by,bz, smem, stream, args, extra)Raw launch with void* argument array
nvrtcCreateProgram, nvrtcCompileProgram, nvrtcGetPTX, nvrtcGetProgramLogRuntime compilation of CUDA source
PTX / cubin / fatbinPortable ISA / SASS binary / multi-arch container

A.11 Tools and Environment (Chapter 16)

ToolPurpose
nvccOffline compiler (-arch=sm_90, -ptx)
nsys profileSystem-level timeline
ncu --set fullKernel-level counters
compute-sanitizer --tool memcheck/racecheck/initcheck/synccheckRuntime error detection
cuda-gdbInteractive device debugger
clock64()In-kernel cycle counter
CUDA_CACHE_MAXSIZEJIT cache size (Chapter 12)