+ All Categories
Home > Documents > Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun...

Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun...

Date post: 30-Aug-2019
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
22
CSP/CML Hopac C# Channels Counter Compile State Send Channels – the CSP/CML Model Radu Nicolescu Department of Computer Science University of Auckland 28 March 2019 31 March 2019 1 / 29
Transcript
Page 1: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Channels – the CSP/CML Model

Radu NicolescuDepartment of Computer Science

University of Auckland

28 March 201931 March 2019

1 / 29

Page 2: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

1 CSP/CML

2 Hopac

3 C# Channels

4 Counter Sample

5 Compile and Run

6 State Sample

7 Async Post

2 / 29

Page 3: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

CSP (wiki)

• Communicating sequential processes (CSP) is a formallanguage for describing patterns of interaction in concurrentsystems• inspired channels, rendezvous in programming

• family of process calculi/algebras: CSP, CCS, π, join, ...

• CSP was first described in a 1978 paper by Tony Hoare, buthas since evolved substantially

• Tony Hoare• algorithm: quicksort

• formal language: CSP

• concurrency: monitors, dining philosophers

• programming languages: occam language, Algol W

• null = billion dollar mistake!4 / 29

Page 4: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

CML (wiki)

• First functional language: LISP (John McCarthy, 1958) = ListProcessor (Lots of Insipid Stupid Parentheses ,)

• LISP family: LISP, Scheme, Clojure... JavaScript!

• ML family: ML (Meta Language), Standard ML, CAML(Categorical Abstract Machine Language), OCAML (ObjectCAML), Scala, F# (OCAML for .NET)

• John Reppy CML (Concurrent ML): channels, rendezvous,events from CSP

• Channels: CML (language), Go (language), F# Hopac(library), C# Channels (library), JavaScript, Rust, Kotlin, ...

• Rendezvous, Joins: Ada, JoCAML (Joins CAML), Cω, ...

5 / 29

Page 5: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Channels – bird’s eye view

• Named communication channels between concurrent tasks

• Default: multiple writers, aka multiple readers

• Default: zero-size buffer, aka rendezvous (sync handshake)

• Extensions: bounded buffers, even unbounded (≈ actors)

• Many lightweight async tasks (aka green threads)

• Theory: channels ≡ actors (but shine for different apps)

6 / 29

Page 6: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Hopac Readings

• Hopac repositoryhttps://github.com/Hopac/Hopac

• Hopac Programming Guide https://github.com/Hopac/

Hopac/blob/master/Docs/Programming.md

• Hopac Reference – monads, high-order combinatorics...https://hopac.github.io/Hopac/Hopac.html

• Demistify FP – Hopachttps://www.demystifyfp.com/tags/hopac/

8 / 29

Page 7: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Peformance – Hopac vs Akka

Stream performance – the lower the betterhttp://vaskir.blogspot.com/2016/05/

akkanet-streams-vs-hopac.html9 / 29

Page 8: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Peformance – Hopac vs Mailbox vs Kotlin

Actor-like performance – the higher the betterhttps://vasily-kirichenko.github.io/fsharpblog/actors

10 / 29

Page 9: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Hopac Basics

• Create a default typed channel

1 l e t ch = Ch<s t r ing> ( )

• A job = lightweight async task (as a sugared monad)here w/ async sleep

1 j o b {2 do ! t i m e O u t M i l l i s 1003 }

• Communicate via a channel

