Back to series
· 14 min read

How Do GPUs Work and Why Are They Suited for Parallel Computing?


1. What Is a GPU?

A GPU (Graphics Processing Unit) was originally used to handle graphics rendering tasks, such as vertex transformation, rasterization, texture sampling, and pixel shading.

Graphics rendering has a notable characteristic: large amounts of data need to perform the same or similar computations.

For example, a 4K image contains:

3840 \times 2160 = 8,294,400

pixels.

Each pixel may need to undergo color computation, lighting computation, texture sampling, and other operations. These computations are often independent of one another and can be executed simultaneously.

The GPU is designed precisely around this kind of highly concurrent, high-throughput computing task.

As computing demands have grown, GPU applications have expanded from graphics processing to:

  • Scientific computing: matrix operations, numerical simulation, fluid dynamics.
  • Artificial intelligence: neural network training, model inference.
  • Image processing: filtering, convolution, image enhancement.
  • Video processing: encoding, decoding, special-effects rendering.
  • Intensive computing: cryptography, statistical analysis, Monte Carlo simulation.

Note that a GPU is not simply a CPU with more compute cores. The two differ markedly in hardware organization, execution model, memory system, and optimization goals.

2. Design Goals of CPUs and GPUs

The core difference between CPUs and GPUs is that their optimization goals differ.

The CPU focuses more on the execution efficiency of a single task, while the GPU focuses more on the overall throughput of a large number of tasks.

1. CPU: Low Latency and Complex Control

A CPU typically has a relatively small number of complex, feature-rich cores.

Each core contains fairly complete execution units, such as:

  • Instruction fetch and decode units.
  • Branch prediction units.
  • Out-of-order execution units.
  • Register renaming units.
  • Integer and floating-point arithmetic units.
  • Multi-level caches.
  • Complex control logic.

The CPU needs to execute various types of programs efficiently, including:

  • Conditional branching.
  • Function calls.
  • Loops and recursion.
  • Operating system scheduling.
  • File and network I/O.
  • Irregular memory access.

Therefore, the CPU invests a large number of transistors in improving single-thread performance and control-flow handling capability.

2. GPU: High Throughput and Massive Concurrency

A GPU typically has a large number of compute units, and each compute unit can schedule and execute a large number of threads.

Its design priorities include:

  • A large number of arithmetic logic units.
  • Highly concurrent thread execution.
  • High-bandwidth video memory.
  • Multi-thread switching.
  • Massive data parallelism.
  • High-throughput floating-point computation.

A GPU does not necessarily make a single thread run faster than on a CPU; rather, it lets a large number of threads execute simultaneously, so that more computation is completed per unit of time.

3. Architectural Comparison of the Two

Comparison Dimension CPU GPU
Number of cores Relatively few Large number of compute units
Design goal Low latency High throughput
Control capability Strong Relatively simple
Single-thread performance Usually stronger Usually weaker
Parallelism capability Task-level and thread-level parallelism Massive data parallelism
Cache system Usually larger and more hierarchical More emphasis on bandwidth and concurrent access
Memory system System memory Dedicated video memory or shared memory architecture
Suitable tasks Complex control, serial logic Large-scale repetitive computation

You can understand the two as follows:

  • CPU: good at quickly completing complex and ever-changing tasks.
  • GPU: good at simultaneously completing a large number of structurally similar tasks.

3. GPU Internal Architecture

Modern GPUs typically adopt a multi-level compute-unit organization. Naming and specific implementations differ among vendors, but the overall design approach is similar.

Taking an NVIDIA GPU as an example, it mainly includes:

  1. GPU global scheduling and compute resources.
  2. Multiple SMs (Streaming Multiprocessors).
  3. CUDA Cores, Tensor Cores, registers, and shared memory inside the SM.
  4. L1 and L2 caches and memory controllers.
  5. Video memory and high-speed interconnect interfaces.

1. Overall Structure of the GPU

A GPU can be seen as a parallel computing system composed of multiple SMs, caches, memory controllers, and external video memory.

