From 224a87d42224c62542b6d820d7c5e582a6b31bca Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Wed, 17 Oct 2018 10:38:07 +0200 Subject: [PATCH] Distributed vector --- Parallel/Parallel.ml | 22 ++++++++++++++++++++++ Parallel/Parallel.mli | 5 +++-- run_parallel.ml | 8 ++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Parallel/Parallel.ml b/Parallel/Parallel.ml index f3d4f13..7f2db2e 100644 --- a/Parallel/Parallel.ml +++ b/Parallel/Parallel.ml @@ -15,6 +15,7 @@ let rank = assert (result >= 0); result +let master = rank = 0 let barrier () = Mpi.barrier Mpi.comm_world @@ -90,5 +91,26 @@ module Vec = struct let () = Mpi.scatter_float_array a a_local 0 Mpi.comm_world in { result with data = Lacaml.D.Vec.of_array a_local } + + let to_array vec = + let final_size = vec.global_last - vec.global_first + 1 in + let buffer_size = (Lacaml.D.Vec.dim vec.data) * size in + let buffer = Array.make buffer_size 0. in + let data = Lacaml.D.Vec.to_array vec.data in + let () = Mpi.gather_float_array data buffer 0 Mpi.comm_world in + if final_size = buffer_size then + buffer + else + Array.init final_size (fun i -> buffer.(i)) + + + let of_vec a = + Lacaml.D.Vec.to_array a + |> of_array + + + let to_vec v = + to_array v + |> Lacaml.D.Vec.of_array end diff --git a/Parallel/Parallel.mli b/Parallel/Parallel.mli index 1de2e00..1657722 100644 --- a/Parallel/Parallel.mli +++ b/Parallel/Parallel.mli @@ -6,6 +6,9 @@ val size : int val rank : Mpi.rank (** Rank of the current distributed processe. *) +val master : bool +(** True if [rank = 0]. *) + val barrier : unit -> unit (** Wait for all processes to reach this point. *) @@ -42,7 +45,6 @@ module Vec : sig val of_array : float array -> t (** [of_array ar] @return a distributed vector initialized from array [ar]. *) -(* val to_array : t -> float array (** [to_array v] @return an array initialized from vector [v]. *) @@ -51,7 +53,6 @@ module Vec : sig val to_vec : t -> Lacaml.D.vec (** [to_vec v] @return a Lacaml vector initialized from vector [v]. *) -*) end diff --git a/run_parallel.ml b/run_parallel.ml index c475045..54b67b0 100644 --- a/run_parallel.ml +++ b/run_parallel.ml @@ -5,7 +5,11 @@ let () = (* let v = Parallel.Vec.init 47 (fun i -> float_of_int i) in *) - let a = Array.init 47 (fun i -> float_of_int (i+1)) in - let v = Parallel.Vec.of_array a in + let a = Array.init 6 (fun i -> float_of_int (i+1)) |> Lacaml.D.Vec.of_array in + let v = Parallel.Vec.of_vec a in Format.printf "%a" Parallel.Vec.pp v; + let w = Parallel.Vec.to_vec v in + if Parallel.master then + Format.printf "@[%a@]@;" (Lacaml.Io.pp_lfvec ()) w; + print_newline ();