synchronization with shared memory amano, hideharu textbook pp.60-68

33
Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Upload: ethelbert-hunt

Post on 03-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Synchronization with shared memory

AMANO, Hideharu

Textbook pp.60-68

Page 2: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Fork-join: Starting and finishing parallel processes

fork Usually, these processes (threads)can share variables

fork

join

join

• Fork/Join is a way of synchronization• However, frequent fork/join degrades performance•Operating Systems manage them

These processes communicate each other

Synchronization is required !

Page 3: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Synchronization

An independent process runs on each PU (Processing Unit) in a multiprocessor. When is the data updated?

Data send/receive (readers-writers problems) A PU must be selected from multiple PUs.

Mutual exclusion All PUs wait for each other

Barrier synchronization

Synchronization

Page 4: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Readers-Writers   Problem

01

Writer : writes data then sets the synchronization flag

Reader : waits until flag is set

Writer Reader

Write(D , Data) ;Write(X,1) ;

X

Polling until(X==1) ;

Page 5: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Readers-Writers   Problem

Reader : reads data from D when flag is set, then resets the flag

Writer Reader

Writer : waits for the flag is reset

X

Polling until(X==0) ;Polling until(X==1) ;data = Read(D) ;Write(X , 0) ;

Page 6: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Multiple Readers

03

Writer : writes data into D, then writes 3 into c.

Reader :  Polling until( c!=0   )(Each reader reads data once.)

Writer ReaderD

c

Write(D , Data) ;Write(c , 3) ;

Page 7: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Multiple ReadersIterative execution

Reader: decrements cWriter : waits until c==0

Writer

Reader

3-1

22-1

There is a problem!!

Polling until(c==0) ;

Polling until(c!=0)data   =   Read(D);counter   =   Read(c) ;Write(c , counter-1) ;Polling until(c==0);

1-1

Page 8: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

The case of No Problem

3-1=2

2-1=1

counter=Read(c) ;Write(c , counter-1) ;

counter=Read(c) ;Write(c , counter-1) ;

1Shared data

PU

An example of correct operation

Page 9: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Problem

3-1=2

23-1=2

Multiple PUs may get the same number

An example of incorrect operation

Page 10: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Indivisible operation

The counter is a competitive variable for multiple PUs

A write/read to/from such a competitive variable must be protected inside the critical   section.

For protection, an indivisible operation which executes continuous read/write operations is required.

Page 11: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

An example of indivisible operation( Test   and   Set )

01

t =Test&Set(X)

11

Reads x and if 0 then writes 1 indivisibly.

Polling until (Test & Set(X)==0) ;

critical section

Write ( X , 0 );

Page 12: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Various indivisible operations

•Swap(x,y): exchanges shared variable x with local variable y•Compare & Swap (x,b.y): compares shared variable x and constant b, and exchanges according to the result.•And-write/Or-write: reads bit-map on the shared memory and writes with bit operation.•Fetch & *(x,y): general indivisible operation

•Fetch & Dec(x): reads x and decrements (if x is 0 do nothing).•Fetch&Add(x): reads x and adds y•Fetch&Write1: Test & set•Fetch&And/Or: And-write/Or-write

Page 13: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

An example using Fetch&Dec

Writer

Reader

3-1

22-1

Polling until(c==0) ;

data  =  Read(D);Fetch&Dec (c) ;Polling until( c ==0) ;

1-1

Page 14: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Multi-Writers/Readers Problem

Writer

Reader21

Selects a writer from multiple writers

data  =  Read(D);Fetch&Dec (c) ;Polling until(c==0) ;

1 Writer-Multi   Readers

Mutual exclusion

3

0

Page 15: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Implementation of a synchronization operation

PU PU

SnoopCache

PU

SnoopCache

PU

SnoopCache

Main   Memory

A   large   bandwidth   shared  bus

1 (DE)Test &Set

Read0

Modify

Write1

Bus mastership is lockedbetween Read/Write operation

0

Page 16: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Snoop   Cache and Synchronization

PU PUPU

SnoopCache

PU

SnoopCache

Main   Memory

A   large   bandwidth   shared  bus

