For many years, Multicore Ocaml was pretty high on the vaporware
charts.
In fact it took so long for Ocaml 5 to ship that when it happened
back in 2022 I had long lost track of it and didn’t bother to educate
myself on what the final result looks like to the user.
Turns out this was a mistake, as the result is rather polished and
strikingly easy to use (much more so than earlier approaches to
concurrency like lwt).
What usecase could I possibly have that would benefit from multithreading? Interestingly enough it’s the static site generator that powers this blog which over the years has become a bottleneck. The markdown to HTML conversion itself (leveraging the excellent Omd library) is still blazing fast. However, generating thumbnails from images with CamlImages may take several seconds on the tiny Hetzner CAX11 instance the blog is served from. Especially when posting articles that delay can test my patience as I’m usually tired from a day on the bike when I do that.
Recently during a long overdue server maintenance I realized this part could benefit from utilizing all four cores of this ARM VPS. It didn’t originally, because back when I wrote most of the blog code Ocaml was still all single core. It’s never too late, I suppose.
Practical Use
To
Domains
-
from the memory model / distinct GC domains (?)
memory model: https://github.com/ocaml/ocaml/blob/trunk/runtime/memory.c#L44-L307
The approach we take in multicore OCaml is to avoid as much as possible of this shared state. First, to reduce state shared between mutators and GC, we do not use an explicit gray colour. Instead, each domain maintains its own stack of gray objects, and when the write barrier needs to gray an object it marks it and adds it to the local domain’s gray stack. Since GC and OCaml code are interleaved on a single domain, no synchronisation is necessary. (This scheme does mean that it’s possible for an object to be on the mark stack of two separate domains. See below).
-
Retrofitting Parallelism onto OCaml https://core.ac.uk/download/pdf/328720849.pdf
commit 132b1f76e502524dadb0bb8217841462c686b6af Author: Philipp Gesang phg@phi-gamma.net Date: Mon Nov 30 00:38:01 2026 +0100
emit pages concurrentlydiff --git a/bin/bolg.ml b/bin/bolg.ml index 3640cf2..50da1b3 100644 --- a/bin/bolg.ml +++ b/bin/bolg.ml @@ -3,6 +3,7 @@ let program_name = "bolg"
open Bolglib module Log = Common.Log +module T = Domainslib.Task open Types
let plugin_registry = Hashtbl.create 8 @@ -546,24 +547,27 @@ let emit_entry_contents sidebar plugdata listdata pfx path entry = end
(* XXX needs error propagation *) -let rec emit_entry sidebar src pfx path entry = +let rec emit_entry pool sidebar src pfx path entry = let path' = entry.ey_name :: path in
- let plugdata = apply_plugins src pfx path entry
- and listdata =
- let children = List.sort nodes_by_date entry.ey_children
- |> List.map (emit_entry sidebar entry.ey_path pfx path')
- |> List.map (fun child ->
T.async pool (fun _ ->
inemit_entry pool sidebar entry.ey_path pfx path' child))- let plugdata = apply_plugins src pfx path entry
- and listdata = List.map (T.await pool) children in emit_entry_contents sidebar plugdata listdata pfx path entry
-let emit_toplevel sidebar bl = +let emit_toplevel pool sidebar bl = let entry = bl.bg_data in let top = bl.bg_root ^ "/" ^ Config.site_root in
- ignore (emit_entry sidebar Config.site_root top [] entry)
- ignore (emit_entry pool sidebar Config.site_root top [] entry)
-let emit_site sidebar bl = +let emit_site pool sidebar bl = let c0 = Mtime_clock.counter () in Log.debug "emitting site under [%s]" bl.bg_root;
- emit_toplevel sidebar bl;
- emit_toplevel pool sidebar bl; let dt = (Mtime_clock.count c0 |> Mtime.Span.to_float_ns) /. 1_000_000_000. in Log.debug "finished generating site in %.3f s " dt
@@ -581,7 +585,7 @@ let bolg_help man_format cmds topic =
let initialize verbose = Log.set_verbose verbose
-let bolg_gen root verbose = +let bolg_gen root verbose jobs = initialize verbose; match root with | None -> failwith "need a path" @@ -590,7 +594,10 @@ let bolg_gen root verbose = | None -> Log.error "gen: scan of [%s] failed" r | Some bl -> let sidebar = Sidebar.create bl in
emit_site sidebar bl
let pool = T.setup_pool ~num_domains:(max 0 (jobs - 1)) () inFun.protect~finally:(fun () -> T.teardown_pool pool)(fun () -> T.run pool (fun _ -> emit_site pool sidebar bl)) end
let bolg_scan root verbose = @@ -613,6 +620,15 @@ let verbose = let doc = "Print runtime information to stderr." in Arg.(value & flag & info [ "v"; "verbose" ] ~doc)
+let jobs =
- let doc =
- "Number of domains (roughly: threads) for parallel page emission."
- in
- Arg.(
- value
- & opt int (Domain.recommended_domain_count ())
- & info [ "j"; "jobs" ] ~docv:"JOBS" ~doc)
let man_common = [ `S Manpage.s_bugs; @@ -626,7 +642,7 @@ let cmd_gen = let doc = "generate files" in let man = man_common in let info = Cmd.info "gen" ~version:"0.2.0" ~doc ~man in
- Cmd.v info Term.(const bolg_gen $ root $ verbose)
- Cmd.v info Term.(const bolg_gen $ root $ verbose $ jobs)
let cmd_help = let topic =
diff --git a/lib/common.ml b/lib/common.ml index da84bb9..7b17ff0 100644 --- a/lib/common.ml +++ b/lib/common.ml @@ -1,20 +1,26 @@ module Log = struct let verbose = ref false let set_verbose f = verbose := f
-
let next_domain = Atomic.make 0
-
let domain_key =
-
Domain.DLS.new_key (fun () -> Atomic.fetch_and_add next_domain 1)
-
let domain_id () = Domain.DLS.get domain_key
let terminate_line oc = output_char oc '\n'; flush oc
-
https://github.com/ocaml-multicore/domainslib
-
Retrofitting Parallelism onto OCaml https://core.ac.uk/download/pdf/328720849.pdf
-
Solved in the process: Memory model https://ocaml.org/manual/5.0/memorymodel.html
-
Merge request from 2021 https://github.com/ocaml/ocaml/pull/10831