๐ Table of Contents
๐ Overview
Parallel and concurrent programming unlock the full potential of modern hardware, enabling applications to perform multiple tasks simultaneously. While these concepts might seem intimidating at first, understanding their fundamentals is crucial for building efficient, responsive software that can handle today's computational demands.
What you'll learn:
- ๐ Design and implement concurrent systems
- โก Write high-performance parallel algorithms
- ๐ง Master multithreading and multiprocessing
- ๐ Build distributed computing applications
- ๐ Analyze and optimize performance
- ๐ฎ Leverage GPU computing capabilities
๐ง Core Concepts
Understanding parallel and concurrent programming begins with grasping the distinction between these approaches and sequential programming:
| Concept |
Description |
Key Characteristic |
| Sequential Programming |
Executes instructions one after another in a single thread |
Single path of execution |
| Concurrent Programming |
Manages multiple tasks that may overlap in time |
Dealing with lots of things at once |
| Parallel Programming |
Executes multiple tasks simultaneously across multiple cores |
Doing lots of things at once |
๐ก Key Insight: Concurrency is about structure (dealing with multiple tasks), while parallelism is about execution (running multiple tasks simultaneously).
Quick Example
# Sequential: Tasks run one after another
task1()
task2()
task3()
# Concurrent: Tasks can overlap in time
async def concurrent_execution():
await asyncio.gather(task1(), task2(), task3())
# Parallel: Tasks run simultaneously on different cores
with multiprocessing.Pool() as pool:
pool.map(process_task, [task1, task2, task3])
๐ Learning Path
Progress through these topics to build a comprehensive understanding of parallel and concurrent programming:
| # |
Topic |
Focus Area |
Difficulty |
Resource |
| 1๏ธโฃ |
Foundations |
Basic terminology, processes vs threads, synchronization primitives |
๐ข Beginner |
Basic Terminology |
| 2๏ธโฃ |
Multithreading |
Thread creation, management, synchronization, and safety patterns |
๐ข Beginner |
Multithreading |
| 3๏ธโฃ |
Multiprocessing |
Process management, inter-process communication, shared memory |
๐ก Intermediate |
Multiprocessing |
| 4๏ธโฃ |
Async Programming |
Event loops, async/await patterns, non-blocking I/O operations |
๐ก Intermediate |
Asynchronous Programming |
| 5๏ธโฃ |
Distributed Computing |
Message Passing Interface (MPI), cluster computing, communication patterns |
๐ด Advanced |
MPI Programming |
| 6๏ธโฃ |
Hardware Architecture |
Multi-core systems, memory hierarchies, GPU computing, performance considerations |
๐ก Intermediate |
Hardware Foundations |
| 7๏ธโฃ |
Performance Evaluation |
Benchmarking, profiling, Amdahl's Law, speedup analysis |
๐ด Advanced |
Evaluating Performance |
| 8๏ธโฃ |
Parallel Design |
Design patterns, algorithms, load balancing, scalability |
๐ด Advanced |
Designing Parallel Programs |
| 9๏ธโฃ |
GPU Programming |
CUDA, OpenCL, GPU architectures, kernel optimization |
๐ด Advanced |
GPU Programming |
๐ฏ Recommended Learning Sequence
graph LR
A[Foundations] --> B[Multithreading]
A --> C[Multiprocessing]
B --> D[Async Programming]
C --> D
D --> E[Distributed Computing]
A --> F[Hardware Architecture]
F --> G[Performance Evaluation]
E --> H[Parallel Design]
G --> H
F --> I[GPU Programming]
๐ป Code Examples
This repository includes practical implementations in multiple programming languages:
๐ Repository Structure
src/
โโโ ๐ python/ # Python implementations
โ โโโ mpi/ # MPI examples (14+ examples)
โ โโโ asynchrony/ # Async/await patterns
โ โโโ threads/ # Threading examples
โ โโโ processes/ # Multiprocessing examples
โ
โโโ โ๏ธ cpp/ # C++ implementations
โ โโโ multithreading/ # C++20 jthread, atomics, standard synchronization
โ โ โโโ legacy/ # Older implementations for comparison
โ โโโ multiprocessing/ # Scoped POSIX resources, spawn, persistent workers
โ โโโ asynchrony/ # Futures, bounded async I/O, coroutine mechanics
โ โโโ mpi/ # C++20 ownership around the MPI C interface
โ โโโ common/ # Shared ownership, queue, and I/O helpers
โ
โโโ ๐จ js/ # JavaScript/Node.js
โโโ asynchrony/ # Promises, async/await
โโโ multithreading/ # Node.js worker threads
โโโ multiprocessing/ # Child processes, cluster
๐ฅ Featured Examples
๐ MPI - Parallel Matrix Multiplication
Distributed matrix multiplication using Message Passing Interface:
# Example: Parallel computation across multiple processes
mpiexec -n 4 python src/python/mpi/parallel_matrix_multiplication.py
โก Async - Parallel Web Fetching
Asynchronous HTTP requests with modern async/await:
# Example: Fetch multiple URLs concurrently
python src/python/asynchrony/fetch_parallel.py
๐งต Multithreading - Producer-Consumer
Classic synchronization pattern with thread-safe queues:
# Example: Thread coordination with locks and conditions
python src/python/threads/producer_consumer.py
๐ Quick Start
# Clone the repository
git clone https://github.com/djeada/Parallel-And-Concurrent-Programming.git
cd Parallel-And-Concurrent-Programming
# Run Python examples
python src/python/asynchrony/async_generator.py
# Run C++ examples (C++20 compiler and standard library)
g++ -std=c++20 -pthread src/cpp/multithreading/atomic_wait.cpp -o thread_demo
./thread_demo
# Run JavaScript examples
node src/js/asynchrony/03_future_callback.js
C++: modern approaches first
Follow the C++ guide and problem-to-solution table. The main
path uses C++20: std::jthread for scoped workers, stop tokens for cooperative
cancellation, scoped locks, standard semaphores/latches/barriers, and parallel
algorithms for independent data processing. Futures and mutexes remain useful
when they fit the problem.
Older thread wrappers, manual std::thread lifecycle examples, pthreads, and
hand-built synchronization primitives live in
src/cpp/multithreading/legacy/ for explicit
comparison. POSIX process and MPI examples are separate platform/distributed
topics with scoped resource owners and checked failures. Process workers are
reused and return results over IPC; MPI matrix multiplication handles uneven
partitions. HTTP transfers use bounded libcurl asynchronous I/O, and the TCP
example owns nonblocking connections. Their notes describe the same approaches.
Coroutine mechanics examples are labeled separately from asynchronous I/O.
C++ and Node.js examples that deliberately demonstrate unsafe or inefficient
patterns use _antipattern (or _antipatterns for multiple pitfalls) before their
filename extension.
Their comments explain the intentional problem and point to safer alternatives.
C++ MPI examples require an MPI toolchain; C++ HTTP examples require libcurl development
headers and linking with -lcurl. Retain src/cpp/common/ when copying the examples.
๐ฏ Interactive Quizzes
Test your understanding with a quiz for each topic. Every question has one correct answer and a short explanation, and each quiz links the notes it covers. Take them interactively, with shuffled options, scoring and explanations, on the Technical Quizzes page.
| # |
Topic |
Covers |
Questions |
Link |
| 1 |
Basic Terminology |
Processes, threads, concurrency vs parallelism, memory models |
41 |
Quiz 1 |
| 2 |
Multithreading |
Locks, conditions, semaphores, deadlocks, race conditions |
63 |
Quiz 2 |
| 3 |
Multiprocessing |
Process pools, IPC, shared memory, start methods |
52 |
Quiz 3 |
| 4 |
Asynchronous Programming |
Event loops, coroutines, futures, cancellation |
51 |
Quiz 4 |
| 5 |
Distributed Computing and MPI |
Message passing, collectives, consistency |
45 |
Quiz 5 |
| 6 |
Hardware and Architecture |
Caches, false sharing, NUMA, GPUs |
41 |
Quiz 6 |
| 7 |
Performance Evaluation |
Amdahl, Gustafson, scaling, profiling |
45 |
Quiz 7 |
| 8 |
Parallel Design |
Decomposition, load balancing, communication |
44 |
Quiz 8 |
| 9 |
GPU Programming |
SIMT, warps, memory hierarchy, coalescing, streams |
45 |
Quiz 9 |
๐ Performance Analysis
Explore performance evaluation tools and scripts:
Available Analysis Scripts
| Script |
Description |
Concepts |
| ๐ฏ Amdahl's Law |
Calculate theoretical speedup limits |
Maximum speedup with parallel portions |
| ๐ฏ Gustafson's Law |
Scaled speedup analysis |
Speedup with increasing problem size |