A comprehensive console application demonstrating various concepts of parallelism in .NET.
This application provides hands-on examples of:
- 3 Types of Parallelism: Data, Task, and Pipeline
- 4 Levels of Parallelism: Bit-Level, Instruction-Level, Thread-Level, and Process-Level
- 4 Parallel Patterns: Producer-Consumer, Fork-Join, MapReduce, and Master-Worker
- Performance Metrics: Sequential vs Parallel comparisons with metrics (Speedup and efficiency)
- .NET 9.0 SDK
- Visual Studio Code or any .NET-compatible IDE
cd ParallelismDemo/HPC
dotnet runThe application presents an interactive menu where you can explore each concept with live examples.
Parallelism is the simultaneous execution of multiple computations. It differs from Concurrency:
- Concurrency: Managing multiple tasks that can run in overlapping time periods (not necessairly simultaneously)
- Parallelism: Actually executing multiple tasks at the exact same time (requires multiple CPU cores)
- Performance: Utilize multiple CPU core to complete work faster
- Throughput: Process more data in the same time period
- Responsiveness: Keep UI responsive while performing backgroung work
- Efficiency: Better utilize modern multi-core hardware
Concept: Perform the same operation on different pieces of data simultaneously. Example: Processing 1000 images - apply the same filter to all images in parallel.
Parallel.For: Process array indices in parallel Parallel.ForEach: Process collection items in parallel PLINQ (Parallel LINQ): Parallel queries on data
Use Cases:
- Image/Video processing
- Data transformation
- Batch processing
Concept: Perform different operations concurrently. Each task may do completely different work. Example: While downloading a file, process user input, and update the UI simultaneously.
Parallel.Invoke: Execute different methods in parallel. *Task-based Operations: Async operations
Use Cases:
- Multiple I/O operations (database + web services + file)
- Different computational tasks
- Background processing
Concept: Data flow through multiple processing stages*, with different stages running concurrently. Example: Image processing pipeline: Load -> Resize -> Apply Filter -> Compress -> Save
TPL Dataflow: Multi-stage data processing
Channel-based Pipeline: Using System.Threading.Channels
Benefits:
- Different stages can run simultaneously
- Stage 1 processing item N while Stage 2 processing item N-1
- Good for streaming/continous data processing
Use Cases:
- Video/audio processing
- ETL (Extract, Transform, Load) operations
- Data streaming pipelines
- Manufacturing simulations
Concept: Process multiple bits or data elements in a single CPU instruction.
Example: Process 8 integers simultaneously using Vector<int> (on 256-bit CPU)
Technology: SIMD (Single Instruction, Multiple Data)
Use Cases:
- Used in graphics, scientific computing, AI/ML
Concept: CPU executes multiple instructions simultaneously using internal parallelism. Techniques used internally:
- Pipelining: Breaking instruction execution into stages
- Superscalar: Multiple execution units working in parallel
- Out-of-order execution: CPU reorders instructions for efficiency
- Branch prediction: Speculative execution
Key Points:
- Handled automatically by CPU
- Modern CPU can execute 4-6 instructions per clock cycle
- Write code with independent operations when possible
- Compiler and CPU optimize this
Concept: Multiple threads executing concurrenctly on multiple CPU cores. This is what most developers work with! Example: Running 4 worker threads on a 4-core CPU
Key Points
- Each thread can run on a different CPU core
- Thread pool manages threads efficiently
- Watch for thread safety issues (race conditions)
- Overhead: thread creation, context swithching, synchronization
Performance Metrics
- Speedup: Sequential Time / Parallel Time
- Efficiency: Speedup / Number of Cores
Concept: Multiple separate processes executing independently, possible on different machines. Characteristics:
-
Each process has its own memory space
-
Processes are isolated for each other
-
Can run on different machines (distributed computing)
-
Communication achived via IPC (Inter-Process Communication)
Examples:
- Distributed computing (Hadoop, Spark)
- Microservices architecture
- Containerized workloads (Docker/Kubernetes)
Concept: Some threads produce work items, other threads consume and process them. Components:
- Producers: Generate work items
- Consumers: Process work items
- Queue: Thread-safe buffer (
BlockingCollection) Example: Web scrapper (producers fetch URLs, consumers process pages)
Benefits:
- Decouples production from consumption
- Handles different production/consumption rates
- Easy to scale (add more producers/consumers)
NOTE: Fork = Split Concept: Split work into subtasks (fork), process in parallel, then combine results (join). Steps:
- Fork: Divide large task into smaller independent subtasks
- Execute: Process subtasks in parallel
- Join: Combine/aggregate results from all subtasks
Example: Calculate sum of large array by splitting into chunks (groups) Benefits:
- Natural for divide-and-conquer algorithms
- Scales with number of cores
- Simple to understand and implement
Concept: Transform data in parallel (map), then aggregate results (reduce).
Phases:
- Map: Apply transformation to each data element in parallel
- Reduce: Aggregate/combine all transformed results
Example Scenario: Count word occurrences in multiple documents
- Map: Count words in each document (parallel)
- Reduce: Combine counts from all documents
Benefits:
- Popularized by Google (Hadoop, Spark)
- Scales to massive datasets
- Natural for data analytics
Concept: Master thread distributes work to worker threads and collects results.
Components:
- Master: Creates work items, distributes to workers, collects results
- Workers: Process work items independently
- Work Queue: Holds pending work items
Example Scenario: Render farm (master assigns frames to workers)
Benefits:
- Central coordination
- Load balancing
- Easy monitoring
ParallelismDemo/
├── Program.cs # Main entry point with menu system
├── Examples/
│ ├── DataParallelismExamples.cs # Data parallelism demonstrations
│ ├── TaskParallelismExamples.cs # Task parallelism demonstrations
│ ├── PipelineParallelismExamples.cs # Pipeline parallelism demonstrations
│ ├── LevelsOfParallelismExamples.cs # Different parallelism levels
│ ├── ParallelPatterns.cs # Common parallel patterns
│ └── PerformanceComparison.cs # Performance benchmarks
└── README.md
Good Candidates:
- CPU-intensive computations
- Large datasets with independent operations
- Multiple independent I/O operations
- Batch processing Bad Candidates:
- Small workloads (overhead > benefits)
- High Sequential algorithms
- Operations with heavy synchronization
- Already fast operations
Race Conditions: Multiple threads accessing shared data Deadlocks: Threads waiting for each other indefinitely Execussive Parallelism: Too many threads causing overhead False Sharing: Cache line contention Premature Optimization: Parallelizing before measuring need