Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ~num_domains in Task.setup_pool optional #91

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

* Make ~num_domains argument for Task.setup_pool optional (#87, #91)

## v0.5.0

This release includes:
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ We can parallelise this program using Domainslib:

```ocaml
(* fib_par.ml *)
let num_domains = try int_of_string Sys.argv.(1) with _ -> 1
let n = try int_of_string Sys.argv.(2) with _ -> 1
let n = try int_of_string Sys.argv.(1) with _ -> 1
let num_domains = try Some (int_of_string Sys.argv.(2) - 1) with _ -> None

(* Sequential Fibonacci *)
let rec fib n =
Expand All @@ -40,7 +40,7 @@ let rec fib_par pool n =
fib n

let main () =
let pool = T.setup_pool ~num_domains:(num_domains - 1) () in
let pool = T.setup_pool ?num_domains () in
let res = T.run pool (fun _ -> fib_par pool n) in
T.teardown_pool pool;
Printf.printf "fib(%d) = %d\n" n res
Expand All @@ -51,28 +51,28 @@ let _ = main ()
The parallel program scales nicely compared to the sequential version. The results presented below were obtained on a 2.3 GHz Quad-Core Intel Core i7 MacBook Pro with 4 cores and 8 hardware threads.

```bash
$ hyperfine './fib.exe 42' './fib_par.exe 2 42' \
'./fib_par.exe 4 42' './fib_par.exe 8 42'
$ hyperfine './fib.exe 42' './fib_par.exe 42 2' \
'./fib_par.exe 42 4' './fib_par.exe 42 8'
Benchmark 1: ./fib.exe 42
Time (mean ± sd): 1.217 s ± 0.018 s [User: 1.203 s, System: 0.004 s]
Range (min … max): 1.202 s … 1.261 s 10 runs

Benchmark 2: ./fib_par.exe 2 42
Benchmark 2: ./fib_par.exe 42 2
Time (mean ± sd): 628.2 ms ± 2.9 ms [User: 1243.1 ms, System: 4.9 ms]
Range (min … max): 625.7 ms … 634.5 ms 10 runs

Benchmark 3: ./fib_par.exe 4 42
Benchmark 3: ./fib_par.exe 42 4
Time (mean ± sd): 337.6 ms ± 23.4 ms [User: 1321.8 ms, System: 8.4 ms]
Range (min … max): 318.5 ms … 377.6 ms 10 runs

Benchmark 4: ./fib_par.exe 8 42
Benchmark 4: ./fib_par.exe 42 8
Time (mean ± sd): 250.0 ms ± 9.4 ms [User: 1877.1 ms, System: 12.6 ms]
Range (min … max): 242.5 ms … 277.3 ms 11 runs

Summary
'./fib_par2.exe 8 42' ran
1.35 ± 0.11 times faster than './fib_par.exe 4 42'
2.51 ± 0.10 times faster than './fib_par.exe 2 42'
'./fib_par.exe 42 8' ran
1.35 ± 0.11 times faster than './fib_par.exe 42 4'
2.51 ± 0.10 times faster than './fib_par.exe 42 2'
4.87 ± 0.20 times faster than './fib.exe 42'
```

Expand Down
3 changes: 2 additions & 1 deletion lib/task.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ let run (type a) pool (f : unit -> a) : a =
let named_pools = Hashtbl.create 8
let named_pools_mutex = Mutex.create ()

let setup_pool ?name ~num_domains () =
(* Domain.recommended_domain_count is guaranteed to be at least 1 *)
let setup_pool ?name ?(num_domains = Domain.recommended_domain_count () - 1) () =
if num_domains < 0 then
invalid_arg "Task.setup_pool: num_domains must be at least 0"
else
Expand Down
5 changes: 3 additions & 2 deletions lib/task.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ type !'a promise
type pool
(** Type of task pool *)

val setup_pool : ?name:string -> num_domains:int -> unit -> pool
val setup_pool : ?name:string -> ?num_domains:int -> unit -> pool
(** Sets up a task execution pool with [num_domains] new domains. If [name] is
provided, the pool is mapped to [name] which can be looked up later with
[lookup_pool name].
[lookup_pool name]. [~num_domains] defaults to
[Domain.recommended_domain_count () - 1].

When [num_domains] is 0, the new pool will be empty, and when an empty
pool is in use, every function in this module will run effectively
Expand Down
6 changes: 4 additions & 2 deletions test/LU_decomposition_multicore.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module T = Domainslib.Task
let num_domains = try int_of_string Sys.argv.(1) with _ -> 1
let mat_size = try int_of_string Sys.argv.(2) with _ -> 1200
let mat_size = try int_of_string Sys.argv.(1) with _ -> 1200
let num_domains =
try int_of_string Sys.argv.(2)
with _ -> Domain.recommended_domain_count ()

let k = Domain.DLS.new_key Random.State.make_self_init

Expand Down
6 changes: 3 additions & 3 deletions test/enumerate_par.ml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
let num_domains = try int_of_string Sys.argv.(1) with _ -> 1
let n = try int_of_string Sys.argv.(2) with _ -> 100
let n = try int_of_string Sys.argv.(1) with _ -> 100
let num_domains = try Some (int_of_string Sys.argv.(2) - 1) with _ -> None

module T = Domainslib.Task

let _ =
let p = T.setup_pool ~num_domains:(num_domains - 1) () in
let p = T.setup_pool ?num_domains () in
T.run p (fun _ ->
T.parallel_for p ~start:0 ~finish:(n-1) ~chunk_size:16 ~body:(fun i ->
print_string @@ Printf.sprintf "[%d] %d\n%!" (Domain.self () :> int) i));
Expand Down
6 changes: 3 additions & 3 deletions test/fib_par.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let num_domains = try int_of_string Sys.argv.(1) with _ -> 1
let n = try int_of_string Sys.argv.(2) with _ -> 43
let n = try int_of_string Sys.argv.(1) with _ -> 43
let num_domains = try Some (int_of_string Sys.argv.(2) - 1) with _ -> None

module T = Domainslib.Task

Expand All @@ -15,7 +15,7 @@ let rec fib_par pool n =
T.await pool a + T.await pool b

let main =
let pool = T.setup_pool ~num_domains:(num_domains - 1) () in
let pool = T.setup_pool ?num_domains () in
let res = T.run pool (fun _ -> fib_par pool n) in
T.teardown_pool pool;
Printf.printf "fib(%d) = %d\n" n res
8 changes: 4 additions & 4 deletions test/game_of_life_multicore.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let num_domains = try int_of_string Sys.argv.(1) with _ -> 1
let n_times = try int_of_string Sys.argv.(2) with _ -> 20
let board_size = try int_of_string Sys.argv.(3) with _ -> 16
let n_times = try int_of_string Sys.argv.(1) with _ -> 20
let board_size = try int_of_string Sys.argv.(2) with _ -> 16
let num_domains = try Some (int_of_string Sys.argv.(3) - 1) with _ -> None

module T = Domainslib.Task

Expand Down Expand Up @@ -62,7 +62,7 @@ let rec repeat pool n =
| _-> next pool; repeat pool (n-1)

let ()=
let pool = T.setup_pool ~num_domains:(num_domains - 1) () in
let pool = T.setup_pool ?num_domains () in
print !rg;
T.run pool (fun _ -> repeat pool n_times);
print !rg;
Expand Down
6 changes: 4 additions & 2 deletions test/spectralnorm2_multicore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* Modified by Mauricio Fernandez
*)

let num_domains = try int_of_string Sys.argv.(1) with _ -> 1
let n = try int_of_string Sys.argv.(2) with _ -> 2000
let n = try int_of_string Sys.argv.(1) with _ -> 2000
let num_domains =
try int_of_string Sys.argv.(2)
with _ -> Domain.recommended_domain_count ()

module T = Domainslib.Task

Expand Down
4 changes: 3 additions & 1 deletion test/sum_par.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
let num_domains = try int_of_string Sys.argv.(1) with _ -> 2
let n = try int_of_string Sys.argv.(2) with _ -> 100
let num_domains =
try int_of_string Sys.argv.(2)
with _ -> Domain.recommended_domain_count ()

module T = Domainslib.Task

Expand Down
6 changes: 3 additions & 3 deletions test/summed_area_table.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module T = Domainslib.Task
let num_domains = try int_of_string Sys.argv.(1) with _ -> 4
let size = try int_of_string Sys.argv.(2) with _ -> 100
let size = try int_of_string Sys.argv.(1) with _ -> 100
let num_domains = try Some (int_of_string Sys.argv.(2) - 1) with _ -> None

let transpose a =
let r = Array.length a in
Expand Down Expand Up @@ -29,7 +29,7 @@ let calc_table pool mat =
let _ =
let m = Array.make_matrix size size 1 (*Array.init size (fun _ -> Array.init size (fun _ -> Random.int size))*)
in
let pool = T.setup_pool ~num_domains:(num_domains - 1) () in
let pool = T.setup_pool ?num_domains () in
let _ = T.run pool (fun _ -> calc_table pool m) in

(* for i = 0 to size-1 do
Expand Down
8 changes: 5 additions & 3 deletions test/task_throughput.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

let n_domains = try int_of_string Sys.argv.(1) with _ -> 1
let n_iterations = try int_of_string Sys.argv.(2) with _ -> 1024
let n_tasks = try int_of_string Sys.argv.(3) with _ -> 1024
let n_iterations = try int_of_string Sys.argv.(1) with _ -> 1024
let n_tasks = try int_of_string Sys.argv.(2) with _ -> 1024
let n_domains =
try int_of_string Sys.argv.(3)
with _ -> Domain.recommended_domain_count ()

module T = Domainslib.Task

Expand Down