caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Faking concurrency using Unix forks and pipes (ray tracing results)
@ 2007-06-04 10:56 Jon Harrop
  2007-06-04 15:33 ` [Caml-list] " Jonathan Bryant
  0 siblings, 1 reply; 6+ messages in thread
From: Jon Harrop @ 2007-06-04 10:56 UTC (permalink / raw)
  To: caml-list


I just got a first working version of .NET style asynchronous invocation 
working in OCaml using process forking.

The following OCaml function forks a new process and computes "f x" in that 
process, returning a function that blocks and returns the result using 
marshalling.

let invoke (f : 'a -> 'b) x : unit -> 'b =
  let input, output = Unix.pipe() in
  match Unix.fork() with
  | 0 ->
    Unix.close input;
    let output = Unix.out_channel_of_descr output in
    Marshal.to_channel output (try `Res(f x) with e -> `Exn e) [];
    exit 0
  | _ ->
      Unix.close output;
      let input = Unix.in_channel_of_descr input in
      fun () ->
	match Marshal.from_channel input with
	| `Res x -> x
	| `Exn e -> raise e

This function tries to account for reraising exceptions on the parent process 
but that is untested.

You can write a higher-order "map" function in terms of invoke like this:

let ( |> ) x f = f x

let map (f : 'a -> 'b) a : 'b array =
  Array.map (invoke f) a |>
      Array.map (fun f -> f())

When you apply this map to an array, a new process is forked for each element. 
As forking is time consuming, you should only apply this to short arrays.

The performance characteristics of this approach are very interesting. 
Firstly, I can observe doubled performance on my dual core by invoking two 
simple but CPU-intensive operations concurrently:

  map fib [|43; 43|]

However, performance is easily degraded using this approach, partly because 
forking is expensive but also because of other effects that I do not yet 
understand. My original benchmark summed the elements of an array using 
fold_left. For some reason, this is extremely inefficient, as if the entire 
array is copied.

Anyway, this function is so simple that it took no time to work it into my ray 
tracer benchmark. The benefits of concurrency on my dual-core system reduce 
the time taken by OCaml from 4s to 3s.

I'll try a concurrent F# version and see how it compares...

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-06-04 18:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-04 10:56 Faking concurrency using Unix forks and pipes (ray tracing results) Jon Harrop
2007-06-04 15:33 ` [Caml-list] " Jonathan Bryant
2007-06-04 15:39   ` Jonathan Bryant
     [not found]   ` <9b415f950706040850v586a285ax1448d23c0c78a375@mail.gmail.com>
2007-06-04 16:13     ` Jonathan Bryant
     [not found]       ` <9b415f950706040933r22e1560fhc088368ccb8444fa@mail.gmail.com>
2007-06-04 16:53         ` Jonathan Bryant
2007-06-04 18:00           ` Brian Hurt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).