Docsity

Synchronization Techniques for Multi-Processor Systems: Mutexes, Atomic Ops, and Barriers , Exams of Computer Science

Partial preview of the text

Download Synchronization Techniques for Multi-Processor Systems: Mutexes, Atomic Ops, and Barriers and more Exams Computer Science in PDF only on Docsity!

CS

Synchronization

Synchronization • Shared counter/sum update example

– Use a mutex variable for mutual exclusion – Only one processor can own the mutex

•Many processors may call lock(), but

only one will succeed (others block)

•The winner updates the shared sum,

then calls unlock() to release the mutex

•Now one of the others gets it, etc.

– But how do we implement a mutex?

•As a shared variable (1 – owned, 0 –free)

Atomic Read-Update Instructions • Atomic exchange instruction

– E.g., EXCH R1,78(R2) will swap content of register R1 and

mem location at address 78+R

– To acquire a mutex, 1 in R1 and EXCH

  • Then look at R1 and see whether mutex acquired • If R1 is 1, mutex was owned by somebody else

and we will need to try again later

  • If R1 is 0, mutex was free and we set it to 1,

which means we have acquired the mutex

• Other atomic read-and-update instructions

– E.g., Test-and-Set

LL & SC Instructions • Atomic instructions OK, but specialized

– E.g., SWAP can not atomically inc a counter

• Idea: provide a pair of linked instructions • A load-linked (LL) instruction

– Like a normal load, but also remembers the address in a

special “link” register

• A store-conditional (SC) instruction

– Like a normal store, but fails if its address is not the same

as that in the link register

– Returns 1 if successful, 0 on failure

• Writes by other processors snooped

– If address matches link address, clear link register

Implementing Locks • A simple swap (or test-and-set) works

– But causes a lot of invalidations

•Every write sends an invalidation •Most writes redundant (swap 1 with 1)

• More efficient: test-and-swap

– Read, do swap only if 0

•Read of 0 does not guarantee success (not atomic) •But if 1 we have little chance of success

– Write only when good chance we will succeed

Example: Test and Test and Set

try: mov

R3,#

ll

R2,0(R1)

sc

R3,0(R1)

bnez R2,try beqz R3,try try: mov

R3,#

ll

R2,0(R1)

bnez R2,try sc

R3,0(R1)

beqz R3,try

Simple Barrier Synchronization

lock(counterlock); if(count==0) release=0; / First resets release / count++; / Count arrivals / unlock(counterlock); if(count==total){ / All arrived / count=0; / Reset counter / release = 1; / Release processes / }else { / Wait for more to come / spin(release==1); / Wait for release to be 1 / }

Problem: not really reusable - Two processes: fast and slow - Slow arrives first, reads release, sees 0 - Fast arrives, sets release to 1, goes on to execute other code, comes to barrier again, resets release, starts spinning - Slow now reads release again, sees 0 again - Now both processors are stuck and will never leave

Correct Barrier Synchronization

localSense=!localSense; / Toggle local sense / lock(counterlock); count++; / Count arrivals / if(count==total){ / All arrived / count=0; / Reset counter / release=localSense; / Release processes / } unlock(counterlock); spin(release==localSense); / Wait to be released /

• Release in first barrier acts as reset for second

  • When fast comes back it does not change release,

it just waits for it to become 0

  • Slow eventually sees release is 1, stops spinning,

does work, comes back, sets release to 0, and both go forward.

init: localSense = 0, release = 0

Large-Scale Systems: Locks • Contention even with test-and-test-and-set

– Every write goes to many, many spinning procs – Making everybody test less often reduces contention for

high-contention locks but hurts for low-contention locks

– Solution: exponential back-off

  • If we have waited for a long time, lock is probably high-contention • Every time we check and fail, double the time between checks
    • Fast low-contention locks (checks frequent at first) – Scalable high-contention locks (checks infrequent in long waits)

– Special hardware support

Memory Consistency • Coherence is about order of accesses to the same address• Consistency is about order of accesses to different addrs• Possible outcomes on Proc 2 in program order

  • (F,D) can be (0,0), (0,1), (1,1)

• Execution order can also give (1,0)

  • Exposes instruction reordering to programmer– We need something that makes sense intuitively Proc 1 ST 1->DST 1->F Proc 2 LD FLD D

Program order

Execution order Proc 1 ST DST F

Proc 2 LD DLD F

Proc 2 can reorder loads

Sequential Consistency • The result of any execution should be the same as if

the accesses executed by each processor were keptin order and the accesses among differentprocessors were arbitrarily interleaved

– The same interleaved order seen by everybody

• Simple implementation

– A processor issues next access only when its previous

access is complete

– Sloooow!

• A better implementetion

– Processor issues accesses as it sees fit, but detects and

fixes potential violations of sequential consistency

Relaxed Consistency Models • Two kinds of memory acesses

– Data accesses and synchronization accesses

• Synchronization accesses determine order

– Data accesses ordered by synchronization – Any two of accesses to the same variable in two different

processes, such that at least one of the accesses is awrite, are always ordered by synchronization operations

• Good performance even in simple implementation

– Sequential consistency for sync accesses – Data accesses can be freely reordered

(except around sync accesses)