35
R 7 :: 1 – 0024 Spring 2010 Parallel Programming Parallel Programming 0024 0024 Recitation Week 7 Recitation Week 7 Spring Semester 2010 Spring Semester 2010

– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010

Embed Size (px)

Citation preview

– R 7 :: 1 – 0024 Spring 2010

Parallel ProgrammingParallel Programming00240024

Recitation Week 7Recitation Week 7

Spring Semester 2010Spring Semester 2010

– R 7 :: 2 – 0024 Spring 2010

Today‘s programToday‘s program

Assignment 6Assignment 6

Review of semaphoresReview of semaphores Semaphores Semaphoren and (Java) monitors

Semaphore implementation of monitors (review)Semaphore implementation of monitors (review)

Assignment 7Assignment 7

– R 7 :: 3 – 0024 Spring 2010

SemaphoresSemaphores

Special Integer variable w/2 atomic operationsSpecial Integer variable w/2 atomic operations P() (Passeren, wait/up) V() (Vrijgeven/Verhogen, signal/down)

Names of operations reflect the Dutch origin of the Names of operations reflect the Dutch origin of the inventor ...inventor ...

– R 7 :: 4 – 0024 Spring 2010

class Semaphoreclass Semaphore

public class Semaphore {public class Semaphore {

private int value;private int value;

public Semaphore() { public Semaphore() {

value = 0; value = 0;

}}

public Semaphore(int k) { public Semaphore(int k) {

value = k; value = k;

}}

public synchronized void P() { /* see next slide */ }public synchronized void P() { /* see next slide */ }

public synchronized void V() { /* see next slide */ } public synchronized void V() { /* see next slide */ }

}}

– R 7 :: 5 – 0024 Spring 2010

P() operationP() operation

public synchronized void P() {public synchronized void P() {

while (value == 0) {while (value == 0) {

try {try {

wait();wait();

}}

catch (InterruptedException e) { }catch (InterruptedException e) { }

}}

value --;value --;

}}

– R 7 :: 6 – 0024 Spring 2010

V() operationV() operation

public synchronized void V() {public synchronized void V() {

++value;++value;

notifyAll();notifyAll();

}}

– R 7 :: 7 – 0024 Spring 2010

CommentsComments

You can modify the value of an semphore instance only You can modify the value of an semphore instance only using the P() and V() operations.using the P() and V() operations. Initialize in constructor

EffectEffect P() may block V() never blocks

Application of semaphores:Application of semaphores: Mutual exclusion Conditional synchronization

– R 7 :: 8 – 0024 Spring 2010

2 kinds of semaphores2 kinds of semaphores

Binary semaphoreBinary semaphore Value is either 0 or 1 Supports implemention of mutual exclusion

Semaphore s = new Semaphore(1);

s.p()

//critical section

s.v()

Counting (general) semaphoreCounting (general) semaphore Value can be any positive integer value

– R 7 :: 9 – 0024 Spring 2010

FairnessFairness

A semaphore is considered to be “fair“ if all threads A semaphore is considered to be “fair“ if all threads that execute a P() operation eventually succeed.that execute a P() operation eventually succeed.

Different models of fairness –Different models of fairness –

Semaphore is “unfair”: a thread blocked in the P() Semaphore is “unfair”: a thread blocked in the P() operation must wait forever while other threads (that operation must wait forever while other threads (that executed the operation later) succeed.executed the operation later) succeed.

– R 7 :: 10 – 0024 Spring 2010

Semaphores in JavaSemaphores in Java

java.util.concurrent.Semaphorejava.util.concurrent.Semaphore

acquire()acquire() instead of instead of P()P()

release()release() instead of instead of V()V()

ConstructorsConstructors

Semaphore(int permits);Semaphore(int permits);

Semaphore(int permits, boolean fair);Semaphore(int permits, boolean fair); permits: initial value fair: if true then the semphore uses a FIFO to

