so#ware transac-onal memory · – stm a == a computaon producing a value with type a that does...
TRANSCRIPT
![Page 1: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/1.jpg)
So#wareTransac-onalMemory
COS326DavidWalker
PrincetonUniversity
![Page 2: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/2.jpg)
LearnYouaHaskell!
hFp://learnyouahaskell.com/
![Page 3: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/3.jpg)
Hardware
Concurrency primitives
Library Library Library
Library
LibraryLibrary
Library
Libraries build layered concurrency abstractions
Whatwewant
![Page 4: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/4.jpg)
Immutable Data
FirstIdea:Don’tUseMutableData/Effects
Library Library
Library
Hardware
Library Library
Library
Good:• no race conditions, • no deadlock• interleavings don’t matter• deterministic• equivalent to sequential code it looks pretty to boot
Bad:• Can’t interact with the
world.• The world changes.• Threads can’t talk back and
forth.
![Page 5: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/5.jpg)
SecondOp-on:Locks
let deposit (a:account) (amount:int) : unit = with_lock a.lock (fun () -> if a.bal + amount < max_balance then a.bal <- a.bal + amount))
let with_lock l f = Mutex.lock l; let res = try f () with exn -> (Mutex.unlock l; raise exn) in Mutex.unlock l; res
Associatealockwitheachmutableobject.Acquire&releasesurroundingeachaccessReasonasifasequenceofinstruc-onsoccurssequen-ally.
![Page 6: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/6.jpg)
But....Managingmul-plemutableobjectsgotreallyhardbecauseyoucan'teasilyputtwogoodprogramcomponentstogethertomakeanothergoodprogramcomponent:
let withdraw (a:account) = ... let deposit (a:account) = ... let transfer (a:account)(b:account)(amt:float) = withdraw a amt; deposit b amt
otherthreadscanseeabadbalancevalueinbetweenwithdrawalanddeposit
hugeproblem:programmerss-llhavetothinkaboutallpossibleinterleavings
![Page 7: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/7.jpg)
And....Managingmul-plemutableobjectsgotreallyhardbecauseprogramsthatuselocksjustdon'tcomposeverywell:
let pop_two (s1:‘a stack) (s2:‘a stack) : (‘a * ‘a) option = with_lock s1.lock (fun _ -> with_lock s2.lock (fun _ -> match no_lock_pop s1, no_lock_pop s2 with | Some x, Some y -> Some (x,y) | Some x, None -> no_lock_push s1 x ; None | None, Some y -> no_lock_push s2 y ; None))
AndwehadtoworryaboutforgeZngtoacquirelocksorcrea-ngdeadlocks...
![Page 8: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/8.jpg)
SecondIdea:UseLocksorMPI
Hardware
Library Library
LibraryLibrar
y
Locks
Locks/MPI �(a) are hard to reason about(c) have lots of error modes
“Buildingcomplexparallelprogramsislikebuildingaskyscraperoutofbananas.”--SimonPeytonJones
![Page 9: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/9.jpg)
TheProblem
LocksareanindirectwayatgeZngtowhatwereallywant:
Howmightwedesignalanguageprimi-vethatencapsulatesthisno-on?
a; b; c; d;
executetheseinstruc-onsinorderwithoutinterrup-onbyotherthreads
![Page 10: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/10.jpg)
ThirdIdea:AtomicBlocks
Atomic blocks
Library Library Library
Library
LibraryLibrary
Library
Hardware
Atomic blocks are much easier to use, and do compose
Atomic blocks are pieces of code that you can count on to operate exactly like sequential programs
Tricky gaps, so a little harder than immutable data but you can do more stuff
![Page 11: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/11.jpg)
AtomicBlocksCutDownNondeterminism
ac-on1: ac-on2:
read x write x read x write x
read x write x read x write x
![Page 12: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/12.jpg)
read x write x read x write x
read x write x read x write x
ac-on1: ac-on2:
withtransac-ons:
orread x write x read x write x
read x write x read x write x
read x write x read x write x
read x write x read x write x
withoutatomictransac-ons:vastlymorepossibleinterleavings
read x
write x
read x
write x
read x
write x
read x
write x
AtomicBlocksCutDownNondeterminism
![Page 13: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/13.jpg)
STMINHASKELL
![Page 14: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/14.jpg)
ConcurrentThreadsinHaskell
main = do
id <- fork action1 action2 ...
fork :: IO a -> IO ThreadId
ac-on1andac-on2inparallel
![Page 15: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/15.jpg)
AtomicBlocksinHaskell
main = do id <- fork (atomic action1) atomic action2 ...
Idea: add a function atomic that guarantees atomic execution of a suspended (effectful) computation
ac-on1andac-on2atomicandparallel
![Page 16: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/16.jpg)
read x write x read x write x
read x write x read x write x
ac-on1: ac-on2:
withtransac-ons:
orread x write x read x write x
read x write x read x write x
read x write x read x write x
read x write x read x write x
main = do id <- fork (atomic action1) atomic action2 ...
AtomicBlocksinHaskell
![Page 17: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/17.jpg)
RecallMonadsKeyidea:
– Monadictypingconstrainstheuseofeffec5ulopera6ons
• int->int:– cannotaccessareference– cannotdooIOsoyouknowthatthatfunc-onispure
• int->IOint:– returnsasuspendedcomputa-onthatdoesaccessreferences– IOintcomputa-oncanbecomposedwithothercomputa-ons
Wewilldothesamethingtoimplementtransac-ons.Newkindofreferencethatcanonlybeaccessedinsideanatomicblock
![Page 18: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/18.jpg)
AtomicDetails
Introduceatypeforimpera-vetransac-onvariables(TVar)andanewMonad(STM)totracktransac-ons.– STMa==acomputa-onproducingavaluewithtypeathatdoes
transac-onalmemorybookkeepingontheside– HaskelltypesystemensuresTVarscanonlybemodifiedintransac-ons
withtypeSTMa• justlikeHaskellrefscanonlybeusedinsidecomputa-onswithtypeIOa
atomic :: STM a -> IO a new :: a -> STM (TVar a) read :: TVar a -> STM a write :: TVar a -> a -> STM ()
TVar a == ‘a ref
Haskell OCaml
![Page 19: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/19.jpg)
AtomicExample
-- inc adds 1 to the mutable reference r
inc :: TVar Int -> STM ()
inc r = do
v <- read r
write r (v+1)
main = do
r <- atomic (new 0) fork (atomic (inc r)) atomic (inc r);
![Page 20: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/20.jpg)
AtomicExample
-- inc adds 1 to the mutable reference r
inc :: TVar Int -> STM ()
inc r = do
v <- read r
write r (v+1)
main = do
r <- atomic (new 0) fork (atomic (inc r)) atomic (inc r);
Haskellislazysothesecomputa-onsaresuspendedandexecutedwithintheatomicblock
![Page 21: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/21.jpg)
STMinHaskell
TheSTMmonadincludesaspecificsetofopera-ons:• Can’tuseTVarsoutsideatomicblock• Can’tdoIOinsideatomicblock:
• atomicisafunc-on,notasyntac-cconstruct– calledatomicallyintheactualimplementa-on
• ...and,bestofall...
atomic :: STM a -> IO a new :: a -> STM (TVar a) read :: TVar a -> STM a write :: TVar a -> a -> STM()
atomic (if x<y then launchMissiles)
![Page 22: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/22.jpg)
STMComputa-onsCompose
ThetypeguaranteesthatanSTMcomputa-onisalwaysexecutedatomically.– GluemanySTM
computa-onstogetherinsidea“do”block
– ThenwrapwithatomictoproduceanIOac-on.
inc :: TVar Int -> STM ()
inc r = do
v <- read r write r (v+1)
inc :: TVar Int -> STM () inc2 r = do
inc r
inc r
inc :: TVar Int -> STM () foo = atomic (inc2 r)
Composi6onisTHEwaytobuildbigprogramsthatwork
![Page 23: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/23.jpg)
Excep-ons
let with_lock l f = Mutex.lock l; let res = try f () with exn -> (Mutex.unlock l; raise exn) in Mutex.unlock l; res
whenexcep-onsgetthrown,weareo#eninthemidstofsomecomplexac-on.here,wemustunlockthelocktogetbacktotheini-alstatemoregenerally,wemighthavemutatedmanypiecesofstateandmust"undo"ourchanges
![Page 24: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/24.jpg)
TheSTMmonadsupportsexcep-ons:
Inthecall(atomics),ifsthrowsanexcep-on,thetransac6onisabortedwithnoeffectandtheexcep-onispropagatedtotheenclosingcode.Noneedtorestoreinvariants,orreleaselocks!(yous-llneedtodealwiththeexcep-on...)
throw :: Exception -> STM a catch :: STM a ->(Exception -> STM a)-> STM a
Excep-ons
![Page 25: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/25.jpg)
Starva-on
Worry:Couldthesystem“thrash”bycon-nuallycollidingandre-execu-ng?No:Atransac-oncanbeforcedtore-executeonlyifanothersucceedsincommiZng.Thatgivesastrongprogressguarantee.But:Apar-cularthreadcouldstarve:
Thread 1Thread 2Thread 3
![Page 26: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/26.jpg)
THREEMOREIDEAS:RETRY,ORELSE,ALWAYS
![Page 27: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/27.jpg)
Idea1:Composi-onalBlocking
• retrymeans“abortthecurrenttransac-onandre-executeitfromthebeginning”.
• Implementa-onavoidsearlyretryusingreadsinthetransac-onlog(i.e.acc)towaitonallreadvariables.– ie:retryonlyhappenswhenoneofthevariablesreadonthepathto
theretrychanges
withdraw :: TVar Int -> Int -> STM () withdraw acc n =
do bal <- readTVar acc if bal < n then retry writeTVar acc (bal-n)
retry :: STM ()
![Page 28: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/28.jpg)
Composi-onalBlocking
• Retryingthreadiswokenupautoma-callywhenacciswriFen,sothereisnodangerofforgoFenno-fies.
• NodangerofforgeZngtotestcondi-onsagainwhenwokenupbecausethetransac-onrunsfromthebeginning.
• Correct-by-construc6ondesign!
withdraw :: TVar Int -> Int -> STM () withdraw acc n =
do { bal <- readTVar acc; if bal < n then retry; writeTVar acc (bal-n) }
![Page 29: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/29.jpg)
WhatmakesRetryComposi-onal?
retrycanappearanywhereinsideanatomicblock,includingnesteddeepwithinacall.Forexample,
waitsfor:• a1balance>3• anda2balance>7• withoutanychangetowithdrawfunc6on.
atomic (do { withdraw a1 3; withdraw a2 7 })
![Page 30: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/30.jpg)
Idea2:Choice
Supposewewanttotransfer3dollarsfromeitheraccounta1ora2intoaccountb.
orElse :: STM a -> STM a -> STM a
atomic (
do (withdraw a1 3) `orElse` (withdraw a2 3)
deposit b 3
)
Try this ...and if it retries, try this
then afterward, do this
![Page 31: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/31.jpg)
Choiceiscomposable,too!
transfer :: TVar Int -> TVar Int -> TVar Int -> STM ()
transfer a1 a2 b = do withdraw a1 3 `orElse` withdraw a2 3 deposit b 3
atomic ( transfer a1 a2 b
`orElse` transfer a3 a4 b )
Thefunc-ontransfercallsorElse,butcallstotransfercans-llbecomposedwithorElse.
![Page 32: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/32.jpg)
ComposingTransac-ons
• Atransac-onisavalueoftypeSTMa.• Transac-onsarefirst-classvalues.• Buildabigtransac-onbycomposingliFletransac-ons:insequence,usingorElseandretry,insideprocedures....
• Finallysealupthetransac-onwithatomic::STMa->IOa
![Page 33: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/33.jpg)
Equa-onalReasoning
STM supports nice equations for reasoning:
a `orElse` (b `orElse` c) == (a `orElse` b) `orElse` sretry `orElse` s == ss `orElse` retry == s�
These equations make STM an instance of a structure known as a MonadPlus -- a Monad with some extra operations and properties.
![Page 34: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/34.jpg)
Idea3:Invariants
Theroutetosanityistoestablishinvariantsthatareassumedonentry,andguaranteedonexit,byeveryatomicblock.– muchlikeinamodulewithrepresenta6oninvariants– thisgivesyoulocalreasoningaboutyourcode
• Wewanttochecktheseguarantees.Butwedon’twanttotesteveryinvarianta#ereveryatomicblock.
• Hmm....Onlytestwhensomethingreadbytheinvarianthaschanged....ratherlikeretry.
![Page 35: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/35.jpg)
Invariants:OneNewPrimi-ve
always :: STM Bool -> STM ()
newAccount :: STM (TVar Int)
newAccount = do { r <- new 0; always (accountInv r); return v }
accountInv r = do { x <- read r; return (x >= 0)};
An arbitrary boolean valued STM computation
Any transaction that modifies the account will check the invariant (no forgotten checks). If the check fails, the transaction restarts. A persistent assert!!
![Page 36: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/36.jpg)
Whatalwaysdoes
• Thefunc-onalwaysaddsanewinvarianttoaglobalpoolofinvariants.
• Conceptually,everyinvariantischeckedaseverytransac-oncommits.
• Buttheimplementa-onchecksonlyinvariantsthatreadTVarsthathavebeenwriFenbythetransac-on
• ...andgarbagecollectsinvariantsthatarecheckingdeadTvars.
always :: STM Bool -> STM ()
![Page 37: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/37.jpg)
Whatdoesitallmean?• Everythingsofarisintui-veandarm-wavey.• Butwhathappensifit’sraining,andyouareinsideanorElse
andyouthrowanexcep-onthatcontainsavaluethatmen-ons...?
• Weneedaprecisespecifica-on!
![Page 38: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/38.jpg)
No way to wait for complex conditions
One exists
See “Composable Memory Transactions” for details.
Take COS 510 to understand what it means!
![Page 39: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/39.jpg)
HASKELLIMPLEMENTATION
![Page 40: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/40.jpg)
Implementa-on
Anaiveimplementa-on(c.f.databases):– Everyloadandstoreinstruc-onlogsinforma-onintoathread-locallog.
– Astoreinstruc-onwritesthelogonly.– Aloadinstruc-onconsultsthelogfirst.– Validatethelogattheendoftheblock.
• Ifsucceeds,atomicallycommittosharedmemory.• Iffails,restartthetransac-on.
![Page 41: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/41.jpg)
StateoftheArtCirca2003No
rmal
ised
exec
ution
time
Sequential baseline (1.00x)
Coarse-grained locking (1.13x)
Fine-grained locking (2.57x)
Traditional STM (5.69x)
Workload: operations on a red-black tree, 1 thread, 6:1:1
lookup:insert:delete mix with keys 0..65535
See “Optimizing Memory Transactions” for more information.
![Page 42: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/42.jpg)
NewImplementa-onTechniquesDirect-updateSTM
– Allowstransac-onstomakeupdatesinplaceintheheap– Avoidsreadsneedingtosearchthelogtoseeearlierwritesthatthe
transac-onhasmade– Makessuccessfulcommitopera-onsfasteratthecostofextraworkon
conten-onorwhenatransac-onaborts
Compilerintegra-on– Decomposetransac-onalmemoryopera-onsintoprimi-ves– Exposetheseprimi-vestocompilerop-miza-on(e.g.tohoist
concurrencycontrolopera-onsoutofaloop)
Run-mesystemintegra-on– Integratestransac-onswiththegarbagecollectortoscaletoatomic
blockscontaining100Mmemoryaccesses
![Page 43: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/43.jpg)
Results:ConcurrencyControlOverheadNo
rmal
ised
exec
ution
time
Sequential baseline (1.00x)
Coarse-grained locking (1.13x)
Fine-grained locking (2.57x)
Direct-update STM (2.04x)
Direct-update STM + compiler integration
(1.46x)
Traditional STM (5.69x)
Scalable to multicore
Workload: operations on a red-black tree, 1 thread, 6:1:1
lookup:insert:delete mix with keys 0..65535
![Page 44: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/44.jpg)
Results:Scalability(forsomebenchmark;yourexperiencemayvary)
#threads
Fine-grained locking
Direct-update STM + compiler integration
Traditional STM
Coarse-grained locking
Micro
seco
nds
per
oper
ation
![Page 45: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/45.jpg)
Performance,SummaryNaïveSTMimplementa-onishopelesslyinefficient.Thereisalotofresearchgoingoninthecompilerandarchitecturecommuni-estoop-mizeSTM.
– hardware-supportedSTM
![Page 46: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/46.jpg)
STMWRAPUP
![Page 47: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/47.jpg)
STMinMainstreamLanguages
TherearesimilarproposalsforaddingSTMtoJavaandothermainstreamlanguages.
class Account { float balance; void deposit(float amt) { atomic { balance += amt; } } void withdraw(float amt) { atomic { if(balance < amt) throw new OutOfMoneyError(); balance -= amt; } } void transfer(Acct other, float amt) { atomic { // Can compose withdraw and deposit. other.withdraw(amt); this.deposit(amt); } } }
![Page 48: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/48.jpg)
WeakvsStrongAtomicity
• UnlikeHaskell,typesystemsinmainstreamlanguagesdon’tcontrolwhereeffectsoccur.
• Whathappensifcodeoutsideatransac-onconflictswithcodeinsideatransac-on?– WeakAtomicity:Non-transac-onalcodecanseeinconsistentmemorystates.Programmershouldavoidsuchsitua-onsbyplacingallaccessestosharedstateintransac-on.
– StrongAtomicity:Non-transac-onalcodeisguaranteedtoseeaconsistentviewofsharedstate.Thisguaranteemaycauseaperformancehit.For more information: “Enforcing Isolation and Ordering in STM”
![Page 49: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/49.jpg)
EveninHaskell:Easier,ButNotEasy.
Theessenceofshared-memoryconcurrencyisdecidingwherecri6calsec6onsshouldbeginandend
– Toosmall:applica-on-specificdataraces(Eg,mayseedepositbutnotwithdrawiftransferisnotatomic).
– Toolarge:delayprogressbecausedenyotherthreadsaccesstoneededresources.
InHaskell,wecancomposeSTMsubprogramsbutatsome
point,wemustdecidetowrapanSTMin"atomic"
Programscans-llbenon-determinis-candhardtodebug
![Page 50: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/50.jpg)
S-llNotEasy,Example
Considerthefollowingprogram:
Successfulcomple-onrequiresA3toruna#erA1butbeforeA2.Sodele-ngacri-calsec-on(byuncommen-ngA0)changesthebehavioroftheprogram(fromnon-termina-ngtotermina-ng).
Thread 1 // atomic { //A0 atomic { x = 1; } //A1 atomic { if (y==0) abort; } //A2 //}
Thread 2 atomic { //A3 if (x==0) abort; y = 1; }
Initially, x = y = 0
![Page 51: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/51.jpg)
STMConclusions
Atomicblocks(atomic,retry,orElse)drama-callyraisethelevelofabstrac-onforconcurrentprogramming.
– Givesprogrammerbacksomecontroloverwhenandwheretheyhavetoworryaboutinterleavings
Itislikeusingahigh-levellanguageinsteadofassemblycode.Wholeclassesoflow-levelerrorsareeliminated.
– Correct-by-construc-ondesign
Notasilverbullet:– youcans-llwritebuggyprograms;– concurrentprogramsares-llharderthansequen-alones– aimedonlyatsharedmemoryconcurrency,notmessagepassing
![Page 52: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/52.jpg)
WHAT'SNEXT?
![Page 53: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/53.jpg)
Inthiscourse• Anintroduc-ontofunc-onalprogramming
– immutabledata– func-onsasdata– coolabstrac-ons:
• futures,lazycomputa-ons,streams,parallelcollec-ons,atomicblocks,con-nua-ons,modules,functors,monads
• Inmostcases,Itriedtoexplainhowanabstrac-onhelpedaprogrammerreasonabouthisorherprogram
• Because,intheend,thatisthemostimportantaspectofagoodprogramminglanguage:itmakesiteasiertoreasonaboutyourprograms
![Page 54: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/54.jpg)
COS510(Spring2015)
Anintroduc-ontomechanicalreasoningaboutprogramsandprogramminglanguages.
It'sagradcoursebutundergradsareencouragedtotakeit!Colleen(room510)willfilloutallthesignaturesontheformconcerning
undergraduatestakingagraduatecourse!
![Page 55: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/55.jpg)
ProgrammingLanguageTheoryOCaml'stypesystemmakespredic-ons:
e:int->int
"expressione,ifitterminates,willevaluatetoafunc-onvaluefunx->..."
CanweprovethatOCaml'spredic-onsalwayscometrue?
That'sactuallyapreFydifficultthingtoshowingeneral!Thereareinfinitelymanywell-typedprograms!Andthemayexecuteforarbitrarymanysteps!
![Page 56: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/56.jpg)
Also,recallthemidterm:
letreciterate(f:intlist->intlist)(x:intlist):intlist=matchxwith[]->[]|hd::tl->iteratef(ftl)letreciterate2(x:intlist)(f:intlist->intlist):intlist=matchxwith[]->[]|hd::tl->iterate2(ftl)fTheorem:forallf:intlist->intlist.forallx:intlist.iteratefx==iterate2xf
casex=hd::tl:iteratef(hd::tl) (LHS)==matchhd::tlwith... (eval)==iteratef(ftl) (eval)==iterate2(ftl)f (byIH)==matchhd::tlwith... (rev.eval)==iterate2(hd::tl)f (rev.eval)
oops!Canwedevisea
programminglanguagethatcancheckthecorrectnessofour
proofs?
![Page 57: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/57.jpg)
Yuppers!
Coq:Atheoremprovingenvironment
![Page 58: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/58.jpg)
Yuppers!
![Page 59: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/59.jpg)
Yuppers!
![Page 60: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/60.jpg)
Yuppers!
![Page 61: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/61.jpg)
Yuppers!
![Page 62: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/62.jpg)
Yuppers!
![Page 63: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/63.jpg)
Yuppers!
![Page 64: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/64.jpg)
Yuppers!
![Page 65: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars](https://reader033.vdocuments.mx/reader033/viewer/2022053118/609df2b1e404822ea11b0467/html5/thumbnails/65.jpg)
Summary
Ifyouenjoyed326,especiallythetheore-calparts,thereisalotmorefunstufftolearnin510.
HappyHolidays!