GPU Architecture and CUDA Programming: From Hardware Fundamentals to Parallel Thinking

    A practical introduction to GPU architecture and CUDA programming. This article explains how SMs, Tensor Cores, CUDA Cores, and warps work together, why hardware-aware design matters for performance, and how beginners can build a solid mental model before writing optimized CUDA code. Learn the key differences between GPU and CPU multithreading and a step-by-step learning path from vector addition to tiled matrix multiplication.

    GPU Architecture and CUDA Programming: From Hardware Fundamentals to Parallel Thinking

    Why Understanding CUDA Fundamentals Matters

    Many beginners jump straight into CUDA APIs and syntax. They can write programs that run, but they often have no idea why performance varies so dramatically between implementations. The core of CUDA programming isn’t syntax—it’s understanding GPU hardware. Only by building a mental model of “how the hardware actually works” can you understand why code should be organized a certain way and why memory access patterns matter so much.

    One caveat: high-performance matrix computation libraries on GPUs (such as the various high-performance linear algebra template libraries) involve massive, heavily optimized codebases that are not beginner-friendly to read directly. That’s why this article prioritizes clarity over strict rigor, sacrificing some edge-case details to help you build an overall framework first.

    The Big Picture: GPU as an Amplified Multi-Core Processor

    At an abstract level, a GPU is essentially a many-core processor, conceptually similar to how CPUs adopt multi-core designs. You can think of a modern data center GPU as a chip containing over a hundred SMs (Streaming Multiprocessors), where each SM roughly corresponds to a group of CPU cores.

    All SMs read data from a two-level memory hierarchy to perform computation:

    • L2 Cache (SRAM): Small in capacity but fast, serving as a buffer between SMs and GPU memory;
    • Global memory (DRAM/HBM): Large capacity and high bandwidth, but with relatively high access latency.

    An important goal of CUDA kernel optimization, therefore, is maximizing L2 cache hit rates and reducing repeated accesses to distant global memory. This resembles CPU program optimization, with one key difference: on CPUs, the compiler and hardware handle most cache optimization for you, while on GPUs, the programmer must actively participate in the process. This is why GPU programming is often called “performance-sensitive programming.”

    Inside the SM: Two Types of Compute Units

    Looking deeper into an SM, you’ll find it’s not a homogeneous pile of cores—it clearly divides into two categories of compute resources:

    1. Tensor Cores

    These are compute units specialized for matrix multiply-accumulate operations. The dominant workload in neural network training and inference is large-scale matrix multiplication. Tensor Cores achieve throughput far beyond general-purpose cores by completing small matrix block computations within a single instruction cycle. Readers interested in this area can explore systolic arrays and similar classic circuit designs, which inspired these specialized compute units.

    2. CUDA Cores

    These include various ALUs such as INT32, FP32, and FP64. Each ALU performs a corresponding scalar arithmetic operation—similar in capability to the general arithmetic a CPU can do.

    3. Organization

    An SM is typically divided into several sub-partitions, each equipped with both Tensor Cores and CUDA Cores, along with on-chip storage such as register files and shared memory. This “specialized + general-purpose” hybrid design is what allows GPUs to support both graphics rendering and deep learning workloads.

    Warps and Scheduling: The Key to Understanding GPU Concurrency

    SMs also contain several warp schedulers—a concept many introductory courses overlook, yet one that’s extremely important.

    What is a warp?

    A warp is a scheduling unit composed of a fixed number of threads (typically 32), conceptually similar to a group of threads on a CPU. But GPU thread counts dwarf CPU thread counts—a single kernel routinely launches hundreds of thousands or even millions of threads. To keep such a massive thread collection running efficiently, GPUs favor highly homogeneous threads: threads within the same warp execute the same instruction, just on different data.

    This is the classic SIMD (Single Instruction, Multiple Data) parallel architecture.

    Why does this matter?

    • If threads within the same warp take different branches (branch divergence), the GPU must execute each branch serially, causing a sharp performance drop;
    • If threads in a warp access non-contiguous memory addresses, accesses cannot be coalesced into efficient large transactions, reducing bandwidth utilization;
    • Warp schedulers hide memory latency by rapidly switching between warps—when one warp is waiting for data, the scheduler immediately switches to another ready warp.

    Once you understand warps, many CUDA best practices (avoiding branch divergence, ensuring coalesced memory access, choosing appropriate block sizes) stop being rules to memorize and become natural consequences of the hardware architecture.

    CUDA Programming vs. CPU Multithreading

    CUDA programming resembles CPU multithreading in mindset, but with several distinctive new characteristics:

    1. Different thread scales: CPU concurrency is typically bounded by core count (a handful to a few dozen), while GPUs target “thousands of concurrent threads, tens of thousands resident”;
    2. Hierarchical organization: Threads are organized in a Thread → Warp → Block → Grid hierarchy. Blocks are assigned to SMs for execution, and threads within a block can cooperate through shared memory;
    3. Explicit memory hierarchy management: Programmers must manually use shared memory and carefully design global memory access patterns rather than relying entirely on hardware caching;
    4. Separating compute from memory access: The essence of a high-performance kernel is “keeping compute units constantly fed with data”—that is, using sufficient parallelism to hide latency.

    Building on these principles, here’s a progressive learning path:

    1. Start with vector addition: Understand Grid/Block/Thread organization and kernel launch syntax;
    2. Implement matrix multiplication: This classic exercise—from naive implementation to shared-memory tiling—gives you hands-on experience with the value of memory access optimization;
    3. Learn performance profiling: Use profiling tools to examine metrics like memory bandwidth utilization and occupancy, letting data drive your optimizations;
    4. Study high-performance tiled matrix multiplication: Understand how techniques like tiling, pipelining, and double buffering approach the hardware’s theoretical limits;
    5. Go further: Dive into advanced features such as Tensor Core programming interfaces, asynchronous copies, and warp-level primitives.

    Conclusion

    The right order for learning CUDA is: understand the hardware first, then write code. Keep three core concepts in mind—SMs are the basic unit of parallelism, Tensor Cores and CUDA Cores each have their own roles, and warps are the atomic unit of scheduling. When every line of your code maps to “how the hardware executes it,” you’ve truly crossed the entry threshold of CUDA programming.