CUDA C++ LabGPU architecture

When shared-memory addresses collide

Change the stride of a shared-memory read and group the simultaneous requests by bank.

Before you run

Shared memory is divided into banks that serve groups of word addresses. This starter stores each shared element’s integer index as its value, so a returned integer identifies the element read. Each shared element occupies four bytes. The optional model uses four-byte FP32 words to explore the same bank mapping and broadcasts.

Definitions

Service round
One step of this bank model: each bank serves one distinct word, including a broadcast to several lanes. This is not a measured GPU cycle.
Bank conflict
Lanes of one instruction need different words from the same shared-memory bank.

Read the starter excerpts

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

__global__ void readWords(int* output) {
    __shared__ int words[1024];
    int lane = threadIdx.x;
    for (int i = lane; i < 1024; i += 32) words[i] = i;
    __syncthreads();
    output[lane] = words[lane * stride];
}

readWords cooperatively initialises shared words, then all threads reach a barrier. Each lane reads words[lane * stride]. Each returned integer is a shared-word index, not a byte address. The host calculates service rounds for the stated bank model; the console is not a hardware timer.

CUDA C++ · launch inside main()

readWords<<<1, 32>>>(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 Modelled service rounds in Console. The CPU maps the returned word indices to banks using word index modulo 32.

Change the source

In the CUDA source editor, above readWords, change stride from 32 to 33. Predict Modelled service rounds and click Compile and run again. Use words[lane * stride] in readWords and the remainder after dividing the word index by 32 to explain the change. These software counts describe bank requests in the model.

Read the starter’s execution diagram

Follow the starter’s execution
  1. Initialise shared wordsComplete the block barrier before reads.
  2. Select wordsEach lane forms lane × stride.
  3. Group by bankDistinguish broadcasts from different-word conflicts.

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

Hardware context

Shared memory is organised into banks that can serve requests in parallel. In this model, word index modulo 32 selects a bank. Repeated requests for one word can broadcast; different words in one bank need separate rounds.

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.
RegistersThread working values.
Execution unitsShared arithmetic hardware.
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: shared-bank requests

Predict and run

A bank serves words from shared memory. The model uses 32 banks with successive FP32 words cycling through them. For one warp load, a bank can broadcast one word to multiple lanes. Distinct words in the same bank need separate service rounds.

Try it

Select lane × 32 and predict service rounds, then click Run model. Repeat with lane × 33 and Word 0 for every lane. Compare the word addresses and bank groups: are conflicting requests for different words, or repeated requests for the same word?

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

// Device excerpt. All 1,024 words in s are initialised
// cooperatively, then the block reaches __syncthreads().
int lane = threadIdx.x; // launch one block of 32
output[lane] = s[lane * 32];

A word index modulo 32 identifies its bank. The largest read, 31 × 33, fits in the declared 1,024-word allocation.

Optionally enter a prediction, then click Run model.

Apply what you saw

Why do stride 32 and a broadcast both select bank 0 but require different service rounds?

Compare your explanation

Stride 32 requests 32 different words in bank 0. A broadcast requests the same word 32 times and uses one round.

Follow shared-memory addresses to their banks 1 min 29 sec · Video walkthrough, captions and transcript

Use fullscreen to inspect the code and diagram labels.

Read the transcript

Shared memory is banked storage

On this teaching model, the SM’s shared memory has thirty-two banks, and successive four-byte words select successive banks. For a word index, taking the remainder after division by thirty-two identifies its bank.

Different words can select one bank

With stride thirty-two, lane zero requests word zero, lane one word thirty-two, and lane two word sixty-four. Those are different addresses, but each maps to bank zero.

One bank has distinct words to serve

Across the warp, bank zero receives thirty-two distinct-word requests. The model needs thirty-two service rounds. Shared memory is on the SM, but being local does not make every address pattern equally serviceable.

Changing the address pattern spreads banks

Stride thirty-three changes the bank remainder for successive lanes. This warp’s words now use all thirty-two banks. The model can serve the distinct requests in one round.

All lanes request the same word

Consider a third case: every lane requests word zero. They all select bank zero again. Pause and predict whether that necessarily means thirty-two distinct-word service rounds.

A same-word read can broadcast

There is only one distinct word to supply, so the model broadcasts it to the readers in one round. Count distinct words within each bank. The starter’s reported service rounds are address-model results, not measured GPU time.

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

    Different shared-memory words in the same bank can need separate service; a same-word read can broadcast.

    Two lanes request the same shared word. Is that the same conflict as requesting two different words in its bank?

    Compare your answer

    No. A same-word read can broadcast. Different words mapped to the same bank can require separate service under the stated bank model.

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

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.