© 2005 bei Prof. Dr. Uwe Kastens © 2014 bei Prof. Dr. Uwe Kastens • • • Exercises: • • • • aretaughttounderstandandapply The participants Homework assignments willbesolved by thoseteams. Small teamswillsolve by given alecturer. assignments practically supported The exercises willbetightlyintegrated withthelectures. fundamental concepts English languageinalecture. techniques systematic methods Parallel Programming typicalfor parallel programming inJava; Prof. Dr. UweKastens Winter 2014/2015 for developing parallel programs, and Objectives high-level paradigms ofparallel programs, PPJ-2 PPJ-1 © 2006 bei Prof. Dr. Uwe Kastens hed,GP, KMS KMS Modellierung GP, KMS GP, KMS KMS ofprograms verfication ofproperties Course thatteaches it , monitorsinJava threads, KMS KMS , concurrency, parallelism, monitor derProgrammierung 2, Grundlagen address spaces, threads, processstates interleaved execution process, concurrency, 2 parallelism, 1, Programmierung der Grundlagen foundations inparallel programming practical experience inprogramming Java Topic ekTopic Week 49 SynchronousmessagePassing 9. 14 8.MessagesinDistributed Systems 12 7.AsynchronousMessagePassing 11 6.DataParallelism: LoopParallelization 7 5.DataParallelism: Barriers 6 4.SystematicDevelopment ofMonitors 5 3.MonitorsinGeneral andinJava 4 ofParallel 2.Properties Programs 2 1.Introduction 1 10. Conclusion Prerequisites Contents Systemsoftware (KMS) Konzepte undMethodender PPJ-4 PPJ-3 © 2012 bei Prof. Dr. Uwe Kastens © 2014 bei Prof. Dr. Uwe Kastens Doug Lea: Jim Farley: Wong:Scott Oaks, Henry David Gries: Gregory R.Andrews: Gregory R.Andrews: Gregory „ Course material „ Course material „ Course material derProgrammierung“ (inGerman) „Grundlagen Course material „ Course material programming, http://ag-kastens.upb.de/lehre/material/model http://ag-kastens.upb.de/lehre/material/swei http://ag-kastens.upb.de/lehre/material/ppje Concurrent Programming inJava Java The ScienceofProgramming Modellierung Konzepte undMethodenderSystemsoftware Software-Entwicklung I+II Parallel Programming Foundations ofmultithreaded, parallel,anddistributed Concurrent Programming Addison-Wesley, 2000 Java Threads Organization ofthecourse “ (inGerman) , 2nded.,O‘Reilly, 1999 Literature “ , O‘Reilly, 1998 , Springer-Verlag, 1981 “ WS, SS1998/1999:(inGerman) , Addison-Wesley, 2ndEd., 2000 , Addison-Wesley, 1991 “ (inGerman) PPJ-5 PPJ-6 © 2011 bei Prof. Dr. Uwe Kastens © 2005 bei Prof. Dr. Uwe Kastens Threads concurrent processes: interleaved processes parallel processes process process switching iseasyandfast. Processes, thatareexecuted inparallel orinterleaved inonecommonaddressspace; several processes, whichareexecuted simultaneously onseveral processors Variable state:contentsofthestorage andthepositionofexecution ofaprogramExecution initsstorage (addressspace). ofasequentialpart processes, thatcanbeexecuted inparallel orinterleaved frequent processswitching gives theillusionthatallprocessexecute steadily. interleaved execution cansimulate parallel execution; processes areswitched by acommonprocessmanagerorby theprocessesthemselves. several processes, whichareexecuted onasingleprocessor piecewise alternatingly Fundamental notions(repeated):Statesandtransitionsof : (lightweight processes, Leichtgewichtsprozesse): Fundamental notions(repeated):Parallel processes p3 p2 p1 : p3 p2 p1 : release bereit ready deallocate processor allocate processor

processes Scheduling wartend blocked rechnend running

block see KMS2-17,2-18 PPJ-7 PPJ-8 © 2005 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens • • Technique 1 Processes, threadsinJava The processiscreatedasan This technique(implement theinterface The following theprocess: callstarts the the i. e. itperforms itstask(method objects concurrently executed inthe { ... class MyTaskimplementsRunnable } aTask.start(); aTask=new(newMyTask (...)); user‘s class istobedefined asasubclass new process neednotbeinfluenced Create threadsinJava -technique: implement public voidrun() public MyTask(...){...} {...} ofclass The applicationclassesfollow • • • • • : A visualization ofprocesses,games ; e. g.productioninafactory to avoid blocking oftheuserinterface. processes, Time consumingcomputationsshouldbeimplementedby concurrent Events arepropagatedby aspecificprocessofthesystem. e. g. parallel sorting ofhugesetsdata e. g.parallel sorting several processescooperate onacommontask, e. control g.factoryrobots, airplane processes inthecomputercontrolexternal facilities, Event-based userinterfaces Speed-up ofexecution Control Animation Simulation user‘sclass implementstheinterface Thread ofmachinesin Applications ofparallelprocesses : ofrealprocesses: The new process starts executingThe new process starts inparallel with theinitiatingone. with certain properties withcertain : object ofthepredefinedclass common address space run ) and then terminates, or ) andthenterminates, Real-Time: The interface requirestoimplementthemethod by parallel computation: Runnable different objectives any further; - the program part tobeexecuted- theprogram part asaprocess. : ofaclassdifferent from ) shouldbeusedif Runnable oftheprogram (orapplet), Thread . The constructor method. The constructor : : Runnable Thread run PPJ-10 PPJ-9 © 2005 bei Prof. Dr. Uwe Kastens © 2005 bei Prof. Dr. Uwe Kastens public finalvoidstop() throwsSecurityException; public staticvoidsleep(longmillisec) throwsInterruptedException; public voidjoin()throwsInterruptedException; public voidresume(); public voidsuspend(); public voidstart(); public voidrun(); The processiscreatedasan The user‘sclassisdefinedasa Technique 2 the new process The classcannothave anothersuperclass! it. e. theprocessortoterminate g.tointerrupt methods further This technique(subclassof The following theprocess: callstarts not tobeused! the callingprocesswaits atleastfor thegiven timespan(inmilliseconds), e. g. the callingprocesswaits untiltheindicatedprocesshasterminated (deprecated), e.suspends theindicatedprocesstemporarily: g. (deprecated, -prone), theexecutionstarts oftheprocess is tobeoverridden withamethodthatcontainsthecodetobeexecuted asaprocess try {Thread.sleep (1000);}catch(Exceptione){} try {auftrag.join();}catch(Exception e){} } ... { class DigiClockextendsThread clock.start(); Thread clock=newDigiClock(...); DigiClock (...){...} {...} public voidrun() Create threadsinJava -technique: subclass of resumestheindicatedprocess: : May theprocessinaninconsistentstate. terminate needs to be further influenced needs tobefurther oftheuser‘sclassaretobedefinedandcalledfromoutsideclass, Important methodsoftheclassImportant Thread The new process starts executingThe new processstarts inparallel withtheinitiatingone. object oftheuser‘sclass subclass ofthepredefinedclass clock.resume(); ) shouldbeusedif clock.suspend(); The program part tobeexecutedThe program part asaprocess. ; hence, Overrides the (itisa Thread Thread Thread The constructor method. The constructor Thread objectaswell): : Thread method run PPJ-12 PPJ-11 . © 2008 bei Prof. Dr. Uwe Kastens © 2011 bei Prof. Dr. Uwe Kastens Technique } • • inanapplet Processes, whicharestarted } publicvoidinit() { public classDigiAppextendsApplet The processiscreatedinthe • • • publicvoidrun() { class DigiClockextendsThread every secondasaformatted text. The processdisplays the Otherwise they bindresources, although they arenot visible. are to be terminated ( are tobeterminated may besuspended,while theappletisinvisible ( other is tobeinterrupted is tobeterminated better usesynchronization orcontrolvariables instead of private Labelline; public DigiClock(Labelt){line=t;} private volatilebooleanrunning=true; public voidstopIt(){running=false;} } private DigiClockclock; public voiddestroy(){clock.stopIt(); } public voidstop(){/*see below */} public voidstart(){/*see below */} } ("------"); Label new = clockText Label { while(running) { } line.setText(newDate().toString()); { clock.start(); clock =newDigiClock(clockText); add (clockText); super classes arenotneeded try {sleep(1000);}catch(Exceptionex){} process asasubclass of Example: Digitalclock asaprocess inanapplet(2) Example: Digitalclock asaprocess inan applet (1) by acallof by callsoffurther stopIt current dateandtime init ), whentheappletisdeallocated ( stopIt methodofthesubclass Thread Method, that terminates theprocessfromoutside: Method, thatterminates . Thread , , becauseit methods, iterate until it is terminated fromtheoutside iterate untilitisterminated stop , start suspend Applet ); destroy label tobeusedfor thetext , resume : terminate process terminate ). suspend process resume process raeprocess create write thedate write state variable start process start pause PPJ-14 PPJ-13 © 2011 bei Prof. Dr. Uwe Kastens © 2009 bei Prof. Dr. Uwe Kastens Example A: Proof that in parallel. Branches of Example A: Does an Explain! Check each proof for correctness! {x+1=1} co x :=0;y0 z :=x+y {x=1 oc {y+1=1} z :=x+y oc x:=+1// co x :=0;y0 Hoare Logic, Weakest Precondition,techniquesfor parallel programs Methods: ∧ assignment ofprocess p y=1} x :=+1 y :=+1 z =2 y :=+1 co-oc → {z=2} 2. PropertiesofParallel Programs holdsattheend. {x+y=2} • • • Goals: areexecuted certain develop proof properties formal reasoning Proofs ofparallelprograms {x=0 {y=1} {x=1} properties canbeproven ∧ parallel programs suchthat y=0} // interfere withan ofparallel programs aboutparallel programs Example B Example B proven. Show that Example B: z :=x+y {x+1>0} {y+1>0} co x :=0;y0 {x>0 oc z :=x+y {x+1=1} {x=1 oc {y+1=1} co x :=0;y0 z :=x+y oc x:=y+1// co x :=0;y0 assertion ofprocessassertion q ∧ ∧ 2 1 y>0} y=1} : : z =2 y :=x+1 y :=x+1 x :=y+1 y :=x+1 x :=y+1 → → {z {z=2} cannotbe {x+y {x+y=2} ≥ 2} {x {x=0 ≥ {y>0} {x>0} ≥ {y=1} {x=1} 0 2} ∧ ∧ ? y y=0} // // ≥ PPJ -15ab PPJ -15a 0} © 2008 bei Prof. Dr. Uwe Kastens © 2008 bei Prof. Dr. Uwe Kastens Predicates Formal calculus for combined. theeffectsdescribe ofeachkindstatement, anddefinehow proofstepscanbecorrectly using areconstructed Proofs ofprogram properties {true} Two are: specialassertions correctness). (partial If theexecution of The triple Q Each triple achieve atthatposition.Thepredicatesareexpressions over variables oftheprogram. A predicate, like apostconditionof no operation P of xsubstitutedby e free occurrences {P} assignment statement sequence [x/e] {P} {Q} {P} { P skip {P} S1{Q}S2{R} means:Pwithall characterizing allstates, characterizing and Axioms andinference rulesfor sequentialconstructs [x/e] S S S {P} S{Q} 1 {P} S{Q} (assertions) arestatedfor program (assertions) positions: 2 1 }x:=e{P} ; S {P} 2 Q {R} {R} {Q} , characterizes the S proving propertiesofalgorithmsorprograms isbeguninastateof S 7 . describes aneffect describes oftheexecution of is correct,ifthefollowing holds: Hoare Logic:abriefreminder 1 2 6 {INV} {INV selecting iteration set ofstates {false} ∧ do 3 5 stronger precondition B P i } S multiple alternative (guarded command) Β and {Q} S { R} { P} {Q} S { P} {P P {P} 1 i → {N} 1 {INV}, ∧ ¬(Β ∧ characterizing nostate. characterizing if if itterminates S B Β 1 → thatany execution oftheprogram can i } S 1 ]..[] ... [] axioms 1 → ∨...Β i {} 1 {Q}, { R} S ≤ 1 Β i []... ≤ n and n → n ) ⇒ ≤ S S , thefinalstateisin . i inference rules Β n P ≤ Q n iscalledaprecondition, od weaker postcondition n → [C. A.R.Hoare, 1969] {INV {R} { R} S { P} S {Q} S { P} n fi ¬(Β ∧ {Q} → which 1 { Q} ∨...∨Β PPJ -15c PPJ -15b Q n ) 4 } © 2005 bei Prof. Dr. Uwe Kastens © 2011 bei Prof. Dr. Uwe Kastens otodto:a=G with postcondition: rcniin x,y precondition: do a { INV: Gisgcdofaandb a :=x;by; { Gisgcdofxandy { a=G} { INV od If ( terminate execution of wp (S,Q)=P Q The states. preconditions weakest Program thatcharacterize program positionsarealsoannotatedby assertions of notion the on based is Logic 1981]: [Dijkstra, 1976;Gries Hoare as calculus similar A for parallel programs. We needweakest preconditiononlyinthedefinition of„non-interference“ in proof proofmethodcomparedtoHoareLogic. This conceptisamoregoaloriented total correctness { INV fi [] a<=b-> if a>b-> { INV} onapredicate P ∧ ≠ weakest precondition ⇒ a { INV b-> ∧ = wp (S,Q) { INV} b :=-a { Gisgcdofaandb-a { Gisgcdofaandb { INV} a :=-b { Gisgcdofa-bandb { Gisgcdofaandb a { assertions over variables{ assertions } b } inastateof ≠ ∧ b} → a S Verification: algorithm computesgcd isbeguninany stateof ≠ ∈ , b P characterizes Ι (wpisa ∧ ∧ ¬ N ). then x>0 i. e. x>0,y0;letGbegreatest commondivisorofxandy (a>b Q ∧ {P} S{Q} ∧ Weakest precondition a>0 y>0 } predicate transformer ∨ wp (S,Q)=P ∧ ∧ a ∧ a>0 a>0 ≤ ∧ ∧ b>0 } the largest setofstates b) a>0 a-b>0 → ∧ ∧ : holdsinHoareLogic. b>0 b>0 ∧ INV} P , thentheexecution is b-a>0 } ∧ b>0 } ∧ ∧ b>a} a>b} „there isno3rdcasefor theif->INV“ ofastatement → → ). • • the loopterminates: a+b >0isinvariant a+b decreasesmonotonic suchthatifthe S guaranteed to mapsapredicate PPJ -15e PPJ-15d © 2005 bei Prof. Dr. Uwe Kastens © 2011 bei Prof. Dr. Uwe Kastens We getthe and check theresultsofany interleaved execution order. The coarser execution modelissufficient. A Processes thatarenotblocked may beswitched Assume thattheassignments Two processesoperate onacommonvariable An example shows how different resultsareexhibited by switching processesdifferently. Assume thesequences ofstatements processes onasingleprocessor. Check whatthevalue of scheduling strategy rcs2 2=acut 2=t ;account= t2; Process2: t2=account; - 5; Process1: t1=account; + 10;account=t1; 1. 6. 4. 2. .wp(S, 7. 5. 3. Interleaving -usedasanabstractexecution model same variety ofresults P (x =y+1) false (y true i =wp(statement, ≤ ≥ 0 x) wp( = wp( = wp( = =wp( = account =50; true Examples for weakestpreconditions =wp( = reducesthatfreedomofthescheduler.

) = d

if x>=ythenz:=else a if x>=ythenz:=else if x>=ythenz:=else

of themisguaranteed toterminate the setofallstatessuchthatexecution ofSbeguninone a if x>=ythenz:=else

- f Q i :=+1 areatomic.Try any interleaved execution orderofthetwo )

, becausethereare

and

b e account ,

i

at arbitrary points at arbitrary ≤ 1 ) (or no globalvariables :

account

f c and isineachcase.

intime. ) , , z =max(x,y) , z =y-1 z =y , in z =y+1 areatomic

) b )