manage blocked threads

– R 7 :: 11 – 0024 Spring 2010

Semaphores and monitorsSemaphores and monitors

Monitor: model for synchronized methods in JavaMonitor: model for synchronized methods in Java

Both constructs are equivalentBoth constructs are equivalent

One can be used to implement the otherOne can be used to implement the other

– R 7 :: 12 – 0024 Spring 2010

Example from Mar 18Example from Mar 18

See slides for context

– R 7 :: 13 – 0024 Spring 2010

Buffer using condition queuesBuffer using condition queues

class BoundedBuffer extends Buffer {

public BoundedBuffer(int size) {

super(size);

}

public synchronized void insert(Object o)

throws InterruptedException {

while (isFull())

wait();

doInsert(o);

notifyAll();

}// insert

– R 7 :: 14 – 0024 Spring 2010

Buffer using condition queues, continuedBuffer using condition queues, continued // in class BoundedBuffer

public synchronized Object extract()

throws InterruptedException {

while (isEmpty())

wait();

Object o = doExtract();

notifyAll();

return o;

}// extract

} // BoundedBuffer

– R 7 :: 15 – 0024 Spring 2010

Emulation of monitor w/ semaphoresEmulation of monitor w/ semaphores

We need 2 semaphores:We need 2 semaphores:

One to make sure that only one synchronized method One to make sure that only one synchronized method executes at any tiven time executes at any tiven time

call this the “access semaphore” (S) call this the “access semaphore” (S)

binary semaphorebinary semaphore

One semaphore to line up threads that are waiting for One semaphore to line up threads that are waiting for some condition some condition

call this the “condition semaphore” (SCond) – also binarycall this the “condition semaphore” (SCond) – also binary

threads that wait must do an “acquire” and this will not threads that wait must do an “acquire” and this will not completecomplete

For convenience: For convenience: Counter waitThread to count number of waiting threads i.e., threads in queue for SCond

– R 7 :: 16 – 0024 Spring 2010

Basic ideaBasic idea

1)1) Frame all synchronized methods with S.acquire() and Frame all synchronized methods with S.acquire() and S.release()S.release()This ensures that only one thread executes a synchronized This ensures that only one thread executes a synchronized

method at any point in timemethod at any point in time

Recall S is binary.Recall S is binary.

2)2) Translate wait() and notifyAll() to give threads Translate wait() and notifyAll() to give threads waiting in line a chance to progress (these threads waiting in line a chance to progress (these threads use SCond) use SCond) To simplify the debate, we require that “notifyAll()” is the last To simplify the debate, we require that “notifyAll()” is the last

action in a synchronized methodaction in a synchronized method

Java does not enforce this requirement but the Java does not enforce this requirement but the mapping of synchronized methods into semaphores is mapping of synchronized methods into semaphores is simplified.simplified.

– R 7 :: 17 – 0024 Spring 2010

Buffer with auxiliary fieldsBuffer with auxiliary fields

class BoundedBuffer extends Buffer {

public BoundedBuffer(int size) {

super(size);

access = new Semaphore(1);

cond = new Semaphore(0);

}

private Semaphore access;

private Semaphore cond;

private int waitThread = 0;

// continued

– R 7 :: 18 – 0024 Spring 2010

1) Framing all methods1) Framing all methods

public void insert(Object o)throws InterruptedException {

access.acquire(); //ensure mutual exclusion

while (isFull())

wait();

doInsert(o);

notifyAll();

access.release();

}// insert

– R 7 :: 19 – 0024 Spring 2010

NotesNotes

There is one semaphore for There is one semaphore for all all synchronized methods synchronized methods of one instance of “BoundedBuffer”of one instance of “BoundedBuffer”

Why?Why?

Must make sure that insert and extract don’t overlap.Must make sure that insert and extract don’t overlap.

There must be separate semaphore to deal with waiting There must be separate semaphore to deal with waiting threads.threads.

