CUDA C++ LabGPU architecture

Explain an SM resource trade-off

Calculate a residency limit and identify which hardware observation could test an optimisation.

Reusing an input can reduce loads while increasing the storage each block needs. Before timing a larger tile, explain which SM resource could limit the blocks that can stay resident.

PyTorch

import torch
x = torch.arange(8, dtype=torch.float32)
y = x * 2.0 + 1.0
print(y)

The elementwise result is [1.0, 3.0, …, 15.0]. PyTorch may select or compile its own implementation. This page investigates hardware constraints; the controls do not predict PyTorch’s resource use.

Download the complete PyTorch example

Definitions

Residency
Blocks and warps whose execution resources are allocated on an SM.
Occupancy
The number of resident warps divided by the maximum warps that the SM supports.
Register spill
A compiler decision to place some thread working values in local memory instead of registers.

Which limit wins?

Use an explicitly illustrative SM with 65,536 32-bit registers, 64 KiB shared memory (1 KiB = 1024 bytes), 64 warps, 2048 threads and at most 16 resident blocks. Ignore register/shared allocation granularity and reserved storage. Real devices have different budgets and allocation rules.

For 256 threads, 32 registers per thread and 16 KiB shared per block, the register limit allows 8 blocks; shared memory allows 4; the warp and thread limits allow 8. The minimum is 4 blocks, or 32 resident warps: 50% of the model’s limit.

Increasing registers per thread can lower the register-based block limit. That limit alone does not say that spilling occurs or that the kernel slows down. More registers might avoid redundant loads; forcing fewer registers might create spills.

Follow a memory request

A spilled thread value uses CUDA local memory, which is backed by device memory and may be served by caches. “Local” describes thread ownership, not SM-local shared storage. Global requests travel through the SM memory path and shared L2 towards device memory on misses. Cache policies and paths depend on the architecture and instruction.

Latency is how long a dependency waits; bandwidth is the rate at which data can be transferred. Extra ready warps can hide some latency. They cannot increase a saturated device-memory bandwidth indefinitely. Fewer address sectors are a useful hypothesis, not a measured DRAM byte count.

Inspect compiler and profiler evidence

For native inspection, download matmul-investigation.cu and compile it with nvcc -O3 -Xptxas=-v matmul-investigation.cu -o matmul-investigation. Read the compiler’s register, shared-memory and spill reports. This optional step requires the CUDA toolkit; running or profiling the program also requires an NVIDIA GPU. Use Nsight Compute’s LaunchStats, Occupancy, MemoryWorkloadAnalysis and SchedulerStats sections to compare launch resources, active and ready (eligible) warps, cache/device traffic and stalls. Metric names vary by GPU/tool version. A local load alone does not establish spilling: arrays can also use local memory.

Execution relationships · order, not measured duration
  1. AllocateEach resident block claims registers, shared memory, threads and warp slots.
  2. Take the minimumThe tightest resource limit bounds resident blocks.
  3. Test the hypothesisCheck compiler/profiler evidence and time the correct kernel.

Compare SM resource limits

Set Threads per block to 256, Registers per thread to 32 and Shared memory per block to 16 KiB. Click Run model and find which row has the smallest Maximum blocks from this resource value. Keep the thread count and shared memory unchanged, set Registers per thread to 96 and predict resident blocks before running again. Does the limiting resource change?

Enter a resident-block prediction, then click Run model.

Hardware context

Registers and shared memory are finite resources of each SM. Residency is an allocation limit, not a timeline or a guarantee that every resident warp has a ready instruction.

Read the hardware key
Resident warp
A warp has 32 lane positions; a small block can leave some unused. A resident warp has its state and required resources allocated on an SM. Its next instruction may still be waiting for data.
Instruction
One operation run by GPU hardware, such as a load, multiply or store.
Warp scheduler
Selects ready warp instructions for available execution hardware.
Registers
Hold working values associated with individual threads.
Execution units
Perform arithmetic and other instructions for threads; they are shared hardware.
Load/store units
Execute memory instructions, forming requests from active lanes’ addresses.
L1 / L2 cache
Keep recently used data on the GPU chip. L1 is local to an SM; L2 is shared across SMs in this simplified view.
Shared memory
SM-local storage allocated to blocks for explicit data exchange. It may share physical storage with L1, depending on the GPU.
Bank
A subdivision of shared memory that serves a group of word addresses.
Tensor cores
Matrix execution units used by supported warp-wide matrix instructions.
Computer / system · inside one SM

CPU side · CUDA host

CPUmain() and CUDA calls.
System RAMCPU memory for arrays.

GPU device

GPU compute chip

SM · streaming multiprocessor
Assigned blocks → resident warps → threads
Logical groups using this SM’s resources
Warp schedulersReady warp instructions.
RegistersThis lessonThread working values.
Execution unitsShared arithmetic hardware.
Tensor coresSupported matrix multiply-accumulate instructions.
Load/store unitsMemory instructions and addresses.
L1 cacheRecently used data.
Shared memory · banksPer-block data exchange.
Other SMs

Have their own execution hardware and registers.

Blocks may be assigned to different SMs.

Shared L2 cacheData cached for multiple SMs.
Copy enginesTransfer data independently of SM arithmetic when supported.
Device memoryGPU arrays; separate from the compute chip.

Simplified NVIDIA-style architecture. Boundaries show containment; arrows name relationships. Unit counts, block placement and timing vary by GPU. A thread or warp is not a dedicated physical processor.

Apply what you found

A larger tile cuts calculated global loads but doubles shared storage per block. What would justify keeping it?

Compare your explanation

Verify every output, inspect whether residency or spills changed, then compare repeated kernel timings on the target shapes and GPU. Cache and memory-traffic metrics can explain a gain or loss; occupancy or source load counts alone cannot choose the winner.

For toolkit installation and optional local execution, use desktop CUDA setup. The browser examples have been checked in functional simulation.

References launch and occupancy · hardware multithreading

External references open in a new tab and need internet access. Browser activities have no installed toolkit version; match documentation to your toolkit for native execution.

  • CUDA · launch and occupancy (new tab)

    Per-block registers, shared memory and thread counts constrain how many blocks can reside on an SM.

    If a block uses more shared memory, can you conclude that more blocks fit on an SM or that the kernel becomes faster?

    Compare your answer

    Neither follows. Greater per-block use can reduce residency. Reuse and other effects may still help, so verify correctness and measure the actual workload on the target GPU.

  • CUDA · hardware multithreading (new tab)

    A scheduler can issue a ready warp instruction while another warp waits, when the required execution resources are available.

    Warp A awaits a load, but warp B has ready operands. Must the SM wait for A before issuing B?

    Compare your answer

    No. A scheduler can issue a ready instruction from B to suitable available hardware. This does not establish the latency or throughput of a particular GPU.

Further GPU investigation Run this CUDA example on an NVIDIA GPU

In the CUDA source editor, open Editor tools and choose Export .cu to save the complete program. Use an NVIDIA GPU and a matching CUDA toolkit to run it outside the browser.

GPU setup and tools

Browser simulation checks program behaviour; physical GPU timing requires a hardware measurement.

Enable JavaScript to save completion.

App status

Course status

Checking for updates

Version and download details

Downloads include all lessons, recordings, diagrams, examples, browser simulators and the CUDA compiler. Browser storage overhead is additional. External links need internet access.

Running examples outside the browser needs separate PyTorch or CUDA/GPGPU-Sim tools.

Add CUDA C++ Lab to your apps

Use your browser’s install option for an app icon and a separate window. Course status shows whether the course is downloaded and ready to use offline.