Parallel programming with models
of computation
Prof. Dr.-Ing. Jeronimo Castrillon
Tunis, Tunisia. December 2-4, 2015
Chair for Compiler Construction
Georg-Schumann-Str. 7A, 2 OG
Background: TU Dresden + Cfaed
2
© J. Castrillon. Parallel Programming
TU Dresden – Department of Computer Science
q TU Dresden
q Established in 1828 (university since 1961)
q ~37000 students, ~8000 employees
q Member of TU9 since 2006 and “excellent”
label since 2012
q CS Department
q Established in 1990
q 6 institutes, 26 professorships
q 1800 students, 300 PhD students
3
© J. Castrillon. Parallel Programming
Cfaed: Center for Advancing Electronics Dresden
q One of two excellence clusters at TU Dresden
q Goal:
“to explore new technologies for electronic information
processing which overcome the limits of CMOS technology”
Coordinator:
Gerhard Fettweis
4
© J. Castrillon. Parallel Programming
Cfaed: Research program revisited
q German Excellence Cluster: Goal – “to explore new technologies for electronic
information processing which overcome the limits of CMOS technology”
Information
Processing
Systems research to
handle heterogeneity
Devices &
Circuits
Materials &
Functions
H
HAEC:
Highly Adaptive
Energy-Efficient
Computing
s
e
r
i
)
s
u
c
o
f
y
r
t
s
u
d
n
i
(
S
O
M
C
w
o
n
a
N
n
o
c
i
l
i
S
A
Biology to inspire solutions
I
Biological
Systems
F Orchestration
l
y
G Resilience
b
m
e
s
s
A
l
l
r
a
u
c
e
o
m
o
B
i
l
i
a
c
m
e
h
C
D
E
n
o
b
r
a
C
B
i
c
n
a
g
r
O
C
Material research for
post-CMOS technologies
5
© J. Castrillon. Parallel Programming
Cfaed and the chair for CC (est. 06.2014)
q German Excellence Cluster: Goal – “to explore new technologies for electronic
information processing which overcome the limits of CMOS technology”
Information
Processing
Devices &
Circuits
Materials &
Functions
)
s
u
c
o
f
y
r
t
s
u
d
n
i
(
S
O
M
C
I
Biological
Systems
l
y
b
m
e
s
s
A
l
l
r
a
u
c
e
o
m
o
B
i
l
i
a
c
m
e
h
C
D
E
n
o
b
r
a
C
B
i
c
n
a
g
r
O
C
s
e
r
i
w
o
n
a
N
n
o
c
i
l
i
S
A
6
© J. Castrillon. Parallel Programming
CCC: Research directions
q Programming parallel
heterogeneous systems
q Domain-specific languages and
optimization
q Tools and methodologies for Post-
CMOS systems
q Optimization: Performance, energy
efficiency & resilience
7
© J. Castrillon. Parallel Programming
The team
Jeronimo
Castrillon
Sebastian
Ertel
Andrés
Goens
Sven
Karol
Robert
Khasanov
Norman
Rink
8
© J. Castrillon. Parallel Programming
About this course
Parallel programming with models of computation
q Structure: Four lecture blocks and exercises + final exam
q Lecture blocks
q LB1: Introduction to parallel programming
q LB2: Compilers and task-based programming models
q LB3: Models of Computation & Dataflow
q LB4: KPN programming flow
q Interrupt whenever needed
q Not a lot of background
required
q Help me regulate speed
q Exercises (hands on – HO)
q Different tools/frameworks (some not available, dependency on your system)
q Little guidance à Allow exploration
q Skeletons for some exercises will be provided
9
© J. Castrillon. Parallel Programming
About this course: Intended agenda
Parallel programming with models of computation
09:00 AM
12:30 PM
14:00 PM
LB1-I
LB1-I
HO-I
LB2-I
break
LB2-I (cont)
break
LB2-II
HO-II
LB3-I
break
LB3-II
HO-III
LB4/Q&A
17:30 PM
10
© J. Castrillon. Parallel Programming
Lecture 1: Introduction to parallel programming
Parallel programming with models of computation
Advertisement
Prof. Dr.-Ing. Jeronimo Castrillon
Tunis, Tunisia. December 2-4, 2015
Chair for Compiler Construction
Georg-Schumann-Str. 7A, 2 OG
Contents
q Introduction: Why parallel programming
q Architectures: Single and multi-cores
q Fundamentals of parallelism
q Examples
12
© J. Castrillon. Parallel Programming
Introduction
13
© J. Castrillon. Parallel Programming
Programming
q Programming in this lecture refers to
q Programming model/language
q The infrastructure underneath (compilers, software
synthesis, etc.)
q Programming model
q Bridge between applications and underlying hardware
q Abstraction of the computer system for algorithms and data structures
q Programming language and application programming interfaces (APIs)
q Implementation of the abstractions
q Language: formal constructed language to communicate instructions to a machines
14
© J. Castrillon. Parallel Programming
Programming models
Asanovic, K.; Bodik, R.; Catanzaro, B. C.; Gebis, J. J.; Husbands, P.; Keutzer, K.; Patterson, D. A.; Plishker, W. L.; Shalf, J.; Williams, S. W. & Yelick, K. A.
The Landscape of Parallel Computing Research: A View from Berkeley. EECS Department, University of California, Berkeley, 2006
15
© J. Castrillon. Parallel Programming
Sequential programming model – Von Neumann
q The program is a sequence of
instructions in memory
q Execution controlled by control unit
q Instruction register (IR) holds current
instruction
Memory & I/O
q Program counter (PC) points to next
Control unit
Processing unit
instruction in memory
q Computation in data-path
(processing unit)
q Good abstraction
q High-level languages (C, …)
q Efficient HW implementation
Inst. reg
Prog. counter
Reg. file
ALU
Central Processing Unit (CPU)
16
© J. Castrillon. Parallel Programming
Sequential language: C programming
Source code
Compiler
Target code
while (y < z)
{
int x = a + b;
y += x;
}
Errors
ADD R1, R2, R3
Loop: ADD R4, R1, R4
SLT R6, R4, R5
BEQ R6, loop
17
© J. Castrillon. Parallel Programming
Why parallel programming?
q Ubiquitous parallel computing!
q It is not new… but it became mainstream
q We are carrying 10s of processors around (will soon be 100s)
q Easy at the high end (many requests), way more difficult at the low end
A6X, 2012
ARM6, 1992
http://oldcomputers.net/
apple-newton.html
http://en.wikipedia.org/wiki/IPad_2
#mediaviewer/File:IPad_3.png
http://en.wikipedia.org/wiki/Fil
e:GPS_ARM610_die.JPG
http://www.engadget.com/2012/11/02/ipad-4th-
generation-found-carrying-new-powervr-sgx-554mp4-gpu/
http://www.kalray.eu
18
© J. Castrillon. Parallel Programming
MPPA, 2014
Why parallel computing?
Transistors
(thousands)
Moore’s law
Diminishing returns for
single core
Consequence:
go parallel
Single-thread
Performance
Frequency
(MHz)
Typical power
(W)
Core count
M. Horowitz, F. Labonte, et al. Dotted-line by C. Moore,
“Data processing in exascale-class computer systems,” The
Salishan Conference on High Speed Computing, 2011
19
© J. Castrillon. Parallel Programming
Limits for big single processor
1.
Power density: Voltage scaling slower than
transistors/area
q Approaching that of a hot plate
2. Diminishing returns in single application performance
http://www.extremetech.com/computing/112
147-hot-chips-laptop-manufacturer-sues-
q 52%/year up to 2002, 20%/year after that
amd-over-allegedly-defective-chips
q Instruction-level parallelism, caches, … not doing the trick anymore (see below)
Yield: the bigger, the more difficult to get it right
q Easier to validate
q If some are broken, chip can still work
Speed of light
q 1 Tflop/s è A “float” every 10-12s @ c=3x108m/s = 0.3mm
q 1 Tbyte in 0.3x0.3mm2 è 1 bit / atom
© J. Castrillon. Parallel Programming
3.
4.
20
Why parallel computing? (2)
What to do with transistors?
Improve single-core
architectures
)
g
o
l
(
n
o
i
t
u
l
o
v
E
Use multi-core
architectures
s
E
P
f
o
r
e
b
m
u
N
4000
3500
3000
2500
2000
1500
1000
500
0
ITRS Trend: PE Count
Transistors
(thousands)
Number of processing elements
in SoCs (ITRS, 2011)
2317
1851
3771
2974
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
1460
1137
899
558
709
Year
Multicore penetration
(EETimes, 2011)
447
129
343
266
185
Single-thread
Performance
Frequency
(MHz)
Typical power
(W)
Core count
~2005
Time
M. Horowitz, F. Labonte, et al. Dotted-line by C. Moore,
“Data processing in exascale-class computer systems,” The
Salishan Conference on High Speed Computing, 2011
21
© J. Castrillon. Parallel Programming
Sequential vs. parallel
The cooking analogy: idea by Prof. T. Strufe (also: “Rajaraman, V. Elements of parallel computing. 1990”)
Single-core era
Multi-core era
Recipe
Recipe
Data-level
parallelism (DLP)
peel
cut
heat oil
Time
22
© J. Castrillon. Parallel Programming
Pipeline-level
parallelism (PLP)
…
fry
Task-level
parallelism (TLP)
Time
Technology, languages & compilers
What we did with transistors
Single-cores: don’t change the recipe
(transparent for the programmer)
q Frequency scaling: sit down and relax
1990
2000
1x/min
100x/min
q Architecture innovation
è Incremental compiler improvements
Instruction
specialization
SIMD
(single-instruction
multiple-data)
23
© J. Castrillon. Parallel Programming
Technology, languages & compilers (2)
What we did with transistors
ASM
C CODE
q First compilers 1950-1960: translators
(compile: to put things together)
q Later: optimizing compilers
q Exploit new architectural features
q Widening performance gap
q But: where were we without compilers?
d
n
o
c
e
s
r
e
p
s
e
m
a
r
F
150
100
50
0
24
© J. Castrillon. Parallel Programming
scalar
autovectorized
manual
118
115
105
44
28
28
29
27
28
GCC
ICC
Courtesy: Prof. B. Juurlink, M. Alvarez. TU Berlin
LLVM
Technology, languages & compilers (3)
What we did with transistors
Partitioning
…
Parallel
languages
Advertisement
Multi-cores: change recipe
q Frequency scaling: cooks not getting
any faster
??
q Forced to hire and coordinate
multiple cooks
25
© J. Castrillon. Parallel Programming
Multi-core
Challenges in parallel programming
q Parallelism itself
q Recognize/express/exploit different kinds
q Granularity
q Mapping
q Exploit specialization
q Improve locality (memory and interconnect)
& reduce synchronization
q Load balancing
q Timeliness
q In some domains, e.g., lost packages in LTE
q Performance modeling
26
© J. Castrillon. Parallel Programming
Data-parallel
(DLP)
Pipeline (PLP)
100W
5 W
Different domains, different conditions
General purpose processing
(GPP) & high-performance
computing (HPC)
Graphical processing
units (GPU)
Embedded computing
en.wikipedia.org/wiki/
Opteron#mediaviewer/
File:AMD_Opteron_Six_
Cores.jpg
http://www.engadget.com
/2012/08/20/engadget-
primed-gpu-compute/
q Need for speed
q GPP: any application
q HPC: regular computations
q Extreme number of cores
q SIMD principle
http://www.eetimes.com/document.asp?doc_id=125
4745
q Known application class, e.g.,
signal processing, multimedia
q Extreme specialization
http://www.hpcc.ecs.soton.ac.uk/EandT/c
ourseware/HPCoverview/definition.html
27
© J. Castrillon. Parallel Programming
128!
(Traditional) Sequential programming enough?
q It is difficult to extract parallelism fundamentally
sequential models (von Neumann)
q Programming style favors
q Collecting information in data structures
q Global, shared view of memory
q “Impossible” for compilers to extract parallelism
28
© J. Castrillon. Parallel Programming
Parallel models and languages needed
What we did with transistors
ASM
C CODE
?? CODE
q Programming model: no consensus (threads, tasks, OpenXX, CUDA, MPI, …)
q Different models, architectures, domains, …
29
© J. Castrillon. Parallel Programming
Multi-core compilation
Programming
languages
Compiler: good
HW abstraction,
some performance
gap
Architectures
& μ-arch
Single-processors
The past
Programming
languages
(?)
Extremely costly and error-prone:
parallelism, specialization, timeliness, …
Manual process
Architectures
& μ-arch
Almost no abstraction, both
productivity and performance gaps
(het.) Multi-processors
The present
30
© J. Castrillon. Parallel Programming
Multi-core compilation (2)
)
g
o
l
(
n
o
i
t
u
l
o
v
E
Programming
languages
(?)
Manual process
Architectures
& μ-arch
(het.) Multi-processors
The present
Adapted from: W. Ecker, et al., “Hardware-dependent Software - Principles and Practice,”
in Hardware-dependent Software - Introduction and Overview. Springer, 2008.
2x/10 Months
Gap
1,2x/10 Months
Time
Improve the efficiency of programming
heterogeneous platforms for specific
application domains
31
© J. Castrillon. Parallel Programming
Architectures
32
© J. Castrillon. Parallel Programming
A computing system
Computer
architecture
Storage
Interconnect
q Compute: Type of instruction set, data widths, …
q Storage: registers, scratchpad, memory hierarchy, disks, …
q Interconnect: buses, interrupts, network-on-chips
33
© J. Castrillon. Parallel Programming
Computing
Domain/app. Specific
(hardware)
ASIC
PLA
FPGA
Better
performance
(e.g.,
MOPS/Watt)
Domain/app. Specific
(programmable)
ASIP
NPU
VLIW
DSP
GPP
RISC
CISC
Super-
scalar
Adapted from: H. Blume, H. Hubert, H. Feldkamper, and T. Noll, “Model-based exploration of the design space
for heterogeneous systems on chip,” in Application-Specific Systems, Archi- tectures and Processors, 2002.
Proceedings. The IEEE International Conference on, 2002, pp. 29–40.
34
© J. Castrillon. Parallel Programming
Flexibility
(“generality)
Storage
q Hierarchy
q Exploit temporal and spatial locality in
programs
CPU
Reg. file
256 B – 8 KB
0.25 – 1 ns
Chip
L1 cache
16 – 64 KB
1 – 5 ns
1 – 4 MB
5 – 25 ns
q Smaller (closer to CPU) è Faster single
Board
L2 cache
access memory
q Good design: Constant throughput
q Why?
q CPU speed: 2x/1.5 yrs
q DRAM speed: 2x/10 yrs
4 – 256 MB
25 – 100 ns
500 GB+
3 – 10 ms
“unlimited”
10 – 2000 ms
Main memory
Disk
Network
35
© J. Castrillon. Parallel Programming
Numbers for 2012, source: http://web.stanford.edu/class/archive/cs/cs143/cs143.1128/
Interconnect
On-Chip Communication Architectures – Networks-on-Chip. (ICS 295) Sudeep Pasricha and Nikil Dutt
36
© J. Castrillon. Parallel Programming
Single processors
What we did with transistors
ASM
C CODE
q Pipelining, Superscalar, out-of-order execution, branch prediction, DSP-
extensions, speculative execution, SIMD, VLIW, HW multi-threading, …
37
© J. Castrillon. Parallel Programming
CISC vs. RISC
q CISC: Complex Instruction Set Computer (common in the 80s)
q Multi-cycle, complex instructions, e.g., search inside a list
q CPU-Memory speed gap: CPU could process instructions faster than data arrival
è Do many operations per each slow memory access
q Semantic gap: Make assembly closer to programmers (… later came the compilers)
q RISC: Reduced Instruction Set Computer (common today)
q Memory gap reduced by technology (later mitigated with memory hierarchy)
q Semantic gap closed by compiler technology
q Principle: Increase throughput by executing many smaller instructions (pipelining)
38
© J. Castrillon. Parallel Programming
CISC vs. RISC (2)
q The famous equation:
time
program
=
time
cycle
x
cycle
instruction
x
instructions
program
RISC simpler HW
è Faster clock
CPI (clocks per instruction):
RISC ≈ 1, CISC > 2
CISC complex ISA
è Fewer instructions
39
© J. Castrillon. Parallel Programming
Pipelining
q Introduced in the 1980s, for example for Stanford MIPS processor
q Break instructions into phases (reduce critical path è faster clock)
q Allow phases of different instructions to execute in parallel
q Prominent “laundry example” (Henessy & Patterson)
40
© J. Castrillon. Parallel Programming
Superscalar
q Main characteristic: Extension of RISC principle – Use multiple functional units to
increase parallel execution (i.e., CPI < 1)
Source: wikipedia.org
Superscalar pipeline
q Desktop computers are superscalar: Since the first Intel P5 Pentium in 1993
RISC pipeline
41
© J. Castrillon. Parallel Programming
Digital signal processors (DSPs)
6-stage pipeline
Address generation unit (AGU)
Immediate
11
Adress-
Adress-
Register
Register
+/-+/-
Modify-
Modify-
Register
Register
MEMMEM
Multiply-accumulate (MAC) unit
42
© J. Castrillon. Parallel Programming
Very Large Instruction Word (VLIW)
q Main characteristic: One fetch from memory includes several instructions
q ILP similar to superscalars (CPI < 1), but dependency checks done at compile time
q Single instructions are RISC-like
MEM
ADD
NOP
LOAD
ALU
ALU
Load/
store
NOPs: Not utilized
Advertisement
resources. Potentially
higher code size
Register
Register
Register
Parallel functional
units
q Used in embedded devices (low energy) and for DSP (VLIW-DSP)
43
© J. Castrillon. Parallel Programming
Reality check
IPC: 2-Issue
IPC: 6-Issue
Kunle Olukotun, Basem A. Nayfeh, Lance Hammond, Ken Wilson, and Kunyung Chang. 1996. The case for a single-chip multiprocessor. In Proceedings of the
seventh international conference on Architectural support for programming languages and operating systems (ASPLOS VII). ACM, New York, NY, USA, 2-11.
DOI=10.1145/237090.237140 http://doi.acm.org/10.1145/237090.237140
44
© J. Castrillon. Parallel Programming
Vectors and SIMD
q Similar concept, different terminology
q Vector-processing:
q Large registers to store a vector
q Operator works on the elements of the vector
A
B
C
a1
b1
a2
b2
a3
b3
a4
b4
C = A+B
a1+b1
a2+b2
a3+b3
a4+b4
q SIMD: Single-instruction multiple data
q Associated with a more distributed setup
q “Processing Elements” receive the same
instruction and work on different data
q Popular: GPUs
PC
PE
PE
PE
PE
Mem
Mem
Mem
Mem
45
© J. Castrillon. Parallel Programming
Simultaneous multi-threading
q Duplicate HW resources (registers)
q Overlap the execution of threads
q Fill delays/unused of one thread with computation of the other
Stages
Time
Stages
Stages
46
© J. Castrillon. Parallel Programming
Multi-processors
What we did with transistors
ASM
C CODE
?? CODE
q So far: A bit of parallelism via SMT
q Now: Explicit parallel machine
47
© J. Castrillon. Parallel Programming
Principle
q For reasons above: more, simpler processors
Resources
Time
q Two separate processors (PC), i.e., MIMD (see later)
q Could be themselves superscalars, have multi-threading, speculation, caches …
48
© J. Castrillon. Parallel Programming
Multi-core/MPSoCs: Examples
Texas Instruments Keytone 2
Tilera GX
http://www.theregister.co.uk/2013/03/05/hp_moonshot_server_ti_
keystone_arm_chip/
Tilera.com
49
© J. Castrillon. Parallel Programming
TI Keystone II
MEM
subsystem
DMAs,
sema-
phores
PMU
L1
A15
L1
A15
L2
A15
L1
A15
L1
VLIW DSP
L1,L2
NoC
Peripherals
Communication
support
HW queues
Network
Processor
Packet DMA
50
© J. Castrillon. Parallel Programming
TI Keystone II: Computing
MEM
subsystem
DMAs,
sema-
phores
PMU
L1
A15
L1
A15
L2
A15
L1
A15
L1
VLIW DSP
L1,L2
NoC
Peripherals
Communication
support
HW queues
Network
Processor
Packet DMA
q Quad ARM Cortex A15 CorePacs @ 1.4 GHz, integrated NEON & FP vector
q 32KB L1 memory per ARM A15
q 4 MB L2 shared cache (Snoop control unit for coherency)
q AMBA 4.0 AXI: Also access to MEM with up to 6 MB shared SRAM with VLIW DSPs
q Cluster-‐level and core-‐level power management and low-‐power standby modes
51
© J. Castrillon. Parallel Programming
TI Keystone II: Computing (2)
MEM
subsystem
DMAs,
sema-
phores
PMU
L1
A15
L1
A15
L2
A15
L1
A15
L1
VLIW DSP
L1,L2
NoC
Peripherals
Communication
support
HW queues
Network
Processor
Packet DMA
q 8 C66x CorePacs @ 1.25 GHz (fixed and floating point)
q L1 memory: partitionable as cache and/or RAM (32KB L1P + 32KB L1D)
q Error detection for L1P and memory protection
q L2 memory: also cache and/or RAM (1 MB)
q Error detection and correction for all L2
q Connection to MEM and NoC
52
© J. Castrillon. Parallel Programming
TI Keystone II: Memory
MEM
subsystem
DMAs,
sema-
phores
PMU
L1
A15
L1
A15
L2
A15
L1
A15
L1
VLIW DSP
L1,L2
NoC
Peripherals
Communication
support
HW queues
Network
Processor
Packet DMA
q Recall up to 6 MB shared protected SRAM
q Up to two interfaces to 72-bit DDR3 (8 GB and 2 GB respectively)
53
© J. Castrillon. Parallel Programming
TI Keystone II: Navigator (Comm. Support)
MEM
subsystem
DMAs,
sema-
phores
PMU
L1
A15
L1
A15
L2
A15
L1
A15
L1
VLIW DSP
L1,L2
NoC
Peripherals
Communication
support
HW queues
Network
Processor
Packet DMA
q Consist: Queue manager; multiple, dedicated packet DMAs
q Inter-core/IP/Peripherals communication (messages and data exchanges)
q Low-‐overhead processing and routing of packet traffic (low host interaction)
q Dynamic load optimization
q Up to 8K or 16K HW queues and 512K to 1M descriptors
q Network processor: acceleration for encryption and TCP/UDP support (CRC)
54
© J. Castrillon. Parallel Programming
TI Keystone II: Software development
55
© J. Castrillon. Parallel Programming
Source: https://training.ti.com/system/files/docs/keystone-ii-software-overview-slides.pdf
Fundamentals of parallelism
56
© J. Castrillon. Parallel Programming
Parallelism challenges (recall)
q Parallelism itself
q Recognize/express/exploit different kinds
q Granularity
q Mapping
q Exploit specialization
q Improve locality (memory and interconnect)
& reduce synchronization
q Load balancing
q Timeliness
q In some domains, e.g., lost packages in LTE
q Performance modeling
57
© J. Castrillon. Parallel Programming
Data-parallel
(DLP)
Pipeline (PLP)
100W
5 W
Terminology: Parallelism vs. Concurrency
q Concurrency
q Tasks/threads running on overlapping time periods
q Does not mean: running at the same time
q Example: Multi-tasking on a single threaded core
q Can happen at different levels (fine, coarse)
q Parallelism
q Tasks can run at the same time
58
© J. Castrillon. Parallel Programming
Terminology: Flynn’s Taxonomy
q Single Instruction, Single Data stream (SISD):
q Single threaded RISC
q Single Instruction, Multiple Data streams (SIMD):
q One instruction on several data (see before)
q Multiple Instruction, Single Data stream (MISD):
q Do the same thing several times (not very common)
q Good for fault tolerance – Majority voting)
q Multiple Instruction, Multiple Data streams (MIMD):
q Most general one
q A multi-cores are examples of MIMD
Flynn, M. J. Some Computer Organizations and Their Effectiveness. Computers, IEEE Transactions on, 1972, C-21, 948 -960
59
© J. Castrillon. Parallel Programming
wikipedia
Terminology: Types of parallelism (revisited)
Advertisement
q Instruction-Level Parallelism (ILP)
q Task-Level Parallelism (TLP):
q Computation divided into multiple tasks that operate in
parallel on different data
q Data-Level Parallelism (DLP):
q Computation is replicated into tasks that operates on
different data (SIMD)
q Pipeline-Level Paralellimm (PLP):
q Computation is broken into a sequence of stages which are
repeated for different data (mixture of DLP and TLP)
60
© J. Castrillon. Parallel Programming
Terminology: Speedup, efficiency & Amdahl
q Speedup: How much faster is the new implementation
q Efficiency: How good are we using resources
q Amdahl’s law: How much faster can we really get
q S: Portion of the program that cannot be parallelized (serial)
q n: number of cores
t(1)
t(n)
tseq
tseq(S + (1
x(n) =
S + (1
S)/n)
=
=
1
S)/n
How much faster can we get if 99% of the code can be parallelized?
61
© J. Castrillon. Parallel Programming
Terminology: Amdahl’s law (1)
62
© J. Castrillon. Parallel Programming
wikipedia
Terminology: Gustafson’s law
q Counterpart of Amdahl’s
q Amdahl: Assumes a fixed problem size
q Gustafson: If more resources are there, one can solve bigger problems
q Speedup: (on n cores with sequential portion α)
q Assumption:
x(n) = t(1)/t(n) = n
↵
·
(n
1)
q After a sequential portion A (OS boot) follows a scalable parallel B (on each core)
q Total sequential time is:
t(1) = A + n
+ n
B
A + B
·
=
A
A + B
·
B
+ n
1
·
✓
A
A + B
◆
Maximal
speedup?
= ↵ + n
(1
·
↵)
x(n) =
A + n
B
·
A + B
↵
·
= n
=
A
A + B
1)
(n
63
© J. Castrillon. Parallel Programming
Terminology: Gustafson’s law (2)
64
© J. Castrillon. Parallel Programming
wikipedia
Terminology: Amdahl vs Gustafson
l
maximal
speed-up
increased
speed-up
e
l
l
a
r
a
p
l
a
i
t
n
e
u
q
e
s
Adapted from: Wilfried
Verachtert, IMEC, 2007
65
© J. Castrillon. Parallel Programming
Terminology: Programming models – Classification
q Explicit
q Visible to the programmer
q Encoded in the application
implementation
q Implicit
q Automatic/transparent to programmer
q Encoded in the programming model
Asanovic, K., et al. The Landscape of Parallel Computing Research: A View from
Berkeley. EECS Department, University of California, Berkeley, 2006 ]
66
© J. Castrillon. Parallel Programming
Agenda
09:00 AM
12:30 PM
14:00 PM
LB1-I
HO-I
LB2-I
break
LB2-I (cont)
break
LB2-II
HO-II
LB3-I
break
LB3-II
HO-III
LB4/Q&A
LB1-I
17:30 PM
67
© J. Castrillon. Parallel Programming
Examples
q MPI, Pthreads & OpeMP
68
© J. Castrillon. Parallel Programming
Message passing interface (MPI)
q Defacto standard for distributed memory & scientific computing
q Often MPI across nodes + OpenMP within nodes (+ILP within processors)
q Recall classification
q Implicit:
q Synchronisation: implied by communication (explicit also possible)
q Communication mapping: Routing, bandwidth usage, channels are implicit
q Implementation: Standardized API
69
© J. Castrillon. Parallel Programming
More on MPI
q Communication
q Point to point: [non]blocking & [a]synchronous
q Collective: one-many, many-one, many-many Code parallelization cannot be
incremental
q Supports coarse level parallelism and parallel I/O Fortran 77 and C/C++
q Very large API (152)
q Performance oriented standard
q History
q MPI 1.0 (1994): Basic communicators, Fortran 77 and C bindings
q MPI 2.2 (2009): Single sided comms., I/O, C++, …
q MPI 3.0 (2014): More process management
70
© J. Castrillon. Parallel Programming
MPI Skeleton
MPI_Comm_rank(…, &my_rank);
for (i = my_ranksize; i < my_rank(size+1); i++)
<Do something: Receive, compute, send, …>
if (my_rank != 0)
MPI_Send(&result, …);
else {
total = result;
MPI_Comm_size(…, &num_procs);
for (source = 1; source < num_procs; source++) {
MPI_Recv(&result, …);
total = total + result;
}
}
71
© J. Castrillon. Parallel Programming
Minimal subset of MPI
MPI_Init()/MPI_Finalize()/MPI_Abort()
MPI_COMM_WORLD MPI_Comm_size()/MPI_Comm_rank()
MPI_Send()/MPI_Recv()
72
© J. Castrillon. Parallel Programming
Minimal subset of MPI
MPI_Init()/MPI_Finalize()/MPI_Abort()
MPI_COMM_WORLD MPI_Comm_size()/MPI_Comm_rank()
MPI_Send()/MPI_Recv()
q Init: Need to be called before any other call to MPI
q Finalize/abort: Counterpart to end an MPI program
q Abort tries to end things nicely
73
© J. Castrillon. Parallel Programming
Minimal subset of MPI
MPI_Init()/MPI_Finalize()/MPI_Abort()
MPI_COMM_WORLD MPI_Comm_size()/MPI_Comm_rank()
MPI_Send()/MPI_Recv()
q MPI_COMM_WORLD:
q handle to a set of processes that may communicate (communicator set)
q Default value: all of them
q MPI_Comm_size: Size of the communicator (number of MPI processes)
q MPI_Comm_rank: Internal process rank (who am i?)
74
© J. Castrillon. Parallel Programming
Minimal subset of MPI
MPI_Init()/MPI_Finalize()/MPI_Abort()
MPI_COMM_WORLD MPI_Comm_size()/MPI_Comm_rank()
MPI_Send()/MPI_Recv()
q MPI_Send(void *buf, int cnt, MPI_Datatype type, int dest,
int tag, MPI_Comm comm)
q Blocking send cnt elements from *buf of type to rank dest and tag from
communicator set comm (variants Ssend, Bsend, Rsend)
q MPI_Recv(void *buf, int cnt, MPI_Datatype type, int src,
int tag, MPI_Comm comm, MPI_Status *stat)
q Analogously (irrespective of the version of send used)
75
© J. Castrillon. Parallel Programming
Pthreads: Posix Threads API
q Recall classification
POSIX: Portable Operating System Interface for uniX
q Explicit
q Task identification/mapping into threads
q Synchronization (locks & mutexes)
q Implicit
q Data distribution/communication: Over shared memory
76
© J. Castrillon. Parallel Programming
Pthreads API
q Locks: mutex, read-write locks
q Condition variables: essentially a one element multi-reader queue
q Barriers (optional part of the specification)
q Implementation: supported by a mixture of hardware (shared memory) and
system software (thread management, locking)
q Pthreads: Often as backend for other models (GNU OpenMP)
77
© J. Castrillon. Parallel Programming
Shared data
q Variables declared outside of main are shared
q Object allocated on the heap may be shared (if pointer is passed)
q Variables on the stack are private: passing pointer to these around to other
threads can cause problems
78
© J. Castrillon. Parallel Programming
Pthreads skeleton
threads = (pthread_t ) malloc(n sizeof(pthread_t));
pthread_attr_init(&pthread_custom_attr);
for (i = 0; i < n; i++)
pthread_create(&threads[i], &pthread_custom_attr, task, …);
for (i = 0; i < n; i++) {
void task(void arg) {
pthread_mutex_lock(&mutex);
<Receive messages>
pthread_mutex_unlock(&mutex);
}
…
pthread_mutex_lock(&mutex);
<Send messages>
pthread_mutex_unlock(&mutex);
return NULL;
for (i = 0; i < n; i++)
}
pthread_join(threads[i], NULL);
79
© J. Castrillon. Parallel Programming
Basics of Pthreads
pthread_create()/pthread_join()
PTHREAD_MUTEX_INITIALIZER/init/lock/unlock/destroy
pthread_barrier_init()/pthread_barrier_wait()
80
© J. Castrillon. Parallel Programming
Basics of Pthreads
pthread_create()/pthread_join()
PTHREAD_MUTEX_INITIALIZER/init/lock/unlock/destroy