Skip to content

Commit 2ac25c7

Browse files
authored
Merge pull request #18 from ocaml-multicore/ctk21/parallel_for_reduce_div_con
Use same algorithm for parallel_for_reduce as parallel_for
2 parents b2da603 + c8f3dc4 commit 2ac25c7

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

lib/task.ml

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,31 @@ let teardown_pool pool =
5757
done;
5858
Array.iter Domain.join pool.domains
5959

60-
let parallel_for_reduce pool reduce_fun init ~chunk_size ~start ~finish ~body =
61-
assert (chunk_size > 0);
62-
let work s e =
63-
let rec loop i acc =
64-
if i > e then acc
65-
else loop (i+1) (reduce_fun acc (body i))
66-
in
67-
loop s init
60+
let parallel_for_reduce ?(chunk_size=0) ~start ~finish ~body pool reduce_fun init =
61+
let chunk_size = if chunk_size > 0 then chunk_size
62+
else begin
63+
let n_domains = (Array.length pool.domains) + 1 in
64+
let n_tasks = finish - start + 1 in
65+
if n_domains = 1 then n_tasks
66+
else max 1 (n_tasks/(8*n_domains))
67+
end
6868
in
69-
let rec loop i acc =
70-
if i+chunk_size > finish then
71-
let p = async pool (fun _ -> work i finish) in
72-
p::acc
69+
let rec work s e =
70+
if e - s < chunk_size then
71+
let rec loop i acc =
72+
if i > e then acc
73+
else loop (i+1) (reduce_fun acc (body i))
74+
in
75+
loop s init
7376
else begin
74-
let p = async pool (fun _ -> work i (i+chunk_size-1)) in
75-
loop (i+chunk_size) (p::acc)
77+
let d = s + ((e - s) / 2) in
78+
let p = async pool (fun _ -> work s d) in
79+
let right = work (d+1) e in
80+
let left = await pool p in
81+
reduce_fun left right
7682
end
7783
in
78-
let ps = loop start [] in
79-
let results = List.map (await pool) ps in
80-
List.fold_left reduce_fun init results
84+
work start finish
8185

8286
let parallel_for ?(chunk_size=0) ~start ~finish ~body pool =
8387
let chunk_size = if chunk_size > 0 then chunk_size

lib/task.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ val await : pool -> 'a promise -> 'a
2727
* be returned. If the task had raised an exception, then [await] raises the
2828
* same exception. *)
2929

30-
val parallel_for: ?chunk_size:int -> start:int -> finish:int ->
30+
val parallel_for : ?chunk_size:int -> start:int -> finish:int ->
3131
body:(int -> unit) -> pool -> unit
3232
(** [parallel_for c s f b p] behaves similar to [for i=s to f do b i done], but
3333
* runs the for loop in parallel. The chunk size [c] determines the number of
@@ -37,9 +37,9 @@ val parallel_for: ?chunk_size:int -> start:int -> finish:int ->
3737
* scheme.
3838
*)
3939

40-
val parallel_for_reduce : pool -> ('a -> 'a -> 'a) -> 'a -> chunk_size:int ->
41-
start:int -> finish:int -> body:(int -> 'a) -> 'a
42-
(** [parallel_for_reduce p r i c s f b] is similar to [parallel_for] except
40+
val parallel_for_reduce : ?chunk_size:int -> start:int -> finish:int ->
41+
body:(int -> 'a) -> pool -> ('a -> 'a -> 'a) -> 'a -> 'a
42+
(** [parallel_for_reduce c s f b p r i] is similar to [parallel_for] except
4343
* that the result returned by each iteration is reduced with [r] with initial
4444
* value [i]. *)
4545

0 commit comments

Comments
 (0)