10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-06 22:23:42 +01:00

Distributed vector

This commit is contained in:
Anthony Scemama 2018-10-17 10:38:07 +02:00
parent 2ffd82436d
commit 224a87d422
3 changed files with 31 additions and 4 deletions

View File

@ -15,6 +15,7 @@ let rank =
assert (result >= 0); assert (result >= 0);
result result
let master = rank = 0
let barrier () = let barrier () =
Mpi.barrier Mpi.comm_world 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 let () = Mpi.scatter_float_array a a_local 0 Mpi.comm_world in
{ result with data = Lacaml.D.Vec.of_array a_local } { 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 end

View File

@ -6,6 +6,9 @@ val size : int
val rank : Mpi.rank val rank : Mpi.rank
(** Rank of the current distributed processe. *) (** Rank of the current distributed processe. *)
val master : bool
(** True if [rank = 0]. *)
val barrier : unit -> unit val barrier : unit -> unit
(** Wait for all processes to reach this point. *) (** Wait for all processes to reach this point. *)
@ -42,7 +45,6 @@ module Vec : sig
val of_array : float array -> t val of_array : float array -> t
(** [of_array ar] @return a distributed vector initialized from array [ar]. *) (** [of_array ar] @return a distributed vector initialized from array [ar]. *)
(*
val to_array : t -> float array val to_array : t -> float array
(** [to_array v] @return an array initialized from vector [v]. *) (** [to_array v] @return an array initialized from vector [v]. *)
@ -51,7 +53,6 @@ module Vec : sig
val to_vec : t -> Lacaml.D.vec val to_vec : t -> Lacaml.D.vec
(** [to_vec v] @return a Lacaml vector initialized from vector [v]. *) (** [to_vec v] @return a Lacaml vector initialized from vector [v]. *)
*)
end end

View File

@ -5,7 +5,11 @@ let () =
(* (*
let v = Parallel.Vec.init 47 (fun i -> float_of_int i) in 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 a = Array.init 6 (fun i -> float_of_int (i+1)) |> Lacaml.D.Vec.of_array in
let v = Parallel.Vec.of_array a in let v = Parallel.Vec.of_vec a in
Format.printf "%a" Parallel.Vec.pp v; 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 ();