CUDA C++ LabGPU architecture

Find a parallel program’s progress dependency

Find which threads reach a block barrier and explain why the rest must also arrive.

Before you run

Four threads initialise shared storage, and every thread reaches the block barrier before any dependent read. readers changes which threads consume neighbouring values. The optional model separately diagnoses what happens when a barrier’s participant set is incomplete.

Definitions

Participation
The threads that reach a particular collective operation.
Progress dependency
An action that has to occur before another action can continue.

Read the starter excerpts

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

__global__ void exchange(int* output) {
    int t = threadIdx.x;
    __shared__ int shared[4];
    shared[t] = t + 1;
    __syncthreads(); // Keep this launch-wide: all four lanes arrive.
    if (t < readers) output[t] = shared[(t + 1) % 4];
}

main() launches exchange with four threads. Every thread writes its shared slot and reaches __syncthreads before testing readers. The readers setting chooses which threads use those values after all four threads pass the barrier.

CUDA C++ · launch inside main()

exchange<<<1, 4>>>(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 all four Read outputs in Console. Two threads read after the barrier.

Change the source

In the CUDA source editor, above exchange, change readers from 2 to 4. Predict the four Read outputs, then run again and inspect them in Console. In the kernel, check that the condition changes the readers after the barrier; every thread still reaches the barrier.

Read the starter’s execution diagram

Follow the starter’s execution
  1. Publish shared valuesAll four threads supply a value.
  2. All arriveKeep the barrier outside t < readers.
  3. Selected threads readOnly the consumer set changes.

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

Hardware context

Resident threads can wait for other threads at a block barrier. Enough hardware resources alone do not repair a missing participant. This safe kernel keeps the barrier outside the conditional reads.

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.
Tensor cores
Matrix execution units used by supported warp-wide matrix instructions.
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 schedulersThis lessonReady warp instructions.
RegistersThread working values.
Execution unitsShared arithmetic hardware.
Tensor coresSupported matrix multiply-accumulate instructions.
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: barrier participation

Predict and run

Four threads initialise shared storage. Every thread must reach the same block barrier before dependent work begins. Changing the branch around the barrier changes who reaches it.

Try it

Select All four under Threads that reach the barrier and click Run model. Select Only threads 0 and 1, predict the arrivals and rerun. Identify the missing participants in the trace. The diagnostic counts participation; it does not execute an invalid CUDA barrier.

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 four-thread block. Each writes one shared word.
s[threadIdx.x] = input[threadIdx.x];
__syncthreads();
// Dependent reads would follow here.

The invalid case is a diagnostic. Its trace counts arrivals and skips without assigning a CUDA runtime or numeric outcome.

Optionally enter a prediction, then click Run model.

Apply what you saw

You still need only threads 0 and 1 to read the shared words. Where would you move the condition?

Compare your explanation

Put the condition around the reads after the barrier. All four threads first write and arrive, then the two readers can use the completed shared data.

Separate consumers from barrier participants 1 min 31 sec · Video walkthrough, captions and transcript

Use fullscreen to inspect the code and diagram labels.

Read the transcript

Four writers prepare shared values

The four-thread block writes values one through four into its shared workspace. A block barrier follows those writes. Only after it do the selected readers consume neighbouring values.

A block barrier is a collective boundary

All four non-exited block threads must reach the matching barrier in this example. Having enough SM resources does not repair a missing participant. The barrier contract and the hardware’s capacity are different questions.

The reader condition comes after the barrier

With two readers, threads zero and one consume shared values after all four writers have arrived. The condition selects consumers. It does not select which writers participate in the earlier collective.

Reduce the consumer set to one

Consider readers equal to one, with the same four launched threads and no early exits. Pause and predict the required number of barrier arrivals. Is it now one or four?

All four still reach the barrier

The required arrivals stay at four. Only the number of consumers changes. The safe program leaves the barrier outside the reader condition, so the shared writes precede the dependent reads.

Diagnose participation without inventing a result

If some of these non-exited threads skip a barrier taken by the others, this is invalid collective participation. The diagnostic does not assign a numerical result or promise how a real GPU will fail. Fix the participant set before reasoning about the output.

References synchronisation functions · launch and occupancy

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

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

  • CUDA · launch and occupancy (new tab)

    Per-block registers, shared memory and thread counts constrain how many blocks can reside on an SM.

    If a block uses more shared memory, can you conclude that more blocks fit on an SM or that the kernel becomes faster?

    Compare your answer

    Neither follows. Greater per-block use can reduce residency. Reuse and other effects may still help, so verify correctness and measure the actual workload on the target GPU.

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.