1 j o b {2 do ! ch ∗<− m // do ! Ch . g i v e ch m // sync post3 do ! ch ∗<+ m // do ! Ch . send ch m // async post4 l e t ! m = ch // l e t ! m = Ch . t a k e ch // receive5 }

11 / 29

Page 10: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

C# Channels Readings

• MSDN System.Threading.Channels Namespacehttps:

//docs.microsoft.com/en-us/dotnet/api/system.

threading.channels?view=dotnet-plat-ext-2.1

• Github System.Threading.Channelshttps://github.com/dotnet/corefx/tree/master/src/

System.Threading.Channels

• Exploring System.Threading.Channelshttps://ndportmann.com/system-threading-channels/

13 / 29

Page 11: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

C# Channels Basics

• Create a size 1 one-to-one typed channel (no size 0)

1 var opt = new BoundedChannelOpt ions ( 1 )2 { S i n g l e W r i t e r=true , S i n g l e R e a d e r=true ,} ;3 var ch = Channel . CreateBounded<s t r ing> ( opt ) ;

• Use the existing async/await framework

• Communicate via a channel

1 async Task . . . {2 await ch . W r i t e r . Wri teAsync (m) ; // async post34 var m = await ch . Reader . ReadAsync ( ) ; // receive5 }

14 / 29

Page 12: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Counter – F# Mailbox

• actor = inbox + async function

1 l e t agent = M a i l b o x P r o c e s s o r . S t a r t ( fun i n b o x −>2 l e t rec l o o p count =3 async {4 l e t ! msg = i n b o x . R e c e i v e ( )5 do ! Async . S l e e p 1006 return ! l o o p ( count +1)7 }8 l o o p 09 )

• easy to post message from outside

1 agent . Post m

16 / 29

Page 13: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Counter – F# Hopac

• separate channel, job (async-like) function

1 l e t ch = Ch<s t r ing> ( )23 l e t agent =4 l e t rec l o o p count = j o b {5 l e t ! msg = ch // l e t ! msg = Ch . t a k e ch6 do ! t i m e O u t M i l l i s 1007 return ! l o o p ( count +1)8 }9 s t a r t ( l o o p 0)

• easier to post message from another job, that could run sync

1 l e t s e t u p = j o b {2 do ! ch ∗<− m // do ! Ch . g i v e ch m3 }45 run s e t u p

17 / 29

Page 14: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Counter – C# Channels

• separate channel (w/ options)=

1 BoundedChannelOpt ions one2one =2 new BoundedChannelOpt ions ( 1 )3 { S i n g l e W r i t e r=true , S i n g l e R e a d e r=true , } ;45 Channel<s t r ing> ch =6 Channel . CreateBounded<s t r ing> ( one2one ) ;

• separate async function (not started)

1 async Task agent ( ) {2 var count = 0 ;3 f o r ( ; ; ) {4 var msg = await ch . Reader . ReadAsync ( ) ;5 count += 1 ;6 await Task . Delay ( 1 0 0 ) ;7 }8 }

18 / 29

Page 15: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Counter – C# Channels

• easier to post message from another async task

1 async Task Main ( ) {2 var a = agent ( ) ; // create agent and start it async34 await ch . W r i t e r . Wri teAsync (m) ;5 }

19 / 29

Page 16: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Compile and Run – F# Mailbox

• Mailbox in FSharp.Core, so no special configuration required

• All F# code: Linqpad preamble ⇔ module line in .FS

1 <Query Kind=” FSharpProgram ” />23 module M

• Command-line compilation

1 f s c F#−Actor−c o u n t e r s . f s

• For all F# programs: you may also want to create a properMain function, that will invoke the rest

1 [<E n t r y P o i n t >]2 l e t main a r g s =3 . . .4 0

21 / 29

Page 17: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Compile and Run – F# Hopac

• Hopac NOT in FSharp.Core, additional configuration required

• Command-line compilation – two libraries

1 f s c −r : Hopac . d l l −r : Hopac . Core . d l l2 F#−Hopac−c o u n t e r s . f s

• Runtime – one more library

1 Hopac . P l a t f o r m . d l l

• Runtime – F#-Hopac-counters.exe.config,to ensure server garbage collection

1 <c o n f i g u r a t i o n >2 <runt ime>3 <g c S e r v e r e n a b l e d=” t r u e ”/>4 </runt ime>5 </ c o n f i g u r a t i o n>

22 / 29

Page 18: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Compile and Run – C# Channels

• Channels lib NOT in system, additional configuration required

• Command-line compilation – three libraries + netstandard

1 c s c −r : m s c o r l i b . d l l , n e t s t a n d a r d . d l l ,2 System . Thread ing . Channe l s . d l l ,3 System . Thread ing . Tasks . E x t e n s i o n s . d l l ,4 System . Runtime . C o m p i l e r S e r v i c e s . Unsafe . d l l5 C#−Channels−c o u n t e r s . c s

• Runtime – one more library

1 System . C o l l e c t i o n s . Immutable . d l l

• Code – see and compare the samples

1 s t a t i c void Main ( s t r i ng [ ] a r g s ) {2 new Program ( ) . Main2 ( ) . Wait ( ) ;3 }

23 / 29

Page 19: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

State – F# Mailbox, F# Hopac, C# Channels

12 // please see samples

25 / 29

Page 20: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Hopac Async Post

• Async communication via a 0-size channel?

1 j o b {2 do ! ch ∗<− m // do ! Ch . g i v e ch m // sync post34 do ! ch ∗<+ m // do ! Ch . send ch m // async post5 }

• Ch.give is a sync post: the writer awaits (logically suspended)until the rendezvous!

• Ch.send is an async post: the writer generates a hidden helperjob that takes charge of the actual writing!

• this helper job awaits until the rendezvous! Cf. code sample!

• thus the original writer job can continue!

27 / 29

Page 21: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Hopac Async Post

• Simulating async send on sync give

1 l e t ( ∗<++ ) ch m = // ch ∗<+ m2 j o b {3 l e t h e l p = j o b {4 do ! ch ∗<− m5 }6 s t a r t h e l p7 }

• Usage – awaiting for help start (not completion)

1 do ! ch ∗<++ m // do ! ch ∗<+ m

28 / 29

Page 22: Channels the CSP/CML Model - cs.auckland.ac.nz · 1 let agent = MailboxProcessor . Start ( fun inbox > 2 let rec loop count = 3 async f 4 let ! msg = inbox . Receive 5 do! Async .

CSP/CML Hopac C# Channels Counter Compile State Send

Async Post Samples – out of the box features

size sync write async write read

F# Hopac Chan = 0 ∗<− .give ∗<+ .send .take

“ BoundedMb ≥ 0 .put .take

“ Mailbox ∞ .send .take

F# Actors ∞ .Post .Receive

C# Channels ≥ 1, ∞ .WriteAsync .ReadAsync

GO Channels ≥ 0 <− <−JS Channels ≥ 0 .push .shift

More later: backpressure, select, ...

29 / 29


Recommended