Why?Why?

Imagine the buffer is full. A thread that attempts to Imagine the buffer is full. A thread that attempts to insert an item must wait. But it must release the access insert an item must wait. But it must release the access semaphore. Otherwise a thread that wants to remove an item semaphore. Otherwise a thread that wants to remove an item will not be able to executed the (synchronized!) extract method.will not be able to executed the (synchronized!) extract method.

– R 7 :: 20 – 0024 Spring 2010

2) Translate wait and notifyAll2) Translate wait and notifyAll

The operationThe operation

wait()wait()

is translated intois translated into

waitThread ++waitThread ++

S.release(); S.release(); // other threads can execute synchronized // other threads can execute synchronized methods methods

SCond.acquire(); // wait until condition changesSCond.acquire(); // wait until condition changes

S.acquire();S.acquire();

waitThread --waitThread --

– R 7 :: 21 – 0024 Spring 2010

Each occurrrence of Each occurrrence of NotifyAll() NotifyAll() is translatedis translated

if (waitThread > 0) {if (waitThread > 0) {

for (int i=0; i< waitThread; i++) { for (int i=0; i< waitThread; i++) {

SCond.release();SCond.release();

}}

}}

All threads waiting are released and will compete to All threads waiting are released and will compete to (re)acquire S.(re)acquire S.

They decrement waitThread after they leave They decrement waitThread after they leave SCond.acquireSCond.acquire

Note that to enter the line (i.e., increment waitThread) the Note that to enter the line (i.e., increment waitThread) the thread must hold the access semaphore S thread must hold the access semaphore S

– R 7 :: 22 – 0024 Spring 2010

Recall that S.release is done at the end of the Recall that S.release is done at the end of the synchronized methodsynchronized method

So all the threads that had lined up waiting for So all the threads that had lined up waiting for SCond compete to get access to SSCond compete to get access to S

No thread can line up while the SCond.release No thread can line up while the SCond.release operations are done since this thread holds S.operations are done since this thread holds S.

– R 7 :: 23 – 0024 Spring 2010

NoteNote

We wake up only all threads – they might not be able to We wake up only all threads – they might not be able to enter their critical section if the condition they waited enter their critical section if the condition they waited for does not hold. But all threads get a chance.for does not hold. But all threads get a chance. This approach is different from what we discussed in the

lecture

– R 7 :: 24 – 0024 Spring 2010

Translate “wait()”Translate “wait()”

public void insert(Object o) throws InterruptedException {

access.acquire();

while (isFull()) {

waitThread ++;

access.release(); // let other thread access object

cond.acquire(); // wait for change of state

access.acquire()

waitThread --;

}

doInsert(o);

notifyAll();

access.release();

}// insert

– R 7 :: 25 – 0024 Spring 2010

Translate “notifyAll()”Translate “notifyAll()”

public void insert(Object o) throws InterruptedException {

access.acquire(); while (isFull()) {

waitThread ++;

access.release(); // let other thread access object

cond.acquire(); // wait for change of state

access.acquire()

waitThread --;

}

doInsert(o);

if (waitThread > 0) {

for (int i=0; i<waitThread; i++) {

cond.release(); } }

access.release();

}// insert

– R 7 :: 26 – 0024 Spring 2010

ExampleExample

Consider the buffer, one slot is empty, four operationsConsider the buffer, one slot is empty, four operationsinsert I1insert I1

insert I2insert I2

insert I2insert I2

extract Eextract E

I1 – access.acquireI1 – access.acquire

I2 – access.acquire: blocks on accessI2 – access.acquire: blocks on access

I3 – access.acquire: blocks on accessI3 – access.acquire: blocks on access

I1 – waitThread == 0 I1 – waitThread == 0

I1.releaseI1.release

– R 7 :: 27 – 0024 Spring 2010

(cont)(cont)

I2 – access.acquire completesI2 – access.acquire completes

