Professional Documents
Culture Documents
L3 Chp1 Web
L3 Chp1 Web
Introduction
Lecture 3
Today’s Lesson
● Avoid
● Releasing pets when there’s no food
● Putting out food if uneaten food remains
Producer/Consumer
● Need a mechanism so that
● Bob lets Alice know when food has been put
out
● Alice lets Bob know when to put out more
food
● Problem definition:
● Producer should not try to add data if the buffer is
full
● Consumer should not try to remove data from an
empty buffer
● Surprise solution:
● Can protocol
Can Protocol Revisited
A can on Alice’s window-sill; the string leads to Bob’s house
A B
Can Protocol Revisited
A B isEmpty?
yes
Can Protocol: Correctness
● Mutual Exclusion?
● YES
● Bob and the pets are never in the yard together
● Starvation-free?
● YES
● If Bob is always willing to feed and Alice always alert,
and pets always hungry, then pets eat infinitely often.
B D Letter
A C E Tiles
From Scrabble™ box
WRITER
Write One Letter at a Time …
W A S
H
B D
A C E
WRITER
To post a message
W A S H T H E C A R
I need to
communicate
something
else first
WRITER
Let’s send another message
L A
S E L L B L U E
M PS
WRITER
Uh-Oh
S E L L T H E C A R
OK
WRITER
READER
Readers-Writers Problem
● Devise a protocol so that
● Writer writes one letter at a time
● Reader reads one letter at a time
● Reader sees
● Old message or new message
● No mixed messages
● Possible solutions:
● Can use mutual-exclusion and can-and-string
protocols to solve the problem, but both require
waiting
● Will explore solutions that do not require
waiting later on
1.5
The Harsh Realities of Parallelization
Why do we care about parallelization?
Can be parallelized
Pass each file to a separate thread for processing
Amdahl’s law
● The extent to which we can speed up a
complex job is limited by how much of the job
must be executed sequentially.
● Definition:
● Then:
● Sequential part of the task takes 1 – p time
● Parallel part of the task takes p/n time
● Parallelization is thus: 1 – p + p/n
Amdahl’s law
1 (normalized) time
𝑝
1−𝑝 +
𝑛
Amdahl’s Law
1
Speedup =
p
1−p +
n
Note: p is not a fraction of the code, but of the
execution time of the algorithm
Example
● Ten processors
● 60% concurrent, 40% sequential
● How close to 10-fold speedup?
1
Speedup = 0.6
= 2.17
1 − 0.6 +
10
Example
● Ten processors
● 80% concurrent, 20% sequential
● How close to 10-fold speedup?
1
Speedup = 0.8 = 3.57
1 − 0.8 +
10
Example
● Ten processors
● 90% concurrent, 10% sequential
● How close to 10-fold speedup?
1
Speedup = 0.9
= 5.26
1 − 0.9 +
10
The Moral of the Story
● Making good use of our multiple
processors (cores) means finding ways
to effectively parallelize our code
● Minimize sequential parts
● Reduce idle time in which threads wait
Multicore Programming
● This is what this course is about…
● The % that is not easy to make concurrent
yet may have a large impact on overall
speedup
● Next Week:
● A more serious look at mutual exclusion