or e ) PPJ -15f ) PPJ-17a © 2005 bei Prof. Dr. Uwe Kastens © 2008 bei Prof. Dr. Uwe Kastens • • • • • • becauseithasoneofthefollowingobserved properties: action Atomic The construct has The construct (AMO): At-most-once property interleaving actionsoftwo processes: hc M rpryfrnse osrcs osdrtems nlsn n ob atomic. be to one enclosing most the Consider constructs. nested for property AMO Check Interleaving analysisis one statementinSisAMOandallotherstatementsdonothave any globalvariable. x may bereadby adifferent process, but Edoesnotcontainany globalvariable. E isAMOandxnotreadby adifferent process, or v occursonlyonceinE. E hasatmostonevariable v, by thatiswritten adifferent process, and i. e. explicitly atomic. Statement sequenceS: Assignment x:=E Expression E: Synchronization it hasthe it isa Examples (1) (3) p2: p1: 2 y= x= p2: p1: non-interruptable machine instruction AMO euneo oeo oe prtos h nenlsae fwihcnntbe not can which of states internal the operations, more) or (one of sequence A : : property, or < < at mostone s=0;+1; t=0;+1; prohibits, thattheactionisinterleaved withthoseofotherprocesses, assume atomic AMO constructs <...>: atomic AMOconstructs : dc ba < < < x+1 y+1 simpler t= b a x =0;yz pointwhereanotherprocesscaninteract: << , if > > ; ; Atomic by AMO atomic decompositioniscoarser x Atomic actions > + > > < 1 , 1 x= p1: p2: p1: p2: >> ; tobeglobal >< < < < x= y =1; x=2; t=x+1; ab ca de < y < b a > 1 + > < . > > < z =2; ; z > > > ; > ; PPJ-17c PPJ-17b (4) (2) © 2005 bei Prof. Dr. Uwe Kastens © 2008 bei Prof. Dr. Uwe Kastens Let T be a statement in S that is not part ofanatomicstatementand Let TbeastatementinSthatisnotpart Then Qiscritical. Let Critical assertions non-interference NI(A,C) oftheproofp,For every ithastobeproven assertion critical that then non-interference NI(A,C) .C: 1. .C: 3. C: 2. Does Ainterfere withC?DependsonC: Example: Interference between an assertion andanassignment Example: Interference betweenanassertion {P} S{Q} { { { C =wp(T, R) provided thatthepreconditionDallows toexecute AinastatewhereCholds. i. e. C assertion assignment A:{D}x=e a ==1 a==1 a >0 a ==1 a==1 a >0 CC the execution of A doesnotinterfere withC (can notchangeC) :...<...>{ ...<...>{ q: p: osdrasnl critical single a Consider Consider processespandqwith ∧ p: q: be thestatementsequenceofprocesspwithitspre-andpostcondition. ∧ d<0 ∧ _____f_____ ∧ d<0 d +1>0 onp, ifthefollowing canbeproven inprogramming logic: d +1>0 iscritical. characterize {C1} <...atomicaction...>{C2}<...>{C3}{C4} ∧ d+1>0 Interference betweenprocesses holdsfor each holdsbetween } a=d+1{ }a=d+1{ } a=d+1{ d+1 >0 C in qhaving preconditionDinaproofofqand { C } <...>... observable states D}x=..{D2}y=... {D1} x=... ∧ seto np in C assertion }A{C} A D} } a=d+1;{Q}<...>... assignment A assertions atobservableassertions states a==1 a >0 A a ==1 ofaprocessp: ∧ n single a and } d<0 } is provable of every otherprocessq: is notprovable } sprovable is sinetAi q in A assignment ⇒ R non-interference itspostcondition; ⇒ ⇒ NI (A,C) non-interference interference , . : PPJ-17e PPJ-17d © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens h hl ttmn seeue sa atomic an action; hence as executed is statement whole The ni h condition the until causes theexecuting processtobeblocked for statement The executes thestatements The statementfor terminates whenall terminates {z =2 z :=x+y { x=1 oc co { x=0 x :=0;y { x=1 oc {z =2 z :=x+y { x=0 x :=0;y co S> co S {y+1 =1} {x+1 =1} {x+1 =1} {y+1 =1} } } 1 ∧ ∧ ∧ ∧ //...S Two inference rulesfor concurrentexecution y=1}=>{x+y2} y=0} y=1} y=0} B holdsatthebeginof y:=y+1 x:=x+1 y:=x+1 x:=y+1 => B odto synchronization condition concurrent processes stu;then true; is S i {x+y =2} have terminated. {y=1} {y=1} S {x=1} {x=1} Non-interference checks n i {P {P oc concurrently. It 1 i } S ∧ // // S i ... {Q sexecuted. is is notcorrect ∧ i }, 1 P S . n NI(a, C) NI( { x+1=1 { y+1=1 } ≤ co S i y:= x+1 ≤ C n, are 1 //...S ∧ ∧ y+1=1} x+1=1} , holds for all4cases, e.g. interference-free theorems Non-Interference istobeproven. y+1 =1 is notcorrect y:= y+1 y:= x+1 {P} ) does nothold: n aatB- > S -> B Q PPJ -17g PPJ -17f n } {Q} © 2011 bei Prof. Dr. Uwe Kastens © 2011 bei Prof. Dr. Uwe Kastens • • • • Communication Deadlock (Verklemmung): Language constructs for synchronization Synchronization 4. 3. 2. 1. Some processes arewaiting cyclically for eachother, andarethusblocked forever messages (seeabove) , monitor, conditionvariable (programming modelwithcommonstorage) a process waits until a certain conditionissatisfiedby adifferenta processwaits untilacertain process the sametime regions)may statement sequences(critical notbeexecutedcertain by several processesat asynchronous synchronous only inaprogramming modelwith condition synchronization (Bedingungssynchronisation): Ausschluss): (gegenseitiger messages using acommon,globalvariable, A synchronizationconditioncanmake aninterfering assignmentinterference-free. condition synchronization A non-interference-free assertion atomic action interference-free by weakening them. intheproofsofconcurrentprocessescan in somecasesbemade The assertions weakened assertions disjoint fromthesetofvariables w ocretprocesses concurrent Two disjoint variables : . S1{C}S2... q:: ... p:: ...x:=e Communication andsynchronization ofparallelprocesses : . S1{C}S2... q:: ... p:: ...x:=e inprogramming model messages:senderwaits for thereceiver (languages:CSP, Occam,Ada,SR) between parallel processes:exchange ofdataby ofparallel processes: messages:senderdoesnotwait for thereceiver (languages:SR) : : 3. Monitors andinJava ingeneral : p Avoiding interference : and q common storage distributed q C readsfromandviceversa. r nefrnefe ftesto variables of set the if interference-free are executed inthisstate S2 cannotbe can behiddeninanatomicaction. p:: .. : . S1{C}S2... q:: ... : q:: ... p:: ...x:=e or common storage with < B =wp(x:=e,C) S1 {C}S2 or C holdsafter > ... x:=e p x:=e rtst is to writes PPJ -17h > PPJ-18 ... © 2008 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens Condition synchronizationusuallyhastheform • • They areexecuted by processes whenexecuting procedure. acallofanentry A The process iswaiting ons. Semaphores arecounters. Theeffect ofacallV(s)onsemaphore isnotlostifno Note condition variable signal (c): wait (c) condition variable c thedifference between conditionvariables andsemaphores: if notBthenwait (c); Definition Monitor • • • • they are common storage used within entry proceduresfor used withinentry at mostoneprocessexecutes procedureatany time. an entry procedures: Condition variables The monitorguarantees A monitorhas A monitorisa A callsignal(c)has PPJ-22). (see semantics signal The signalexecuting processcontinues itsexecution inthemonitor. the of variant the signal-and-continue on depends monitor the in Which ofthetwo processesimmediatelycontinues itsexecution The executing processreleases then theprocessaccessesmonitoragainandcontinues. until itisreleasedby asubsequentcallsignal(c); waits inasetassociatedtoc, The executing process highlevel synchronizationconceptintroducedin : : [C.A.R. Hoare1974,P. Hansen1975] Brinch called by processes cisdefinedtohave 2operations tooperate onit. isusedtosynchronize onthe Monitor - general concept Monitor -general program module entry proceduresentry ; itencapsulatesdatawithitsoperations. Condition variables aredefinedinthemonitorand no effect, ifnoprocess iswaiting : mutual exclusion for callsofentry leaves themonitor ; themonitoris or for concurrentprogramming with condition synchronization (whichoperate onitsdata); one arbitrary processone arbitrary while notBdowait (c); condition B and passive . . thatwaits for c. for c. . PPJ-19b PPJ-19a © 2015 bei Prof. Dr. Uwe Kastens © 2008 bei Prof. Dr. Uwe Kastens entry procedure entry to execute an processes wait monitor Buffer end; process Consumer(i:1..m) end; process Producer(i:1..n) end; notFull, notEmpty: buf: Queue(k); loop loop d:=produce(); end; entry get(vard:Data) end; entry put(d:Data) signal (notFull); d :=front(buf);dequeue do length(buf)=0-> signal (notEmpty); enqueue (buf,d); do length(buf)=k-> Buffer.get(d); entry procedures entry re-enter themonitor semantic, they wait to in caseof signal(c); processes have executed Synchronization ina monitor put get

Example: boundedbuffer signal-and-wait Condition consume(d);end; process inthemonitor Buffer.put(d); d:Data; d:Data; wait (notFull); wait (notEmpty); ; 2 conditionvariables: stateofthebuffer end; notEmpty notFull condition variables od; od; allowed tore-enter they wait tobe executed wait(c), processes have by asignal(c) PPJ-21 PPJ-20 © 2011 bei Prof. Dr. Uwe Kastens © 2010 bei Prof. Dr. Uwe Kastens signal-and-continue signal-and-wait • • • Processes competefor themonitor signal-and-exit processes thatwait toexecute procedure anentry process thatisinthemonitor, may beexecuting processes thatareblocked by executing Variant The releasedprocessentersthemonitor The processthatexecutes The releasedprocessentersthemonitor leaves themonitor. The processthatexecutes signal The releasedprocesshastowait untilthemonitorisfree. The The processthatexecutes signalcontinues execution inthemonitor. signal-and-wait signal-and-continue p-wait p-signal p-entry p-wait p-signal p-entry Variants ofsignal-waitsemantics:examples ofexecution do length(buf)=k->wait(notFull);od; than processeswaiting forprocedures entry The processthathasexecuted signalgetsahigherpriority signal-and-urgent-wait callmay bechangedmeanwhile;thewaiting conditionhastobechecked again: semantics: signal(c) signal(c) semantics: {s} {s} semantics: {s} Variants ofsignal-waitsemantics signal signal {s} {s} p-wait hasexecuted p-signal executes procedure waits toenteranentry p-entry 3 processes: may bemodifiedhere: statewhen : {?} leaves themonitorandwaits to re-enter themonitor. terminates the entry procedurecalland theentry terminates wait(c) signal(c) immediately immediately signal(c) wait(c) is executed signal(c) , signal-and-urgent-wait p-wait p-signal p-entry signal-and-exit p-wait p-signal p-entry -withoutastatechangeinbetween -withoutastatechangeinbetween signal(c) signal(c) state {s} {s} {s} thatheldatthe {s} PPJ-22a PPJ-22 © 2011 bei Prof. Dr. Uwe Kastens © 2003 bei Prof. Dr. Uwe Kastens • • • Operations for conditionsynchronization: condition variables cannotbe declared All processesthatareblocked by procedures:Entry Objects arbitrary object. arbitrary synchronized respect totheclass. There arealso Always call notify() notifyAll() wait() Non- They aresynchronized synchronizationvariable by aninternal oftheobject(). they areexecuted under are tobecalledfrominside If several processes } synchronizedpublicvoidput(Datad){...} { class Buffer can bemarked operationsMethods ofaclass, oninstancevariables whichimplementcritical hl !odto)ty{wi(;}cth(nerpeEcpine {} e) (InterruptedException catch } wait(); { try (!Condition) while after private Queuebuf; ... {...} synchronized publicDataget() ofany classcanbeusedas synchronized notify wait synchronized Monitors inJava: conditionsynchronization , blocks (signal-and-continue semantics). the executing processcontinues inthemonitor they thencompete for themonitor; releases releases waits intheuniquesetofblocked processesoftheobject releases themonitorobject,and blocks only usable ifallprocesses wait for thesame condition (signal-and-continue semantics); the executing processcontinues inthemonitor for thisobject; notifyAll inloops synchronized: Monitors inJava: mutual exclusion canbeusedtospecifyexecution regionwithrespecttoan ofacritical call methodscanbeexecuted atany timeconcurrently. theexecuting process; an arbitrary all , becausewith mutual exclusion synchronized processesthatareblocked by the synchronized class methods wait waiting conditionmay bechanged: monitors areheldinasingleset; one oftheprocessesthatareblocked by signal-and-continue (there isonlyanimplicitone) methods . methods: : they arecalledundermutual exclusion with for thesameobject, wait semantics for thisobject; . wait PPJ-24 PPJ-23 © 2015 bei Prof. Dr. Uwe Kastens © 2003 bei Prof. Dr. Uwe Kastens } rvt uu u;// privateQueuebuf; { class Buffer } {//synchronized publicObjectget() } {//synchronized publicvoidput(Objectelem) public Buffer(intn){buf=newQueue(n);} http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/Condition.html http://java.sun.com/j2se/1.5.0/docs/guide/concurrency/index.html oiyl(;// // // return elem; notifyAll(); buf.dequeue(); Object elem=buf.first(); while (buf.isEmpty()) // buf.enqueue (elem); oiyl(;// notifyAll(); hl bfiFl()// while (buf.isFull()) The cpdlcs n upr o nerpigtraswihaewiigt cur a acquire to waiting are which threads interrupting lock. for support and locks, scoped attempting toacquirealock, synchronization performance lock implementation with locks keyword, there are anumber of Locks ... concurrent building in blocks building as used be classes orapplications. ... to designed are which classes try {wait();}catch(InterruptedExceptione){} try {wait();}catch(InterruptedExceptione){} Java 2platform . The -Whilelocking isbuilt intothe Java language viathesynchronized java.util.concurrent.locks A Monitorclass for boundedbuffers , but which also supports specifyingatimeoutwhen , but whichalso supports Concurrency UtilitiesinJava 2 includesa multiple conditionvariables perlock inconvenient limitationstobuilt-in monitor package of every blocked processchecks itswaiting condition every blocked processchecks itswaiting condition changesthewaiting conditionofthe changes thewaiting conditionofthe the same memory semanticsas the samememory

a consumer process tries totakea consumerprocesstries anelement

a producer process tries tostoreanelement a producerprocesstries concurrencyutilities Queue oflengthntostoretheelements package provides ahigh- waits whilethebuffer isempty waits whilethebuffer isfull . Theseare , non-lexically put get method method PPJ-25j PPJ-25 © 2015 bei Prof. Dr. Uwe Kastens © 2005 bei Prof. Dr. Uwe Kastens One can • • • • • MI hastobetruewhenever aprocess may leave or(re-)enterthemonitor A The Example ofamonitorinvariant for theboundedbuffer: monitor invariant (MI) before eachcallof before andaftereachcallof before andaftereachcallof at the after the class BoundedBuffer{ } monitor invariant has tobeproven fe h ntaiain tteedo nr rcdrs eoeclso at(n inli (*)). if signal (and wait procedures, aftercallsofwaitat thebeginningofentry (andsignal if(*)). of calls before procedures, entry of end the at initialization, the after public voidput(Objectx)throwsInterruptedException{ int putptr,takeptr,count; final Object[]items=newObject[100]; final final final public Objectget()throwsInterruptedException{ } } beginning assume thatthemonitor invariant holds oklc(;explicitmutualexclusion } finally{ while(count==items.length) try { lock.lock(); oklc(;explicitmutualexclusion } finally{ while(count==0) try { lock.lock(); initialization oklc e enrnLc(;explicitlock conditionvariables Condition notEmpty=lock.newCondition(); Condition notFull=lock.newCondition(); Lock lock=newReentrantLock(); oEpysga(;specificsignal notEmpty.signal(); ++count; if (++putptr==items.length)putptr=0; items[putptr] =x; oFl.inl) specificsignal return x; notFull.signal(); --count; if (++takeptr==items.length)takeptr=0; Object x=items[takeptr]; Concurrency UtilitiesinJava 2(example) andatthe 3. SystematicDevelopment ofmonitors signal lock.unlock(); lock.unlock(); , specifies with end ofeachprocedure entry signal wait signal-and-exit , acceptable statesofamonitor Monitor invariant with oEpyaat) specificwait notEmpty.await(); } } I 0<= buf.length() <=n MI: for theprogram positions signal-and-wait oFl.wi(;specificwait notFull.await(); semantics (*). attheotherpositions semantics(*), , explicit mutualexclusion explicit mutualexclusion : PPJ-25k PPJ-26 © 2008 bei Prof. Dr. Uwe Kastens © 2010 bei Prof. Dr. Uwe Kastens end; monitor Buffer Definethe 1. .Specifya 2. .Insert 3. 5. 4. e. proceduresputandget g.boundedbuffer: elementcount;entry e. g.: find acondition Consider every operation thatmay violate Eliminate unnecessary callsof Eliminate unnecessary Too many signalcallsdonotinfluencecorrectness-they onlycauseinefficiency. e. g. after every statechangethat notificationofprocesses:Insert define oneconditionvariable e. g. Caution: Caution: Missingsignalcallsmay causedeadlocks! incaseof Loop isnecessary aconditionalwaitinfrontinsert oftheoperation: end; // // entry get(vard:Data) // end; entry put(d:Data) buf=newQueue(n);end init buf: Queue; dequeue(buf); d :=front(buf); enqueue (buf,d); signal( do { dequeue(buf); signal( MI Derivation step1:monitor conditional waits !( : length(buf)wait( semanticslacks controlofstatechanges signal-and-continue may c for eachcondition Bounded buffers make awaiting condition signal c ); entry proceduresentry withoutsynchronization MI c c (see PPJ-28) ); state , e. g. a consumer process tries totakea consumerprocesstries anelement a producer process tries tostoreanelement a producerprocesstries of od Cond orthe Cond enqueue(buf) and may entry proceduresentry Cond instep4! state: true insert true ; buf, length(buf) Cond &&MI holds, PPJ-27a PPJ-27 © 2015 bei Prof. Dr. Uwe Kastens © 2008 bei Prof. Dr. Uwe Kastens end; monitor Buffer end; monitor Buffer end; // // // entry get(vard:Data) // end; entry put(d:Data) buf=newQueue(n);end init notFull, notEmpty:Condition; buf: Queue; end; // // // entry get(vard:Data) // end; entry put(d:Data) buf=newQueue(n);end init buf: Queue; dequeue(buf); d :=front(buf); /* length(buf)>0 enqueue (buf,d); /* length(buf)0&&MI*/ do /* length(buf)>0*/ enqueue (buf,d); /* length(buf)0&&MI*/ do length(buf)<=0->wait( enqueue (buf,d); /* length(buf)=N->wait( length(buf) <=0 length(buf) >=N Derivation step3:insert Derivation step4:insert ->wait( ->wait( Bounded buffers Bounded buffers notEmpty notFull notEmpty notFull a consumer process tries totakea consumerprocesstries anelement totakea consumerprocesstries anelement a producer process tries tostoreanelement a producerprocesstries tostoreanelement a producerprocesstries conditional ); od; ); od; ); od; ); od; notifications state: state: MI: 0<=length(buf) <=N MI: 0<=length(buf) <=N waits buf, length(buf) buf, length(buf) PPJ-27d PPJ-27ca © 2010 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens end; monitor Buffer end; monitor Buffer end; // // entry get(vard:Data) // end; // entry put(d:Data) buf=newQueue(n);end init notFull, notEmpty:Condition; buf: Queue; end; // // // entry get(vard:Data) // end; entry put(d:Data) buf=newQueue(n);end init notFull, notEmpty:Condition; buf: Queue; flnt(u)= N1 >sga(oFl) // if length(buf)== (N-1)->signal(notFull); dequeue(buf); // d :=front(buf); /* length(buf)>0&&MI*/ do length(buf)<=0->wait(notEmpty); od; signal(notEmpty); if (length(buf)==1) enqueue (buf,d); /* length(buf)=N->wait(notFull);od; /* length(buf)0&&MI*/ do /* length(buf)>0*/signal(notEmpty); enqueue (buf,d); /* length(buf)=N Derivation step4:insert ->wait( ->wait( Bounded buffers Bounded buffers notEmpty notFull a consumer process tries totakea consumerprocesstries anelement totakea consumerprocesstries anelement a producer process tries tostoreanelement a producerprocesstries tostoreanelement a producerprocesstries unnecessary notifications unnecessary // // notcorrectundersignal-and-continue notcorrectundersignal-and-continue ); od; ); od; notifications state: state: MI: 0<=length(buf) <=N MI: 0<=length(buf) <=N buf, length(buf) buf, length(buf) e PPJ-28 see see PPJ-28 PPJ-27e PPJ-27da © 2015 bei Prof. Dr. Uwe Kastens © 2010 bei Prof. Dr. Uwe Kastens E. g.for theboundedbuffer statesw.r.t signallingareconsidered: changed between thesignalcallandresumeofreleasedprocess. These argumentsdo Processes needonlybeawakened whenthestatechangeisrelevant: Examples A Processes 0 0 don’t give anon-ex. resource don’t give anon-ex. resource than have beengiven ... anddon’ttake back more requestRes( Monitor invariant oio nain requestRes( Monitor invariant 0 stronger invariant: The ≤ ≤ ≤ monitor aalif/do(!(1 avail aaldo(!( avail avail && Taxicab provider (n=1),drive withaweight ofn Lending bikes ingroups (n identity i.e. whenawaiting processcanbereleased. fromwhen thewaitingconditionCondchanges falsetotrue, grants accesstoasetofk : requestnresources, 1 full 0 oftheresources may berelevant: usea boolean array avail[1] ...avail[k] ≤ inUse Pattern: Allocatingcounted resources not signal applyfor no signalcall inli) signal(av); avail++; inUse--; avail =avail - signal(av); signal(iu); avail--; inUse++; if/do (!(1 avail--; Relevant statechanges (notFull); ≥ 1),allocatingblocks ofstorage (n n signal-and-continue ≤ ≤ avail)) wait( ≤ ≤ n avail)) wait(av); avail)) wait(av); ≥ ≤ 1resourcesofthe k, and return them afterhaving usedthem. k,andreturn no signalcall 1 n n not empty and not full returnRes( ) returnRes( ) signal( ; av[n] ;avail =avail + ); ≥ 1tonsonabridge semantics;there no signalcall signal same kind avail++; if/do (!(1 (notEmpty); av[1] ≥ ≤ /* nowait! */ 1), inUse)) wait(iu); . 1 n ); ...signal( ) ) Cond n empty ; /* nowait! */ may be av[avail] PPJ-29 PPJ-28 ); © 2003 bei Prof. Dr. Uwe Kastens © 2011 bei Prof. Dr. Uwe Kastens privateResources mon;privateRandomrand; { class ClientextendsThread import java.util.Random; morethanithasreceived =>simplerinvariant:Assumption: Processdoesnotreturn Processes A } monitor mon=m;identid;roundsrd;maximummax; { public Client(Resourcesm,intid,rd,max) private intident,rounds,maximum; privateintavail; { class Resources } while(rounds>0) { public voidrun() } } rand =newRandom(); avail+=n; { synchronized publicvoidputElems(intn) } while(avail=1resourcesthe avail -=n; notifyAll(); rounds--; mon.putElems (m); try {sleep(Math.abs(rand.nextInt()) %1000+1);} mon.getElems (m); request n resources, 1<=n<=k, and return themafterhaving requestnresources, usedthem. 1<=n<=k,andreturn Processes andmainprogram for resource monitor try {wait();}catch(InterruptedExceptione){} catch (InterruptedExceptione){} Monitor for resource allocation } publicstaticvoidmain(String[] args) { public classTestResource } intavail=20; { for (inti=0;i<5; i++) Resources mon=new Resources(avail); // waiting isnotneededbecauseofassumption new Client (mon,i,4,avail).start(); // a number generator determines howa number many generator determines // elements arerequestedineachround, same kind // and whenthey are returned // negatedwaiting condition // notify()would bewrong! . // invariant: avail >=0 // requestnelements // return nelements // return PPJ-31 PPJ-30 © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens readers shared A monitorgrants accesstoadatabase: readingandwriting readers shared A monitorgrants accesstoadatabase: readingandwriting end; entry releaseRead() end; entry requestRead() init nr=0;nw=0;end monitor ReadersWriters end; entry releaseRead() end; entry requestRead() init nr=0;nw=0;end monitor ReadersWriters nr--; nr++; //numberwriters //numberreaders nw: int; nr: int; nr--; nr++; //numberwriters //numberreaders nw: int; nr: int; , , writers exclusive writers exclusive Readers-Writers problem (Step2) Readers-Writers problem (Step1) . . entry requestWrite() entry requestWrite() end; end; entry releaseWrite() end; end; end; entry releaseWrite() end; Monitor invariant RW: nw--; nw++; nw--; nw++; (nr ==0||nw0)&&<=1 PPJ-32b PPJ-32a © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens readers shared A monitorgrants accesstoadatabase: readingandwriting readers shared A monitorgrants accesstoadatabase: readingandwriting end; entry releaseRead() end; entry requestRead() init nr=0;nw=0;end monitor ReadersWriters end; entry releaseRead() end; entry requestRead() init nr=0;nw=0;end monitor ReadersWriters signal( { maybe { RW&&nr>=0} { RW&&nr>0}nr--; { RW} nr++; { od; do !( //numberwriters //numberreaders nw: int; nr: int; { RW&&nr>0}nr--; { RW} nr++; { od; do !( //numberwriters //numberreaders nw: int; nr: int; nw==0 nw==0 -> wait( -> wait( nw==0) nw==0 okToWrite &&RW} &&RW} , , nr==0 writers exclusive writers exclusive ) okToRead okToRead Readers-Writers problem (Step4) } Readers-Writers problem (Step3) ); ); ); . . entry requestWrite() entry requestWrite() end; end; entry releaseWrite() end; end; end; entry releaseWrite() end; Monitor invariant RW: Monitor invariant RW: signal_all( signal( { { RW&&nw==1}nw--; { RW} nw++; { nr==0&&nw<1RW} od; do !( { RW&&nw==1}nw--; { RW} nw++; { od; do !( (nr ==0||nw0)&&<=1 (nr ==0||nw0)&&<=1 nr==0 && nr==0 &&nw<1 -> wait( -> wait( nr==0 &&nw<1 nr==0 &&nw<1 okToWrite okToWrite okToWrite okToRead nw==0 &&RW} } ); ) ) ); ); ); PPJ-32d PPJ-32c © 2008 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens readers shared A monitorgrants accesstoadatabase: readingandwriting end; entry releaseRead() end; entry requestRead() init nr=0;nw=0;end monitor ReadersWriters } privateintnr=0,nw =0; { class ReaderWriter -> if nr==0 { maybe { RW&&nr>=0} { RW&&nr>0}nr--; { RW} nr++; { od; do !( //numberwriters //numberreaders nw: int; nr: int; } nw--; { synchronized public voidreleaseWrite() } while(nr>0||nw0) { synchronized publicvoidrequestWrite () } nr--; { synchronized publicvoidreleaseRead () } while (nw>0) { synchronized publicvoidrequestRead() signal( nw==0 -> wait( notifyAll (); nw++; if (nr==0)notify(); nr++; nw==0) try {wait();}catch(InterruptedException e){} try {wait();}catch(InterruptedExceptione){} &&RW} , nr==0 writers exclusive okToWrite okToRead Readers-Writers problem (Step5) } Readers/writers monitor inJava // monitorinvariant RW: ); ); . // notify 1 writer andall readerswould besufficient! // notify1writer (nr==0||nw0)&&<=1 entry requestWrite() end; end; entry releaseWrite() end; Monitor invariant RW: signal_all( signal( { { RW&&nw==1}nw--; { RW} nw++; { nr==0&&nw<1RW} od; do !( (nr ==0||nw0)&&<=1 nr==0 && -> wait( nr==0 &&nw<1 // awaken issufficient onewriter okToWrite // negatedwaiting condition // negatedwaiting condition okToWrite okToRead nw==0 } ); ) ); ); PPJ-33 PPJ-32e © 2009 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens The taskisalsoanexample for pattern. theClient/Server Example: SleepingBarber Design method A monitorcoordinatesthe Processes passthrougha 2 rendezvous: haircutandcustomerleaves 2 kindsofprocesses:barber(1instance),customer(many instances) him, awakens he the barbergetspaid,letscustomerexit, andawakens awaiting customer, ifany. sleeping, barber the finds customer arrives, thecustomersleepsinoneofotherchairs. Afterfinishingthehaircut, and arrives customer a sits inthebarber‘schair, andsleepswhilehegetshishaircut.Ifthebarberisbusy whena When shop. his enter to In asleepy villageclosetoPaderborn abarberissleepingwhilewaiting for customers substitute counters by variables binary derive waitingconditions characterize Specify statesby counters customer entry proc entry Monitor designfor theSleepingBarberproblem (step1) cleave++; cinchair++; getHairCut processes getHairCut: Counters : allowed statesby invariants Method: rendezvous ofprocesses represent states, incremented in entry procedures: representstates, incrementedin entry rendezvous intherequiredorder sequence ofstates : ofmonitoroperations fromtheinvariants; ; Monitor for barbershop cinchair cleave . over counters; and entry proc entry entry proc entry interact bdone bavail bclose bbusy bbusy++; bavail++; bclose++; bdone++; witheachother. getNextCustomer: finishedCut: . getNextCustomer finishedCut barber process PPJ-35 PPJ-34 © 2009 bei Prof. Dr. Uwe Kastens © 2009 bei Prof. Dr. Uwe Kastens Monitor invariant: BARBER:C1andC2C3: entry proc entry C3: C2: C1: customer Waiting conditionsfor theSleepingBarberproblem (step3) cleave++; do not( cinchair++; do not( Monitor invariant for theSleepingBarberproblem (step2) bavail >=bbusy >=bdonebclose bdone bavail cinchair >=cleave getHairCut: bdone bavail getHairCut >= >= processes > > cinchair cleave cinchair cleave Invariants over counters: Monitor invariant: BARBER:C1andC2C3 C3: C2: C1: >= >= ) ->wait ( and ) ->wait ( bavail >=bbusy >=bdonebclose bdone bavail cinchair >=cleave bclose bbusy >= >= o b ); done; ); done; Monitor for barbershop cinchair cleave cinchair cleave >= >= and bclose bbusy entry proc entry entry proc entry bdone bavail bclose bbusy bclose++; bbusy++; do not( bdone++; do not( bavail++; finishedCut: getNextCustomer: cleave cinchair leads to2waiting conditions leads to2waiting conditions guaranteed by execution order > > bclose getNextCustomer finishedCut bbusy barber ) ->wait ( ) ->wait ( process e c ); done; ); done; PPJ-36 PPJ-35a © 2011 bei Prof. Dr. Uwe Kastens © 2009 bei Prof. Dr. Uwe Kastens entry proc entry proc entry value ranges: {0, 1} variables:new binary value ranges: {0,1} variables:new binary open--; do not( barber--; do not( barber--; do not( do not( Signal operationsfor theSleepingBarberproblem (step4) open--; exit barber exit barber open chair open chair getHairCut: getHairCut: open open barber barber = = exit++; signal(e); exit++; = = = = netcl inl()cl hr odto fxmybcm true: become may x of condition a where call (x) signal call insert chair++; signal(c); chair++; cleave cleave = = cinchair cinchair bdone bdone >0)->wait ( >0)->wait ( increment operations andconditionsaresubstituted: bavail bavail >0)->wait ( >0)->wait ( - - - - bclose bclose - - - - cleave cleave cinchair cinchair bbusy bbusy Substitute counters (step3a) o o ); done; ); done; b b ); done; ); done; New invariants: Old invariants: New invariants: Old invariants: entry proc entry entry proc entry entry proc entry entry proc entry C3: C3: C2: C3: C2: C2: C3: C2: chair--; exit--; do not( open++; do not( barber++; chair--; exit--; do not( open++; signal(o); do not( barber++; signal(b); open open barber bdone bavail barber bdone bavail finishedCut: getNextCustomer: finishedCut: getNextCustomer: exit chair exit chair >=0&& >=0&& >= >= >= >= >0)->wait ( >0)->wait ( >=0&& >=0&& >0)->wait ( >0)->wait ( cinchair cinchair cleave cleave exit exit chair chair >= >= >= >= >=0 >=0 e e ); done; ); done; c c ); done; ); done; bclose bclose >=0 >=0 bbusy bbusy PPJ-37a PPJ-37 © 2003 bei Prof. Dr. Uwe Kastens © 2011 bei Prof. Dr. Uwe Kastens • • • • • • SIMD • • • • • • • • Data parallelism asa Data parallelism asan usually onelementsof Many processesexecute the Applications e. g.32x64processorfield machine typese. g.ConnectionMachine, MasPar fixed topology, usuallya fast communication in in same instructions local memory manyvery processors, finite elementmethodindesignsystems solving differential equations image processing fluid mechanics systolic computations iterative analyze computations on GPUs (Graphical ProcessingUnits);massively parallel processorsongraphic cards SIMD machines vector machines machine: Single Instruction MultipleData machine:SingleInstruction data dependences computations inrounds mainlyin for eachprocessor Data parallelismasanarchitectural model arrays innestedloops (Single Instruction MultipleData),e. g.ConnectionMachine, (SingleInstruction MasPar , e. g.Cray programming modelfor parallelcomputers lock step architectural modelofparallelcomputers regular datastructures technical, scientificcomputing lock step massively parallel : 2phasesareiterated: compute-shiftdatatoneighbourprocesses grid 5. DataParallelism: Barriers same operationsatthetimeondifferent data ofcomputations, , synchronize with : arrays, sequences, matrices, lists. transform program Barriers , e. g. and parallelize : : field ofprocessors loops ; PPJ-39 PPJ-38 © 2008 bei Prof. Dr. Uwe Kastens © 2003 bei Prof. Dr. Uwe Kastens upt sequencesofsumstheprefixes ofa sequenceaofnumbers; output: input: • • • • • • • eunil dataparallel: sequential: simple example addition: matrix no communication data mapping these canbeparallelized directly, sincethereareno communication withneighboursinthesamedirectionlock step processes execute thesameprogram onindividualdatain lock step (arrays,regular datastructures lists)aremappedontoafieldofprocessors round parallel algorithmic idea: parallel algorithmic r =0 o i=0 ;i+ distributeA,B for (i=0;i=0) { while (dist=0) bm.barrier(); s[procNo] +=addIt; // value beforeoverwritten addIt =s[procNo-dist]; while (N>2 s =0; // synchronize: execute this.task(); partner=p[(i +N-2 wait forf==0;setf=1; s++; wait partner.f;resetpartner.f=0 dist =1 Prefix sumswithbarriers s ) //distance ; s-1 ) moduloN: s-1 ) moduloN]; (i +N-2 // globalarraysaands s-1 // doubleddistance ) moduloN i PPJ-47 PPJ-46a © 2003 bei Prof. Dr. Uwe Kastens © 2003 bei Prof. Dr. Uwe Kastens ehd worker process method: intarray last;last[i]containstheindex ofthelastelementlistlink[i] intarray linkstoreslists;link[i]containstheindex ofthesuccessorornil output: input: Notation inModula-2*withsynchronous(andasynchronous)loopsfor parallel machines not longerthan d last[i] points totheendofthoselists whichare BEGIN VAR dist:CARDINAL; VAR a,s,t:ARRAY[0..N-1]OFINTEGER; } intnewlast=nil; { while (d=0THEN END s[i] :=+t[i]; t[i] :=s[i-dist]; Finding listends:dataparallelapproach =newlast; last[last[i]] icomputeslast[i]=last[last[i]]inlogN ; nil nil nil rounds parallel loopinlock step parallel loopinlock step for eachstatement nil nil nil implicit barrier nil nil nil PPJ-49 PPJ-48 © 2009 bei Prof. Dr. Uwe Kastens © 2011 bei Prof. Dr. Uwe Kastens • • • • • Iteration space • • • • • Development steps(automatedby ): Regular loops iteration iselaborated sequentially iteration neednothave borders orthogonal loop boundsareingeneral notknown before run-time each point(i n-dimensional spaceofintegralpoints transform index spaces such thatmany accessesarelocal, map tofieldorvector ofprocessors keep datadependencesforward intime data-flow: definitionanduseofarray elements sequential execution ofiteration space map arrays toprocessors innerloop(s) parallelize transform loops analyze nested loops computation ofPascal’s triangle example: END FOR FOR I:=0..N DECLARE B[-1..N,-1..N] END FOR FOR J:=0..I datadependences B[I,J] := 1 , ...,i 5.2 /6.DataParallelism: LoopParallelization of aloopnestdepthn: B[I-1,J]+B[I-1,J-1] on orthogonal data structures -parallelized datastructures for onorthogonal operating on n ) representsanexecution loopbody oftheinnermost Iteration spaceofloopnests arrays , (polytope) N J END FOR FOR I:=1..N D ECLARE B[0..N,0..N+1] END FOR FOR J:=1..I data parallel B[I,J] := B[I-1,J]+B[I-1,J-1] 1-N N 1 -1 j j 1 N -N -1 1 1N                             j 1 N N i processors i i N C-5.11 /PPJ-50 I C-5.12 /PPJ-51 © 2009 bei Prof. Dr. Uwe Kastens © 2011 bei Prof. Dr. Uwe Kastens • • • Data dependencefrom iterationpointi1toi2 holds for alliteration pointsexcept attheborder positive each dependencevector must be the execution order d used initeration Flow-dependences can relative Iteration M =3,N4 FOR I:=0..N FOR I:=0..N = M Computation ofPascal´s triangle Example: N J J END FOR FOR I:=0..N DECLARE B[-1..N,-1..N] i2 -i1 FOR J:=I..I+M FOR J:=0..I END FOR FOR J:=0..I dependence vector , i.e. i N 1 computesavalue thatis =(i2 B[I,J] := N d Examples for Iterationspacesofloopnests B[I-1,J]+B[I-1,J-1] 1 I I =(0,...,0,d -i1 i2 Data DependencesinIterationSpaces (flow dependence) 1 , ...,i2 , cannotpointbackward intime: not bedirectedagainst FOR I:=0..N N J n FOR I:=0..M+N N -i1 i J , ...),d FOR J:=0..IBY2 FOR J:=max(0,I-M).. n ) M lexicographically i >0 N M+N I I min (I,N) : FOR I:=0..NBY2 N J FOR J:=0..I N J backward N I (0,-1) (0,1) C-5.12a /PPJ-51a C-5.13 /PPJ-52 N (1,-5) forward I (1,0) © 2011 bei Prof. Dr. Uwe Kastens Data dependencesmust • • • transformed to The • • non-linear transformations in time in space systolic improve execute innermostloop(s)inparallel one dimension,causesgaps that Tiling Scaling not withinparalleldimensions lexicographically positive iteration space cover tiles : reusevalues storedincache : introduce computationandcommunication scheme locality : usestorage ofexecuting processor, : stretch theiterationspace new coordinates ofdataaccesses; offixed size ofaloopnestis additional innerloops point forward intime , e. g. Loop Transformation and . Goals: Transformations convex polytope in loop nests data of , i.e. tiling linear basictransformations: • • • SRP transformations of theloopnest for onedimension outer looptothatofaninnerone Permutation Reversal Skewing : additeration countofan : flipexecution order scaling : exchange two loops (next slides) C-5.14a /PPJ-54 C-5.14 /PPJ-53 © 2009 bei Prof. Dr. Uwe Kastens © 2009 bei Prof. Dr. Uwe Kastens Iteration countofoneloopisnegated Transformation systematictransformation, matrices: check dependencevectors () general transformationgeneral matrix 1 for i=0toM 0 ... 1 N Reversal Skewing Permutation j -1 for j=0toN 1 ...... 0 1 Transformations definedby matrices M i )( () ( () )( () 1 0 0 f 1 1 original , thatdimensionisenumerated backward Reversal -1 0 1 0 0 1 )( () transformed * * 0 * 1 () () () -1 i i i j j j 0 2-dimensional: ======* for ir=0toM ( ( ( () f -N for jr=-Nto0 * -j i i i j j i i+j jr ... ) ) ) old == ( loop variables -j i i’ i’ i’ j’ j’ j’ ) ) ) ) M C-5.14b /PPJ-55 C-5.15 /PPJ-55a ir new ir jr ) © 2006 bei Prof. Dr. Uwe Kastens © 2006 bei Prof. Dr. Uwe Kastens the Two loopsoftheloopnestareinterchanged iteration spaceisshifted; The general transformationgeneral matrix: transformationgeneral matrix: for i=0toM for i=0toM execution order () () iteration count N N j j for j=0toN for j=0toN 0 1 1 ...... 0 1 0 f 1 1 1 0 1 1 1 ...... 0 0 1 1 ofiteration points of anouterloopis M M i i execution order original original Permutation changes; added tothecountofaninnerloop; Skewing ofiteration points for ip=0toN for is=0toM )( () ( () for jp=0toM for js=f*istoN+f*is ; theiteration spaceisflipped; 0 1 1 f transformed transformed new dependencevectors must belegal...... 0 1 1 0 2-dimensional: 2-dimensional: * * () () remains unchanged i i j j N+M old old == == js N ( ( loop variables loop variables jp M f*i+j j i i ) ) N C-5.16 /PPJ-55b -.7/PPJ-55c / C-5.17 new new ip is ip js jp M ) ) is © 2006 bei Prof. Dr. Uwe Kastens © 2009 bei Prof. Dr. Uwe Kastens negative positive Each byThe boundsofaloopnestaredescribed a • • • • ( ( for transformation ofindex expressions intheloopbody: concatenation oftransformations inverse Transformation matrix Transformation matrix Transformation matrix inequality separatesthespace -1 0 0 1 -1 0 0 1 factors represent factors represent -1 1 0 0 -1 e. g.Reversal 1 0 1 ) e. g. ) B *i * * Inequalities DescribeLoopBounds e. g. ( e. g. ( T T Use ofTransformation Matrices transforms old defines upper ≤ j i lower j i c ) ( ) T ≤ )( () 0 bounds )( () bounds 1 )( () ≤ -1 new iterationcounts ( ( 1 0 1 defines 1 0 0 first 0 1 in„insideandoutsideoftheiteration space“: N 0 M 0 ) -1 -1 -1 dependence vectors N 0 M 0 0 0 0 ) T * ) 1 old iterationcounts then set oflinearinequalities () * * * () () 4j 3-j 2i 1-i () 1 0 -i+j 1 4j 3-j 2i T 3: 1, 4: i i’ j’ j 1 2 transformed 1 example 1 ≤Ν ≤Μ : ≤0 ≤0 example 2 -1 ≤Ν ≤Μ ≤0 0 T 0 j in terms oftheoldones: interms ≤0 == = = ≤ 2 ≤ *T min (i,N) T ( j -1 = 1 -1 -j 1 -j’ = i i’ intonew ones: * i´=i ( N N ) in terms ofnew ones, interms T ) ) 0 1 1 = 2: 1+ 3: ( . -1 0 1 i’ j’ i 0 ≤ j i ) ≤ ) M i 4 ) 4 3 3 T * T d=d´ -.9/PPJ-56a / C-5.19 C-5.18 /PPJ-56 * i=i´ M M 2 2 © 2006 bei Prof. Dr. Uwe Kastens © 2006 bei Prof. Dr. Uwe Kastens 8. Write thecomplete loopswithnew Write loopvariables ipand jp andnew loop bounds. 8. Specifytheloopboundsby inequalities and 7. Computetheinverse ofthetransformation and matrix 6. ofthecomposedtransformation and Computethematrix 5. transformation Applyapermutation anddraw theiteration space. 4. Applyaskewing transformation and draw theiteration space. 3. Computethedependencevectors and draw examples ofthemintotheiteration space. 2. Draw theiteration space. 1. Parallelize theabove loop. The inverse ofatransformation matrix ( transform themby theinverse ofthetransformation matrix. use ittotransform the index expressions. use ittotransform thedependencevectors. Explain why theinnerloopnow can beexecuted inparallel. Why cantheinnerloopnotbeexecuted inparallel? for i=0toN new bounds: example 1 ( Example for Transformation andParallelization ofaLoop for j=0toM kwn inverse skewing -1 B *T 1 1 -1 1 1 a[i, j]=(a[i,j-1]+a[i-1,j])/2; 1 0 -1 ) -1 1 0 0 ) ( * -1 1 Transformation ofLoopBounds ( 1 0 ’c i’ ) j’ i’ ) ≤ ( ( T - 1 -1 transforms asetofinequalities: 0 0 1 N 0 M 0 B ) -1 1 0 0 ) -i´+j´ 4 i´-j´ 3 2i´ -i´ 1 * ≤Μ ≤0 ( ≤0 ≤Ν -1 1 T N -1 1 0 1 ) = ( 4 3 B *T -1 B *T -1 1 1 - 1 C-5.21 /PPJ-56c C-5.20 /PPJ-56b -1 -1 1 0 0 i’ M 2 ) ≤ c © 2006 bei Prof. Dr. Uwe Kastens 8. 7. Bounds: 5.: M=4 orig.: for ip=0toM+N Solution oftheTransformation andParallelization Example j () FOR I:=0..N DECLARE B[-1..N,-1..N] END FOR 1 1 for jp=max(0,ip-M)tomin(ip,N) N . . 3.: 1., 2.: J FOR J:=0..I END FOR ( a[jp, ip-jp]=(a[jp,ip-jp-1]+a[jp-1,ip-jp])/2; 0 1 B[I,J] := -1 0 0 1 B B[I-1,J]+B[I-1,J-1] () -1 0 0 1 ) 1 0 N=7 Transformation andParallelization () = i M+N M=4 N M 0 () 0 c original 0 1 N I new: Iteration space ( () B *T -1 1 0 0 1 1 (I, J)->J-I)=(IS, JS) transformed -1 -1 N=7 1 1 1 ) - 1 0 END FOR FOR IS:=0..N DECLARE B[-1..N,-1..N] END FOR FOR JS:=-IS..0 () ip-jp 4 -jp 1 -N -ip+jp 3 2jp JS 0 1 B[IS,JS+IS] := ≤0 ≤Ν N=7 B[IS-1,JS+IS]+B[IS-1,JS-1+IS] = jp ≤0 ≤Μ ()

1 1

parallel processormap JS mod2

2, 3=>jp

1, 4=>max(0,ip-M) 2, 4=>ip 1, 3=>0 sequential timeIS M=4 4.: 6.: () ≤ ≤ N ≤ ip min(ip,N) M+N Inverse 0 1 C-5.22 /PPJ-56d IS C-5.23 /PPJ-57 -1 1 M+N ≤ jp ip © 2011 bei Prof. Dr. Uwe Kastens END FOR FOR IS:=0..N DECLARE B[-1..N,-1..N] J N FOR JS:=-IS..0 END FOR B[IS,JS+IS] := B[IS-1,JS+IS]+B[IS-1,JS-1+IS] • • • Goal • Index space (Skewing, Reversal, Permutation, ...) same same mathematicalmodel same propertiesasiterationspace no restrictions Distribute array elements n-dimensional spaceofintegral index points(polytope) as many Data distribution for parallelloops : ... DECLARE B[-1..N,-N..N] transformations N B[IS,JS] :=

I P0 writes B[I,J] accesses aspossible arelocal. Data onP0 of anarray: 50% local rgnltransformed original B[IS-1,JS-1]+B[IS-1,JS-1] by datadependences Data Mapping index spaceofB

areapplicable

(i,j) ->(i,j-i) skewing f=-1 over processors, suchthat 100%local -N J N C-5.24 /PPJ-58 C-5.25 /PPJ-59 N I © 2010 bei Prof. Dr. Uwe Kastens © 2010 bei Prof. Dr. Uwe Kastens After anotify-call anew receive-call may have stolenthe onlymessage=>wait loopisneeded All waiting processes wait for the same condition=>notify() issufficient. } {//public classChannel • • • operations Channel Message Processes viachannels sendandreceivemessages send empty() receive() send (b) public public public public Channel() private Queue and { returnmsgQueue.empty ();} } { { msgQueue.enqueue(msg); { msgQueue=newQueue();} : : value ofacomposeddatatypeorobjectclass queue receive yieldstrue,ifthechannelisempty;result : while (msgQueue.empty()) betrsl sQeefot) // return result; msgQueue.dequeue(); Object result=msgQueue.front(); addsthemessagebtoendofqueuechannel; : yieldstheoldestmessageanddeletesitfromchannel; : onachannel: boolean empty synchronized Objectreceive synchronized voidsend try { block does of arbitrary length,containingmessages ofarbitrary areexecuted undermutual exclusion. not block s theexecuting processaslongthechannelisempty. receive wait() 7. Asynchronous MessagePassing msgQueue Channels implementedinJava the executing process(incontrast tosynchronoussend) ; }catch(InterruptedException e) {} () ; implementation ofachannelusingqueuemessages notify() (Objectmsg) () // ; } not necessarily up-to-date. send wake areceivingprocess the queueisnotempty PPJ-61 PPJ-60 © 2015 bei Prof. Dr. Uwe Kastens © 2003 bei Prof. Dr. Uwe Kastens pair of output port input port link e. g.between clientandserver tight synchronization, one processrequests-theothersreplies; (unusual structure) e. g.aprocessdistributes taskstomany servers channel belongstothesendingprocess; one sender-many receivers from several clientprocesses e. processreceives g.aserver tasks channel belongstothereceivingprocess; many senders -onereceiver; transformation steps(pipeline) e. g.processesform chainsof one sender : request andreply channels; ofaprocess: ofaprocess: In general, theprocesses Problem: completed, someoperations arepending. Otherwise the 3. 2. Allchannels areempty. following1. the terminates processes of system When All No isconnectedto processesare processesare system stateisnotwell-defined Processes andchannels Termination conditions one receiver terminated blocked onareceive ; do notknow theglobalsystemstate . ; operation. Server q , e.g. taskisnot conditions request link output port input port reply uthold: must . PPJ-62a PPJ-62

Client p © 2003 bei Prof. Dr. Uwe Kastens © 2003 bei Prof. Dr. Uwe Kastens Application reply channel: request channel: One A may includedintherequestmessage besenttotheserver one for eachclient(input port), oftheserver input port message object server processserver respondstorequestsofseveral client processes server : server distributes packages: server dataorwork onrequests Operations Client / server: basicchannelClient /server: structure setArg (ind,val), getArg(ind),getArgList() setKind (k),getKind() constructors request may have suitable structure for arbitrary the vector argument kind kind empty onmessages: Message structures different synchronization different kindsofmessageswith a channelonwhichthesenderexpects areply special case: e. g.number andoridentitiesofresources e. g.signaldifferent kindsofevents kinds ofmessages, withoutdata only reply reply particular purpose particular client client ... data : PPJ-64 PPJ-63 © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens Server mustServer may notblock whileanotherport benon-empty: onanemptyinputport Termination: processestherequests The server Different requests } while (running) { thus, sectionsare itisguaranteed thatcritical server server f(pnigepy) msg=pending.dequeue(); ...} { msg=getPort.receive(); ...} if (!pending.empty()) if (!getPort.empty()) { msg=putPort.receive();...} if (!putPort.empty()) { Different kindsofoperationsondifferent channels pending pending Server processes:Server different kindsofoperations terminate clients, emptychannel,queue.terminate (operations) arerepresentedby different put strictly sequentially it is If a request requiresaspecificcondition stored inaqueue get requests get put put requests reply channels reply channels request not executed interleaved get ; untiltheconditionholds. put kinds ofmessages . . PPJ-66 PPJ-65 © 2003 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens Caution: Termination: Checking several channels: Hence, anew non-blocking channelmethodisintroduced: That isnotacceptable whenseveral channelshave tobechecked inturn. may block. If several processesreceive from achannel Several processes, server } if ((msg=ch1.receiveMsgOrNull())== null) { while (msg==null) }} ... { public classChannel if ((msg=ch2.receiveMsgOrNull()) ==null) if(msgQueue.empty())return null; { public synchronizedObjectreceiveMsgOrNull () if (!ch.empty())msg=ch.receive(); a server server ... Thread.sleep (500); return result; msgQueue.dequeue(); Object result=msgQueue.front(); receive empty requestchannels, emptyqueues, emptyreplychannels onachannel several clientprocesses Receive withoutblocking may block Several servers put get ch aserver! , thenthecheck , several requestchannels reply reply client client ... PPJ-68 PPJ-67 © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens • • • • Example for an specific protocol reply channels commitstothetask;itthenleadsaconversationone server withtheclientaccordingtoa a clientsendsan several equivalent servers ( open openReply) ((readreadReply) |(writewriteReply))* (close closeReply) a oprt ntemultiple monitorsare not related conditionvariables, wait - signal calls clientprocesses are blocked guaranteed forprocedure entry same requestchannels procedures entry may cooperate onthe passive program module procedures callsofentry replies aredelayed passivemonitor queue ofpendingrequests sequentially requests arehandled different channels kinds ofmessagesand/or request -replyviachannels active process active monitor server server ... Conversation sequencesbetween client andserver openReply application pattern Active monitor (server) vs.passive monitor Active monitor(server) arecontainedinthe accessReply opening request , e. g. accessReply access access respondtorequestsof 6. multiple servers 5. delayed service 4. mutual exclusion operations 3. server 2. client communication 1. program structure is„fileservers“: ona open open channel common and openReply several clients messages. for ( allservers accessReply accessReply open ) client client ... PPJ-70 PPJ-69 © 2011 bei Prof. Dr. Uwe Kastens © 2012 bei Prof. Dr. Uwe Kastens tree The connectiontothesenderoffirstmessage isconsideredtobeanedgeofa All channelsarefinallyemptybecauseof(3). All nodesarefinallyreachedbecauseof(2). Thenodethenreceives 3. Afterhaving 2. Thenodewaits for amessageonitsincominglinks. 1. Method Problems Broadcast • • Net A nodeknows onlyitsdirectneighboursand thelinkstoandfromeachneighbour: initiator including tothesenderoffirstmessage nodes donotknow thegraph beyond theirneighbours graph may have cycles ofthegraph. That information may beusedtosimplifysubsequent broadcasts. Finally allchannelshave tobeempty. A messageissentfromaninitiatornodesuchthatitreachesevery nodeinthenet. 1 edge node : bi-directionalgraph, connected,irregularstructure; Distributed processes: Broadcast inanet ofprocessors (for allnodesbut theinitiatornode): 1 : apairoflinks(channels)whichconnecttwo nodesinbothdirections : aprocess : : 2 5 received thefirst message 2 5 8. MessagesinDistributed Systems 4 n-1 redundantmessages 4 Broadcast method 6 3 6 3 itsendsa 7 total number ofmessages:2*|edges| 7 fromtheremainingneighbours copy toallofitsnneighbours 6 spanning - 7 PPJ-72 PPJ-71 © 2011 bei Prof. Dr. Uwe Kastens © 2008 bei Prof. Dr. Uwe Kastens • • • • • Operations ofeach node • • • Task Method (roughly) 4 messages 2 messages have arrived. is repliedwitha i. e. probe toallneighbours except tos from neighbours Then combinetheechoesand Wait until Each further After having The nodehas theechoesonspanningtreeedges. return aspanningtree, let thefirstreceptiondetermine distribute theprobes like abroadcast, Aninitiatorrequestscombined : n -1probes The information is e. g. sum of certain valuese. localtoeachnode, topologyofthegraph, g.sumofcertain someglobalstate. n -1dummiesandechoes aresentoneach aresentoneach received thefirst probe incoming probe n neighbours dummy : . , senda Probe andecho: detailed operations initiator combined message. (except theinitiator): 1 Probe andecho inanet withan other edge spanning treeedge E send ittos E onitsway throughthenet( 2 5 information from allnodes incoming andoutgoinglinktoeach , E . . 4 E . E 6 3 E 1. P echo E inthegraph ( ); 7 P E ofthem. D probe P P P E D ). PPJ-74 PPJ-73 © 2003 bei Prof. Dr. Uwe Kastens © 2003 bei Prof. Dr. Uwe Kastens Server side Server Setup I/O-streamsonthesocket: Client side Get amachineaddress: • • • • • • • Port Socket I/O streams Several sockets Sockets arecreatedfrom Abstraction of via requests send may machines other on processes client a an Socket client =listener.accept(); ...client.close(); ... ServerSocket listener =newServerSocket(port); PrintWriter out= BufferedReader in= Socket myServer=new(addr2, port); addr1=InetAddress.getByName("java.sun.com"), InetAddress sever process : abstract connectionpoint new PrintWriter (myServer.getOutputStream(), true); new BufferedReader : (new InputStreamReader(myServer.getInputStream())); : createasocket machine: thatconnects totheserver : createaspecificsocket, acceptincomingconnections: host canbesetuponasocket. server network software is determined to isdetermined on one port may several serve ononeport clients. sockets addr3 =InetAddress.getLocalHost(); addr2 =InetAddress.getByName("206.26.48.100"), Connections via ports andsocketsConnections viaports machine address and port numbermachine address andport Sockets andI/O-streams of a computer; numerically encoded numerically of acomputer; for communication viaports. respond to a certain port, respond toacertain port

I/O streams I/O streams I/O ahn aeadpr number port and name machine e. g. port 13:dateandtime e. g.port . client client PPJ-76 PPJ-75 © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens • • • • • General scheme ofoperations: tree structuredsolutionspace methodforAlgorithmic thesolutionof sequential algorithm: Data structures: worker process manager process processgeneral structure: (e. g.sumofthecostsedgesS) Bound MinCost (S) is Sa solution is apartial solutionSisextended partial iterate until thequeueisempty: provides solutionsofsubtasks. creates new subtasks, and solves onesubtaskafteranother, combines thesolutionsofsubtasks manages thesubtaskstobesolved and update the queue a new solutionandabetterboundmay befound check thethuscreatednew elements remove thefirstelementandextend it complete : costsofthebestsolutionsofar. =C:eachsolutionthatcanbecreatedfrom ShasatleastcostC Applications Speedup A setof A taskisdecomposeddynamicallyina • • a queue sorted accordingtoMinCost;aboundvariable aqueuesorted Branch &Bound,DivideConquer, Backtracking image processing solving solution? (e. g.areallnodesreached) in parallel ondifferent processors. solve subtasks valid worker processes iftheprocessesareexecuted combinatorial problems ? (e. g.istheaddednodereachedfirsttime?) : dynamically issearchedfor abestsolution toS Branch andBound Worker paradigm ofthebagandmay combinatorial problems 1 , S 2 , ...(e. g.addanedgetoapath) decomposable ofthesamekind withmethodslike manager bag ofsubtasks create new ones subtasks solutions tasks, e.g. (e. g.traveling salesperson) . . worker worker . PPJ-78 PPJ-77 © 2006 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens for asingle Worker Protocol Each A 19 22 ABCD ABCDE central manager processcentral manager 20 17 10 X 5 worker process AB DE B ABE ABC 15 32 reqEl(getEl[getBound] (putEl|putBound)*reqEl)*terminate : ABCEDA 2 26 20 20 ABCED ABCE C 3 5 10 20 33 23 queue 5 bound manager 15 B&B example: Travelling salesperson 18 ABECDA 7 3 ABECD 15 25 extends anelement,checks it,computesitscosts, andanew bound BCABED ABEC 13 Parallel Branch &Bound(central) 25 Connection graph 2 Solution AB 520 holdsthequeueandboundvariable 6 14 11 8 ABEDCA ABEDC 8 11 13 27 33 C C ACE ACD ACB 30 reqEl X putEl getEl putBound 31 ACDE terminate getBound 25 28 3 22 Solution space 1 X CAD AC no choice A 29 30 12 ADCBE 25 DBADCE ADCB 22 X 25 X 9 D ADE ADC 213 12 25 13 23 ADCEBA ADCEB 25 17 20

25 order ofnodecreation 4 worker 24 10 16 ADECB 18 DCADEB ADEC 28 X

10

i

cost sofar path 17 21 PPJ-78a ADEBC PPJ-79 X 16 26 © 2003 bei Prof. Dr. Uwe Kastens © 2003 bei Prof. Dr. Uwe Kastens Idea: A token rotates through the ring and marks theprocesses( Atoken andmarks rotates throughthering Idea: a Determine Task: loadisbalancedbetweenThe work neighbours, e. g.organized inaring Several reqLoad manager processes cooperate When thetoken reachestheendofmarked sequence, the stateholdsglobally mayAt theendofmarked sequencethemark beresetagain. that have reachedthestateinquestion(inactive). inform allprocesses, e. g.„allprocessesareinactive“. manager manager getLoad Parallel Branch &Bound(distributed) i+1 newBound global stateofprocesses i as inPPJ-79 interface Termination inaring ...... -onefor eachworker process. work again work receives thatcommunicate ina worker worker i+1 i • • • Termination condition all taskchannelsareempty no managerhasanothertask all workers areinactive, yellow directed ring receives thetoken and has nowork token again receives the ) : , and PPJ-81 PPJ-80 © 2005 bei Prof. Dr. Uwe Kastens © 2011 bei Prof. Dr. Uwe Kastens client stub server skeleton server registry remote interface • • • Tasks Comparable techniques In Java RMIisavailable java.rmi. viathelibrary Remote MethodInvocation (RMI) method call interface identify objects object server- put (..){...} : systemprocessfor themachineandfor aport; : establishes relationsbetween namesandobjectreferences Method callsfor objectsonremotemachines (RMI) : proxy ofthe server, performs I/Otransfer ontheclientside for remoteaccessesandexecutable proxies for theremoteobjects (skeleton, stub) object server put (..){...} interface remote , parameter andresultaretransferred (objectserialization) proxy for ofthe server objects, remoteaccesses toserver : : specialrequirementsfor interface methods performs I/Otransfer side, ontheserver acrossmachineborders(objectmanagement,namingservice) skeleton registry : CORBAwithIDL,MicrosoftDCOMCOM name port : Callofamethodfor anobjectthatisonaremotemachine port RMI inJava client interface remote stub r.put(x, y); serv.put(x, y); client serv PPJ-83 PPJ-82 © 2011 bei Prof. Dr. Uwe Kastens © 2005 bei Prof. Dr. Uwe Kastens 8. Start someclientson client machines. Start 8. machine. ontheserver someservers Start 7. machine: ontheserver aregistry Start 6. objectonatargetmachineandcallsmethods: Clientidentifiestheserver 5. Generate theskeleton class; andstubfromtheadaptedserver 4. mainprogram Server objectandentersitintotheregistry: createstheserver 3. Develop classtoaremoteinterface: anadapterclasstoadapttheserver 2. Definearemoteinterface: 1. Example: make a Default Port is1099 rmiregistry [port]& v =serv.get(key); RemoteMap serv=(RemoteMap)remoteRegistry.lookup(remObjectName); Registry remoteRegistry=LocateRegistry.getRegistry(hostName); rmic RemoteMapAdapter copy theclientstubontomachine: Naming.rebind (registeredObjectName,adapter); RemoteMapAdapter adapter=new(adaptee); Hashtable adaptee=newHashtable(); } { publicRemoteMapAdapter(Hashtablea)adaptee=a;} public classRemoteMapAdapterextendsUnicastRemoteObject { publicObjectget(Objectkey)throwsRemoteException;...} public interfaceRemoteMapextendsjava.rmi.Remote ... { returnadaptee.get(key);} public Objectget(Objectkey)throwsRemoteException implements RemoteMap Hashtable RMI development steps(continued) available object asaserver RMI development steps PPJ-85 PPJ-84 © 2013 bei Prof. Dr. Uwe Kastens © 2003 bei Prof. Dr. Uwe Kastens The classofsuchobjectshastoimplementtheinterface For objectsingeneral That isstraight-forward for values of Parameters andresultsofRMIcalls } privateStrings; { class SIPairimplementsjava.io.Serializable import java.io.Serializable; on thereceiver sideanew objectiscreatedfromthosevalues. The values oftheirvariables aretransferred, Origin A send-receive-pair isboth the messageistransferred, like anassignment v:=b; When bothsenderandreceiver processesarereadyfor thecommunication, • • Operations space isprovided for Processes communicate and synchronize directly public StringtoString(){returns+"-"i;} public SIPair(Stringa,intb){s=a;ib;} private inti; ed() blocks receive (v) send (b): : Communicating SequentialProcesses(CSP)[C.A.R. Hoare, CACM 21,8,1978] q : blocksprocessisreadytosendamessage. untilthepartner : receive Objects asparameters ofRMIcalls 9. Synchronous message passing : only onemessage until the partner processisreadytoreceive themessage untilthepartner v data transfer andsynchronization point basic types aretransferred viaI/Ostreams. (insteadofachannel). and strings Serializable , . send : p PPJ-87 PPJ-86 © 2015 bei Prof. Dr. Uwe Kastens © 2013 bei Prof. Dr. Uwe Kastens • • • Guarded command:(invented by E.W. Dijkstra) A communication statementinaguard yields Notation ofan Execution ofaguarded command multiple ports Notation Example the communication andthebranch statementare executed. Otherwise If If open false true fi [] [] if if (i:1..n) Condition;p[i] ?v->Statements fi rnhmyb ae,i a if taken, be may branch a all guards arefalse some guards are true :..p?Port1(v1,..,vn)...... q!Port1(a1,..,an)...... q: ... p: p?v ... q!ex...... q: p: os varc:int; Cons: varx:int; Copy: varp:int; Prod: odto3 r?z Condition3; q?y Condition2; p!x Condition1; , if the partner process isreadytocommunicate , ifthepartner , if the partner process isterminated, , ifthepartner otherwise(processisnotready, notterminated) inCSPundOccam: : copy datafromaproducertoconsumer: theprocessisblocked untiloneoftheabove cases holds. Notations for synchronous messagepassing indexed selection and otu >Copy?c;...od do true-> Prod?x;Cons!xod do true-> p:=...;Copy!od do true-> composed messages theguardedcommand iscompletedwithoutexecuting anything. , oneofthemischosen, condition -> Statement3 -> Statement2 -> Statement1 : receive send Selective wait dependsontheguards: thevalue oftheexpression avalue fromprocess stu n a and true is may beused: communication p andassignittovariable Cons Copy Prod ex toprocess senabled is q x c ( guard PPJ-89 PPJ-88 ) v © 2003 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens Why candeadlocks notoccur? synchronizationNecessary only Synchronous communication provides both end process Worker(i:=0toN-1) const N:=6;vara[0:N-1]:int; end process Prod end process Buffer Example d{SUMandd>=N-1} od do d sum :=a[i]; var d:=1,sum,new:int od do p<42;Buffer!p->p:=p+1; fron var p:=0:int; := front cnt--; -> buf[front] ! Cons 0; od > cnt [] do Prefix sumscomputedwithsynchronous messages d :=2*d if (i-d)>=0->Worker(i-d)?new; sum:=+newfi ->Worker(i+d) !sumfi if (i+d)cnt++;rear:=%N+1; cnt Statement2 Condition2; [] do odto1 p!x->Statement1 Condition1; until allguards yieldfalse: (cf. synchronousbarriers,PPJ-48) Guarded loops { repeatstheexecution ofitsguarded Invariant transfer ofdataandsynchronization. SUM: sum=a[i-d+1]+...a[i]} end process Cons od do Buffer?c->printc; var c:int; a processfor eachelement get new value fromtheleft shift oldvalue totheright double thedistance t%N+1; PPJ-91 PPJ-90 © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens • • Technique Scheme ofthe Scheme ofthe oprRepl oprReq for each end process Server() end process Client(I:=1toN) • • • od [] o(c:1..N)ConditionOpr1; Client[c]?oprReq(oprArgs) do ...... Server ?oprRepl(myRes) Server !oprReq(myArgs) ... fid=0 Prove by induction. Choose isuchthati=N,thenprocessonlyreceives: There isalways aprocessthatdoes ! and? As sychronization pattern for transfer oftheparameters : correspondingly for other operations ... Client/Server schemeClient/Server withsynchronous messages j =i+d,i.e. j-d=i>=0 no process first receivesandthensends kind ofoperation for transfer ofthereply client processes server processserver operations occuralways No insynchronous prefixsums 0 -> ... that the server offers, thattheserver acommunication via : i-d Client[c] !oprRepl(oprResults) process therequest... : ec processjexecutes , hence in pairs not sendbut receives j-d i : , thereis Worker(j-d)?new Worker(i+d)!sum no deadlock i+d j ... : 2 ports N-1 : PPJ-93 PPJ-92 © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens • • • • • • Occam: (open source) Kent Retargetable occam ( can becommunicated viachannels formal processcalculuswherenames ofchannels pi-calculus imperative statementforms; send ( ( parallel processes( language for extended 2006to fundamental datatypes, arrays, records communication via a program of isanestedstructure initially developed in1983atINMOSLtd.asnative concurrent programming language, basedon SEQ Synchronous variants Client/Server: andcomparison ), guardedcommands( 5. Specialcase:resourceallocationwithrequestandrelease. 4. .The 3. Ifanoperation can 2. Variants ofsynchronous servers haveSynchronous servers the .Extensionto 1. ! ) andreceive ( sequence ofsend,receive, and guardedcommands. Conversation protocols Conversation sequences - therequestisnever delayed and - thereisnoresultreturned, its argumentshave tobestoredinapendingqueue it hastobedelayed, and whether acommunication isenabled use i. e. active monitors(PPJ-70). same characteristics asasynchronous servers (Milneret.al,1999): guarded commandloops reply port canbeomitted reply port INMOS Transputer Synchronous messages inOccam Occam-pi 1:1 channels PAR multiple instancesofservers ? ) operations, procedures, ), sequentialcodeblocks not beexecuted immediately ALT , University ofKent, GB ), synchronous systems areimplementedby a areexecuted „processtherequest“. inthepart KRoC tocheck : if ) CSP : PAR CHAN OFINTchn: , SEQ SEQ , b :=+1 chn ?b INT b: chn !a a :=42 INT a: PPJ-94a PPJ-94 © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens CHAN OFDatain,out • • • • • • Ada: SEQ PAR guarded commands( operations passdatainbothdirections, entry synchronous communication ( processes ( proposals initiatedby the (Paris) winsa dedicated for concurrency notions Ada 2005 Standard ISO 95 Ada Ada 83reference manual 1979: JeanIchbiahatCII-Honeywell-Bull purpose general WHILE TRUE Data d: -- SEQ -- SEQ WHILE TRUE Data d: Queue (k)buf: only oneproducerprocess , extensions in !d d =produce() ALT task process buffer embedded systems out !front(buf) in ?d competition Synchronous rendezvous inAda , programming language enqueue(buf, d) dequeue(buf) task type Bounded Buffer inOccam ! select nldn oconstructs oo including , &length(buf)0 ) ), SEQ WHILE TRUE Data d: -- end Consumer; begin task bodyConsumeris task typeConsumer; end Producer; begin task bodyProduceris task typeProducer; SEQ only oneconsumerprocess end loop; loop d: Data; end loop; loop d: Data; consume (d) out ?d consume (d); Buffer.Get (d); Buffer.Put (d); d :=produce(); PPJ-94b PPJ-94aa © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens task typeBufferis begin task bodyBufferis end Buffer; end Buffer; .Pure 1. Overview 5. 3. 2. 4. entry Put(d:inData); entry Get(d:outData); loop d: Data; buf: Queue(k); end loop; can be operands ofanoperation andargumentsofacall Concurrency parallelized evaluation Lazy paradigm Recursive taskdecomposition Actors inScala Message passinginErlang Dataflow languages select end select; 10. Concurrentandfunctionalprogramming functional programs donothave side-effects when length(buf)>0=> or when length(buf) evaluated inany order dequeue(buf); end Get; accept Get(d:outData)do end Put; accept Put(d:inData)do bag ofsubtasks accordingthe d :=front(buf); enqueue(buf, d); notions Ada: Synchronous rendezvous -- flsslasto leads lists of guarded command -- anddataflow machinessupport interface in functionallanguages -- -- inputport pipelining output port , inparticular rgasta rnfr streams transform that programs can beparallelized accordingtothe paradigma concurrently : end Consumer; begin task bodyConsumeris task typeConsumer; end Producer; begin task bodyProduceris task typeProducer; end loop; loop d: Data; end loop; loop d: Data; : stream programming consume (d); Buffer.Get (d); Buffer.Put (d); d :=produce(); a be can , PPj-94c PPJ-94ba © 2013 bei Prof. Dr. Uwe Kastens © 2013 bei Prof. Dr. Uwe Kastens Wesley, 2000,pp. 17-19] Multithreaded, Parallel, and Distributed Programming, Addison of Foundations Andrews: [G. See Fork two concurrentprocesses. trapezoid the from eps than less areas ofthetwo trapezoids two untilthesumof subintervals Recursively integral Compute an Wesley, 2000,pp. 17-19] Multithreaded, Parallel, andDistributed Programming, Addison of Foundations Andrews: [G. See trapezoid the from eps than less areas ofthetwo trapezoids two untilthesumof subintervals Recursively integral Compute an y y over f(x)between aandb. over f(x)between aandb. . . a a refine theinterval refine theinterval (m-a) (f(a)+f(m))/2 (m-a) (f(a)+f(m))/2 approximation ofthe approximation ofthe Recursive adaptivequadraturecomputation Recursive adaptivequadraturecomputation a a b b ∫ ∫ f(x) dx f(x) dx * * (b-a)/2 m = (b-a)/2 m = rao h big the of area big the of area differs differs into into b b f(x) f(x) x x in m=(r-l)/2and let fun quad(f,l,r,area,eps)= in m=(r-l)/2and let fun quad(f,l,r,area,eps)= quad (f,a,b,(f(a)+f(b)/2*(b-a),0.001) initial call: end quad (f,a,b,(f(a)+f(b)/2*(b-a),0.001) initial call: end let then if abs(larea+rarea-area)>eps let then if abs(larea+rarea-area)>eps else area end in (lar+rar) else area end in (lar+rar) rarea =(fm+fr)*(r-m)/2and larea =(fl+fm)*(m-l)/2and fr =f(r)and fm =f(m)and fl =f(l)and rarea =(fm+fr)*(r-m)/2and larea =(fl+fm)*(m-l)/2and fr =f(r)and fm =f(m)and fl =f(l)and oc rar =quad(f,m,r,rarea,eps) // lar =quad(f,l,m,larea,eps)and co rar =quad(f,m,r,rarea,eps) lar =quad(f,l,m,larea,eps)and PPj-94e PPj-94d © 2015 bei Prof. Dr. Uwe Kastens © 2013 bei Prof. Dr. Uwe Kastens transform astream ofnumbers: consume thefirst nelementsintoalist: produce astreamofnumbers: Programming the Streams establish auseful Lazy listsarecalled function which computesthetailofsequencewhenneeded That canbeachieved by replacingthe(pointerto)tailoflistby aparameterless evaluation Lazy Eager evaluation: Linear lists Functions onstreamscanbeunderstood ascommunicating concurrentprocesses. qae Cn x f)=Cn x*x, * (x Cons = xf)) (x, (Cons squares = raise Empty | fun squaresNil= = [] = x ::take( take(Cons(x, xf),n) take(Nil, n) | | take(xq,0) fun fun fromk=Cons(k, datatype ’aseq=Nil|Consof*(unit->’aseq) datatype ’alist=nil|::of* are fundamental data structures infunctionalprogramming, arefundamentaldatastructures e.g. in take 10 ae(qae (from1),10); (squares take decide uponsolution decide uponconvergence use random numbers summarize nytoeeeet fals r optdwihaegigt eaccessed. be to going are which computed are list a of elements those only creation allelementsofalistaretobecomputed,before any canbeaccessed. consumer streams Streams infunctionalprogramming Examples for streamfunctions(1) 1 49... ofastreamcanbe programming paradigm . fn()=> from(k+1) squares stream separated 1 23... int seq-> 'a seq*int->list int ->seq : enumerate solutionsspace iterate approximations random numbers sequence ofnumbers ); xf () fromprogramming its producer n)= qae (xf()) squares => fn() , n-1); from 1 : SML use . : PPJ-94g PPj-94f ); © 2013 bei Prof. Dr. Uwe Kastens © 2013 bei Prof. Dr. Uwe Kastens add thenumbers oftwo streams: take (primes,25); val primes=sieve(from2); fun sieve(Cons(p,nf))= fun siftp= Filter-Schema: Eratosthenes Sieve of numbers: Compute prime add_=Nil; | fun add(Cons(x,xf),Cons(y,yf))= Cons (p,fn()=>sieve(siftp(nf()))); filter (fnn=>modp<>0); u filter fun filter | take 10 ('a ->bool) fpe thenCons(x,fn()=>filterpred(xf())) if predx All recursively constructedsift-sieve-pairs canexecute concurrently! 50 525456... pred pred Cons (x+y, Examples for streamfunctions(2) Recursive streamcomposition -> (Cons(x,xf)) Nil primes else filterpred(xf()); 'a seq =Nil fn() =>add(xf(),yf()) sieve: hd tl ->'aseq = add sieve

int seq*-> 50 51 52... 51 50

0 1 2 ... sift sift: multiples ofp eliminate filter ) from 0 from 50 hd tl pred from p PPJ-94i PPJ-94h 2 © 2015 bei Prof. Dr. Uwe Kastens © 2013 bei Prof. Dr. Uwe Kastens G nrw:Fudtoso uttrae,Prle,adDsrbtdPormig dio ely 00 p 326-328] pp. 2000, Wesley, Addison Programming, Distributed and Parallel, Multithreaded, of Foundations Andrews: [G. grained parallelization by compiler, 1983 Assignment Language),noside-effects, fine- SISAL effects; 1976,Wadge, Ashcroft Lucid: Textual languages: Finally, inp. eachprocessholdsaprime The L processes A pipelineoffilters: first number p : (StreamsandIteration inaSingle streamcomputationsby equations, noside process Sieve[i=2toL] process Sieve[1] arecreated,eachsendsa do Sieve[i-1] ?p int p,next for [1=3tonby2] od received isaprime.It fi if (nextmodp)!=0-> Sieve[2] !i Sieve[i-1] ?next Sieve ofEratosthenesinCSP Sieve[i+1] !next Dataflow languages # passoddnumberstoSieve[2] interfaces hardware. toinstrumentation bytriggered available of input.Strongsupport connected by wires, concurrentexecution Nodes representstream processingfunctions LabVIEW (NationalInstruments, 1986) stream ofnumbers Visual languages: -># receivenextcandidate -># used tofilter # pisaprime # passiton dataflow andobject-oriented Prograph (Acadia University 1983): thefollowing numbers. to itssuccessor. : PPj-94k PPj-94j © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens Can notblock onanunexpected message! that matches apattern process’ the Searches end receive selective receive: yield aprocessidandmessage. The operands areexpressions which asynchonous messagesend: process creation: ... Pattern2 [whenGuard2]-> Pattern1 [whenGuard1]-> Pid !Message ArgumentList) FunctionName, spawn(Module, = Pid http://www.erlang.org TelecommunicationsEdition, Ericsson SystemsLaboratories, PrenticeHall,1996] [J. R.Virding,C. Armstrong, Wikström,M.Williams:ConcurrentProgramming inERLANG,Second • • • • Erlang Explanations andexamples taken from requirements: distributed, fault-tolerant, soft-real-time, non-stoppingsoftware single-assignment processes initial applicationarea: multi-paradigm: Actions2 ; Actions1 ; developed et.alat 1986by JoeArmstrong, communicate via Basic communication constructs functional mailbox , andreceives it. variables, telecommunication o a for Language Erlang and asynchronous message message no sharedmemory concurrent clients send end. loop(Val) -> start() -> -export([start/0,loop/1]). -module(counter). processes: A modulethatcreatescounter Initial example receive spawn(counter, loop,[0]). Ericsson between processes increment -> passing loop(Val +1) increment messagestoit PPj-94m PPj-94l © 2013 bei Prof. Dr. Uwe Kastens © 2013 bei Prof. Dr. Uwe Kastens clients. are notdisclosedto implementation details messages into and The callsof pid toaname. register initialized. allocated resources The two listsof removed messages are Unexpected recursion). iterated The receive is in thereply. identifies itself process The counter to replyit. the client’s pid, self() messages kinds of They send3 processes. called by client functions Interface allocate maintainstwoA server listsof free different kindsof (tail- gives are r transformed are . Thus, . torequestaresourceandfunction soitsthe associates allocate free and Example: Allocation server (interface) Example: Allocationserver end. value(Counter) increment(Counter) start() %% Firsttheinterfacefunctions. -export([start/0,loop/1,increment/1,value/1,stop/1]). -module(counter). loop(Val) -> %% Thecounterloop. stop(Counter) end. are Counter ! receive receive {Counter,Value}->Value Complete example: Counter ->spawn(counter,loop,[0]). end. request( free(Resource) ->request( allocate() ->request( % Theinterfacefunctions. start(Resources) -> -export([start/1,server/2,allocate/0,free/1]). -module(allocator). free andallocatedresources register receive {resource_alloc,Reply} ->Reply resource_alloc ! Pid =spawn(allocator,server, increment Other stop {From,value} { ->Counter! self() -> - true; -> -> Request ->Counter! (resource_alloc, Pid). -> ,value} free - From!{self(),Val}, -> ) -> [ Resources,[]] to return thatresource. toreturn , loop loop loop stop {self(),Request} alloc . Clientscallafunction (Val) (Val); (Val +1); . increment {free,Resource} ). ), . , PPj-94o PPj-94n ). © 2013 bei Prof. Dr. Uwe Kastens © 2013 bei Prof. Dr. Uwe Kastens general: object-oriented constructs: • • • • • • • • • • functional constructs: execution (asinSML);object-oriented functional constructs model(Java) Scala The or itreturns updates thelists, list, returns resource whether thereturned s_free call. in therecursive and and theresourceor s_allocate s_free s_allocate them intocallsof transforms and messages receives thetwo kindsof The function restrictable typeparameters, caseclasses matching basedonpattern case constructs since 2003, author: Martin Odersky, Martin since 2003, author: www.scala.org, docs.scala-lang.org compilable andexecutable togetherwithJava classes complex language, and quitecomplex languagedescription compact functionalnotation very andsubtypingpolymorphism polymorphism static typing,parametric mixins(traits) object-oriented classes definealltypes(typesareconsequently oo-includingbasictypes),subtyping, localtypeinference restricted polymorphism; parametric functions onlists, streams,... provided inabiglanguagelibrary nested functions, higherorderfunctions, currying, updates server : Object-oriented language(like: Object-oriented Java, morecompactnotation),augmentedby . : member R isinthefree ok Scala: object-orientedandfunctionallanguage error thetwo lists callloops. server Example: Allocation server (implementation) Example: Allocationserver and returns and server checks . no yes , end. s_free(Free, Allocated,From,R)-> s_allocate( s_allocate( server( case member({R,From},Allocated)of server( From !{resource_alloc, server( From !{resource_alloc,{ end. receive false ->From!{resource_alloc,error}, true ->From!{resource_alloc,ok}, {From,{free,R}} -> {From,alloc} -> Free, Allocated s_free( s_allocate( [], Allocated Free, [{R,From}|Allocated] [], Allocated [R|Free], Allocated server(Free, Allocated) server([R|Free], Free, Allocated Free, Allocated delete({R,From}, ) -> ). , From)-> no yes Allocated)); }, ,R}}, , From,R) , From)-> , From); ); PPJ-94q PPj-94p © 2015 bei Prof. Dr. Uwe Kastens © 2015 bei Prof. Dr. Uwe Kastens • • • usedtosimplifyreplying: Constructs • • • • • An Verlag Heidelberg2007] Berlin 4467, pp. 171–190,2007.©Springer- LNCS 2007, COORDINATION (Eds.): Vitek Threads andEvents; inA.L. MurphyandJ. [P. Haller, M.Odersky:ActorsThatUnify Verlag Heidelberg2007] Berlin 4467, pp. 171–190,2007.©Springer- LNCS 2007, COORDINATION (Eds.): Vitek Threads andEvents; inA.L.MurphyandJ. [P. Haller, M.Odersky:ActorsThatUnify sender is storedin (as in matches oneofthecasepatterns mailbox for thefirstmessagethat mailbox process thatexecutes for areply, it. andreturns a !?msg reply( The senderofareceived message case x receive send asynchronous actor {body} actor : Erlang p !msg isalightweight process: operation searchesthe msg isacatch-allpattern sends sender ) ) sends messagepassing puts msg createsa msg msg to body a into to , waits p Actors inScala(2) Actors inScala(1) ’s val customer=actor { } } } val customer=actor{ } val orderMngr=actor{ Example: orders andcancellations val orderMngr= Example: orders andcancellations orderMngr } } receive { orderMngr !Order(self,myItem) while (true) while (true) } match { case Ack(o) } receive { receive { case x=>junk+= case Ack(o)=>... case Cancel(o)=> case x case Cancel(sender,o) case Order(sender,item) case Order(item)=> } else if (o.pending){ } elsesender!NoAck if (o.pending){ sender !Ack(o) val o= reply( val o= !? reply( cancelOrder(o) sender !Ack(o) cancelOrder(o) handleOrder(sender,item) handleOrder( =>junk+=x Order(myItem) actor { =>... Ack(o) reply( Ack(o) ) NoAck ) sender ) => ,item) => PPj-94s PPj-94r © 2013 bei Prof. Dr. Uwe Kastens © 2013 bei Prof. Dr. Uwe Kastens 24.Describe the symmetric disseminationbarrier. thesymmetric 24.Describe thetreebarrier.23.Describe explain rule; theflagrules. 22.Explain thebarrier 21.Explain duplicationofdistanceattheexample prefixsums. Barriers whenusingthe methodof 20.How arewaiting conditionsandreleaseoperations inserted 19.Explain thedevelopment steps for themethod„Rendezvous ofprocesses“. 18.Formulate amonitorinvariant for scheme? thereaders/writers 17.Explain how monitorsaresystematicallydeveloped in5steps. 16.Where doesamonitorinvariant hold?Wherehasittobeproven? 15.When cannotifybeusedinsteadofnotifyAll? ofmonitorsinJava.14.Explain theproperties 13.Signal-and-continue requiresloopstocheck waiting-conditions. Why? 12.How doyou implementseveral conditionswithasinglecondition variable? 11.Which arethe3reasonswhy aprocessmay wait for amonitor? 10.Explain thesemanticsofconditionvariables andthevariants thereof. Explainhow thetwo kindsofsynchronizationareusedinmonitors. 9. Monitors Explaintechniquestoavoid interference between processes. 8. How isnon-interference between processesproven? 7. How isinterference between processesdefined? 6. Explainthenotions:atomicaction,at-most-onceproperty. 5. Whatdoestheweakest preconditionwp(s, Q)=Pmean? 4. Explainaxiomsandinference inHoareLogic. rules 3. Properties ofParallel Programs How areThreadscreatedinJava (3steps)? 2. Explainthenotions:sequential,parallel, interleaved, concurrentexecution ofprocesses. 1. Introduction counting variables? 11. Check your knowledge (1) Check your knowledge (2) PPJ-95a PPJ-95 © 2013 bei Prof. Dr. Uwe Kastens © 2013 bei Prof. Dr. Uwe Kastens 46.Describe a server for aserver 46.Describe resourceallocationaccording totheschemefor synchronous than redundant less and compact more messages synchronous on based programs are 45.Why for44.Explain theconstructs selective wait withsynchronous messages. 43.Compare thefundamentalnotionsofsynchronous andasynchronousmessages. Synchronous messages 42.Explain thetechniquefor inaring. termination theprocessinterface41.Describe for distributed branch-and-bound. 40.Explain theworker paradigm. Messages indistributed systems 39.How many messagesaresentinaprobe-and-echoscheme? 38.Which operations doesanodeexecute ofaprobe-and-echo? whenitispart 37.Which operations doesanodeexecute ofabroadcastinnet? whenitispart 36.Explain thenotionofconversation sequences. 35.What problem processesreceive occursifserver eachfromseveral channels? for34.Explain channelstructures paradigm. theclient/server 33.Explain typicalchannelstructures. 32.Explain thenotionofachannelanditsoperations. Asynchronous messages 31.How formally? areboundsofnestedloopsdescribed 30.Which transformations canbeusedtoparallelize theinnerloopifdependencevectors 29.How arethetransformationused? matrices 28.Explain theSRPtransformations. 27.Explain whichdependencevectors may occurinsequential(parallel) loops. 26.Show iteration spacesfor given loopsandviceversa. 25.Explain how listendsarefound inparallel. Data parallelism messages. those withasynchronous messages? are (0,1)and(1,0)? Check your knowledge (4) Check your knowledge (3) PPJ-97 PPJ-96 © 2013 bei Prof. Dr. Uwe Kastens 51.Explain the characteristics of Scala, in particular itsActors. ofScala,inparticular 51.Explain thecharacteristics selective receive. inparticular 50.Explain concurrencyinErlang, 49.Compare implementationsoftheSieve ofEratosthenes usingstreamsorCSP. 48.What arebenefitsofstreamprogramming? 47.Explain why paradigms infunctionalandconcurrentprogramming matchwell. Concurrent andfunctionalprogramming Check your knowledge (5) PPJ-98