CUDA C++ LabGPU architecture

Investigate a matrix multiplication

Choose a tile width, predict its resource trade-off and decide what to measure on a GPU.

Before you run

Compare tile reuse with the storage each block needs. This starter multiplies 8 × 8 matrices and prints calculated input loads and shared bytes per block. The optional resource model uses an explicitly imaginary SM budget to explore how many blocks could fit.

Definition

Resource limit
A per-SM capacity that can constrain how many blocks are resident together.

Read the starter excerpts

Read-only CUDA C++ · multiply · kernel body excerpt

for (int begin = 0; begin < width; begin += tile) {
    left[y][x] = row < width && begin + x < width
        ? a[row * width + begin + x] : 0.0f;
    right[y][x] = begin + y < width && col < width
        ? b[(begin + y) * width + col] : 0.0f;
    __syncthreads();
    for (int k = 0; k < tile; ++k) sum += left[y][k] * right[k][x];
    __syncthreads();
}
if (row < width && col < width)
c[row * width + col] = sum;

Inside multiply, tile sets both shared array dimensions and the block’s two-dimensional thread shape. main derives the grid from width and tile. Zero padding handles partial tiles while every thread reaches the barriers. main verifies every output against a CPU reference on nonuniform inputs before reporting calculated loads and shared bytes; those counts alone do not identify a faster kernel.

CUDA C++ · launch inside main()

multiply<<<blocks, threads>>>(left, right, output);

The settings inside <<< >>> give the block count and threads per block. Parentheses pass the arguments to the kernel. These read-only excerpts show the starter. The CUDA source editor contains the storage and launch settings.

Edit and run CUDA

The CUDA source editor opens with the complete starter or your saved draft. To run the starter again, use Editor tools → Restore starter. Choose Undo restore there to recover the previous draft.

Click Compile and run with the starter. Find mismatches, input loads in this tiled algorithm and shared bytes per block in Console. The starter checks 64 values against its CPU reference.

After comparing tile widths, try width = 7 and look for 49 checked values and zero mismatches. This width leaves tiles that are only partially full.

Change the source

In the CUDA source editor, above multiply, change tile from 2 to 4. Run again and check mismatches is 0. Compare input loads in this tiled algorithm and shared bytes per block in Console. Explain the reuse/storage trade-off before choosing a real GPU measurement.

Read the starter’s execution diagram

Follow the starter’s execution
  1. Choose tile widthSet the block shape and shared arrays.
  2. Verify the productCheck correctness before comparing resources.
  3. Form a hypothesisMeasure the real GPU to test performance.

Conceptual dependencies in this example; placement and timing are not specified.

Hardware context

Each resident block consumes shared storage and registers on its SM. Larger tiles can improve reuse while reducing how many blocks fit. The teaching resource budget is separate from the device-specific occupancy limits.

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.
RegistersThread working values.
Execution unitsShared arithmetic hardware.
Tensor coresSupported matrix multiply-accumulate instructions.
Load/store unitsMemory instructions and addresses.
L1 cacheRecently used data.
Shared memory · banksThis lessonPer-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.
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.

Explore the model: SM resource limits

Predict and run

Tile reuse reduced input loads in the earlier matrix lesson. Now compare the storage that a block needs. Use 8 × 8 matrices and an imaginary streaming multiprocessor with 128 bytes of shared memory, 1,024 registers, eight warp slots and a limit of eight blocks.

Try it

Select 2 · tiled under Compile-time tile width and predict blocks per teaching SM. Click Run model and inspect each resource’s capacity and use per block. Repeat with 4 · tiled. Which resource gives the smaller limit? These calculations do not select the faster real GPU kernel.

These controls change only this model and its displayed excerpt; they do not edit the CUDA program above. Enter a prediction to compare with the model result.

CUDA C++ excerpt

// Direct version uses no shared tiles.
constexpr int T = 1;
dim3 block(T, T), grid(8 / T, 8 / T);
direct<<<grid, block>>>(A, B, C, 8);

The model charges 16 registers per thread and one warp slot per block. For each resource, divide capacity per SM by use per block and round down. The smallest result, capped at eight blocks, bounds resident blocks. The resident block limit is per SM assuming enough blocks are available. The displayed example grid may contain fewer blocks.

Optionally enter a prediction, then click Run model.

Apply what you saw

How would you test whether a width that needs fewer global loads actually finishes faster on a real GPU?

Compare your explanation

Use identical inputs, check outputs, warm up both kernels and time repeated launches with CUDA events. Inspect shared memory per block, achieved occupancy and memory traffic in the profiler.

Further practice: Investigate SM resource limits.

Balance reuse against SM resource demand 1 min 44 sec · Video walkthrough, captions and transcript

Use fullscreen to inspect the code and diagram labels.

Read the transcript

Reuse has a resource cost

For the eight-by-eight calculation, a larger tile can reduce the algorithm’s global operand loads. The same change also increases each block’s shared storage and thread working state. We need both sides of that comparison.

Per-block storage grows with the tile

Two shared input tiles need thirty-two bytes per block at tile width two, and one hundred and twenty-eight at tile width four. These are storage requirements of this algorithm, not measurements of device-memory transactions.

A finite SM budget limits resident blocks

Use an imaginary SM with one hundred and twenty-eight shared bytes, one thousand and twenty-four registers, and a maximum of eight blocks. These intentionally small teaching budgets are separate from a real device’s limits. The smaller tile allows four resident blocks in this model.

The larger tile leaves room for fewer blocks

At tile width four, one block consumes the whole shared-memory budget, so only one block fits. The reduced operand-load count does not tell us whether that loss of resident work helps or hurts execution time.

Increase only the shared-memory budget

Keep tile width four and the same register budget, but raise shared capacity to five hundred and twelve bytes. Pause and predict whether eight blocks must now fit. Which other resource could still limit residency?

The tightest resource still wins

The model permits four blocks, not eight. Each larger block also needs two hundred and fifty-six registers under the chosen register assumption. Use counts to form a hypothesis, verify the full numerical result, then measure a named real GPU before ranking performance.

References launch and occupancy · shared memory

The browser exercises need no installed CUDA documentation version. For local execution, use the documentation matching your toolkit. External references need internet access.

  • 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 · shared memory (new tab)

    Ordinary __shared__ arrays belong to one block. Order shared writes before dependent reads with the appropriate barrier.

    Two blocks run on the same SM. May one treat the other block’s ordinary __shared__ array as its own?

    Compare your answer

    No. Ordinary shared arrays have block scope. Sharing an SM does not merge their shared-memory allocations; cross-block cooperation needs an appropriate supported mechanism.

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.