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
.