2009-08-17 94 views

回答

3

目前,唯一的方法是使用MPI,並且您可以在Xavier Leroy's website上找到它的ocaml綁定。

+2

ocaml4multicore是可用的(有限制),請參閱:http://www.algo-prog.info/ocmc/web/ – nlucaroni 2009-09-24 14:43:44

8

使用以下invoke組合子施加函數的值在另一個(叉形)處理,然後當施加()值阻塞等待其結果是:

let invoke (f : 'a -> 'b) x : unit -> 'b = 
    let input, output = Unix.pipe() in 
    match Unix.fork() with 
    | -1 -> (let v = f x in fun() -> v) 
    | 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) []; 
     close_out output; 
     exit 0 
    | pid -> 
     Unix.close output; 
     let input = Unix.in_channel_of_descr input in 
     fun() -> 
      let v = Marshal.from_channel input in 
      ignore (Unix.waitpid [] pid); 
      close_in input; 
      match v with 
      | `Res x -> x 
      | `Exn e -> raise e