flowchart TD
    A["GPU"]
    B["Command and Task Scheduling"]
    C["SM Compute Unit"]
    D["L2 Cache"]
    E["Memory Controller"]
    F["GDDR / HBM Memory"]

    A --> B
    A --> C
    A --> D
    D --> E
    E --> F

    C --> G["CUDA Core"]
    C --> H["Tensor Core"]
    C --> I["Register File"]
    C --> J["Shared Memory / L1 Cache"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EAFE,stroke:#B9A1DB,color:#51416F
    style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style D fill:#FFF3D9,stroke:#E7C77D,color:#765A22
    style E fill:#F1F5F9,stroke:#CBD5E1,color:#334155
    style F fill:#E9E4F8,stroke:#B7A8D8,color:#51416F
    style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style H fill:#F3EAFE,stroke:#B9A1DB,color:#51416F
    style I fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style J fill:#FFF3D9,stroke:#E7C77D,color:#765A22

2. SM: Streaming Multiprocessor

SM is an important computational organizational unit in NVIDIA GPUs.

An SM typically contains:

  • CUDA Core: executes ordinary integer and floating-point operations.
  • Tensor Core: accelerates specific computations such as matrix multiply-accumulate.
  • Warp Scheduler: schedules warp execution.
  • Register File: stores thread-private registers.
  • Shared Memory: provides high-speed storage shared within a thread block.
  • Load/Store Unit: handles memory reads and writes.
  • Special Function Unit: executes certain special mathematical operations.

A GPU consists of multiple SMs, and multiple SMs can execute different thread blocks simultaneously.

For example, a GPU has multiple SMs. The CUDA Runtime organizes a computational task into a Grid, and the hardware scheduler then assigns the Blocks within it to available SMs.

The following must be distinguished:

  • Grid: all thread blocks contained in a single Kernel launch.
  • Block: a set of threads that can be scheduled to execute on an SM.
  • Warp: an important basic unit for instruction scheduling and execution in an SM.
  • Thread: a logical thread that actually executes program instructions.

3. CUDA Core: General-Purpose Compute Unit

CUDA Core is NVIDIA's name for the general-purpose compute execution unit in its GPUs.

It can perform common arithmetic operations, such as:

  • Addition.
  • Multiplication.
  • Floating-point operations.
  • Integer operations.
  • Comparisons and some logical operations.

Note that a CUDA Core is not equivalent to a CPU Core.

A CPU Core can independently perform instruction fetch, decode, branch prediction, and complex control flow; a CUDA Core is typically a finer-grained computational execution resource within an SM and relies on the SM's thread scheduling and instruction execution mechanisms.

Therefore, performance cannot be compared simply by the number of CUDA Cores versus the number of CPU cores.

4. Tensor Core: Matrix Computation Acceleration Unit

Tensor Core is a dedicated compute unit designed by NVIDIA for matrix operations.

Many computations in deep learning can be expressed as matrix multiplication:

C=A\times B

The core computation of matrix multiplication is:

C_{ij}=\sum_{k=0}^{n-1}A_{ik}B_{kj}

Ordinary compute units can complete these operations through multiple multiplications and additions.

Tensor Cores, by contrast, are specially optimized for matrix multiply-accumulate and can process matrix tiles in a single dedicated operation or a group of dedicated operations.

A typical computation form is:

D=A\times B+C

This operation is also called MMA (Matrix Multiply Accumulate).

Tensor Cores are very important in the following scenarios:

  • Attention in Transformers.
  • Fully connected layers.
  • Convolutional neural networks.
  • Large-scale matrix multiplication.
  • Mixed-precision training.

Tensor Cores do not support matrix computations of arbitrary shapes and arbitrary data types. Actual performance depends on the GPU architecture, data type, matrix dimensions, layout, alignment, and the implementation of the compute library.

IV. Why Are GPUs Suitable for Parallel Computing?

The parallel capability of a GPU comes mainly from the parallelizability of the task itself, as well as the hardware's organization and scheduling of a large number of threads.

1. Data Parallelism: Applying the Same Operation to Different Data

Suppose two arrays of length N need to be added element by element:

C_i=A_i+B_i

Each output element depends only on the two input elements at the corresponding position.

Therefore, computations at different positions are independent of one another.

Conventional serial execution:

for i in range(N):
    C[i] = A[i] + B[i]

A GPU can assign different positions to different threads:

```cpp __global__ void vector_add( const float* A, const float* B, float* C, int N ) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) { C[i] = A[i] + B[i]; } } ```

Where:

  • threadIdx.x: the index of the thread within its thread block.
  • blockIdx.x: the index of the thread block within the Grid.
  • blockDim.x: the number of threads per thread block.

Each thread handles one or more elements.

2. Task Parallelism and Data Parallelism

Parallel computing can be categorized from different perspectives.

Data Parallelism

Multiple threads perform the same operation but process different data.

For example:

Y_i=f(X_i)

Image processing, vector operations, and matrix operations all exhibit clear characteristics of data parallelism.

Task Parallelism

Multiple tasks perform different operations.

For example:

  • One task handles data preprocessing.
  • One task handles model inference.
  • One task handles post-processing of the results.

GPUs can also perform task parallelism, but their most typical advantage remains large-scale data parallelism.

3. GPUs Use a Large Number of Threads to Hide Memory Latency

This is the key to understanding GPU performance.

CPUs typically reduce instruction latency through complex out-of-order execution, branch prediction, and caching mechanisms.

GPUs, by contrast, adopt another important strategy: maintaining a large number of warps at different stages of execution at the same time.

Suppose a Warp is waiting for data from GPU memory; the SM can temporarily switch to another Warp that is already prepared and execute its instructions.

This process is called Latency Hiding.

```mermaid flowchart TD A["Warp 0 executes instructions"] B["Warp 0 waits for memory"] C["Scheduler selects another Warp"] D["Warp 1 executes instructions"] E["Warp 2 executes instructions"] F["Warp 0 data returns"] G["Warp 0 resumes execution"] A --> B B --> C C --> D D --> E E --> F F --> G style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976 style B fill:#FFF3D9,stroke:#E7C77D,color:#765A22 style C fill:#F3EAFE,stroke:#B9A1DB,color:#51416F style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F style F fill:#FFF3D9,stroke:#E7C77D,color:#765A22 style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976 ```

Note that Warp switching does not mean that the complete execution state must be saved and restored every time, as with CPU context switching.

GPU hardware typically already maintains the register state and scheduling information for a large number of threads, so it can quickly select another runnable Warp.

This is also why GPUs require large amounts of registers and thread residency resources.

4. SIMD and SIMT

GPU parallel execution often involves two concepts.

SIMD: Single Instruction Multiple Data

A single instruction processes multiple sets of data simultaneously.

For example:

\begin{aligned} C_0 &= A_0+B_0\\ C_1 &= A_1+B_1\\ C_2 &= A_2+B_2\\ C_3 &= A_3+B_3 \end{aligned}

Vector instructions on CPUs, such as AVX, are typical SIMD mechanisms.

SIMT: Single Instruction Multiple Threads

Single Instruction Multiple Threads is an important execution abstraction of the NVIDIA CUDA programming model.

Developers write independent thread programs; the hardware organizes threads into Warps and executes instructions with the warp as the key scheduling unit.

The main differences between the two:

Comparison SIMD SIMT
Programming abstraction Vector instructions Independent threads
Data organization Explicit vectors Logical threads
Control flow Vectorized control Thread control flow
Execution style Parallel across multiple data elements Coordinated execution across multiple threads
Typical scenario CPU vectorization GPU CUDA

SIMT does not mean that every thread in a Warp has a fully independent physical execution unit.

In the typical execution model of NVIDIA GPUs, a Warp contains 32 threads; these threads are scheduled as a warp and execute operations along the same instruction path.

Different GPU architectures may have different warp widths and execution details.

5. Warp: The Key Unit of GPU Parallel Execution

Understanding Warps is the key to understanding GPU performance.

1. What Is a Warp?

In NVIDIA CUDA, a warp typically consists of 32 threads.

When a block contains 128 threads, it typically forms:

128/32=4

warps.

For example:

dim3 block(128);
dim3 grid((N + 127) / 128);

Each block contains 128 threads, typically split into 4 warps.

The SM's warp scheduler selects warps that are eligible for execution and issues their instructions.

2. Warp Divergence: Thread Branch Divergence

Warp divergence is a performance problem that arises very easily in GPU programming.

For example:

if (threadIdx.x % 2 == 0) {
    result = a + b;
} else {
    result = a * b;
}

Within the same warp, some threads take the addition branch while others take the multiplication branch.

In a typical SIMT execution model, the hardware must execute the branch paths separately, using an active-thread mask to control which threads participate in the current instruction.

Suppose that within a warp:

  • 16 threads execute the addition.
  • 16 threads execute the multiplication.

In the typical case, the two branch paths must be executed separately.

flowchart TD
    A["Warp of 32 threads"]
    B["Encounter a conditional branch"]
    C["16 threads satisfy the condition"]
    D["16 threads do not satisfy the condition"]
    E["Execute branch A"]
    F["Execute branch B"]
    G["Reconverge"]

    A --> B
    B --> C
    B --> D
    C --> E
    D -->
    E --> G
    F --> G

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EAFE,stroke:#B9A1DB,color:#51416F
    style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style D fill:#FFF3D9,stroke:#E7C77D,color:#765A22
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#F3EAFE,stroke:#B9A1DB,color:#51416F
    style G fill:#F1F5F9,stroke:#CBD5E1,color:#334155

This reduces the warp's effective execution efficiency.

However, note that:

  • Not all conditional branches cause severe divergence.
  • If all threads within a warp take the same branch, this kind of divergence does not occur.
  • The compiler may optimize some branches through techniques such as predicated execution.
  • Whether a branch affects performance also depends on the number of branch instructions, the length of the execution paths, and the hardware architecture.

3. Occupancy

Occupancy is the ratio of the number of active warps on an SM to the maximum number of warps the hardware can keep resident.

Occupancy= \frac{Active\ Warps}

For example:

A certain SM supports at most 64 resident warps and currently has 32 active warps:

Occupancy=\frac{32}{64}=50\%

Factors that affect occupancy include:

  • The number of registers used per thread.
  • The number of threads per block.
  • The shared memory used per block.
  • The SM's total register capacity.
  • The SM's shared memory capacity.
  • The maximum number of threads and warps supported by the hardware.

One important misconception is that higher occupancy always means better performance.

In reality, this is not the case.

If a kernel has high computational density, regular memory access patterns, and low instruction latency, even lower occupancy can achieve very good performance.

What really deserves attention is whether there are enough active warps to hide computation and memory latency, and whether the compute resources are used effectively.

VI. The GPU Memory Hierarchy

GPUs have enormous compute capability, but the compute units must be continuously fed with data to maintain high utilization.

Therefore, the GPU memory hierarchy is a key factor in determining performance.

GPU memory access typically involves:

  1. Registers.
  2. Shared memory.
  3. L1 cache.
  4. L2 cache.
  5. Global memory.

1. Registers

Registers are a very fast storage resource in the GPU.

Each thread has its own register state, used to hold:

  • Temporary variables.
  • Loop counters.
  • Intermediate calculation results.
  • Addresses and indices.
  • Operation operands.

For example:

__global__ void compute(float* output) {
    int tid = threadIdx.x;

    float a = tid * 1.0f;
    float b = a + 2.0f;
    float c = a * b;

    output[tid] = c;
}

Here, tid, a, b, and c usually use registers when the compiler can keep them as thread-local state.

Registers are limited in number.

If each thread uses too many registers, the number of threads that can reside simultaneously in an SM decreases, which in turn affects Occupancy.

In severe cases, some thread-local data may be placed by the compiler into Local Memory.

Note: although Local Memory has "Memory" in its name, in CUDA it is thread-private logical storage space, usually located in the device memory address space, and is not equivalent to on-chip high-speed registers.

2. Shared Memory

Shared Memory is a storage area inside an SM that can be shared by threads in the same thread block.

Features:

  • On-chip storage.
  • Latency is usually lower than global memory.
  • Accessible by threads within the same block.
  • Its lifetime is usually tied to the execution lifetime of the thread block.
  • Developers need to organize reads, writes, and synchronization appropriately.

For example:

__global__ void sum_shared(
    const float* input,
    float* output
) {
    __shared__ float cache[256];

    int tid = threadIdx.x;
    int gid = blockIdx.x * blockDim.x + tid;

    cache[tid] = input[gid];

    __syncthreads();

    if (tid == 0) {
        float sum = 0.0f;

        for (int i = 0; i < blockDim.x; ++i) {
            sum += cache[i];
        }

        output[blockIdx.x] = sum;
    }
}

Where:

  • __shared__ declares shared memory.
  • __syncthreads() is used for synchronization within a thread block.
  • Threads first write to shared memory.
  • After synchronization completes, threads can safely read data written by other threads.

__syncthreads() is a block-level synchronization barrier and cannot be used directly for global synchronization between different blocks.

In addition, Shared Memory usually shares or is associated with some on-chip resources with L1 Cache; the exact organization depends on the GPU architecture.

3. Global Memory

Global Memory is the device memory space on the GPU that all relevant compute units can access.

Common types of device memory include:

  • GDDR.
  • HBM.

Features:

  • Relatively large capacity.
  • High bandwidth.
  • Relatively high latency.
  • Access cost is affected by caching, access patterns, and memory coalescing.

GPUs typically use a large number of threads to access device memory concurrently and fully utilize high bandwidth.

4. GPU Memory Hierarchy

Memory Type Scope Typical Characteristics Primary Use
Register Single thread Extremely fast, limited capacity Thread-local variables
Shared Memory Single Block On-chip, shareable Data reuse, cooperative computation
L1 Cache SM-related On-chip cache Reduce some memory accesses
L2 Cache Shared across multiple SMs on the GPU On-chip cache Cross-SM data caching
Global Memory GPU-wide Large capacity, high bandwidth Input, output, and intermediate data

It can be understood as a memory hierarchy from small to large:

flowchart TD
    A["GPU Memory Hierarchy"]
    B["Register"]
    C["Shared Memory"]
    D["L1 Cache"]
    E["L2 Cache"]
    F["Global Memory"]

    A --> B
    B --> C
    C --> D
    D --> E
    E --> F

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style D fill:#F3EAFE,stroke:#B9A1DB,color:#51416F
    style E fill:#FFF3D9,stroke:#E7C77D,color:#765A22
    style F fill:#F1F5F9,stroke:#CBD5E1,color:#334155

This diagram represents the memory hierarchy, not that every data access must pass through each level in sequence.

The actual access path depends on cache hits, instruction type, memory space, and hardware architecture.

VII. Why Is GPU Memory Access Important?

GPUs have many compute units, but if memory access efficiency is low, compute resources may sit waiting.

1. Memory Coalescing: Coalesced Memory Access

This is a very important concept in CUDA performance optimization.

Suppose a warp contains 32 threads, and each thread reads a consecutive float:

int i = blockIdx.x * blockDim.x + threadIdx.x;
float value = input[i];

Each thread accesses an adjacent address:

input[0],input[1],input[2],\ldots,input[31]

The hardware can merge these accesses into as few memory transactions as possible.

This is called Coalesced Memory Access.

If the addresses accessed by the threads are highly scattered:

int i = blockIdx.x * blockDim.x + threadIdx.x;
float value = input[i * 1024];

Each thread accesses a different memory region, which may require more memory transactions.

The result is:

  • Effective bandwidth drops.
  • Memory access efficiency decreases.
  • GPU compute units spend more time waiting for data.

2. Bank Conflict: Shared Memory Bank Conflicts

Shared memory is usually divided into multiple banks.

Different banks can service access requests in parallel.

If multiple threads in the same warp access different addresses in the same bank, a bank conflict may occur.

For example, multiple threads simultaneously access different elements in shared memory, but those elements map to the same bank.

The hardware may need to split the access into multiple transactions, increasing access latency.

Note that reads of the same address can be handled by the broadcast mechanism when the conditions are met, and should not be simply equated with a bank conflict.

In matrix transposition, two-dimensional array access, and certain tile layout designs, bank conflicts are a very typical performance problem.

3. Memory Bandwidth: Video Memory Bandwidth

Memory bandwidth determines how much data the GPU can transfer per unit time.

Theoretical bandwidth can be roughly expressed as:

Bandwidth= Memory\ Data\ Rate \times Bus\ Width \div 8

For example:

Suppose the memory data rate is 20 Gbps and the memory bus width is 256 bit:

20\times256\div8=640\ GB/s

This is only the theoretical bandwidth; real applications are also affected by access patterns, cache hits, transaction efficiency, and other hardware factors.

4. Arithmetic Intensity

Arithmetic intensity refers to how many floating-point operations can be completed for each byte of data read or written.

AI=\frac{FLOPs}{Bytes}

Where:

  • FLOPs: the number of floating-point operations.
  • Bytes: the amount of data transferred.

For example:

A kernel completes 1 million floating-point operations and needs to read and write a total of 1 million bytes:

AI=1\ FLOP/Byte

Tasks with low arithmetic intensity are often easily limited by memory bandwidth.

Tasks with higher arithmetic intensity may be limited by the throughput of the compute units.

8. GPU Performance Bottlenecks: The Roofline Model

The Roofline Model is an important tool for analyzing GPU performance.

It estimates the theoretical upper bound of a program's performance using arithmetic intensity, peak compute performance, and memory bandwidth.

Core formula:

P=\min(P_{peak}, AI\times BW)

Where:

  • P: the theoretical upper bound on achievable performance.
  • P_{peak}: peak compute performance.
  • AI: arithmetic intensity.
  • BW: memory bandwidth.

For example:

Suppose a GPU has:

  • Peak compute capability: 100 TFLOPS.
  • Memory bandwidth: 2 TB/s.
  • Kernel arithmetic intensity: 10 FLOP/Byte.

Performance upper bound under the bandwidth limit:

10\times2=20\ TFLOPS

Therefore:

P=\min(100,20)=20\ TFLOPS

Although the GPU's peak compute capability is 100 TFLOPS, the current task is limited by memory bandwidth, so in theory it can only reach about 20 TFLOPS.

1. Compute Bound

Compute Bound means compute capability has become the main bottleneck.

Typical characteristics:

  • A large amount of computation.
  • High data reuse rate.
  • High utilization of compute units.
  • Memory bandwidth is not the main bottleneck.

Optimization directions:

  • Use Tensor Cores.
  • Optimize matrix computations.
  • Increase instruction-level parallelism.
  • Use mixed precision.
  • Reduce unnecessary computation.

2. Memory Bound

Memory Bound means data transfer has become the main bottleneck.

Typical characteristics:

  • A large volume of data reads and writes.
  • Little computation performed per byte.
  • High memory bandwidth utilization.
  • Compute units may be waiting for data.

Optimization directions:

  • Coalesce memory accesses.
  • Reduce global memory reads and writes.
  • Increase data reuse rate.
  • Use Shared Memory.
  • Fuse multiple kernels.
  • Optimize data layout.

3. Roofline Model Illustration

flowchart TD
    A["Analyze GPU Kernel"]
    B["Calculate Arithmetic Intensity"]
    C["Estimate AI × Bandwidth"]
    D["Obtain Peak Compute Performance"]
    E["Compare the Two Performance Upper Bounds"]
    F["Memory Bound"]
    G["Compute Bound"]
    H["Optimize Memory Access"]
    I["Optimize Compute Throughput"]

    A --> B
    B --> C
    C --> E
    D --> E
    E --> F
    E --> G
    F --> H
    G --> I

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#F3EAFE,stroke:#B9A1DB,color:#51416F
    style D fill:#FFF3D9,stroke:#E7C77D,color:#765A22
    style E fill:#F1F5F9,stroke:#CBD5E1,color:#334155
    style F fill:#FFF3D9,stroke:#E7C77D,color:#765A22
    style G fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style I fill:#F3EAFE,stroke:#B9A1DB,color:#51416F

Note: A real Roofline analysis usually also considers the cache hierarchy, peak throughput at different precisions, data transfer paths, and instruction types. You cannot determine the bottleneck of every kernel solely by looking at memory bandwidth and FP32 peak performance.

9. How Does a GPU Execute a CUDA Program?

CUDA is a general-purpose GPU computing platform and programming model provided by NVIDIA.

Developers write kernels in CUDA C/C++, the CPU launches tasks, and the GPU executes parallel computations.

1. Host and Device

CUDA programs typically distinguish between two execution environments:

  • Host: the CPU and its memory.
  • Device: the GPU and its device memory.

The Host is primarily responsible for:

  • Allocating and managing memory.
  • Initializing data.
  • Launching kernels.
  • Managing task execution order.
  • Retrieving computation results.

The Device is primarily responsible for:

  • Executing GPU kernels.
  • Processing data in parallel.
  • Accessing GPU device memory.
  • Performing matrix and vector computations.