From e6c597deebf0c1f7fa85d32afd69d0ef7f4a75e2 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Wed, 17 Oct 2018 00:56:14 +0200 Subject: [PATCH] Scatter OK --- Parallel/Parallel.ml | 25 +++++++++++++++++++++++-- Parallel/Parallel.mli | 2 +- run_parallel.ml | 6 +++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Parallel/Parallel.ml b/Parallel/Parallel.ml index cfb1d89..4c05437 100644 --- a/Parallel/Parallel.ml +++ b/Parallel/Parallel.ml @@ -42,7 +42,7 @@ module Vec = struct () 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_last = min (local_first + step - 1) n in { @@ -50,7 +50,7 @@ module Vec = struct global_last = n ; local_first ; 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) (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 diff --git a/Parallel/Parallel.mli b/Parallel/Parallel.mli index f18de90..1de2e00 100644 --- a/Parallel/Parallel.mli +++ b/Parallel/Parallel.mli @@ -39,10 +39,10 @@ module Vec : sig (** [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]. *) -(* 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]. *) diff --git a/run_parallel.ml b/run_parallel.ml index 40fd09f..70c427d 100644 --- a/run_parallel.ml +++ b/run_parallel.ml @@ -2,6 +2,10 @@ let () = Printf.printf "Hello from rank %d of %d.\n" Parallel.rank Parallel.size; 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;