Reuse inputs in a matrix tile
Compare a direct CUDA matrix product with a tiled kernel and trace which loads the tile reuses.
Before you run
Each output C[row, col] is the dot product of an A row and a B column. A block can compute several neighbouring outputs and load input tiles into shared memory. Several threads can then reuse the same word.
Definitions
- Dot product
- Multiply matching elements from two sequences and add their products.
- Tile
- A small rectangular region of a matrix processed as a group.
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, the block loads left and right tiles into shared memory. After the barrier, each output thread accumulates products using those tiles. The second barrier waits until all threads finish reading the current shared tiles before the next loop iteration overwrites them. When a tile extends past a matrix edge, out-of-range input positions contribute 0.0. Every thread still reaches both barriers, and only valid output positions are written. main checks every product element against an independent CPU reference with nonuniform inputs.
dim3 threads(tile, tile) makes a two-dimensional block. threadIdx.x selects a local column and threadIdx.y a local row. multiply combines these with blockIdx.x and blockIdx.y to choose each output’s column and row. Row-major storage places neighbouring columns next to each other in an array; row * width + col selects one matrix element.
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. In Console, find mismatches, input loads in this tiled algorithm and shared bytes per block. The starter checks 16 values against the CPU reference.
The editable program checks each result with nonuniform inputs and partial tiles. After comparing tile widths, try width = 5 and inspect Checked values and mismatches again.
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: how does reuse change the storage each block needs? The CPU reference checks the product; this is not a timing comparison.
Read the starter’s execution diagram
- × B[0,0]Contributes to C[0,0]
- × B[0,1]Contributes to C[0,1]
The two output threads read the same shared A word and different B words.
Hardware context
Threads reuse shared tiles while each output sum is working state associated with its thread. This multiply kernel uses scalar arithmetic; it does not call matrix-unit instructions.
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.
CPU side · CUDA host
Host ↔ device · copies move data; launches request execution
GPU device
GPU compute chip
SM · streaming multiprocessor
Logical groups using this SM’s resources
Issue to the appropriate units
Registers ↔ execution units · operands and results
Have their own execution hardware and registers.
Blocks may be assigned to different SMs.
Global-memory requests ↔ on-chip interconnect
Chip ↔ device memory · data transfers
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-tile reuse
Predict and run
Try it
Select 1 · separate outputs under Output tile width and click Run model. Read Global input element loads. Select 2 · shared input tiles and predict that count for all 16 outputs before running again. Use Next step to inspect which A and B values feed the first block’s partial C values.
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 per C element. A, B, C are row-major 4 × 4.
int row = blockIdx.y;
int col = blockIdx.x;
float total = 0.0f;
for (int k = 0; k < 4; ++k)
total += A[row * 4 + k] * B[k * 4 + col];
C[row * 4 + col] = total;The first barrier makes the tile available. The second protects it from being overwritten before the last reader finishes. Width 1 uses a 4 × 4 grid of one-thread blocks here for a direct comparison.
Apply what you saw
With Output tile width 2, consider output rows 0–1 and columns 0–1. During the first input tile (k0 = 0), which outputs reuse shared b[1][0], loaded from B[1,0]?
Compare your explanation
C[0,0] and C[1,0] both multiply it against values from different A rows. They share a B word because they have the same output column.
See which values a shared tile reuses 1 min 41 sec · Video walkthrough, captions and transcript
Use fullscreen to inspect the code and diagram labels.
Read the transcript
One output needs a row and a column
For output C at row zero and column zero, a thread combines row zero of A with column zero of B. Neighbouring outputs reuse parts of those inputs. The recording follows where that reuse happens on an SM.
A block loads two shared input tiles
Four threads form a two-by-two output block. They cooperatively load a two-by-two tile from each input into their block’s shared memory. The first block barrier completes those shared writes before the tile is consumed.
Shared inputs feed private working sums
Threads computing the same output row can reuse an A value. Threads computing the same output column can reuse a B value. Each thread keeps its own partial output sum. Sharing the inputs does not merge the output accumulators.
Finish using a tile before replacing it
The second block barrier protects the tile from being overwritten while another thread still uses it. The block loads the next K positions and continues its private sums. The shown C at row zero, column zero finishes at three point zero.
Can another block use this shared tile?
Suppose two blocks are resident on the same SM. Pause and predict whether the second block’s shared array is the same workspace as the first block’s. Does sharing an SM make block-scoped arrays shared between blocks?
Shared-memory ownership stays per block
Each block has its own shared workspace, even when both use the same SM. One block cannot use the other block’s shared array as its tile. Reuse in this kernel comes from cooperating threads within a block, while each thread retains a private output sum.
References shared memory · synchronisation functions
The browser exercises need no installed CUDA documentation version. For local execution, use the documentation matching your toolkit. External references need internet access.
- 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.
- CUDA · synchronisation functions (new tab)
__syncthreads() is a block barrier. Keep its participation uniform across the block.
Only two of four threads read shared values. Should the other two skip a block barrier before those reads?
Compare your answer
No. Keep the block barrier collective. A condition around __syncthreads must be uniform across the block; the conditional consumers belong after the barrier.
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.
Browser simulation checks program behaviour; physical GPU timing requires a hardware measurement.
Enable JavaScript to save completion.