consensus-tut6 - Multicore Programming C nsus onse Tutorial...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Multicore Programming C nsus onse Tutorial 6 CS 0368-3469 S pring 2010 He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m Sm um ary onse • C nsus ue • Que <> A ic( • Atom n, n(n+1)/2) ate e e • Approxim agre m nt • gis r • Trinary re te am nsus • Te conse He rlihy-S havit 2008 22 TheArt of Multiproce Program ing ssor m C nsus onse C onsis nt : all thre de thesam value te ads cide e Valid: thecom on de m cision valueis som thre e ad's input He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 33 Ge ric C nsus Protocol ne onse abstract class ConsensusProtocol implements Consensus { protected Object proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object decide(object value); }} He rlihy-S havit 2008 (4) TheArt of Multiproce Program ing ssor m 44 (4) C nsus Num rs onse be ct nsus num r n be • An obje X has conse d ad nsus – I f it can beuse to solven-thre conse be s • Takeany num r of instance of X the ic ad/writere rs giste • toge r with atom re ple e ad nsus • and im m nt n-thre conse ad nsus – But not (n+1)-thre conse He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 55 Que <> ue upposeweaugm nt t heFIFO Queue<> class e •S with a peek() m thod that re e turns but doe not s re ovet hefirs e m nt in theque . m t le e ue how that theaugm nte que has infinite e d ue •S conse nsus num r. be He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 66 Que <> im m ntation ue ple e public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); this.queue.enq(threadID); return proposed[this.queue.peek()]; } This is corre be ct causeif X is thef irst inde t o be x e ue late e calls will not changeX's position, nque d, r nq() and X store its valuein propose be calling e . d d fore nq() He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 77 A ic Atom (n,n(n+1)/2) ic giste e • Atom (n,n(n+1)/2)-re r assignm nt for n>1 has conse nsus num r at le n. be ast He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 88 R m r: Re inde Atom (2,3) ic A Writes to 0 and 1 Writes to 1 and 2 He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 99 B A ic Atom (n,n(n+1)/2) lds • n fie r0…rn-1 lds re • n(n-1)/2 fie rij , whe i > j. ad ically assigns its input valueto • Each thre i atom n f ie (with its inde lds x). He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 1010 A ic Atom (n,n(n+1)/2) ad n ithe e • Re rij . I f thevalueis null, the ne r assignm nt has occurre d. rwise re ri and rj . I f ri valueis null, the j , ad n • Othe pre de i, and sim ce s ilarly for rj . ithe re qual • I f ne r ri nor rj is null, re ad rij . I f its valueis e t o thevaluere fromri, theHjrlihy-Shavit s i, e vice rsa. ad n e pre de2008 lse -ve ce TheArt of Multiproce Program ing ssor m 1111 b<d<…<c,a He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 1212 b<a<d<…<c He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 1313 Approxim agre m nt ate e e ate e e • Thetwo-thread approxim agre m nt class for a give ε: n all cide • C de (xi)=yi (xi is a real number). in(xa, xb), m ax(xa, xb)] • yi in [m • | ya - yb| < ε for ε > 0 nsus num r? be • What is theconse He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 1414 Approxim agre m nt solution ate e e an ple e d ic giste • C beim m nte using atom re rs → conse nsus == 1 yi ←xi I f yj= ┴ re turn yi while yi-yj| >ε) (| yi ←(yi+yj)/2 //aveTheArt of Multiprocessor Programming rage He rlihy-S havit 2008 1515 Trinary re te gis r s s pare e • Holds value [┴,0,1] , provide com AndS t() and ge t(). olve nsus for K-bits value s: • S n-conse giste – n trinary re rs giste – k trinary re rs He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 1616 Trinary re te solution gis r olution n trinaty re rs giste •S , • yi.cas(┴,1) ach • For e j<>i yj.cas(┴,0) turn sm s xj such that yj=1 alle t • re He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m 1717 Trinary re te solution gis r olution – k trinaty re rs giste •S I de try to se your bit or he a: t lp. 1. Try to se theall k bits t 2. I f fails he to he anothe thre lp lp r ad. atching value . – Look in theannouncearray for a m Re placeyours with theotheHerlihy-S1. 2008 r, goto havit – TheArt of Multiproce Program ing ssor m 1818 Te conse am nsus am nsus obje provide thesam propose and de () ct s e () cide • A Te conse m thods as conse e nsus. am nsus obje solve conse ct s nsus • A te conse as long as no m than t wo distinct value aree r propose . (I f ore s ve d m than two arepropose there ore d, sults areunde d.) fine how how to solvewait-fre n-thre conse e ad nsus, with up to n •S distinct input value , froma supplyerlihy-Shavit 2008nsus obje s am cts. H of te conse TheArt of Multiproce Program ing ssor m 1919 Te conse am nsus S olution 4 threads 2 threads 4 7 5 7 6 7 8 2020 4 4 3 4 5 1 1 2 He rlihy-S havit 2008 TheArt of Multiproce Program ing ssor m This work is lice d unde a C ativeC m Attribution-S Alike2.5 Lice . nse r re om ons hare nse • 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. He rlihy-S havit 2008 • Nothing in this license impairs or restricts the author's moral rights. Art of of Multiproce 21 TheArtMultiprocessor Programming ssor Program ing m 2121 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online