CUDA C++ LabGPU architecture

What changes when inputs use fewer bits?

Round inputs through FP16 and compare the FP32 sum with and without that rounding.

Before you run

The first eight inputs are 1.0003f and the next eight are −1.0f. The starter optionally rounds each input through FP16 before adding it to an FP32 total. Its input arrays remain FP32; the optional model separately compares input storage formats.

Definitions

FP16 and BF16
Two formats that use two bytes per value. They allocate different numbers of bits to precision and range.
Cancellation
Addition of opposite-sign terms leaves a result small compared with the individual terms.

Read the starter excerpts

Read-only CUDA C++ · dot · starter kernel

__global__ void dot(const float* input, float* output) {
    float total = 0.0f;
    for (int k = 0; k < 16; ++k) {
        float value = input[k];
        if (narrowInputs) value = __half2float(__float2half_rn(value));
        total += value; // The second operand contains only ones.
    }
    output[0] = total;
}

dot optionally rounds each FP32 input to FP16 with __float2half_rn and converts it back with __half2float. total remains FP32. This starter isolates rounding while its arrays remain FP32; the model separately compares storage formats.

CUDA C++ · launch inside main()

dot<<<1, 1>>>(input, 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 FP32 accumulation in Console. The starter rounds its inputs through FP16 and then sums them in FP32.

Change the source

In the CUDA source editor, above dot, change narrowInputs from true to false. Predict whether FP32 accumulation will stay zero, then run again and read that field in Console. Use the conversion in dot to explain the difference. The program prints the sum, not the individual rounded operands.

Read the starter’s execution diagram

Follow the starter’s execution
  1. Read FP32Fetch a value from the input array.
  2. Optionally roundConvert to FP16 and back before adding.
  3. Accumulate FP32Keep total in the wider format.

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

Hardware context

Registers hold a thread’s working operands and accumulator. Rounding an operand can change a sum even when the accumulation stays FP32. This example uses conversions and scalar additions, not tensor cores.

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.
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.
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.
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: input rounding

Predict and run

Follow one 16-term dot product. The first eight input values are close to 1.0 and the next eight are −1.0. Every value in the other operand is 1.0. Store the first operand in a chosen format, then convert its values back to FP32 for accumulation.

Try it

Select FP32 under Format for the first operand and click Run model. Read Final FP32 sum. Select FP16, predict Changed inputs and rerun. Compare Input, Stored and Running FP32 sum, then repeat with BF16. Which differences were lost before accumulation?

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

// One thread's loop, with CUDA's cuda_fp16.h / cuda_bf16.h
// included as required. The other operand is always 1.0f.
float total = 0.0f;
for (int k = 0; k < 16; ++k) {
  float x = input[k];
  total += x;
}

The loop represents loading values that were stored in the chosen format. The conversion is written locally to make the rounding visible.

Optionally enter a prediction, then click Run model.

Apply what you saw

Why does accumulating in FP32 fail to recover the FP32-input result when the positive terms were rounded first?

Compare your explanation

Each positive input has already become 1.0. Adding the rounded terms in FP32 cannot recreate the small differences that were removed.

Separate stored precision from arithmetic state 1 min 55 sec · Video walkthrough, captions and transcript

Use fullscreen to inspect the code and diagram labels.

Read the transcript

Where does information disappear?

The starter stores sixteen FP32 inputs: eight copies of one point zero zero zero three, then eight copies of negative one. Its question is what happens when each loaded value is rounded through FP16 before an FP32 accumulation.

The arrays still occupy FP32 storage

The device input allocation still uses four bytes per element. The conversion happens after loading a value into the thread’s working state. This particular kernel changes the value representation during computation; it does not change the input array’s allocated type.

A narrow format has fewer representable values

Rounding sends one point zero zero zero three to exactly one point zero in this FP16 case. Negative one is exactly representable and stays unchanged. Conversion back to FP32 cannot recover the discarded difference.

Wider accumulation keeps the rounded operands

The rounded positive and negative contributions cancel to zero. Keeping FP32 operands gives a small positive reference result. An FP32 accumulator limits further accumulation rounding, but it cannot repair information removed before the addition.

Must every FP16 conversion lose information?

Consider a new row alternating one point zero and negative zero point five. Both values are exactly representable in FP16. Pause and predict whether the round trip must change the operands or the final sum.

Representability decides this case

No input value changes in that case, and the illustrated sum remains four point zero. Reason separately about stored types, operand conversion and accumulation. A smaller format does not by itself establish an accuracy loss or a speed-up.

References half conversion · explicit memory management

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

  • CUDA Math API · half conversion (new tab)

    __float2half_rn rounds an input to FP16. Wider accumulation cannot recover precision already lost in that input.

    If inputs are rounded to FP16 and then accumulated in FP32, does FP32 accumulation restore the lost input precision?

    Compare your answer

    No. Wider accumulation operates on the already-rounded operands. It cannot reconstruct information lost in the input conversion.

  • CUDA · explicit memory management (new tab)

    An FP32 value occupies four bytes. Allocation reserves storage; a separate copy supplies its contents.

    Seven FP32 values need how many bytes? Does allocating those bytes also copy the CPU inputs?

    Compare your answer

    Seven four-byte values need 28 bytes. Allocation reserves storage; a separate copy or other initialisation supplies its contents.

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.