Concurrent & Multicore OCaml: A deep dive KC Sivaramakrishnan 1 & Stephen Dolan 1 Leo White 2 , Jeremy Yallop 1,3 , Armaël Guéneau 4 , Anil Madhavapeddy 1,3 1 2 3 4
Concurrency ≠ Parallelism • Concurrency • Programming technique • Overlapped execution of processes • Parallelism • ( Extreme ) Performance hack • Simultaneous execution of computations
Concurrency ≠ Parallelism • Concurrency • Programming technique • Overlapped execution of processes • Parallelism • ( Extreme ) Performance hack • Simultaneous execution of computations Concurrency ∩ Parallelism ➔ Scalable Concurrency
Concurrency ≠ Parallelism • Concurrency • Programming technique • Overlapped execution of processes • Parallelism • ( Extreme ) Performance hack • Simultaneous execution of computations Concurrency ∩ Parallelism ➔ Scalable Concurrency ( Fibers ) ( Domains )
Schedulers • Multiplexing fj bers over domain(s) Bake scheduler into the runtime system (GHC) •
Schedulers • Multiplexing fj bers over domain(s) Bake scheduler into the runtime system (GHC) • • Allow programmers to describe schedulers! Parallel search —> LIFO work-stealing • Web-server —> FIFO runqueue • Data parallel —> Gang scheduling •
Schedulers • Multiplexing fj bers over domain(s) Bake scheduler into the runtime system (GHC) • • Allow programmers to describe schedulers! Parallel search —> LIFO work-stealing • Web-server —> FIFO runqueue • Data parallel —> Gang scheduling • • Algebraic E ff ects and Handlers
Algebraic e ff ects & handlers
Algebraic e ff ects & handlers • Programming and reasoning about computational e ff ects in a pure setting. Cf. Monads •
Algebraic e ff ects & handlers • Programming and reasoning about computational e ff ects in a pure setting. Cf. Monads • • E ff — http://www.e ff -lang.org/
Algebraic E ff ects: Example exception Foo of int let f () = 1 + (raise (Foo 3)) let r = try f () with Foo i -> i + 1
Algebraic E ff ects: Example exception Foo of int let f () = 1 + (raise (Foo 3)) let r = try f () with Foo i -> i + 1
Algebraic E ff ects: Example exception Foo of int let f () = 1 + (raise (Foo 3)) let r = try f () with Foo i -> i + 1 val r : int = 4
Algebraic E ff ects: Example effect Foo : int -> int exception Foo of int let f () = 1 + (perform (Foo 3)) let f () = 1 + (raise (Foo 3)) let r = let r = try try f () f () with effect (Foo i) k -> with Foo i -> i + 1 continue k (i + 1) val r : int = 4
Algebraic E ff ects: Example effect Foo : int -> int exception Foo of int let f () = 1 + (perform (Foo 3)) let f () = 1 + (raise (Foo 3)) let r = let r = try try f () f () with effect (Foo i) k -> with Foo i -> i + 1 continue k (i + 1) val r : int = 4
Algebraic E ff ects: Example effect Foo : int -> int exception Foo of int let f () = 1 + (perform (Foo 3)) let f () = 1 + (raise (Foo 3)) let r = let r = try try f () f () with effect (Foo i) k -> with Foo i -> i + 1 continue k (i + 1) val r : int = 4
Algebraic E ff ects: Example effect Foo : int -> int exception Foo of int let f () = 1 + (perform (Foo 3)) 4 let f () = 1 + (raise (Foo 3)) let r = let r = try try f () f () with effect (Foo i) k -> with Foo i -> i + 1 continue k (i + 1) val r : int = 4
Algebraic E ff ects: Example effect Foo : int -> int exception Foo of int let f () = 1 + (perform (Foo 3)) 4 let f () = 1 + (raise (Foo 3)) let r = let r = try try f () f () with effect (Foo i) k -> with Foo i -> i + 1 continue k (i + 1) val r : int = 4 val r : int = 5
Algebraic E ff ects: Example effect Foo : int -> int exception Foo of int let f () = 1 + (perform (Foo 3)) 4 let f () = 1 + (raise (Foo 3)) let r = let r = try try f () f () with effect (Foo i) k -> with Foo i -> i + 1 continue k (i + 1) val r : int = 4 val r : int = 5 fj ber — lightweight stack
Scheduler Demo 1 [1] https://github.com/kayceesrk/ocaml15-e ff /tree/master/chameneos-redux
Implementation • Fibers: Heap allocated, dynamically resized stacks • ~10s of bytes • No unnecessary closure allocation costs unlike CPS
Implementation • Fibers: Heap allocated, dynamically resized stacks • ~10s of bytes • No unnecessary closure allocation costs unlike CPS • One-shot delimited continuations • Simpli fj es reasoning about resources - sockets, locks, etc.
Implementation • Fibers: Heap allocated, dynamically resized stacks • ~10s of bytes • No unnecessary closure allocation costs unlike CPS • One-shot delimited continuations • Simpli fj es reasoning about resources - sockets, locks, etc. • Handlers —> Linked-list of fj bers
Implementation • Fibers: Heap allocated, dynamically resized stacks • ~10s of bytes • No unnecessary closure allocation costs unlike CPS • One-shot delimited continuations • Simpli fj es reasoning about resources - sockets, locks, etc. • Handlers —> Linked-list of fj bers handle / sp continue call chain reference handler
Implementation • Fibers: Heap allocated, dynamically resized stacks • ~10s of bytes • No unnecessary closure allocation costs unlike CPS • One-shot delimited continuations • Simpli fj es reasoning about resources - sockets, locks, etc. • Handlers —> Linked-list of fj bers sp handle / handle / continue continue call chain reference handler
Implementation • Fibers: Heap allocated, dynamically resized stacks • ~10s of bytes • No unnecessary closure allocation costs unlike CPS • One-shot delimited continuations • Simpli fj es reasoning about resources - sockets, locks, etc. • Handlers —> Linked-list of fj bers sp handle / continue call chain perform reference handler
Native-code fj bers — Vanilla C
Native-code fj bers — Vanilla C OCaml start program OCaml
Native-code fj bers — Vanilla C OCaml start program OCaml C call C
Native-code fj bers — Vanilla C OCaml start program OCaml C call C OCaml callback OCaml
Native-code fj bers — Vanilla C OCaml start program OCaml C call C OCaml callback OCaml C call C
Native-code fj bers — Vanilla C OCaml start program OCaml C call C OCaml callback OCaml C call C OCaml callback OCaml
Native-code fj bers — E ff ects system stack C
Native-code fj bers — E ff ects system stack OCaml start program C OCaml heap
Native-code fj bers — E ff ects system stack OCaml start program handle C OCaml heap
Native-code fj bers — E ff ects system stack OCaml start program handle C C C call OCaml heap
Native-code fj bers — E ff ects system stack OCaml start program handle C C C call OCaml callback OCaml heap
Native-code fj bers — E ff ects system stack OCaml start program handle C C C call OCaml callback C OCaml heap C call
Native-code fj bers — E ff ects system stack OCaml start program handle C C C call OCaml callback C OCaml heap C call 1. Stack over fm ow checks for OCaml functions • Simple static analysis eliminates many checks
Native-code fj bers — E ff ects system stack OCaml start program handle C C C call OCaml callback C OCaml heap C call 1. Stack over fm ow checks for OCaml functions • Simple static analysis eliminates many checks 2. FFI calls are more expensive due to stack switching • Specialise for calls which {allocate / pass arguments on stack / do neither}
0.25 0.75 0.5 0 1 almabench alt-ergo-parameter_smallest_divisor alt-ergo-carte_autorisee_3 alt-ergo-parameter_relabel alt-ergo-OBF__ggjj_2 alt-ergo-parameter_def alt-ergo-parameter_def Performance : Vanilla OCaml alt-ergo-OBF__yyll_1 alt-ergo-bbvv_351 alt-ergo-controler_carte_13 alt-ergo-div2_sub alt-ergo-parameter_def alt-ergo-ccgg_2055 alt-ergo-parameter_def alt-ergo-parameter_inverse_in_place alt-ergo-ccgg_1759 4.02.2+effects Normalised time (lower is better) alt-ergo-parameter_def alt-ergo-induction_step alt-ergo-ccgg_1618 alt-ergo-ccgg_219 alt-ergo-advance_automaton_25 alt-ergo-nsec_sum_higher_than_1s alt-ergo-fill_assert_39_Alt-Ergo bdd chameneos-async chameneos-lwt cohttp-lwt core_micro cpdf-merge cpdf-reformat cpdf-squeeze cpdf-transform frama-c-deflate frama-c-idct js_of_ocaml jsontrip-sample kb kb-no-exc lexifi-g2pp menhir-fancy menhir-sql 4.02.2+vanilla menhir-standard minilight numal-durand-kerner-aberth numal-fft numal-k-means numal-levinson-durbin numal-lu-decomposition numal-naive-multilayer numal-qr-decomposition numal-rnd_access numal-simple_access patdiff sauvola-contrast sequence sequence-cps setrip setrip-smallbuf thread-ring-async-pipe thread-ring-lwt-mvar thread-ring-lwt-stream thread-sleep-async thread-sleep-lwt valet-async valet-lwt ydump-sample
Recommend
More recommend