11# OCaml 5 Tutorial
22
3- A hands-on tutorial on the new parallelism features in OCaml 5. This tutorial was run on 19th May 2022.
3+ A hands-on tutorial on the new parallelism features in OCaml 5. This tutorial
4+ was run on 19th May 2022.
45
56## Installation
67
@@ -75,8 +76,8 @@ Hyper-Threading gods are kind to us).
7576
7677# ## Fibonacci Number
7778
78- Spawned domains can be joined to get their results. The program
79- [src/fib.ml](src/fib.ml) computes the nth Fibonacci number.
79+ We shall use the program to compute the nth Fibonacci number as the running
80+ example. The program is in [src/fib.ml](src/fib.ml).
8081
8182` ` ` ocaml
8283let n = try int_of_string Sys.argv.(1) with _ -> 40
@@ -102,8 +103,9 @@ Benchmark 1: dune exec src/fib.exe 40
102103
103104On my machine, it takes 500ms to compute the 40th Fibonacci number.
104105
105- The program [src/fib_twice.ml](src/fib_twice.ml) computes the nth Fibonacci
106- number twice in parallel.
106+ Spawned domains can be joined to get their results. The program
107+ [src/fib_twice.ml](src/fib_twice.ml) computes the nth Fibonacci number twice in
108+ parallel.
107109
108110` ` ` ocaml
109111let n = try int_of_string Sys.argv.(1) with _ -> 40
@@ -142,7 +144,9 @@ time as computing it once thanks to parallelism.
142144Domains are heavy-weight entities. Each domain directly maps to an operating
143145system thread. Hence, they are relatively expensive to create and tear down.
144146Moreover, each domain brings its own runtime state local to the domain. In
145- particular, each domain has its own minor heap area and major heap pools.
147+ particular, each domain has its own minor heap area and major heap pools. Due to
148+ the overhead of domains, ** the recommendation is that you spawn exactly one
149+ domain per available core.**
146150
147151OCaml 5 GC is designed to be a low-latency garbage collector with short
148152stop-the-world pauses. Whenever a domain exhausts its minor heap arena, it calls
@@ -157,9 +161,6 @@ For more information, please have a look at the [ICFP 2020 paper and talk on
157161" Retrofitting Parallelism onto
158162OCaml" ](https://icfp20.sigplan.org/details/icfp-2020-papers/21/Retrofitting-Parallelism-onto-OCaml).
159163
160- Due to the overhead of domains, ** the recommendation is that you spawn exactly
161- one domain per available core.**
162-
163164# ## Exercise ★★☆☆☆
164165
165166Compute the nth Fibonacci number in parallel by parallelising recursive calls.
@@ -200,8 +201,8 @@ Benchmark 1: dune exec solutions/fib_par.exe 42
200201 Range (min … max): 1.072 s … 1.191 s 10 runs
201202` ` `
202203
203- This is because of the fact that the work is imbalanced between the two
204- recursive calls of the fibonacci function.
204+ This is because of the fact that the work is not balanced between the two
205+ recursive calls of the Fibonacci function.
205206
206207` ` `
207208fib(n) = fib(n-1) + fib(n-2)
0 commit comments