I2 – buffer fullI2 – buffer full

waitThread =1waitThread =1

I2 – access.releaseI2 – access.release

I2 – cond.acquire – blocks on condI2 – cond.acquire – blocks on cond

I3 – access.acquire completesI3 – access.acquire completes

I3 – buffer full I3 – buffer full

waitThread = 2waitThread = 2

I3 – access.releaseI3 – access.release

I3 – cond.acquire – blocks on condI3 – cond.acquire – blocks on cond

– R 7 :: 28 – 0024 Spring 2010

(cont)(cont)

E – access.acquireE – access.acquire

remove itemremove item

E – SCond.releaseE – SCond.release

E – SCond.releaseE – SCond.release

E – access.releaseE – access.release

One of I2 or I3 will succeed with access.acquire and be One of I2 or I3 will succeed with access.acquire and be able to insert the next itemable to insert the next item

– R 7 :: 29 – 0024 Spring 2010

ExerciseExercise

How would the method “extract” look like (if we used How would the method “extract” look like (if we used semaphores to emulate monitors)?semaphores to emulate monitors)?

– R 7 :: 30 – 0024 Spring 2010

Assignment 7Assignment 7

Hint: Hint: Thread.currentThread() Thread.currentThread() returns a handle to returns a handle to the current thread and might be useful for the the current thread and might be useful for the assignment.assignment.

– R 7 :: 31 – 0024 Spring 2010

OverviewOverview

Your task is to implement a Read/Write LockYour task is to implement a Read/Write Lock

Max. 4 ThreadsMax. 4 Threads

Max. 2 Reader Threads (shared access is allowed) and Max. 2 Reader Threads (shared access is allowed) and 1 Writer Thread1 Writer Thread

A thread that executes read() is a readerA thread that executes read() is a reader

At a later time it can be a writer ....At a later time it can be a writer ....

– R 7 :: 32 – 0024 Spring 2010

The challengeThe challenge

No starvationNo starvation

Efficient implementationEfficient implementation If there are fewer than 2 readers and no waiting writers then

the next reader must be allowed to proceed If there is no contention then a thread must be allowed to

proceed immediately

Your implementation must be fair (you may want to use Your implementation must be fair (you may want to use FIFOQueue.java)FIFOQueue.java)

– R 7 :: 33 – 0024 Spring 2010

Other commentsOther comments

Keep your solution as simple as possibleKeep your solution as simple as possible

Decide what you want to use [your decision]Decide what you want to use [your decision] Monitors (synchronized methods) Semaphores

http://java.sun.com/j2se/1.5.0/docs/api/Semaphore

Only change class Monitor.javaOnly change class Monitor.java If you feel it‘s necessary to change other files, please let us

know.

Please comment your code!Please comment your code!

– R 7 :: 34 – 0024 Spring 2010

You may want to recallYou may want to recall

Thread.currentThread().getId()Thread.currentThread().getId() wait_list.enq(Thread.currentThread().getId()) wait_list.getFirstItem() == Thread.currentThread().getId()

– R 7 :: 35 – 0024 Spring 2010

Your log could look like this …Your log could look like this …

READ LOCK ACQUIRED 1READ LOCK ACQUIRED 1

READ LOCK RELEASED 0READ LOCK RELEASED 0

WRITE LOCK ACQUIRED 1WRITE LOCK ACQUIRED 1

WRITE LOCK RELEASED 0WRITE LOCK RELEASED 0

READ LOCK ACQUIRED 1READ LOCK ACQUIRED 1

READ LOCK ACQUIRED 2READ LOCK ACQUIRED 2

READ LOCK RELEASED 1READ LOCK RELEASED 1

READ LOCK ACQUIRED 2READ LOCK ACQUIRED 2

READ LOCK RELEASED 1READ LOCK RELEASED 1

READ LOCK ACQUIRED 2READ LOCK ACQUIRED 2