Skip to content

Instantly share code, notes, and snippets.

@Sudha247
Created May 19, 2020 11:33
Show Gist options
  • Save Sudha247/185d8dd7bfc7d7a6529a5bfc47a9f022 to your computer and use it in GitHub Desktop.
Save Sudha247/185d8dd7bfc7d7a6529a5bfc47a9f022 to your computer and use it in GitHub Desktop.
module C = Domainslib.Chan
let num_domains = try int_of_string Sys.argv.(1) with _ -> 4
let n = try int_of_string Sys.argv.(2) with _ -> 1024
let chunk_size = n / num_domains
let a = Array.make n 1.
let b = Array.create_float n
type message =
Work of int * int
| Quit
let rec create_work c start left =
if left < chunk_size then begin
C.send c (Work (start, start + left - 1));
for _i = 1 to num_domains do
C.send c Quit
done
end else begin
C.send c (Work (start, start + chunk_size - 1));
create_work c (start + chunk_size) (left - chunk_size)
end
;;
let part a b s e = Array.blit a s b s (e - s + 1)
let rec worker a b c =
match C.recv c with
| Work (s,e) ->
part a b s e;
worker a b c
| Quit -> ()
let _ =
let c = C.make_bounded (num_domains + num_domains + 1) in
create_work c 0 n;
let domains =
Array.init (num_domains - 1) (fun _ ->
Domain.spawn (fun _ -> worker a b c))
in
worker a b c;
Array.iter Domain.join domains
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment