java concurrent programming consensus

Upload: invasion101

Post on 04-Jun-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Java Concurrent Programming Consensus

    1/21

    Multicore Programming

    Consensus

    Tutorial 6CS 0368-3469Spring 2010

  • 8/13/2019 Java Concurrent Programming Consensus

    2/21

    2

    Summary

    Consensus

    Queue

    Atomic( n, n(n+1)/2)

    Approximate agreement

    Trinary register Team consensus

  • 8/13/2019 Java Concurrent Programming Consensus

    3/21

    3

    Consensus

    Consistent: all threads decide the samevalue

    Valid: the common decision value issome thread's input

  • 8/13/2019 Java Concurrent Programming Consensus

    4/21

    4

    Generic Consensus Protocol

    abstract classConsensusProtocolimplementsConsensus {protectedObject[] proposed =newObject[N];private voidpropose Object value) {proposed[ThreadID.get )] = value;}abstractpublicObjectdecide object value);}}

    (4) (4)

  • 8/13/2019 Java Concurrent Programming Consensus

    5/21

    5

    Consensus Numbers

    An object X has consensus numbern If it can be used to solve n-thread

    consensus Take any number of instances of X

    together with atomic read/write registers

    and implement n-thread consensus

    But not (n+1)-thread consensus

  • 8/13/2019 Java Concurrent Programming Consensus

    6/21

    6

    Queue

    Suppose we augmentthe FIFOQueueclass with a peek()method

    that returns but does not removethefirst element in the queue.

    Show that the augmentedqueue hasinfinite consensus number.

  • 8/13/2019 Java Concurrent Programming Consensus

    7/21

    7

    public classQueueConsensusextendsConsensusProtocol {privateQueue queue;publicdecide object value) {propose value);this.queue.enq threadID);returnproposed[this.queue.peek )];}

    Queue implementation

    This is correct because if X is the first indexto beenqueued, later enq() calls will not change X's position,and X stored its value in proposed[] before calling enq().

  • 8/13/2019 Java Concurrent Programming Consensus

    8/21

    8

    Atomic (n,n(n+1)/2)

    Atomic (n,n(n+1)/2)-registerassignment for n>1 has consensus

    number at least n.

  • 8/13/2019 Java Concurrent Programming Consensus

    9/21

    9

    Reminder: Atomic (2,3)

    Writesto 0 and1

    Writesto 1 and2

    A

    B

  • 8/13/2019 Java Concurrent Programming Consensus

    10/21

    10

    Atomic (n,n(n+1)/2)

    n fields r0rn-1

    n(n-1)/2 fields rij, where i > j.

    Each thread i atomically assigns itsinput value to n fields (with its index).

  • 8/13/2019 Java Concurrent Programming Consensus

    11/21

    11

    Atomic (n,n(n+1)/2)

    Read rij . If the value is null, then neitherassignment has occurred.

    Otherwise, read ri and rj . If rivalue isnull, thenj precedes i, and similarly for rj .

    If neither ri nor rj is null, reread rij . Ifits value is equal to the value read from ri,thenj precedes i, else vice-versa.

  • 8/13/2019 Java Concurrent Programming Consensus

    12/21

    12

    b

  • 8/13/2019 Java Concurrent Programming Consensus

    13/21

    13

    b

  • 8/13/2019 Java Concurrent Programming Consensus

    14/21

    14

    Approximate agreement

    The two-threadapproximateagreement class for a given :

    Call decide(xi)=yi(xiis a real number). yi in [min(xa, xb), max(xa, xb)]

    |ya -yb| < for > 0

    What is the consensus number?

  • 8/13/2019 Java Concurrent Programming Consensus

    15/21

    15

    Approximate agreement

    solution Can be implemented using atomic

    registers consensus == 1

    yixiIf yj= return yiwhile(|yi-yj|>)

    yi(yi+yj)/2 //average

  • 8/13/2019 Java Concurrent Programming Consensus

    16/21

    16

    Trinary register

    Holds values [,0,1], providescompareAndSet() and get().

    Solve n-consensus for K-bitsvalues: n trinary registers

    k trinary registers

  • 8/13/2019 Java Concurrent Programming Consensus

    17/21

    17

    Trinary register solution

    Solution n trinaty registers

    yi.cas(,1)

    For each jiy

    j

    .cas(,0)

    return smallest xjsuch that yj=1

  • 8/13/2019 Java Concurrent Programming Consensus

    18/21

    18

    Trinary register solution

    Solution k trinaty registers

    Idea: try to set your bit or help.

    1. Try to set the all k bits

    2. If fails help to help another thread.

    Look in the announce array for a matchingvalue.

    Replace yours with the other, goto 1.

    3. return current value.

  • 8/13/2019 Java Concurrent Programming Consensus

    19/21

    19

    Team consensus

    A Team consensus object provides the samepropose() and decide() methods as consensus.

    A team consensus object solves consensusas long as no more than two distinct values areever proposed. (If more than two are proposed,the results are undefined.)

    Show how to solve wait-free n-thread consensus,with up to n distinct input values, from a supply ofteam consensus objects.

  • 8/13/2019 Java Concurrent Programming Consensus

    20/21

    20

    Team consensus Solution

    174

    4 5 7

    1 3 5 7

    4

    82 4 6

    2 threads

    4 threads

    http://creativecommons.org/licenses/by-sa/2.5/http://creativecommons.org/licenses/by-sa/2.5/
  • 8/13/2019 Java Concurrent Programming Consensus

    21/21

    21Art of MultiprocessorProgramming 21

    This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.

    You are free:

    to Shareto copy, distribute and transmit the work

    to Remixto adapt the work

    Under the following conditions:

    Attribution. You must attribute the work to The Art ofMultiprocessor Programming (but not in any way that suggests thatthe authors endorse you or your use of the work).

    Share Alike. If you alter, transform, or build upon this work, youmay distribute the resulting work only under the same, similar or acompatible license.

    For any reuse or distribution, you must make clear to others the licenseterms 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 fromthe copyright holder.

    Nothing in this license impairs or restricts the author's moral rights.

    http://creativecommons.org/licenses/by-sa/2.5/http://creativecommons.org/licenses/by-sa/2.5/http://creativecommons.org/licenses/by-sa/2.5/http://creativecommons.org/licenses/by-sa/2.5/http://creativecommons.org/licenses/by-sa/2.5/http://creativecommons.org/licenses/by-sa/2.5/http://creativecommons.org/licenses/by-sa/2.5/http://creativecommons.org/licenses/by-sa/2.5/http://creativecommons.org/licenses/by-sa/2.5/