Sunteți pe pagina 1din 19

Multicore Programming

Shared Memory & Synchronization Tutorial 5 CS 0368-3469 Spring 2010

Summary
Registers AtomicMRSWRegister Q. Consensus Queue<> Q. Team consensus Q.

Safe Register
Some valid value if reads and writes do overlap
$*&v 1111
33

write(1001)

read(????)

0000

1001
2007 Herlihy & Shavit

Regular Register
write(0) write(1)

read(1)

read(0)

Single Writer Readers return:

Old value if no overlap (safe) Old or one of new values if overlap


2007 Herlihy & Shavit 44

Atomic Register
write(1001) write(1010) read(1010)

read(1001)

read(1010)

Linearizable to sequential safe register


2007 Herlihy & Shavit 55

Safe Boolean MRSW from Safe Boolean SRSW


public class SafeBoolMRSWRegister implements Register<Boolean> { private SafeBoolSRSWRegister[] r = new SafeBoolSRSWRegister[N]; public void write(boolean x) { for (int j = 0; j < N; j++) r[j].write(x); } public boolean read() { int i = ThreadID.get(); return r[i].read(); }}
Art of Multiprocessor Programming 66

Safe Boolean MRSW Qeustion


True/False: If we replace the safe Boolean SRSW registers with regular Boolean SRSW registers, then the construction yields an regular Boolean MRSW register.

Safe Boolean MRSW Qeustion cont.


R[0]=1 R[1]=1 1read(R[0]) 1/0read(R[1]) 0read(R[2]) R[2]=1

time
2007 Herlihy & Shavit 88

AtomicMRSWRegister
public class AtomicMRSWRegister<T> implements Register<T> { ThreadLocal<Long> lastStamp; // last timestamp written private StampedValue<T>[][] a_table; // each entry is SRSW atomic
public AtomicMRSWRegister(T init, int readers) { this.lastStamp = new ThreadLocal<Long>() { protected Long initialValue () { return 0; }; }; a_table = new StampedValue[readers][readers]; StampedValue<T> value = new StampedValue<T>(init); for (int i = 0; i < readers; i++){ for (int j = 0; j < readers; j++){ a_table[ i ][ j ] = value; } } }
9

AtomicMRSWRegister
public T read() { int me = ThreadID.get(); StampedValue<T> value = a_table[me][me]; // former read
for (int i = 0; i < a_table.length ; i++) // finds max in row value = StampedValue.max(value, a_table[me][i]); for (int i = 0; i < a_table.length ; i++) // writes max in column a_table[i][me] = value; }

return value ;

10

AtomicMRSWRegister
public void write (T v){ long stamp = lastStamp.get() + 1; lastStamp.set( stamp); // remember for next time StampedValue<T> value = new StampedValue<T>(stamp, v); // writes a column for (int i = 0; i < a_table.length ; i++) a_table [i][0] = value; }
11

AtomicMRSWRegister
One per thread
1 1:45 1:45 1:45 1234 1234 1234 1:45 1:45 1:45 2 1234 1234 1234 1:45 1:45 1:45 3 1234 1234 1234 1 2 3

2007 Herlihy & Shavit

12 12

AtomicMRSWRegister Qeustion
True/False: If we replace the atomic SRSW registers with regular SRSW registers, then the construction still yields an atomic MRSW register.

13

AtomicMRSWRegister Qeustion cont.


Yes it does, because regular registers act differently only when two reads are done concurrently with a write.

1:45 1234

write(2:00 5678) read(2:00 5678) read(1:45 1234)

In which case time its OK to read 1234


2007 Herlihy & Shavit 14 14

AtomicMRSWRegister Qeustion cont.


1:45 1234
write(2:00 5678)

read(2:00 5678)

read(1:45 1234)
read(2:00 5678)

In which case Blue will complete writing 2:00 5678 to its column

time

2007 Herlihy & Shavit

15 15

Write the read method for a write-once SRSW register.


public class AcmRegister { //Atomic MRSW registers private BoolRegister[] b = new BoolRegister[3*N]; public void write(int x){ boolean[] v = intToBooleanArray(x); for( int i=0 ; i<N ; ++i) b[i].write(v[i]); for( int i=0 ; i<N ; ++i) b[N+i].write(v[i]); for( int i=0 ; i<N ; ++i) b[(2*N)+i].write(v[i]);

AcmRegister Qeustion

public int read(){ } 16

AcmRegister Qeustion cont.


public class AcmRegister { public int read(){ for( int i=0 ; i<N ; ++i) last[i] = b[(2*N)+i].read(); for( int i=0 ; i<N ; ++i) middle[i] = b[N+i].read(); for( int i=0 ; i<N ; ++i) first[i] = b[i].read();

return choose(first, middle, last);

17

AcmRegister Qeustion cont.


If first == middle return first Else If last == middle return last Else //writer in the middle return first
18

This work is licensed under a Creative Commons AttributionShareAlike 2.5 License.


You are free: to Share to copy, distribute and transmit the work to Remix to adapt the work Under the following conditions: Attribution. You must attribute the work to The Art of Multiprocessor Programming (but not in any way that suggests that the authors endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to http://creativecommons.org/licenses/by-sa/3.0/. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights.

Art of Multiprocessor Programming

19

S-ar putea să vă placă și