© 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 synchronization, monitorsinJava threads, KMS KMS process, 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 Distributed Computing 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(); Thread 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 algorithms; 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, deadlock-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 algorithm 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 )
and
b e account ,
i
at arbitrary points at arbitrary
account
f c and isineachcase.
intime.
) 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
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(lock). 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)
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
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