CUDA C++ LabGPU architecture

Same load, different addresses

Change a warp’s input stride and predict which 32-byte sectors its reads touch.

Before you run

A warp has 32 lane positions, numbered 0–31. This example launches a full warp, with one thread in every position. Here each thread reads one FP32 value and writes it to a contiguous output. A stride is the distance in elements between neighbouring reads. The starter supplies a 32-byte-aligned input with 993 elements. The largest supported read is element 31 × 32 = 992, so that allocation covers every lane.

Definitions

32-byte alignment
The first input address is a multiple of 32 bytes in this model.
Memory sector
An aligned 32-byte region in this address model. Eight consecutive FP32 values fit in one sector.

Read the starter excerpts

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

__global__ void runKernel(const float* input, float* output, int n) {
    constexpr int stride = 1;
    static_assert(stride >= 1 && stride <= 32, "Choose a stride from 1 to 32");
    int lane = blockIdx.x * blockDim.x + threadIdx.x;
    if (lane < n) output[lane] = input[lane * stride];
}

main launches runKernel with one warp of 32 threads. stride sets the distance between input indices, while output remains contiguous. n counts outputs; the input allocation must also cover the largest strided read. The host groups returned indices into sectors.

CUDA C++ · launch inside main()

runKernel<<<1, lanes>>>(deviceInput, deviceOutput, lanes);

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 Sectors touched in Console. The starter’s 32 consecutive FP32 reads touch four aligned 32-byte sectors.

Each FP32 input stores its own element index. The kernel returns the indices it read. For each returned index, the CPU calculates a byte offset (4 × index) and its aligned 32-byte sector. Sectors touched counts the distinct sectors, assuming an aligned input base. A hardware profiler would be needed to observe cache or device-memory traffic.

Change the source

In runKernel in the CUDA source editor, change constexpr int stride = 1 to constexpr int stride = 2, then 32. Before each repeat of Compile and run, predict Sectors touched. Follow input[lane * stride]. Why do neighbouring lanes share fewer 32-byte sectors as the addresses spread out?

Read the starter’s execution diagram

Eight lanes accessing aligned global memory
Bytes 0–31Bytes 32–63
Stride 1Sector 0Lanes 0, 1, 2, 3, 4, 5, 6, 7 Untouched
Stride 2Sector 0Lanes 0, 1, 2, 3Sector 1Lanes 4, 5, 6, 7

This small diagram shows eight lanes; the program launches 32. Each lane reads one FP32 word. Spreading out the addresses changes which sectors supply those values.

Hardware context

One warp issues a load with an address for each active lane. The memory path groups these addresses into requests. Caches may supply data before device memory is accessed; a sector count does not measure device-memory traffic.

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.
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.
Load/store unitsThis lessonMemory instructions and addresses.
L1 cacheRecently used data.
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: lane addresses and sectors

Predict and run

Try it

Set Input element stride to 1 and predict Sectors touched. Click Run model, then inspect the Sector and Lanes columns. Change the stride to 2, predict again and rerun. Which lanes still share a sector?

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

__global__ void gather(const float* x, float* y, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        y[i] = x[0 + i * 1];
    }
}

For this one-warp launch, the host passes n = 32, the number of outputs. i chooses an output and i × stride chooses an input. The input allocation covers the largest read at index 31 × stride.

Optionally enter a prediction, then click Run model.

Apply what you saw

Keep stride 1 but start one FP32 element past the aligned address. How many sectors will the same 32 reads touch?

Compare your explanation

Five. The first sector supplies seven words, three sectors supply eight each, and the fifth supplies the last word.

Follow a warp’s addresses into memory requests 1 min 40 sec · Video walkthrough, captions and transcript

Use fullscreen to inspect the code and diagram labels.

Read the transcript

Same values, different address grouping

One warp requests thirty-two FP32 values. That is one hundred and twenty-eight useful bytes. Changing the input stride can change the number of addressed sectors even when every lane still reads one value.

Addresses begin at the SM

The warp’s threads supply addresses to the SM’s load and store hardware. The memory path groups these addresses. Caches can satisfy requests before a miss reaches device memory.

Adjacent words share sectors

With stride one, lanes zero, one and two read byte addresses zero, four and eight. Those words share the first aligned sector. Across the whole warp, the thirty-two addresses occupy four sectors.

Move each lane eight words apart

Now use stride eight. Each input word is four bytes, so neighbouring lanes are thirty-two bytes apart. Pause and predict whether the warp still addresses four sectors. The useful value count has not changed.

Each lane now reaches another sector

Stride eight places each lane’s word in a different sector. The warp addresses thirty-two sectors for the same thirty-two values. That covers one thousand and twenty-four sector bytes while only one hundred and twenty-eight bytes contain the requested words.

A sector count is not a DRAM read count

These counts describe the addresses touched by one load instruction. They do not tell us how many requests miss the caches or how long the GPU takes. To explain the count, follow lane addresses into aligned sectors. To explain real memory traffic, additional cache and hardware evidence is needed.

References coalesced global-memory access · 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 · coalesced global-memory access (new tab)

    Active lanes’ addresses determine the aligned regions requested. Sector counts do not measure device-memory traffic or time.

    If adjacent active lanes read adjacent FP32 values, can a sector count alone establish how many device-memory reads occur?

    Compare your answer

    No. It counts aligned address regions in the stated model. Cache hits and the actual memory-system behaviour determine traffic beyond those requests.

  • 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.