【问题标题】:How to use multicores in Ocaml to do Monte Carlo simulations?如何在 Ocaml 中使用多核进行蒙特卡罗模拟?
【发布时间】:2010-11-20 05:08:50
【问题描述】:

Ocaml 进程只能使用一个核心,为了使用多个核心,我必须运行多个进程。

是否有任何 Ocaml 框架可用于并行化 Monte Carlo 模拟?

【问题讨论】:

    标签: multicore ocaml simulation montecarlo


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

    使用以下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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多