GPU Lab / interactive book
GPU mapSource ↗

CHAPTER 5

How do threads read those values?

Follow five reads from one warp and compare the sectors they touch.

Follow five reads from one warp

PyTorch

import torch
x = torch.arange(1, 26, dtype=torch.float32, device="cuda").reshape(5, 5)
values = x[:, 0]
y = values * 2
Desktop setup

We keep the same tensor and multiplication. Chapter 4 showed where each value lives. Nearby values can let several threads share a memory request. To see why, first look at how the GPU groups threads that issue a read together.

Definitions

Warp
A group of 32 thread positions on an NVIDIA GPU. Threads from the same block are organised into warps; some positions may be unused.
Lane
A thread’s position within its warp.
One warp: five participating threads
T0T1T2T3T4–––––––––––––––––––––––––––

Threads 0–4 read one value each. The other 27 positions make no read in this example. A warp belongs to one block; larger blocks contain several warps.

The GPU handles these reads in fixed-size regions rather than fetching a separate four-byte piece for every thread. Counting the regions touched helps us compare nearby and spread-out inputs.

Definition

Sector
An aligned 32-byte region used here to group memory requests. One sector can hold eight FP32 values.

“Aligned” means the regions start at byte offsets 0, 32, 64 and so on. Several threads can read values in the same sector. The diagram below lists the sectors touched and the threads sharing each one.

Run the examples below in your browser with no installation or GPU required. The browser simulates the calculation; it does not execute PyTorch or GPU code. To run the PyTorch snippet itself, use a Python environment with PyTorch and an NVIDIA GPU.

Idea 1 of 1

Compare requests for the same five values

We assign one selected value to each of five threads in the same warp. Each storage allocation starts at a 32-byte boundary in this model. Count the sectors touched by their input read.

Try it

Predict the sector count, then run. Compare row 0, column 0 and its packed copy. Each reads five FP32 values: 20 useful bytes.

PyTorch · operation for these controls

values = x[0, :]
y = values * 2

Predict the outputs, then press Run.

Browser simulation · this example keeps its own controls and results.

x: five rows, five columns

Highlighted cells supply the five inputs.

1[0, 0]2[0, 1]3[0, 2]4[0, 3]5[0, 4]6[1, 0]7[1, 1]8[1, 2]9[1, 3]10[1, 4]11[2, 0]12[2, 1]13[2, 2]14[2, 3]15[2, 4]16[3, 0]17[3, 1]18[3, 2]19[3, 3]20[3, 4]21[4, 0]22[4, 1]23[4, 2]24[4, 3]25[4, 4]

View into x’s storage

  1. Thread 0
    1
    Element ?
    Byte offset ?
    × 2 →
    ?
  2. Thread 1
    2
    Element ?
    Byte offset ?
    × 2 →
    ?
  3. Thread 2
    3
    Element ?
    Byte offset ?
    × 2 →
    ?
  4. Thread 3
    4
    Element ?
    Byte offset ?
    × 2 →
    ?
  5. Thread 4
    5
    Element ?
    Byte offset ?
    × 2 →
    ?

Nearby reads can share a sector. These counts describe one input read, excluding the packing copy and output writes. They are not DRAM-read counts or measured time. This thread mapping is a teaching choice; PyTorch may choose a different kernel.

Explain what you found

Try a new case

Predict the sector count for row 1. It still has five neighbouring FP32 values. Must it fit in a single sector? Check with the controls above.

Compare your explanation

Row 1 occupies element offsets 5–9, or bytes 20–39. It crosses the boundary at byte 32, so it touches 2 sectors. Neighbouring values can still cross a sector boundary.

Optional: explore more memory requests

The memory access explorer lets you choose the stride, starting offset and number of active lanes independently. Use it to test patterns beyond our five-value examples. Its sector counts still do not measure GPU time.

You can continue to chapter 6 without doing this extra practice.

References

Use these official references to check the concepts and code on this page.

These browser exercises do not run an installed PyTorch or CUDA toolkit. For optional local execution, choose documentation matching your installed tools. Links open online in a new tab; the questions and answer comparisons below work offline.

Use the documentation

Use the references to explain each answer, then apply it to this page’s example. No installation is needed.

Install GPU Lab

Give GPU Lab its own icon and window. All lessons and simulators work offline once the course is saved.

Use your browser’s install option:

If your browser has no install option, try Chrome or Edge.

App status

Version

Checking for updates

Offline access

Not saved for offline use

Save all lessons, browser simulators, diagrams and example files. Desktop execution needs separate PyTorch or CUDA/GPGPU-Sim tools; their installations are not part of this offline download. External links need internet access.