Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
Created October 24, 2023 18:02
Show Gist options
  • Save ebresafegaga/f1752abc057417beb34b66d827cff9dc to your computer and use it in GitHub Desktop.
Save ebresafegaga/f1752abc057417beb34b66d827cff9dc to your computer and use it in GitHub Desktop.
Trying out Domainslib
let long_computation () =
for i = 0 to 1_000_000_000 do
()
done
(* No parallelism *)
(*
________________________________________________________
Executed in 36.09 secs fish external
usr time 36.08 secs 1.27 millis 36.08 secs
sys time 0.01 secs 0.09 millis 0.01 secs
*)
(* ________________________________________________________
Executed in 84.26 secs fish external
usr time 84.22 secs 0.17 millis 84.22 secs
sys time 0.02 secs 1.10 millis 0.02 secs
*)
let example () =
for i = 0 to 200 do
long_computation ()
done;
print_endline "Done"
(* Inefficient spawning parellelism of domains *)
(*
________________________________________________________
Executed in 9.29 secs fish external
usr time 71.62 secs 491.00 micros 71.62 secs
sys time 0.24 secs 824.00 micros 0.24 secs
*)
let example1 () =
let handles : unit Domain.t list ref = ref [] in
for i = 0 to 200 do
let result = Domain.spawn (fun _ -> long_computation ()) in
handles := result :: !handles
done;
!handles |> List.iter (fun d -> Domain.join d);
print_endline "Done"
let example2 () =
let open Domainslib in
let _num_domains = Domain.recommended_domain_count () in
let pool = Task.setup_pool ~num_domains:4 () in
let () =
Task.run pool (fun _ ->
Task.parallel_for ~start:0 ~finish:200
~body:(fun i -> long_computation ())
pool)
in
print_endline "Done"
let () =
(* let open Domainslib in
let pool = Task.setup_pool ~num_domains:3 () in
pool |> Task.teardown_pool;
print_endline "Done" *)
example2 ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment