effect handlers in multicore ocaml
play

Effect Handlers in Multicore OCaml Daniel Hillerstrm, Daan Leijen, - PowerPoint PPT Presentation

Effect Handlers in Multicore OCaml Daniel Hillerstrm, Daan Leijen, Sam Lindley, Matija Pretnar, Andreas Rossberg, KC Sivaramakrishnan Effect Handlers Multicore OCaml is an OCaml extension with native support for concurrency and


  1. Effect Handlers in Multicore OCaml Daniel Hillerström, Daan Leijen, Sam Lindley, Matija Pretnar, Andreas Rossberg, KC Sivaramakrishnan

  2. Effect Handlers • Multicore OCaml is an OCaml extension with native support for concurrency and shared-memory parallelism ✦ Concurrency expressed through effect handlers ✦ Will land upstream in Q2 2021

  3. Effect Handlers • Multicore OCaml is an OCaml extension with native support for concurrency and shared-memory parallelism ✦ Concurrency expressed through effect handlers ✦ Will land upstream in Q2 2021 effect E : string let comp () = print_string "0 "; print_string (perform E); print_string "3 " let main () = try comp () with effect E k -> print_string "1 "; continue k "2 "; print_string “4 "

  4. Effect Handlers • Multicore OCaml is an OCaml extension with native support for concurrency and shared-memory parallelism ✦ Concurrency expressed through effect handlers ✦ Will land upstream in Q2 2021 effect E : string let comp () = effect declaration print_string "0 "; print_string (perform E); print_string "3 " let main () = try comp () with effect E k -> print_string "1 "; continue k "2 "; print_string “4 "

  5. Effect Handlers • Multicore OCaml is an OCaml extension with native support for concurrency and shared-memory parallelism ✦ Concurrency expressed through effect handlers ✦ Will land upstream in Q2 2021 effect E : string let comp () = effect declaration print_string "0 "; print_string (perform E); print_string "3 " computation let main () = try comp () with effect E k -> print_string "1 "; continue k "2 "; print_string “4 "

  6. Effect Handlers • Multicore OCaml is an OCaml extension with native support for concurrency and shared-memory parallelism ✦ Concurrency expressed through effect handlers ✦ Will land upstream in Q2 2021 effect E : string let comp () = effect declaration print_string "0 "; print_string (perform E); print_string "3 " computation let main () = try comp () with effect E k -> handler print_string "1 "; continue k "2 "; print_string “4 "

  7. Effect Handlers • Multicore OCaml is an OCaml extension with native support for concurrency and shared-memory parallelism ✦ Concurrency expressed through effect handlers ✦ Will land upstream in Q2 2021 effect E : string suspends current let comp () = effect declaration print_string "0 "; computation print_string (perform E); print_string "3 " computation let main () = try comp () with effect E k -> handler print_string "1 "; continue k "2 "; print_string “4 "

  8. Effect Handlers • Multicore OCaml is an OCaml extension with native support for concurrency and shared-memory parallelism ✦ Concurrency expressed through effect handlers ✦ Will land upstream in Q2 2021 effect E : string suspends current let comp () = effect declaration print_string "0 "; computation print_string (perform E); print_string "3 " computation let main () = delimited continuation try comp () with effect E k -> handler print_string "1 "; continue k "2 "; print_string “4 "

  9. Effect Handlers • Multicore OCaml is an OCaml extension with native support for concurrency and shared-memory parallelism ✦ Concurrency expressed through effect handlers ✦ Will land upstream in Q2 2021 effect E : string suspends current let comp () = effect declaration print_string "0 "; computation print_string (perform E); print_string "3 " computation let main () = delimited continuation try comp () with effect E k -> handler print_string "1 "; continue k "2 "; resume suspended print_string “4 " computation

  10. Compilation effect E : string let comp () = print_string "0 "; print_string (perform E); print_string "3 " main let main () = pc try comp () with effect E k -> print_string "1 "; continue k "2 "; print_string “4 " sp

  11. Compilation effect E : string let comp () = print_string "0 "; print_string (perform E); print_string "3 " main let main () = try pc comp () with effect E k -> print_string "1 "; continue k "2 "; print_string “4 " sp

  12. Compilation parent effect E : string let comp () = comp print_string "0 "; print_string (perform E); print_string "3 " sp main let main () = try pc comp () with effect E k -> print_string "1 "; continue k "2 "; print_string “4 "

  13. Compilation parent effect E : string let comp () = comp print_string "0 "; pc print_string (perform E); print_string "3 " comp main let main () = try comp () with effect E k -> sp print_string "1 "; continue k "2 "; print_string “4 " 0

  14. Compilation parent effect E : string let comp () = comp print_string "0 "; pc print_string (perform E); print_string "3 " comp main let main () = try comp () with effect E k -> print_string "1 "; continue k "2 "; print_string “4 " sp 0

  15. Compilation effect E : string let comp () = comp print_string "0 "; pc print_string (perform E); print_string "3 " comp main let main () = try comp () with effect E k -> print_string "1 "; continue k "2 "; print_string “4 " sp k 0

  16. Compilation effect E : string let comp () = comp print_string "0 "; print_string (perform E); print_string "3 " comp main let main () = try comp () with effect E k -> pc print_string "1 "; continue k "2 "; print_string “4 " sp k 0

  17. Compilation effect E : string let comp () = comp print_string "0 "; print_string (perform E); print_string "3 " comp main let main () = try comp () with effect E k -> print_string "1 "; pc continue k "2 "; print_string “4 " sp k 0 1

  18. Compilation effect E : string let comp () = comp print_string "0 "; print_string (perform E); print_string "3 " comp main let main () = try comp () with effect E k -> print_string "1 "; pc continue k "2 "; print_string “4 " sp k 0 1

  19. Compilation parent effect E : string let comp () = comp print_string "0 "; print_string (perform E); print_string "3 " comp main let main () = try comp () with effect E k -> sp print_string "1 "; pc continue k "2 "; print_string “4 " k 0 1

  20. Compilation parent effect E : string let comp () = comp print_string "0 "; print_string (perform E); pc print_string "3 " comp main let main () = try comp () with effect E k -> sp print_string "1 "; continue k "2 "; print_string “4 " k 0 1 2

  21. Compilation effect E : string let comp () = print_string "0 "; print_string (perform E); print_string "3 " main let main () = try comp () with effect E k -> print_string "1 "; continue k "2 "; pc print_string “4 " sp k 0 1 2 3

  22. Compilation effect E : string let comp () = print_string "0 "; print_string (perform E); print_string "3 " main let main () = try comp () with effect E k -> print_string "1 "; continue k "2 "; print_string “4 " sp k pc 0 1 2 3 4

  23. Handlers can be nested effect A : unit effect B : unit let baz () = pc perform A parent parent let bar () = try foo bar baz baz () with effect B k -> continue k () let foo () = try bar () sp with effect A k -> continue k ()

  24. Handlers can be nested effect A : unit effect B : unit let baz () = perform A parent parent let bar () = try foo bar baz baz () with effect B k -> continue k () let foo () = try bar () sp with effect A k -> pc continue k ()

  25. Handlers can be nested effect A : unit effect B : unit let baz () = perform A parent let bar () = try foo bar baz baz () with effect B k -> continue k () let foo () = try bar () sp with effect A k -> pc continue k () k

  26. Handlers can be nested effect A : unit effect B : unit let baz () = perform A parent let bar () = try foo bar baz baz () with effect B k -> continue k () let foo () = try bar () sp with effect A k -> pc continue k () k • Linear search through handlers • Handler stacks shallow in practice

  27. Deep-dive into perform

  28. Deep-dive into perform • Full power of pattern matching for matching effects ✦ Tag test + branching is compiled to a function

  29. Deep-dive into perform • Full power of pattern matching for matching effects ✦ Tag test + branching is compiled to a function https://github.com/ocaml-multicore/ocaml-multicore/blob/parallel_minor_gc/runtime/amd64.S#L865

  30. Performance • Intel(R) Xeon(R) Gold 5120 CPU @ 2.20GHz ✦ For reference, memory read latency is 90 ns (local NUMA node) and 145 ns (remote NUMA node)

Recommend


More recommend