Module ForkJoin

module ForkJoin: sig .. end
Fork/join computations.

val split : ForkJoinPool.t ->
('a -> ('a * 'a) option) -> ('b -> 'b -> 'b) -> ('a -> 'b) -> 'a -> 'b
split pool fork join f x computes f x by leveraging multiple threads from pool.

The fork function is used to determine for a given input value whether the computation should be split (returning Some (x1, x2) will generate two sub-computations with inputs x1 and x2) or not (returning None). The fork function is recursively called inside sub-computations.

The join function is used to combine the results of two sub-computations.

Raises an exception if any call of fork, join, or f raises an uncaught exception, or if the passed pool cannot execute the computation.

As an example, a (very inefficient) way to compute the fibonacci function using several threads is:

  let rec fib n =
    if n <= 1 then
      1
    else
      (fib (n - 2)) + (fib (n - 1))
  let threshold = 10
  let fork n = if n < threshold then None else Some (n - 1, n - 2)
  let join x y = x + y
  let parallel_fib n = split pool fork join fib n
.
 
val split_list : ForkJoinPool.t ->
('a -> 'a list) -> ('b -> 'b -> 'b) -> ('a -> 'b) -> 'a -> 'b
split_list pool fork join f x is similar to split pool fork join f x, except that the fork function can generate more than two sub-computations.
 
val split_array : ForkJoinPool.t ->
('a -> 'a array) -> ('b -> 'b -> 'b) -> ('a -> 'b) -> 'a -> 'b
split_array pool fork join f x is similar to split pool fork join f x, except that the fork function can generate more than two sub-computations.