Designing Parallel Programs in Computing
Designing Parallel Programs in Computing
1
Agenda
• Automatic vs. Manual Parallelization
• Understand the Problem and the Program
• Partitioning
• Communications
• Synchronization
• Data Dependencies
• Load Balancing
• Granularity
• I/O
• Performance Analysis and Tuning
• Limits and Costs of Parallel Programming
2
Automatic vs. Manual Parallelization
• Designing and developing parallel programs has characteristically been a very
manual process. The programmer is typically responsible for both identifying
and actually implementing parallelism.
• Various tools have been available over the years to assist the programmer with
converting serial programs into parallel programs. The most common type of
tool used to automatically parallelize a serial program is a parallelizing
compiler or pre-processor.
3
Automatic vs. Manual Parallelization
• A parallelizing compiler generally works in two different ways:
• Fully Automatic
• The compiler analyzes the source code and identifies opportunities for parallelism.
• The analysis includes identifying inhibitors to parallelism and possibly a cost weighting on
whether or not the parallelism would actually improve performance.
• Loops (do, for) loops are the most frequent target for automatic parallelization.
• Programmer Directed
• Using "compiler directives" or possibly compiler flags, the programmer explicitly tells the
compiler how to parallelize the code.
• May be able to be used in conjunction with some degree of automatic parallelization also.
• The most common compiler generated parallelization is done using on-node shared
memory and threads (such as OpenMP).
4
Automatic vs. Manual Parallelization
• If you are beginning with an existing serial code and have time or budget
constraints, then automatic parallelization may be the answer. However,
there are several important caveats/limitations that apply to automatic
parallelization:
• Wrong results may be produced
• Performance may actually degrade
• Much less flexible than manual parallelization
• Limited to a subset (mostly loops) of code
• May actually not parallelize code if the analysis suggests there are inhibitors, or the
code is too complex
• Most automatic parallelization tools are for Fortran
6
Understand the Problem and the Program
• Example of Parallelizable Problem: Calculate the potential energy for each of
several thousand independent conformations of a molecule. When done, find
the minimum energy conformation.
• Solution: This problem is able to be solved in parallel. Each of the molecular
conformations is independently determinable. The calculation of the minimum
energy conformation is also a parallelizable problem.
• Example of Non-Parallelizable Problem: Calculation of the Fibonacci series
(1,1,2,3,5,8,13,21,...) by use of the formula:
• Solution: This is a non-parallelizable problem because the calculation of the
Fibonacci sequence as shown would entail dependent calculations rather than
independent ones. The calculation of the value uses those of both and . These 7
three terms cannot be calculated independently and therefore, not in parallel.
Understand the Problem and the Program
• Identify the program’s hotspots (These are parts of the code where most of the work or
computation happens—the parts that consume the most CPU time).
• Know where most of the real work is being done. The majority of scientific and technical
programs usually accomplish most of their work in a few places.
• Profilers and performance analysis tools can help here.
• Focus on parallelizing the hotspots and ignore those sections of the program that account
for little CPU usage.
• Identify bottlenecks in the program
• These are areas that slow down the program or prevent it from running efficiently in
parallel. For example, I/O is usually something that slows a program down.
• May be possible to restructure the program or use a different algorithm to reduce or
eliminate unnecessary slow areas.
• Identify inhibitors to parallelism. One common class of inhibitor is data dependence,
as demonstrated by the Fibonacci sequence above. 8
• Investigate other algorithms if possible. This may be the single most important
consideration when designing a parallel application.
Partitioning
• One of the first steps in designing a parallel program is to break the problem
into discrete "chunks" of work that can be distributed to multiple tasks. This is
known as decomposition or partitioning.
• There are two basic ways to partition computational work among parallel tasks:
• Domain Decomposition
• Functional Decomposition
9
Partitioning
• Domain Decomposition: In this type of partitioning, the data associated with a
problem is decomposed. Each parallel task then works on a portion of the data.
10
Partitioning
• Functional Decomposition: In this approach, the focus is on the computation
that is to be performed rather than on the data manipulated by the computation.
The problem is decomposed according to the work that must be done. Each
task then performs a portion of the overall work.
• Functional decomposition lends itself well to problems that can be split into
different tasks. For example:
• Ecosystem Modeling
• Signal Processing
• Climate Modeling
11
Partitioning
• Ecosystem Modeling Example: Each program calculates the population of a
given group, where each group's growth depends on that of its neighbors. As
time progresses, each process calculates its current state, then exchanges
information with the neighbor populations. All tasks then progress to calculate
the state at the next time step.
12
Partitioning
• Signal Processing Example: An audio signal data set is passed through four
distinct computational filters. Each filter is a separate process. The first segment
of data must pass through the first filter before progressing to the second. When
it does, the second segment of data passes through the first filter. By the time,
the fourth segment of data is in the first filter, all four tasks are busy.
13
Partitioning
14
Partitioning
• Climate Modeling Example: Each model
component can be thought of as a
separate task. Arrows represent
exchanges of data between components
during computation: the atmosphere
model generates wind velocity data that
are used by the ocean model, the ocean
model generates sea surface temperature
data that are used by the atmosphere
model, and so on.
• Combining these two types of problem
decomposition is common and natural.
15
Who Needs Communications?
• The need for communications between tasks depends upon your problem
17
Factors to Consider (1)
• There are several important factors to consider when designing your program's
inter-task communications
• Cost of Communications:
• Inter-task communication virtually always implies overhead.
• Machine cycles and resources that could be used for computation are instead used
to package and transmit data.
• Communications frequently require some type of synchronization between tasks,
which can result in tasks spending time "waiting" instead of doing work.
• Competing communication traffic can saturate the available network bandwidth,
further aggravating performance problems.
18
Factors to Consider (2)
• Latency vs. Bandwidth:
• Latency is the time it takes to send a minimal (0 byte) message from point A to point
B. Commonly expressed as microseconds.
• Bandwidth is the amount of data that can be communicated per unit of time.
Commonly expressed as megabytes/sec.
• Sending many small messages can cause latency to dominate communication
overheads. Often it is more efficient to package small messages into a larger
message, thus increasing the effective communications bandwidth.
• Visibility of Communications:
• With the Message Passing Model, communications are explicit and generally quite
visible and under the control of the programmer.
• With the Data Parallel Model, communications often occur transparently to the
programmer, particularly on distributed memory architectures. The programmer may 19
not even be able to know exactly how inter-task communications are being
accomplished.
Factors to Consider (3)
• Synchronous vs. Asynchronous Communications:
• Synchronous communications (Its like two people having a phone call) require some
type of "handshaking" between tasks that are sharing data. This can be explicitly
structured in code by the programmer, or it may happen at a lower-level unknown to
the programmer.
• Synchronous communications are often referred to as blocking communications
since other work must wait until the communications have completed.
• Asynchronous communications (It’s like sending a text message) allow tasks to
transfer data independently from one another. For example, task 1 can prepare and
send a message to task 2, and then immediately begin doing other work. When task
2 actually receives the data doesn't matter.
• Asynchronous communications are often referred to as non-blocking
communications since other work can be done while the communications are taking
place. 20
• Interleaving computation with communication is the single greatest benefit for using
asynchronous communications.
Factors to Consider (4)
• Scope of Communications:
• Knowing which tasks must communicate with each other is critical during the design
stage of a parallel code. Both of the two scopings described below can be
implemented synchronously or asynchronously.
• Point-to-point – involves two tasks with one task acting as the sender/producer of
data, and the other acting as the receiver/consumer.
• Collective – involves data sharing between more than two tasks, which are often
specified as being members in a common group, or collective. Some common
variations (there are more):
21
Factors to Consider (5)
• Efficiency of Communications:
• Oftentimes, the programmer has choices that can affect communications
performance. Only a few are mentioned here.
• Which implementation for a given model should be used? Using the Message
Passing Model as an example, one MPI implementation may be faster on a given
hardware platform than another.
• What type of communication operations should be used? As mentioned previously,
asynchronous communication operations can improve overall program performance.
• Network media – different platforms use different networks. Some networks perform
better than others. Choosing a platform with a faster network may be an option.
22
Factors to Consider (6)
• Overhead and Complexity:
23
Factors to Consider (7)
• Finally, realize that this is only a partial list of things to consider!!!
24
Types of Synchronization
• Barrier:
• Usually implies that all tasks are involved.
• Each task performs its work until it reaches the barrier. It then stops, or "blocks".
• When the last task reaches the barrier, all tasks are synchronized and released.
• Locks/Semaphore:
• Can involve any number of tasks
• Typically used to “protect” access to the critical section of code. Only one task at a time may
use (own) the lock / semaphore / flag.
• The first task to acquire the lock "sets" it and then safely access the critical section.
• Other tasks attempting to acquire the lock must wait until the task owning the lock releases it.
• Can be blocking or non-blocking
• Synchronization Communication Operations:
• Involves only those tasks executing a communication operation
• Tasks performing a communication operation between them require some form of coordination. 25
For example, before a task can perform a send operation, it must first receive an
acknowledgment from the receiving task that it is OK to send.
Data Dependencies
• A dependence exists between
program statements when the order
of statement execution affects the
results of the program.
• The value of A(J-1) must be computed before the value of A(J); therefore
A(J) exhibits a data dependency on A(J-1). Parallelism is inhibited.
• If Task 2 has A(J) and task 1 has A(J-1), computing the correct value of
A(J) necessitates:
• Distributed memory architecture - task 2 must obtain the value of A(J-1) from task
1 after task 1 finishes its computation
• Shared memory architecture - task 2 must read A(J-1) after task 1 updates it
27
Example 2: Loop Independent Data Dependence
task 1 task 2
------ ------
X = 2 X = 4
. .
. .
Y = X**2 Y = X**3
• Although all data dependencies are important to identify when designing parallel 28
programs, loop carried dependencies are particularly important since loops are
possibly the most common target of parallelization efforts.
How to Handle Data Dependencies?
• Distributed memory architectures – communicate required data at
synchronization points.
29
Load Balancing
• Load balancing refers to the practice of distributing work among tasks so that
all tasks are kept busy all the time. It can be considered a minimization of task
idle time.
• Load balancing is important to parallel programs for performance reasons. For
example, if all tasks are subject to a barrier synchronization point, the slowest
task will determine the overall performance.
30
How to Achieve Load Balance? (1)
• Equally partition the work each task receives
• For array/matrix operations where each task performs similar work, evenly distribute
the data set among the tasks.
• For loop iterations where the work done in each iteration is similar, evenly distribute
the iterations across the tasks.
• If a heterogeneous mix of machines with varying performance characteristics are
being used, be sure to use some type of performance analysis tool to detect any
load imbalances. Adjust work accordingly.
31
How to Achieve Load Balance? (2)
• Use dynamic work assignment
• Certain classes of problems result in load imbalances even if data is evenly
distributed among tasks:
• Sparse arrays – some tasks have actual data to work on while others have mostly "zeros".
• Adaptive grid methods - some tasks may need to refine their mesh while others don't.
• N-body simulations - where some particles may migrate to/from their original task domain
to another task's; where the particles owned by some tasks require more work than those
owned by other tasks.
• When the amount of work each task will perform is intentionally variable, or is
unable to be predicted, it may be helpful to use a scheduler – task pool approach.
As each task finishes its work, it queues to get a new piece of work.
• It may become necessary to design an algorithm which detects and handles load
imbalances as they occur dynamically within the code.
32
Granularity
• Computation/Communication Ratio:
• In parallel computing, granularity is a qualitative measure of the ratio of computation
to communication.
• Periods of computation are typically separated from periods of communication by
synchronization events.
• Fine-grain Parallelism
• Coarse-grain Parallelism
33
Fine-grain Parallelism
• Relatively small amounts of computational
work are done between communication events
35
Which is Best?
• The most efficient granularity is dependent on the algorithm and the hardware
environment in which it runs.
36
I/O
• The bad News:
• I/O operations are generally regarded as inhibitors to parallelism
• Parallel I/O systems are immature or not available for all platforms
• In an environment where all tasks see the same filespace, write operations will
result in file overwriting
• Read operations will be affected by the fileserver's ability to handle multiple read
requests at the same time
• I/O that must be conducted over the network (NFS, non-local) can cause severe
bottlenecks
37
I/O
• The good News: Some parallel file systems are available. For example:
• GPFS: General Parallel File System for AIX (IBM)
• Lustre: for Linux clusters (Cluster File Systems, Inc.)
• PVFS/PVFS2: Parallel Virtual File System for Linux clusters
(Clemson/Argonne/Ohio State/others)
• PanFS: Panasas ActiveScale File System for Linux clusters (Panasas, Inc.)
• HP SFS: HP StorageWorks Scalable File Share. Lustre based parallel file system
(Global File System for Linux) product from HP
• The parallel I/O programming interface specification for MPI has been available
since 1996 as part of MPI-2. Vendor and "free" implementations are now
commonly available.
38
Some Options
• Rule #1: Reduce overall I/O as much as possible
• Writing large chunks of data rather than small chunks is usually significantly more
efficient.
• Fewer, larger files performs better than many small files.
• Confine I/O to specific serial portions of the job and then use parallel communications to
distribute data to parallel tasks; e.g., Task 1 could read an input file and then
communicate required data to other tasks. Likewise, Task 1 could perform write
operation after receiving required data from all other tasks.
• For distributed memory systems with shared filespace, perform I/O in local, non-shared
filespace. For example, each processor may have /tmp filespace which can used. This
is usually much more efficient than performing I/O over the network to one's home
directory.
39
• Create unique filenames for each tasks' input/output file(s)
• Aggregate I/O operations across tasks - rather than having many tasks perform I/O,
Performance Analysis and Tuning
• Analyzing and tuning parallel program performance can be much more
challenging than for serial programs.
• Weak Scaling
• The problem size grows as we add more processors. The
goal is to keep execution time the same, even as the problem
grows.
• The total problem size is proportional to the number of
processors used.
• Goal is to run larger problem in same amount of time 44
• Perfect scaling means if 1 processor takes T time for a small
problem, P processors should take the same T time for a
problem that is P times bigger.
Limits and Costs of Parallel Programming
• Scalability Isn't Just About Adding More Machines
• Adding more machines doesn't always mean you'll get better performance.
There are many interrelated factors such as hardware, software, and the nature
of the algorithm that control scalability.
• Some algorithms have inherent limits. For example: Certain parts of a program cannot
be parallelized. After a point, adding more processors might increase communication
and coordination overhead, making performance worse, not better.
• Hardware factors play a significant role in scalability. Examples:
• In SMP machines (Symmetric Multiprocessing), all processors share the same bus to
access memory. If many processors compete for the bus, bandwidth becomes a bottleneck.
• In distributed systems or clusters, processors communicate over a network. If the network
has limited bandwidth, communication becomes slow, limiting performance.
• If there's not enough memory per machine or node, processors spend time waiting to
access memory, reducing scalability. 45
• Faster processors help, but if communication is slow, even a fast CPU will spend time idle,
waiting for data.
Limits and Costs of Parallel Programming
• Parallel support libraries and subsystems (like MPI or OpenMP) may have overhead.
They manage tasks, synchronize processors, and handle communication, but this
adds extra computation and can limit the speedup you get.
46
Limits and Costs of Parallel Programming
• Dark Grey: Time spent on computation, decreasing
with # of processors
47
Limits and Costs of Parallel Programming
• How can we quantify the possible gains from parallelization?
• Karp-Flatt metric
• Calculates the serial fraction for a given parallel configuration and determines
whether the principal barrier to speedup is due to inherently sequential code or
parallelization overhead
48
• Isoefficiency metric
• Evaluate the scalability of a parallel program executing on a parallel computer
Amdahl’s Law
• Formulized in 1967, it analyzes whether a program merits parallelization by
showing an upper-bound on the maximum speedup that can be achieved by a
parallel algorithm of a problem.
• Amdahl's Law helps us predict the maximum speedup you can achieve by
parallelizing a program.
• Amdahl’s Law states that that potential program speedup is defined by the fraction
of code (P) that can be parallelized:
50
Speedup vs. P
• Amdahl’s law assumes that the problem size is fixed. It provides an upper bound
on the speedup achievable by applying a certain number of processors.
51
Amdahl’s Law
• Example 1: If 90% of the computation can be parallelized, what is the maximum
speedup achievable using 8 processors?
• Solution:
52
Amdahl’s Law
• Example 2: Suppose 25% of a sequential algorithm is the parallelizable portion.
The remaining part must be calculated sequentially. Calculate maximum
theoretical speedup for parallel variant of this algorithm using:
• 5 processors
53
Amdahl’s Law
• Little challenge: Determine, according to Amdahl’s law, how many processors
are needed to achieve maximum theoretical speedup while sequential portion
remains the same?
That is why we say actual achievable speedup is always less than or equal to
theoretical speedups
54
Amdahl’s Law: Remarks
• Amdahl’s law shows how execution time decreases as no. of processors
increases up to a certain limit.
• Amdahl’s Effect:
• Unlike Amdahl’s Law, which assumes a fixed problem size and shows
diminishing speedup due to the serial portion, Gustafson-Barsis’s Law argues
that as problem sizes grow, the parallel portion grows as well.
• Solution:
58
Karp-Flatt Metric
• Both Amdahl’s law and Gustafson-Barsis’s law ignore the parallelization
overhead and overestimate the achievable speedup.
• Karp-Flatt metric is used to measure the effectiveness of parallel computing
by analyzing how much serial overhead affects speedup. It helps determine
whether a parallel system is limited by inherent sequential portions or by
inefficiencies like parallelization overhead.
• Process startup time
• Process synchronization time
• Communication overhead
• Imbalanced workload
• Architectural overhead
59
Karp-Flatt Metric
• Given a parallel program exhibiting a speedup (N) while using N processing
units, then experimentally determined serial fraction e can be determined as:
• Interpretation of :
• If is small The problem is highly parallelizable, meaning the slowdown is not due
to serial work.
• If is large The speedup is being significantly limited by sequential execution or
parallel inefficiencies (e.g., communication, synchronization overhead).
• May either stay constant as N increases (system constrained by Amdahl’s Law due to
inherent sequential bottleneck and parallelization overhead is negligible) or increase
as N increases (parallelization overhead dominates the speedup)
60
Karp-Flatt Metric
• Example 1: Suppose in a parallel program, for 5 processors, you gained a
speedup of 1.25x, determine sequential fraction of your program
• Solution:
61
Karp-Flatt Metric
• Example 2: Benchmarking a parallel program on 1, 2,…, 8 processors produces the
following speedup results:
N 2 3 4 5 6 7 8
1.82 2.50 3.08 3.57 4.00 4.38 4.71
• What is the primary reason for the parallel program achieving a speedup of only 4.71 on
8 processors?
• What is the primary reason for the parallel program achieving a speedup of only 4.71 on
8 processors?
65
Isoefficiency Metric
For a system to be scalable, the problem size (W) must grow at least as fast as the total
parallelization overhead .
Remarks on Isoefficiency:
• Determines if adding more processors is beneficial.
• Helps in designing scalable parallel algorithms.
• Shows the cost of parallelization overhead (communication, load balancing, etc.).
67
Isoefficiency Metric
• Example: The Explicit Finite Difference problem is solved on a grid using processors.
Each processor is responsible for a subgrid of size During each time step, every
processor sends boundary values to its four neighbors for which the required
communication time is . Find the isoefficiency function.
• Solution:
68
Isoefficiency Metric
69