Docsity

Introduction to Hardware Locks & Waiting Algorithms: Atomic Primitives & Synchronization, Slides of Computer Science

Toggle sidebar
bg1
Objectives_template
file:///D|/...audhary,%20Dr.%20Sanjeev%20K%20Aggrwal%20&%20Dr.%20Rajat%20Moona/Multi-core_Architecture/lecture13/13_1.htm[6/14/2012 11:57:57 AM]
Module 7: Synchronization
Lecture 13: Introduction to Atomic Primitives
The Lecture Contains:
Synchronization
Waiting Algorithms
Implementation
Hardwired Locks
Software Locks
Hardware Support
Atomic Exchange
Test & Set
Fetch & op
Compare & Swap
Traffic of Test & Set
Backoff Test & Set
Test & Test & Set
TTS Traffic Analysis
Goals of a Lock Algorithm
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Introduction to Hardware Locks & Waiting Algorithms: Atomic Primitives & Synchronization and more Slides Computer Science in PDF only on Docsity!

Module 7: Synchronization

Lecture 13: Introduction to Atomic Primitives

The Lecture Contains:

Synchronization

Waiting Algorithms

Implementation

Hardwired Locks

Software Locks

Hardware Support

Atomic Exchange

Test & Set

Fetch & op

Compare & Swap

Traffic of Test & Set

Backoff Test & Set

Test & Test & Set

TTS Traffic Analysis

Goals of a Lock Algorithm

Module 7: Synchronization

Lecture 13: Introduction to Atomic Primitives

Synchronization

Types

Mutual exclusion Synchronize entry into critical sections Normally done with locks Point-to-point synchronization Tell a set of processors (normally set cardinality is one) that they can proceed Normally done with flags Global synchronization Bring every processor to sync Wait at a point until everyone is there Normally done with barriers

Synchronization

Normally a two-part process: acquire and release; acquire can be broken into two parts: intent and wait Intent: express intent to synchronize (i.e. contend for the lock, arrive at a barrier) Wait: wait for your turn to synchronization (i.e. wait until you get the lock) Release: proceed past synchronization and enable other contenders to synchronize Waiting algorithms do not depend on the type of synchronization

Module 7: Synchronization

Lecture 13: Introduction to Atomic Primitives

Hardwired Locks

Not popular today Less flexible Cannot support large number of locks Possible designs Dedicated lock line in bus so that the lock holder keeps it asserted and waiters snoop the lock line in hardware Set of lock registers shared among processors and lock holder gets a lock register (Cray Xmp )

Software Locks

Bakery algorithm

Shared: choosing[P] = FALSE, ticket[P] = 0;

Acquire : choosing[ i ] = TRUE; ticket[ i ] = max(ticket[0],…,ticket[P-1]) + 1;

choosing[ i ] = FALSE; for j = 0 to P-

while (choosing[j]); while (ticket[j] && ((ticket[j], j) < (ticket[ i ], i )));

endfor

Release : ticket[ i ] = 0;

Does it work for multiprocessors? Assume sequential consistency Performance issues related to coherence? Too much overhead: need faster and simpler lock algorithms Need some hardware support

Module 7: Synchronization

Lecture 13: Introduction to Atomic Primitives

Hardware Support

Start with a simple software lock

Shared: lock = 0; Acquire : while (lock); lock = 1; Release or Unlock : lock = 0;

Assembly translation

Lock: lw register, lock_addr /* register is any processor register */ bnez register, Lock addi register, register, 0x sw register, lock_addr Unlock: xor register, register, register sw register, lock_addr

Does it work?

What went wrong? We wanted the read-modify-write sequence to be atomic

Atomic Exchange

We can fix this if we have an atomic exchange instruction

a ddi register, r0, 0x1 /* r0 is hardwired to 0 */

Lock: xchg register, lock_addr /* An atomic load and store */

bnez register, Lock Unlock remains unchanged

Various processors support this type of instruction Intel x86 has xchg , Sun UltraSPARC has ldstub (load-store-unsigned byte), UltraSPARC also has swap Normally easy to implement for bus-based systems: whoever wins the bus for xchg can lock the bus Difficult to support in distributed memory systems

Module 7: Synchronization

Lecture 13: Introduction to Atomic Primitives

Compare & Swap

addi reg1, r0, 0x0 /* reg1 has 0x0 / addi reg2, r0, 0x1 / reg2 has 0x1 */ Lock: compare & swap reg1, reg2, lock_addr bnez reg2, Lock

Traffic of Test & Set

In some machines (e.g., SGI Origin 2000) uncached fetch & op is supported Every such instruction will generate a transaction (may be good or bad depending on the support in memory controller; will discuss later) Let us assume that the lock location is cacheable and is kept coherent Every invocation of test & set must generate a bus transaction; Why? What is the transaction? What are the possible states of the cache line holding lock_addr? Therefore all lock contenders repeatedly generate bus transactions even if someone is still in the critical section and is holding the lock Can we improve this? Test & set with backoff

Module 7: Synchronization

Lecture 13: Introduction to Atomic Primitives

Backoff Test & Set

Instead of retrying immediately wait for a while

How long to wait? Waiting for too long may lead to long latency and lost opportunity Constant and variable backoff Special kind of variable backoff : exponential backoff (after the i th attempt the delay is k* c i where k and c are constants) Test & set with exponential backoff works pretty well

delay = k Lock: ts register, lock_addr bez register, Enter_CS pause (delay) /* Can be simulated as a timed loop / delay = delayc j Lock

Test & Test & Set

Reduce traffic further

Before trying test & set make sure that the lock is free

Lock: ts register, lock_addr bez register, Enter_CS Test: lw register, lock_addr bnez register, Test j Lock

How good is it? In a cacheable lock environment the Test loop will execute from cache until it receives an invalidation (due to store in unlock); at this point the load may return a zero value after fetching the cache line If the location is zero then only everyone will try test & set