Parallel and Distributed Computing - Module 2
Parallel and Distributed Computing - Module 2
Computing
Module - II
Parallel Algorithm and Design
• A parallel algorithm divides a problem into multiple independent or semi-independent subproblems,
which can be executed simultaneously on different processors.
Goal:
✔ Reduce execution time
✔ Improve speedup and efficiency
✔ Utilize available hardware resources effectively
• Parallel algorithm design focuses on developing algorithms that can efficiently utilize multiple
processors to solve a problem faster than a sequential approach.
• Before designing a parallel algorithm, it is important to understand the basic concepts, issues, and
requirements involved in parallel computation.
Parallel Algorithm and Design
Need for Parallel Algorithm Design
Task:
o A task is the smallest unit of computation that can be executed independently in a parallel
program.
Critical Path:
o The critical path is the longest sequence of dependent tasks in a task dependency graph.
Granularity:
• Granularity refers to the amount of computation performed by a task relative to the
communication and synchronization overhead.
Fine-Grained Decomposition:
• Fine-grained decomposition divides a problem into very small tasks, resulting in high
communication and synchronization overhead.
Coarse-Grained Decomposition:
• Coarse-grained decomposition divides a problem into larger tasks, reducing communication
overhead and improving performance.
Preliminaries
Task
o Tasks are the building blocks of parallel algorithms.
o A task is the smallest unit of computation in a parallel program that can be executed independently on a processor
or core.
o Each task performs a specific part of the overall computation and may run concurrently with other tasks.
1. Independent Tasks
2. Dependent Tasks
o Task dependencies determine execution order and are represented using a Task Dependency Graph (TDG).
Types of dependencies:
Task Granularity
Task Scheduling
o Mechanisms include:
o Shared variables
o Message passing
o Barriers, locks, and semaphores
Task Dependency Graph (TDG)
o A Task Dependency Graph (TDG) is a directed acyclic graph (DAG) that shows how tasks depend on
one another in a parallel program.
o Nodes represent tasks
o Directed edges represent dependencies
o If there is an edge A → B, task B cannot start until task A finishes
o The task corresponding to a node can be executed when all tasks connected to this node by incoming
edges have completed.
Task Dependency Graph (TDG)
Task Dependency Graph for "Finding minimum element
among 23,12,9,30,37,27,8,17"
o Tasks on the critical path must be executed sequentially and cannot be parallelized.
Key Idea
o Even if many processors are available, tasks on the critical path limit the minimum execution time.
o The critical path determines the lower bound on parallel execution time.
A lower bound is the minimum possible time or minimum number of operations required to solve a
problem, regardless of how powerful the system is.
o It represents the minimum possible execution time of a parallel program, even when an unlimited
number of processors are available.
Key Idea
o It does not necessarily mean that tasks execute at the exact same instant, but that their execution
overlaps in time.
o In parallel computing, concurrency is achieved by dividing a program into multiple tasks or subtasks
that can make progress independently and, when sufficient hardware resources are available, execute
simultaneously on multiple processing units such as CPU cores or GPUs.
o Concurrency improves resource utilization and forms the basis for achieving parallelism, which can
lead to better performance and higher efficiency.
Maximum Degree of Concurrency
o The maximum degree of concurrency is the largest number of tasks that can be executed
simultaneously at any point in time in a parallel program, considering task dependencies.
Key Idea
Tasks:
o min(23,12)
o min(9,30)
o min(37,27)
o min(8,17)
o These 4 tasks are independent and can execute at the same time.
Level 2
Tasks:
o min(12,9) o In most cases, the maximum degree of concurrency is less than the total
o min(27,8)
Concurrency = 2 number of tasks due to dependencies among the tasks.
Level 3 o In general, for task dependency graphs that are trees, the maximum degree
Task: of concurrency is always equal to the number of leaves in the tree.
o min(9,8)
Concurrency = 1
Average Degree of Concurrency
•
Average Degree of Concurrency
•
Design Techniques
Decomposition
o Decomposition is the process of dividing a computational problem into smaller tasks so that these
tasks can be executed concurrently on multiple processors.
Types
o Recursive Decomposition
o Data Decomposition
o Exploratory Decomposition
o Speculative Decomposition
Recursive Decomposition
o Recursive decomposition is a general-purpose parallel decomposition technique used to divide a
computational problem into smaller subproblems recursively.
o It is based on the divide-and-conquer principle, where a problem is first divided into independent
subproblems, which are then solved concurrently and finally combined to produce the final result.
Ideology
o Follows the divide-and-conquer approach
o Parallelism increases as recursion unfolds
Example
o Parallel Merge Sort
o Parallel Quick Sort
Recursive Decomposition
Data Decomposition
o Data decomposition is a parallel decomposition technique in which the input data set is divided into
smaller partitions, and the same operation is performed on each partition simultaneously by different
processors.
Key Idea
So, by data decomposition following tasks will be generated to get the product of A and B, and
storing the new matrix in C.
o Tasks are created dynamically at runtime as the computation explores a problem space.
Key Idea
o Some of the speculative tasks may turn out to be unnecessary, but the goal is to reduce waiting time
and improve performance.
Speculative Execution
if (condition) Execute Task A and Task B simultaneously
Task A
Once the condition is resolved:
else
o Keep the correct result
Task B
o Discard the other result
● As soon as the condition has been evaluated, only the results of one task are used, all others are thrown
away.
● This decomposition technique is quite wasteful on resources and seldom used.
Speculative Decomposition
Disadvantages
Characteristics of Speculative Decomposition
Sorting,
Recursive Problem structure Yes
divide-and-conquer
Control-flow-heavy
Speculative Prediction Partially
programs
Mapping Techniques for Load balancing
Mapping Techniques for Load balancing
Mapping
o The main goal of mapping is load balancing—ensuring that all processors get roughly equal work so that no
processor is idle while others are overloaded.
Load Balancing
Objectives of Mapping Techniques
Load balancing ensures that:
A good mapping technique aims to:
o All processors are equally utilized o Balance workload evenly
o Execution time is minimized o Minimize communication cost
o Static Mapping
o Dynamic Mapping
2) Cyclic Distribution
3) Block–Cyclic Distribution
When It Fails
o If tasks take unequal time
o If workload is irregular
o If some blocks contain heavier computations
o Instead of giving each processor one big block, tasks are distributed one-by-one in a cyclic order.
Block Cyclic Distribution
o Block–Cyclic distribution is a static mapping technique that combines both:
o Block distribution (contiguous chunks)
o Cyclic distribution (round-robin assignment)
o Instead of assigning one large block, we assign small blocks cyclically to processors.
o Number of processors = 4
o Block size = 2
Advantages
✔ Good balance between locality and load balancing
✔ Suitable for large-scale HPC systems
✔ Handles moderate irregularity well
Disadvantages
o Slightly more complex
o Requires choosing optimal block size
Block Cyclic Distribution
Feature Block Distribution Cyclic Distribution Block–Cyclic Distribution
Load Balancing Poor if workload uneven Better than block Very good
Large scientific/matrix
Best For Uniform workloads Slightly irregular workloads
computations
Dynamic Mapping
• Dynamic mapping is a mapping technique where tasks are assigned to processors during runtime, rather than
before execution begins.
• Tasks are allocated based on processor availability, allowing better load balancing.
Example
10 tasks
3 processors
Tasks take different execution times
Processors:
• Work Stealing
• Idle processors steal tasks from busy processors.
• It is called work sharing because tasks are shared among processors from a common pool.
• 8 tasks (T1–T8)
• 3 processors
Time P1 P2 P3
t1 T1 T2 T3
t2 T4 T5 T6
t3 T7 T8 Idle
• Processors keep taking tasks from the pool until empty. This prevents one processor from getting stuck with heavy tasks.
Work Sharing Mapping
Where Work Sharing is Used
• Parallel loops
• Web servers
Advantages
Disadvantages
• Good load balancing
o Central task queue may become bottleneck
• Keeps processors busy
o Requires synchronization (mutex/locks)
• Simple dynamic scheduling o Communication overhead
• Unlike work sharing (which uses a global queue), work stealing is decentralized, making it more scalable.
• Work stealing is a dynamic mapping technique in which each processor maintains a local task queue, and when a processor
becomes idle, it steals tasks from the queue of another busy processor.
• Instead of using a global queue (which may become a bottleneck), work stealing distributes queues locally and balances
workload efficiently.
Work Stealing Mapping
Working Mechanism
P1 Queue: T1 T2 T3
P2 Queue: T4 T5
P3 Queue: T6 T7 T8 T9
If P2 becomes idle →
P2 steals a task from P3
Work Stealing Mapping
Where Work Stealing is Used Why Work Stealing is Efficient
Advantages Disadvantages
• Reduced contention
Work Sharing Vs Stealing
Feature Work Sharing Work Stealing
Idle processors obtain tasks from a shared global task Idle processors steal tasks from the local queue
Definition
queue. of another busy processor.
Synchronization Overhead High (due to shared queue locking) Lower (local queues reduce contention)
Bottleneck Possibility Yes – global queue may become bottleneck No central bottleneck
Work Sharing Vs Stealing
Feature Work Sharing Work Stealing
Communication Overhead Moderate (accessing global queue) Occurs only during stealing
Task Locality May be lower due to global sharing Better locality (tasks executed locally first)
Example Systems OpenMP dynamic scheduling Cilk, Intel TBB, Java ForkJoinPool
Instead of assigning:
• Guided mapping assigns large chunks initially and then reduces chunk size gradually as execution progresses.
• This helps reduce scheduling overhead while maintaining good load balancing
Processor Iterations
P1 1–25
P2 26–50
P3 51–75
P4 76–100
Guided (Adaptive) Mapping
• Total Work: 100 Iterations
Stage 1 (Large
Chunks)--------------------------------------------P1:
[=========================]
P2: [=========================]
P3: [=========================]
P4: [=========================]
Disadvantages
Chunk Size Fixed Usually small or single task Decreasing size (large → small)
Processor Utilization May cause idle time High utilization High utilization
Suitable Workload Type Regular & predictable Irregular & unpredictable Moderately irregular
Example Strategy Block distribution Work sharing / Work stealing OpenMP schedule(guided)
o Synchronous parallel processing is a parallel execution model in which multiple processors perform operations
simultaneously and synchronize at predefined points before continuing.
o All processors must reach a synchronization point (barrier) before moving to the next step
o All processors work together and wait for each other at specific points.
Synchronous parallel processing
Types
Basic Idea
Components:
o There is a central clock
oGlobal Clock Generator
o All processing elements (PEs) receive the same clock pulse oControl Unit
oMultiple Processing Elements
o Each clock tick triggers one computation step
oShared or Distributed Memory
o No processor can move ahead independently
Clock pulses coordinate execution.
Global Clock Based System
How It Works
Clock Cycle 1:
Clock Cycle 2:
Clock Cycle 3:
Phase 1:
Time →------------------------------------------------
Each processor computes partial multiplication. P1: Compute | Wait | Compute | Wait
Barrier: P2: Compute | Wait | Compute | Wait
P3: Compute | Wait | Compute | Wait
All processors must finish before combining results.
P4: Compute | Wait | Compute | Wait
Phase 2:
↑ Synchronization Barrier
Processors combine computed values.
Disadvantages
Advantages
o Slow processors delay fast ones
o Simple control flow
o Idle time at synchronization points
o Easier to implement
o Poor performance for irregular workloads
o Deterministic results
Instead of doing:
A[0] + B[0]
A[1] + B[1]
A[2] + B[2]
A[3] + B[3]
SIMD does:
B = [1, 3, 5, 7]
In SIMD:
Key Characteristics of SIMD
All processors execute: ADD A[i], B[i] o Synchronous execution
o Centralized control unit
Processor Operation Result
o Lock-step execution
PE1 2+1 3
o Efficient for vector operations
PE2 4+3 7
o Best for matrix, graphics, scientific computations
PE3 6+5 11
PE4 8+7 15
Programming Principles OF SIMD
1. Data Parallelism 3. Memory Alignment
SIMD uses:
All PEs execute instruction in same clock cycle. Cycle 4 Store result
GPUs:
Thousands of cores executing same shader
instruction.