1 (DE)Test &Set

Test &Set

1(CS)

Even for the line with CS, Test & Set requiresBus transaction

0

→CS

Page 17: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Test , Test & Set

PU PUPU

SnoopCache

PU

SnoopCache

Main   Memory

A   large   bandwidth   shared  bus

Test&Set Test

Test does not require bus, except with the first trial

DE   does not require bus transaction

Test&Set ( c ) if c == 0

0 1Test

Pollingfor CSline

1

Page 18: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Test , Test & Set

PU PUPU

SnoopCache

PU

SnoopCache

Main   Memory

A   large   bandwidth   shared  bus

Cache line is invalidatedRelease critical sectionby writing 0

Test&Set ( c ) if c == 0

0 1

0

1 -

Invalidationsignal

Page 19: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Test , Test & Set

PU PUPU

SnoopCache

PU

SnoopCache

Main   Memory

A   large   bandwidth   shared  bus

Release critical sectionby writing 0

Test&Set ( c ) if c == 0

0 0-

Write back

Test

Page 20: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Test , Test & Set

PU PUPU

SnoopCache

PU

SnoopCache

Main   Memory

A   large   bandwidth   shared  bus

Test & Set is really executed

Test&Set ( c ) if c == 0

0

Test & Set

If other PU issues the request, only a PU is selected.

01

Page 21: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Semaphore

High level synchronization mechanism in the operating system

A sender (or active process) executes V(s) (signal), while a receiver (or passive process) executes P(s) (wait).

P(s) waits for s = 1 and s ← 0 indivisibly. V(s) waits for s = 0 and s ←1 indivisibly. Busy waiting or blocking Binary semaphore or counting semaphore

Page 22: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Monitor

High level synchronization mechanism used in operating systems.

A set of shared variables and operations to handle them.

Only a process can execute the operation to handle shared variables.

Synchronization is done by the Signal/Wait.

Page 23: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Synchronization memory

Memory provides tag or some synchronization mechanism Full/Empty  bit Memory with Counters I - Structure Lock / Unlock

Page 24: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Full/Empty Bit

0Write

→ 1

A data cannot read from 0A data can write into 1

Suitable for 1-to-1 communication1

WriteX

1Read

→ 0

0Read

Page 25: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Memory with counters

0Write

→ 5

A data cannot read from 0A data cannot write except 0

Suitable for 1-to-many communication5

WriteX

5Read

→ 4

0Read

A large memory is required for tag

Page 26: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

I-Structure

PU

SnoopCache

PU

SnoopCache

PU

SnoopCache

PU

SnoopCache

Main   Memory

A   large   bandwidth   shared  bus

Full/Empty with informing mechanism to the receiving PU

An example usingWrite-updateSnoop cache

Page 27: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Lock / Unlock

Process Lock

Process Lock

Process Lock

Process Lock

Process Lock

Page/Line

Only registered processes can be written into the locked page.

Page 28: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Barrier Synchronization

Barrier ;

Barrier ;

Barrier ; Established

All processors (processes) must wait for the barrier establishment.

Wait for other processes

Barrier Operation : Indivisible operations like Fetch&Dec            Dedicated hardware

Page 29: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Dedicated hardware

Open collecter orOpen Drain

Reaches to the barrier

Not yet

Reaches to the barrier

If there is 0, the entire line becomes 0

Page 30: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Extended barrier synchronizations Group barrier : A certain number of PUs for

ms a group for a barrier synchronization. Fuzzy barrier : Barrier is established not at

a line, but a zone. Line barrier vs. Area barrier

Page 31: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Fuzzy barrier

PREP ;

PREP ;

Establish

X

X

X

PREP;

Prepare (PREP) or Synchronize (X), then barrier is established.

Page 32: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Summary

Synchronization is required not only for bus connected multiprocessor but for all MIMD parallel machines.

Consistency is only kept with synchronization →Consistency   Model

Synchronization with message passing → Message passing model

Page 33: Synchronization with shared memory AMANO, Hideharu Textbook pp.60-68

Exercise

Write a program for sending a data from PU A to PU B,C,D only using Test & Set operations.