Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Chapter 1:

Introduction

Lecture 3
Today’s Lesson

● Producer-Consumer Problem (1.3)

● Readers-Writers Problem (1.4)

● Harsh Realities of Parallelization (1.5)


1.3
The Producer-Consumer Problem
The Fable Continues
● Alice and Bob fall in love & marry

● Then they fall out of love & divorce


● She gets the pets – they now get along
● He has to feed them – the pets however
side with Alice and attack Bob
Producer-Consumer
● Alice and Bob can’t meet
● Each has restraining order on the other
● So he puts food in the yard
● And later, she releases the pets

● 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

● Need to investigate which protocol will


manage this coordination problem
Also known as bounded buffer
problem
● Two processes – producer and consumer –
share a common fixed-size buffer

● The producer generates data, puts it into the


buffer and starts again
● At the same time the consumer, consumes the
data one piece at a time

(Image source: http://blog.ashansa.org/2015/10/producer-consumer-pattern.html)


Also known as bounded buffer
problem

● 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

Bob’s Protocol (Producer):


● Wait until the can (on Alice’s window-sill) is up
● Put food in the yard
● Go inside and pull the string to knock down the can

A B
Can Protocol Revisited

Alice’s Protocol (Consumer):


● Wait until the can is down
● Release pets
● Every time pets return, check if there is food left
● If not, reset the can
● Keep the pets inside until the can is down

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.

● Solves producer-consumer problem?


● YES
● The pets never enter the yard unless there is food
● Bob never provides food if there is unconsumed food.
Back to CS

● Producer-consumer problem appears in


almost all parallel and distributed systems
● Protocol used by processors to place data in
communication buffers to be read or
transmitted across a network interconnect or
shared bus
Back to CS

● Producer either goes to sleep or discards data if the


buffer is full.
● The next time the consumer removes an item from the
buffer, it notifies the producer, who starts to fill the buffer
again.
● Consumer can go to sleep if it finds the buffer to be
empty.
● The next time the producer puts data into the buffer, it
wakes up the sleeping consumer.
● Problem solved through inter-process communication
(monitors, semaphores) – covered in Chp 8
(Source: Wikipedia)
Waiting
● Producer-consumer problem can also be
solved using flags
● Both solutions use waiting
● Waiting is problematic
● If one participant is delayed
● So is everyone else
● But delays are common & unpredictable
1.4
The Readers-Writers Problem
Readers-Writers Problem
● Describes the problem of a process
reading a shared object (e.g. a file) while
another process is writing, resulting in
the reader capturing incorrect
information

● Problem illustrated through the Alice &


Bob fable
The Fable drags on …
● Bob and Alice still have issues
● So they need to communicate
● So they agree to use billboards …
Billboards are Large

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?

● Upgrading from a uniprocessor to a n-


way multiprocessor does not mean in n-
fold increase in performance
● We want as much of the code as
possible to execute concurrently (in
parallel)
● A larger sequential part implies reduced
performance
Parallelization
● A program (or algorithm) which can be
parallelized can be split up into two parts:
● A part which cannot be parallelized
● A part which can be parallelized

● Imagine a program that processes files


from disk.
● Need to scan the directory and create a list of
files
● Need to process each file Cannot be parallelized

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:

time it takes one processor to complete the task


speedup =
time it takes n concurrent processors to complete
the same task

sequential part + parallel part


Amdahl’s law
● n – number of processors
● p – fraction of task that can be executed
in parallel (𝑝 ∈ [0,1])

● 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

time it takes one processor to complete the task


speedup =
time it takes n concurrent processors to complete
the same task

sequential part + parallel part

𝑝
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

You might also like