Skip to content

m7mdraafat/ParallelismDemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Parallelism Concepts Demo

A comprehensive console application demonstrating various concepts of parallelism in .NET.

Overview

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)

Prerequisities

  • .NET 9.0 SDK
  • Visual Studio Code or any .NET-compatible IDE

Running the Application

cd ParallelismDemo/HPC
dotnet run

The application presents an interactive menu where you can explore each concept with live examples.

Parallelism Fundamentals

What is Parallelism?

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)

Why Use Parallelism?

  1. Performance: Utilize multiple CPU core to complete work faster
  2. Throughput: Process more data in the same time period
  3. Responsiveness: Keep UI responsive while performing backgroung work
  4. Efficiency: Better utilize modern multi-core hardware

Types of Parallelism

1. Data Parallelism

Concept: Perform the same operation on different pieces of data simultaneously. Example: Processing 1000 images - apply the same filter to all images in parallel.

Techniques Demonstrated:

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

2. Task Parallelism

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.

Techniques Demostrated:

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

3. Pipeline Parallelism

Concept: Data flow through multiple processing stages*, with different stages running concurrently. Example: Image processing pipeline: Load -> Resize -> Apply Filter -> Compress -> Save

Techniques Demonstrated:

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

Levels of Parallelism

Level 1: Bit-Level Parallelism

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

Level 2: Instruction-Level Parallelism (ILP)

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

Level 3: Thread-Level Parallelism (TLP)

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

Level 4: Process-Level Parallelism

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)

    Parallel Patterns

    1. Producer-Consumer Pattern

    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)

2. Fork-Join Pattern

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

3. MapReduce Pattern

Concept: Transform data in parallel (map), then aggregate results (reduce).

Phases:

  1. Map: Apply transformation to each data element in parallel
  2. 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

4. Master-Worker Pattern

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

Project Structure

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

Key Takeaways

When to Use Parallelism

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

Common Pitfalls

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

Learning Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages