mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-05 19:00:33 -05:00
fix tests to use new API
This commit is contained in:
parent
d09da9c092
commit
8aaed6d951
20 changed files with 53 additions and 47 deletions
|
|
@ -33,7 +33,7 @@ the workers of `pool`, as soon as one is available. No result is returned by `ru
|
||||||
|
|
||||||
```ocaml
|
```ocaml
|
||||||
# #require "threads";;
|
# #require "threads";;
|
||||||
# let pool = Moonpool.Fifo_pool.create ~min:4 ();;
|
# let pool = Moonpool.Fifo_pool.create ~num_threads:4 ();;
|
||||||
val pool : Moonpool.Runner.t = <abstr>
|
val pool : Moonpool.Runner.t = <abstr>
|
||||||
|
|
||||||
# begin
|
# begin
|
||||||
|
|
|
||||||
|
|
@ -57,10 +57,14 @@ let () = assert (List.init 10 fib_direct = [ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55 ])
|
||||||
|
|
||||||
let create_pool ~psize ~kind () =
|
let create_pool ~psize ~kind () =
|
||||||
match kind with
|
match kind with
|
||||||
| "fifo" -> Fifo_pool.create ~min:psize ()
|
| "fifo" -> Fifo_pool.create ?num_threads:psize ()
|
||||||
| "pool" -> Ws_pool.create ~min:psize ()
|
| "pool" -> Ws_pool.create ?num_threads:psize ()
|
||||||
| _ -> assert false
|
| _ -> assert false
|
||||||
|
|
||||||
|
let str_of_int_opt = function
|
||||||
|
| None -> "None"
|
||||||
|
| Some i -> Printf.sprintf "Some %d" i
|
||||||
|
|
||||||
let run ~psize ~n ~seq ~dl ~fj ~await ~niter ~kind () : unit =
|
let run ~psize ~n ~seq ~dl ~fj ~await ~niter ~kind () : unit =
|
||||||
let pool = lazy (create_pool ~kind ~psize ()) in
|
let pool = lazy (create_pool ~kind ~psize ()) in
|
||||||
let dl_pool =
|
let dl_pool =
|
||||||
|
|
@ -80,14 +84,16 @@ let run ~psize ~n ~seq ~dl ~fj ~await ~niter ~kind () : unit =
|
||||||
Domainslib.Task.run pool (fun () ->
|
Domainslib.Task.run pool (fun () ->
|
||||||
Domainslib.Task.await pool @@ fib_dl ~pool n)
|
Domainslib.Task.await pool @@ fib_dl ~pool n)
|
||||||
) else if fj then (
|
) else if fj then (
|
||||||
Printf.printf "compute fib %d using fork-join with pool size=%d\n%!" n
|
Printf.printf "compute fib %d using fork-join with pool size=%s\n%!" n
|
||||||
psize;
|
(str_of_int_opt psize);
|
||||||
fib_fj ~on:(Lazy.force pool) n |> Fut.wait_block_exn
|
fib_fj ~on:(Lazy.force pool) n |> Fut.wait_block_exn
|
||||||
) else if await then (
|
) else if await then (
|
||||||
Printf.printf "compute fib %d using await with pool size=%d\n%!" n psize;
|
Printf.printf "compute fib %d using await with pool size=%s\n%!" n
|
||||||
|
(str_of_int_opt psize);
|
||||||
fib_await ~on:(Lazy.force pool) n |> Fut.wait_block_exn
|
fib_await ~on:(Lazy.force pool) n |> Fut.wait_block_exn
|
||||||
) else (
|
) else (
|
||||||
Printf.printf "compute fib %d with pool size=%d\n%!" n psize;
|
Printf.printf "compute fib %d with pool size=%s\n%!" n
|
||||||
|
(str_of_int_opt psize);
|
||||||
fib ~on:(Lazy.force pool) n |> Fut.wait_block_exn
|
fib ~on:(Lazy.force pool) n |> Fut.wait_block_exn
|
||||||
)
|
)
|
||||||
in
|
in
|
||||||
|
|
@ -103,7 +109,7 @@ let run ~psize ~n ~seq ~dl ~fj ~await ~niter ~kind () : unit =
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let n = ref 40 in
|
let n = ref 40 in
|
||||||
let psize = ref 16 in
|
let psize = ref None in
|
||||||
let seq = ref false in
|
let seq = ref false in
|
||||||
let niter = ref 3 in
|
let niter = ref 3 in
|
||||||
let kind = ref "pool" in
|
let kind = ref "pool" in
|
||||||
|
|
@ -112,7 +118,7 @@ let () =
|
||||||
let fj = ref false in
|
let fj = ref false in
|
||||||
let opts =
|
let opts =
|
||||||
[
|
[
|
||||||
"-psize", Arg.Set_int psize, " pool size";
|
"-psize", Arg.Int (fun i -> psize := Some i), " pool size";
|
||||||
"-n", Arg.Set_int n, " fib <n>";
|
"-n", Arg.Set_int n, " fib <n>";
|
||||||
"-seq", Arg.Set seq, " sequential";
|
"-seq", Arg.Set seq, " sequential";
|
||||||
"-dl", Arg.Set dl, " domainslib";
|
"-dl", Arg.Set dl, " domainslib";
|
||||||
|
|
|
||||||
|
|
@ -21,14 +21,14 @@ let with_pool ~kind f =
|
||||||
match kind with
|
match kind with
|
||||||
| "pool" ->
|
| "pool" ->
|
||||||
if !j = 0 then
|
if !j = 0 then
|
||||||
Ws_pool.with_ ~per_domain:1 f
|
Ws_pool.with_ f
|
||||||
else
|
else
|
||||||
Ws_pool.with_ ~min:!j f
|
Ws_pool.with_ ~num_threads:!j f
|
||||||
| "fifo" ->
|
| "fifo" ->
|
||||||
if !j = 0 then
|
if !j = 0 then
|
||||||
Fifo_pool.with_ ~per_domain:1 f
|
Fifo_pool.with_ f
|
||||||
else
|
else
|
||||||
Fifo_pool.with_ ~min:!j f
|
Fifo_pool.with_ ~num_threads:!j f
|
||||||
| _ -> assert false
|
| _ -> assert false
|
||||||
|
|
||||||
(** Run in parallel using {!Fut.for_} *)
|
(** Run in parallel using {!Fut.for_} *)
|
||||||
|
|
|
||||||
|
|
@ -26,13 +26,13 @@ let fib ~on x : int Fut.t =
|
||||||
let () = assert (List.init 10 fib_direct = [ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55 ])
|
let () = assert (List.init 10 fib_direct = [ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55 ])
|
||||||
|
|
||||||
let fib_40 : int =
|
let fib_40 : int =
|
||||||
let pool = Ws_pool.create ~min:8 () in
|
let pool = Ws_pool.create ~num_threads:8 () in
|
||||||
fib ~on:pool 40 |> Fut.wait_block_exn
|
fib ~on:pool 40 |> Fut.wait_block_exn
|
||||||
|
|
||||||
let () = Printf.printf "fib 40 = %d\n%!" fib_40
|
let () = Printf.printf "fib 40 = %d\n%!" fib_40
|
||||||
|
|
||||||
let run_test () =
|
let run_test () =
|
||||||
let pool = Ws_pool.create ~min:8 () in
|
let pool = Ws_pool.create ~num_threads:8 () in
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
List.init 10 (fib ~on:pool)
|
List.init 10 (fib ~on:pool)
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,13 @@ let fib ~on x : int Fut.t =
|
||||||
let () = assert (List.init 10 fib_direct = [ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55 ])
|
let () = assert (List.init 10 fib_direct = [ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55 ])
|
||||||
|
|
||||||
let fib_40 : int =
|
let fib_40 : int =
|
||||||
let pool = Ws_pool.create ~min:8 () in
|
let pool = Ws_pool.create ~num_threads:8 () in
|
||||||
fib ~on:pool 40 |> Fut.wait_block_exn
|
fib ~on:pool 40 |> Fut.wait_block_exn
|
||||||
|
|
||||||
let () = Printf.printf "fib 40 = %d\n%!" fib_40
|
let () = Printf.printf "fib 40 = %d\n%!" fib_40
|
||||||
|
|
||||||
let run_test () =
|
let run_test () =
|
||||||
let pool = Ws_pool.create ~min:8 () in
|
let pool = Ws_pool.create ~num_threads:8 () in
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
List.init 10 (fib ~on:pool)
|
List.init 10 (fib ~on:pool)
|
||||||
|
|
|
||||||
|
|
@ -22,13 +22,13 @@ let rec fib x : int =
|
||||||
)
|
)
|
||||||
|
|
||||||
let fib_40 : int =
|
let fib_40 : int =
|
||||||
let@ pool = Ws_pool.with_ ~min:8 () in
|
let@ pool = Ws_pool.with_ ~num_threads:8 () in
|
||||||
Fut.spawn ~on:pool (fun () -> fib 40) |> Fut.wait_block_exn
|
Fut.spawn ~on:pool (fun () -> fib 40) |> Fut.wait_block_exn
|
||||||
|
|
||||||
let () = Printf.printf "fib 40 = %d\n%!" fib_40
|
let () = Printf.printf "fib 40 = %d\n%!" fib_40
|
||||||
|
|
||||||
let run_test () =
|
let run_test () =
|
||||||
let@ pool = Ws_pool.with_ ~min:8 () in
|
let@ pool = Ws_pool.with_ ~num_threads:8 () in
|
||||||
|
|
||||||
let fut =
|
let fut =
|
||||||
Fut.spawn ~on:pool (fun () ->
|
Fut.spawn ~on:pool (fun () ->
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ let ( let@ ) = ( @@ )
|
||||||
|
|
||||||
open! Moonpool
|
open! Moonpool
|
||||||
|
|
||||||
let pool = Ws_pool.create ~min:4 ()
|
let pool = Ws_pool.create ~num_threads:4 ()
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let x =
|
let x =
|
||||||
|
|
@ -270,7 +270,7 @@ end
|
||||||
let t_eval =
|
let t_eval =
|
||||||
let arb = Q.set_stats [ "size", Evaluator.size ] Evaluator.arb in
|
let arb = Q.set_stats [ "size", Evaluator.size ] Evaluator.arb in
|
||||||
Q.Test.make ~name:"same eval" arb (fun e ->
|
Q.Test.make ~name:"same eval" arb (fun e ->
|
||||||
let@ pool = Ws_pool.with_ ~min:4 () in
|
let@ pool = Ws_pool.with_ ~num_threads:4 () in
|
||||||
(* Printf.eprintf "eval %s\n%!" (Evaluator.show e); *)
|
(* Printf.eprintf "eval %s\n%!" (Evaluator.show e); *)
|
||||||
let x = Evaluator.eval_seq e in
|
let x = Evaluator.eval_seq e in
|
||||||
let y = Evaluator.eval_fork_join ~pool e in
|
let y = Evaluator.eval_fork_join ~pool e in
|
||||||
|
|
@ -288,7 +288,7 @@ let t_for_nested ~min ~chunk_size () =
|
||||||
let ref_l2 = List.map (List.map neg) ref_l1 in
|
let ref_l2 = List.map (List.map neg) ref_l1 in
|
||||||
|
|
||||||
let l1, l2 =
|
let l1, l2 =
|
||||||
let@ pool = Ws_pool.with_ ~min () in
|
let@ pool = Ws_pool.with_ ~num_threads:min () in
|
||||||
let@ () = Ws_pool.run_wait_block pool in
|
let@ () = Ws_pool.run_wait_block pool in
|
||||||
let l1 =
|
let l1 =
|
||||||
Fork_join.map_list ~chunk_size (Fork_join.map_list ~chunk_size neg) l
|
Fork_join.map_list ~chunk_size (Fork_join.map_list ~chunk_size neg) l
|
||||||
|
|
@ -310,7 +310,7 @@ let t_map ~chunk_size () =
|
||||||
Q.Test.make ~name:"map1"
|
Q.Test.make ~name:"map1"
|
||||||
Q.(small_list small_int |> Q.set_stats [ "len", List.length ])
|
Q.(small_list small_int |> Q.set_stats [ "len", List.length ])
|
||||||
(fun l ->
|
(fun l ->
|
||||||
let@ pool = Ws_pool.with_ ~min:4 () in
|
let@ pool = Ws_pool.with_ ~num_threads:4 () in
|
||||||
let@ () = Ws_pool.run_wait_block pool in
|
let@ () = Ws_pool.run_wait_block pool in
|
||||||
|
|
||||||
let a1 =
|
let a1 =
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ let run ~min () =
|
||||||
let@ _sp = Trace.with_span ~__FILE__ ~__LINE__ "step" in
|
let@ _sp = Trace.with_span ~__FILE__ ~__LINE__ "step" in
|
||||||
|
|
||||||
let l1, l2 =
|
let l1, l2 =
|
||||||
let@ pool = Ws_pool.with_ ~min () in
|
let@ pool = Ws_pool.with_ ~num_threads:min () in
|
||||||
let@ () = Ws_pool.run_wait_block pool in
|
let@ () = Ws_pool.run_wait_block pool in
|
||||||
|
|
||||||
let l1, l2 =
|
let l1, l2 =
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
open! Moonpool
|
open! Moonpool
|
||||||
|
|
||||||
let pool = Ws_pool.create ~min:4 ()
|
let pool = Ws_pool.create ~num_threads:4 ()
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let fut = Array.init 10 (fun i -> Fut.spawn ~on:pool (fun () -> i)) in
|
let fut = Array.init 10 (fun i -> Fut.spawn ~on:pool (fun () -> i)) in
|
||||||
|
|
|
||||||
|
|
@ -30,19 +30,19 @@ let run ~pool () =
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
(print_endline "with fifo";
|
(print_endline "with fifo";
|
||||||
let@ pool = Fifo_pool.with_ ~min:4 () in
|
let@ pool = Fifo_pool.with_ ~num_threads:4 () in
|
||||||
run ~pool ());
|
run ~pool ());
|
||||||
|
|
||||||
(print_endline "with WS(1)";
|
(print_endline "with WS(1)";
|
||||||
let@ pool = Ws_pool.with_ ~min:1 () in
|
let@ pool = Ws_pool.with_ ~num_threads:1 () in
|
||||||
run ~pool ());
|
run ~pool ());
|
||||||
|
|
||||||
(print_endline "with WS(2)";
|
(print_endline "with WS(2)";
|
||||||
let@ pool = Ws_pool.with_ ~min:2 () in
|
let@ pool = Ws_pool.with_ ~num_threads:2 () in
|
||||||
run ~pool ());
|
run ~pool ());
|
||||||
|
|
||||||
(print_endline "with WS(4)";
|
(print_endline "with WS(4)";
|
||||||
let@ pool = Ws_pool.with_ ~min:4 () in
|
let@ pool = Ws_pool.with_ ~num_threads:4 () in
|
||||||
run ~pool ());
|
run ~pool ());
|
||||||
|
|
||||||
()
|
()
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ let rec quicksort arr i len : unit =
|
||||||
(fun () -> quicksort arr !low (len - (!low - i)))
|
(fun () -> quicksort arr !low (len - (!low - i)))
|
||||||
)
|
)
|
||||||
|
|
||||||
let pool = Moonpool.Ws_pool.create ~min:8 ()
|
let pool = Moonpool.Ws_pool.create ~num_threads:8 ()
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let arr = Array.init 400_000 (fun _ -> Random.int 300_000) in
|
let arr = Array.init 400_000 (fun _ -> Random.int 300_000) in
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ let rec fib x =
|
||||||
|
|
||||||
let run ~psize ~n ~j () : _ Fut.t =
|
let run ~psize ~n ~j () : _ Fut.t =
|
||||||
Printf.printf "pool size=%d, n=%d, j=%d\n%!" psize n j;
|
Printf.printf "pool size=%d, n=%d, j=%d\n%!" psize n j;
|
||||||
let pool = Ws_pool.create ~min:psize ~per_domain:0 () in
|
let pool = Ws_pool.create ~num_threads:psize () in
|
||||||
|
|
||||||
(* TODO: a ppx for tracy so we can use instrumentation *)
|
(* TODO: a ppx for tracy so we can use instrumentation *)
|
||||||
let loop () =
|
let loop () =
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
open Moonpool
|
open Moonpool
|
||||||
|
|
||||||
(* large pool, some of our tasks below are long lived *)
|
(* large pool, some of our tasks below are long lived *)
|
||||||
let pool = Ws_pool.create ~min:30 ()
|
let pool = Ws_pool.create ~num_threads:30 ()
|
||||||
|
|
||||||
open (val Fut.infix pool)
|
open (val Fut.infix pool)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ let ( let@ ) = ( @@ )
|
||||||
|
|
||||||
let with_pool ~kind () f =
|
let with_pool ~kind () f =
|
||||||
match kind with
|
match kind with
|
||||||
| `Fifo_pool -> Fifo_pool.with_ ~min:4 () f
|
| `Fifo_pool -> Fifo_pool.with_ ~num_threads:4 () f
|
||||||
| `Ws_pool -> Ws_pool.with_ ~min:4 () f
|
| `Ws_pool -> Ws_pool.with_ ~num_threads:4 () f
|
||||||
|
|
||||||
let rec fib x =
|
let rec fib x =
|
||||||
if x <= 1 then
|
if x <= 1 then
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ let () = assert (List.init 10 fib_direct = [ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55 ])
|
||||||
let fib_40 : int lazy_t =
|
let fib_40 : int lazy_t =
|
||||||
lazy
|
lazy
|
||||||
(let@ _sp = Trace.with_span ~__FILE__ ~__LINE__ "fib40" in
|
(let@ _sp = Trace.with_span ~__FILE__ ~__LINE__ "fib40" in
|
||||||
let pool = Fifo_pool.create ~min:8 () in
|
let pool = Fifo_pool.create ~num_threads:8 () in
|
||||||
let r = fib ~on:pool 40 |> Fut.wait_block_exn in
|
let r = fib ~on:pool 40 |> Fut.wait_block_exn in
|
||||||
Ws_pool.shutdown pool;
|
Ws_pool.shutdown pool;
|
||||||
r)
|
r)
|
||||||
|
|
@ -49,12 +49,12 @@ let run_test ~pool () =
|
||||||
|
|
||||||
let run_test_size ~size () =
|
let run_test_size ~size () =
|
||||||
Printf.printf "test pool(%d)\n%!" size;
|
Printf.printf "test pool(%d)\n%!" size;
|
||||||
let@ pool = Ws_pool.with_ ~min:size () in
|
let@ pool = Ws_pool.with_ ~num_threads:size () in
|
||||||
run_test ~pool ()
|
run_test ~pool ()
|
||||||
|
|
||||||
let run_test_fifo ~size () =
|
let run_test_fifo ~size () =
|
||||||
Printf.printf "test fifo(%d)\n%!" size;
|
Printf.printf "test fifo(%d)\n%!" size;
|
||||||
let@ pool = Fifo_pool.with_ ~min:size () in
|
let@ pool = Fifo_pool.with_ ~num_threads:size () in
|
||||||
run_test ~pool ()
|
run_test ~pool ()
|
||||||
|
|
||||||
let setup_counter () =
|
let setup_counter () =
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
open! Moonpool
|
open! Moonpool
|
||||||
|
|
||||||
let pool = Ws_pool.create ~min:4 ()
|
let pool = Ws_pool.create ~num_threads:4 ()
|
||||||
let pool2 = Ws_pool.create ~min:2 ()
|
let pool2 = Ws_pool.create ~num_threads:2 ()
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let fut = Fut.return 1 in
|
let fut = Fut.return 1 in
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,8 @@ let add_test t = tests := t :: !tests
|
||||||
|
|
||||||
let with_pool ~kind () f =
|
let with_pool ~kind () f =
|
||||||
match kind with
|
match kind with
|
||||||
| `Fifo_pool -> Fifo_pool.with_ ~min:4 ~per_domain:1 () f
|
| `Fifo_pool -> Fifo_pool.with_ () f
|
||||||
| `Ws_pool -> Ws_pool.with_ ~min:4 ~per_domain:1 () f
|
| `Ws_pool -> Ws_pool.with_ () f
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
add_test @@ fun ~kind ->
|
add_test @@ fun ~kind ->
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ let ( let@ ) = ( @@ )
|
||||||
|
|
||||||
let with_pool ~kind () f =
|
let with_pool ~kind () f =
|
||||||
match kind with
|
match kind with
|
||||||
| `Fifo_pool -> Fifo_pool.with_ ~min:4 ~per_domain:1 () f
|
| `Fifo_pool -> Fifo_pool.with_ () f
|
||||||
| `Ws_pool -> Ws_pool.with_ ~min:4 ~per_domain:1 () f
|
| `Ws_pool -> Ws_pool.with_ () f
|
||||||
|
|
||||||
(* test proper resource handling *)
|
(* test proper resource handling *)
|
||||||
let run ~kind () =
|
let run ~kind () =
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ let ( let@ ) = ( @@ )
|
||||||
|
|
||||||
let with_pool ~kind ~j () f =
|
let with_pool ~kind ~j () f =
|
||||||
match kind with
|
match kind with
|
||||||
| `Fifo_pool -> Fifo_pool.with_ ~min:j () f
|
| `Fifo_pool -> Fifo_pool.with_ ~num_threads:j () f
|
||||||
| `Ws_pool -> Ws_pool.with_ ~min:j () f
|
| `Ws_pool -> Ws_pool.with_ ~num_threads:j () f
|
||||||
|
|
||||||
type 'a tree =
|
type 'a tree =
|
||||||
| Leaf of 'a
|
| Leaf of 'a
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,8 @@ let run ~kind () =
|
||||||
in
|
in
|
||||||
|
|
||||||
match kind with
|
match kind with
|
||||||
| `Simple -> Fifo_pool.create ~min:3 ~on_init_thread ~around_task ()
|
| `Simple -> Fifo_pool.create ~num_threads:3 ~on_init_thread ~around_task ()
|
||||||
| `Ws_pool -> Ws_pool.create ~min:3 ~on_init_thread ~around_task ()
|
| `Ws_pool -> Ws_pool.create ~num_threads:3 ~on_init_thread ~around_task ()
|
||||||
in
|
in
|
||||||
|
|
||||||
(* make all threads busy *)
|
(* make all threads busy *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue