Skip to content
Phuc's Blog
Go back

Introduction to Parallel Programming

Edit page
📌Series:Parallel Programming
Part 1 of 4
View all articles in this series

This blog will introduce you to Parallel Programming.

Introduction to Parallel Programming

Table of contents

Open Table of contents

What is Parallel Programming?

Many applications have demanded more execution speed and resources:

40 Years of Microprocessor Trend Data

As you can see from the trend chart above, the trade-off between ILP (Instruction-Level Parallelism) scaling and power consumption is no longer linear. Power consumption is a major concern nowadays—modern AI models consume a huge amount of electricity, which can cost even more than the hardware itself.

💬 “Accelerated computing is not all about performance, it’s all about energy — it’s about performance per watt.”

In contrast, the transistor count continues to scale up exponentially according to Moore’s Law, which means we can use more transistors to perform more instructions at the same time. This is the main idea behind parallel programming.

Moore's Law Transistor Count Growth

CPU vs GPU

TypeCore StructurePrimary Focus
CPU - MulticoreHave a few cores, but each core is very powerful and complexFocus on the execution speed (low latency)
GPU - Many coreHave many many cores, but each core is weak and simpleFocus on the throughput (massive parallelism)

CPU: Latency-Oriented Design

CPU High-Level Diagram

CPU Architecture Diagram

  1. Large Caches:
    • Convert long-latency memory (DRAM) access into short-latency cache access (L1/L2/L3).
  2. Powerful ALUs:
    • Reduce operation latency for individual instructions, though at the cost of increased chip area and power consumption.
  3. Sophisticated Control:
    • Feature Branch Prediction.

GPU: Throughput-Oriented Design

GPU High-Level Diagram

GPU Architecture Diagram

  1. Small Caches:
    • Caches are kept small and are used primarily to boost memory bandwidth rather than mask long DRAM latencies.
  2. Massive Array of Simple ALUs:
    • Maximize compute density by packing thousands of small, simple ALUs designed to execute data-parallel operations concurrently.
  3. Simple Control & Latency Hiding:
    • Rely on simple control logic without complex branch prediction.

Strategies of Using Both CPU and GPU

Note
  • CPUs for sequential parts where latency hurts
  • GPUs for parallel parts where throughput matters

CPU Host and GPU Device Architecture

Tip

CUDA (Compute Unified Device Architecture) allows us to write programs taking full advantage of both CPU and GPU (NVIDIA).

Compiling a CUDA Program (NVCC)

A CUDA source file (.cu) contains a mix of standard Host code (C/C++ intended for the CPU) and Device code (CUDA kernels intended for the GPU).

To handle this heterogeneous code, NVIDIA provides the NVCC (NVIDIA CUDA Compiler) driver:

NVCC Compilation Workflow Figure: The NVCC compilation flow separates host and device code to run across heterogeneous computing platforms.

  1. Code Separation: NVCC parses the integrated CUDA program and splits it into two streams:
    • Host Code: Forwarded to a standard host C/C++ compiler (such as GCC, Clang, or MSVC) and linked into standard CPU object code.
    • Device Code: Compiled into an intermediate virtual assembly format called PTX (Parallel Thread Execution) or directly into native GPU machine code (SASS).
  2. Just-In-Time (JIT) Compilation: At runtime, the GPU driver’s Device JIT Compiler can further compile PTX into device-specific binary code for the target GPU architecture.
  3. Execution on Heterogeneous Platforms: The resulting binary runs seamlessly on heterogeneous systems, executing sequential control tasks on the CPU while offloading parallel kernels to the GPU.

Common Languages & Frameworks for Parallel Programming 🛠️

To build parallel applications across CPUs and GPUs, various programming languages provide native extensions, GPU bindings, and specialized frameworks:

Language / DomainPopular Frameworks & LibrariesCommon Use Case
C / C++CUDA C/C++, OpenMP, OpenCL, SYCLSystem programming, high-performance engines, custom GPU kernels
PythonPyTorch, CuPy, PyCUDA, NumbaDeep learning, AI models, GPU-accelerated scientific data analysis
FortranCUDA Fortran, OpenACCSupercomputing, physical simulations, High-Performance Computing (HPC)
Numerical AnalyticsMATLAB (Parallel Computing Toolbox)Matrix calculations, signal processing, engineering modeling

Key Challenges in Parallel Programming ⚠️

While parallel computing offers massive performance gains, it also introduces unique engineering challenges:


Edit page
Share this post:

Previous Post
What is a Neural Network?
Next Post
Getting Started with PlatformIO & ESP32

Comments