10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-07 06:33:39 +01:00

Scatter OK

This commit is contained in:
Anthony Scemama 2018-10-17 00:56:14 +02:00
parent be47e1c9c3
commit e6c597deeb
3 changed files with 29 additions and 4 deletions

View File

@ -42,7 +42,7 @@ module Vec = struct
() ()
let create n = let create n =
let step = n / size + 1 in let step = (n-1) / size + 1 in
let local_first = step * rank + 1 in let local_first = step * rank + 1 in
let local_last = min (local_first + step - 1) n in let local_last = min (local_first + step - 1) n in
{ {
@ -50,7 +50,7 @@ module Vec = struct
global_last = n ; global_last = n ;
local_first ; local_first ;
local_last ; local_last ;
data = Lacaml.D.Vec.create (local_last - local_first + 1) data = Lacaml.D.Vec.create (max 0 (local_last - local_first + 1))
} }
@ -73,4 +73,25 @@ module Vec = struct
(Lacaml.D.Vec.dim result.data) (Lacaml.D.Vec.dim result.data)
(fun i -> f (i+result.local_first-1)) (fun i -> f (i+result.local_first-1))
} }
let of_array a =
let la = Array.length a in
let a =
let n = la mod size in
if n > 0 then
Array.concat [ a ; Array.make (size-n) 0. ]
else
a
in
let result = create la in
let () = Mpi.barrier Mpi.comm_world in
Printf.printf "%d\n%!" rank;
let a_local = Array.make ((Array.length a)/size) 0. in
Printf.printf "%d\n%!" (Array.length a_local);
let () = Mpi.scatter_float_array a a_local 0 Mpi.comm_world in
{ result with data =
Lacaml.D.Vec.of_array a_local
}
end end

View File

@ -39,10 +39,10 @@ module Vec : sig
(** [init n f] @return a distributed vector containing [n] elements, where (** [init n f] @return a distributed vector containing [n] elements, where
each element at position [i] is initialized by the result of calling [f i]. *) each element at position [i] is initialized by the result of calling [f i]. *)
(*
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]. *)

View File

@ -2,6 +2,10 @@
let () = let () =
Printf.printf "Hello from rank %d of %d.\n" Parallel.rank Parallel.size; Printf.printf "Hello from rank %d of %d.\n" Parallel.rank Parallel.size;
let () = Parallel.barrier () in let () = Parallel.barrier () in
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 6 (fun i -> float_of_int (i+1)) in
let v = Parallel.Vec.of_array a in
Format.printf "%a" Parallel.Vec.pp v; Format.printf "%a" Parallel.Vec.pp v;