Skip to content

Commit 46100d7

Browse files
committed
It works
Signed-off-by: Kakadu <Kakadu@pm.me>
1 parent b936c04 commit 46100d7

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

examples/ocaml5.ml

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
let domains_count = 2
1+
(* Run as: time dune exec examples/ocaml5.exe -- -j 4 -l 1000 *)
2+
type cfg = { mutable num_domains : int; mutable length : int }
3+
4+
let cfg = { num_domains = 4; length = 100 }
25

36
let () =
4-
let xs = Array.init 100 (Fun.id) in
5-
let len = Array.length xs in
6-
let total = if len > 0 then len * (len - 1) / 2 else 0 in
7+
Arg.parse
8+
[ ("-j", Arg.Int (fun n -> cfg.num_domains <- n), " number of domains")
9+
; ("-l", Arg.Int (fun n -> cfg.length <- n), " array length")
10+
]
11+
(fun _ -> assert false)
12+
""
13+
14+
let rec slow_fib n = if n <= 1 then n else slow_fib (n - 2) + slow_fib (n - 1)
15+
16+
let () =
17+
(* Quadratic number of iterations *)
18+
let total = if cfg.length > 0 then cfg.length * (cfg.length - 1) / 2 else 0 in
719
let bar ~total =
820
let open Progress.Line in
9-
list
10-
[
11-
spinner ();
12-
bar total;
13-
count_to total;
14-
]
21+
list [ spinner (); bar total; count_to total ]
22+
in
23+
let m = Mutex.create () in
24+
let useful_stuff report _i _j =
25+
assert (0 <= abs (slow_fib 25));
26+
Mutex.protect m (fun () -> report 1)
1527
in
16-
let useful_stuff _i _j = () in
1728
let module T = Domainslib.Task in
18-
let pool = T.setup_pool ~num_domains:domains_count () in
19-
Progress.with_reporter (bar ~total)
20-
(fun report ->
21-
T.run pool (fun () ->
22-
T.parallel_for pool ~start:0 ~finish:(len - 1) ~body:(fun i ->
23-
T.parallel_for pool ~start:(i + 1) ~finish:(len - 1)
24-
~body:(fun j ->
25-
report 1;
26-
useful_stuff i j))));
27-
T.teardown_pool pool
29+
let pool = T.setup_pool ~num_domains:cfg.num_domains () in
30+
Progress.with_reporter
31+
(* ~config:(Progress.Config.v ~ppf:(Format.formatter_of_out_channel stdout) ()) *)
32+
(bar ~total) (fun report ->
33+
T.run pool (fun () ->
34+
T.parallel_for pool ~start:0 ~finish:(cfg.length - 1) ~body:(fun i ->
35+
T.parallel_for pool ~start:(i + 1) ~finish:(cfg.length - 1)
36+
~body:(fun j -> useful_stuff report i j))));
37+
T.teardown_pool pool

0 commit comments

Comments
 (0)