Created
January 12, 2015 15:17
-
-
Save amaloz/7cd0bbde736b0a06db4d to your computer and use it in GitHub Desktop.
Attempt at parallelizing OCaml computation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Core.Std | |
open Async.Std | |
open Async_parallel.Std | |
let workers = ["localhost"; "localhost"] | |
let worker h = | |
Pipe.iter_without_pushback (Hub.listen_simple h) ~f:(fun (id, `Ping) -> | |
printf "%s Got Ping\n%!" (Hub.Client_id.sexp_of_t id |> Sexp.to_string_hum); | |
Hub.send h id `Pong) | |
>>| fun () -> `Done | |
let main () = | |
printf "Start\n%!"; | |
Deferred.List.iter [1; 2; 3; 4] ~f:(fun i -> | |
Parallel.spawn ~where:Parallel.round_robin worker | |
>>= fun (c, res) -> | |
printf "Value = %d\n%!" i; | |
let rec loop = function | |
| 0 -> Channel.close c | |
| i -> | |
Channel.write c `Ping; | |
Channel.read c | |
>>= fun `Pong -> | |
Clock.after (sec 1.) | |
>>= fun _ -> loop (i - 1) | |
in | |
loop 2 | |
) | |
>>> fun _ -> Shutdown.shutdown 0 | |
let _ = | |
Parallel.init ~cluster:{ Cluster.master_machine = Unix.gethostname (); | |
worker_machines = workers } (); | |
main (); | |
never_returns (Scheduler.go ()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment