From db70b226292af1953731054a684df19791508c9b Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Fri, 29 Mar 2019 23:39:47 +0100 Subject: [PATCH] Prepared inter-node --- Parallel_mpi/Parallel.ml | 14 ++++++++++++-- Parallel_mpi/Parallel.mli | 35 +++++++++++++++++++++++++++++++++++ run_parallel.ml | 10 +++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/Parallel_mpi/Parallel.ml b/Parallel_mpi/Parallel.ml index 3c4ab70..5641272 100644 --- a/Parallel_mpi/Parallel.ml +++ b/Parallel_mpi/Parallel.ml @@ -59,7 +59,7 @@ module Node = struct let name = Unix.gethostname () - let comm_node = + let comm = Mpi.allgather (name, rank) Mpi.comm_world |> Array.to_list |> List.filter (fun (n, r) -> name = n) @@ -69,10 +69,20 @@ module Node = struct |> Mpi.(comm_create comm_world) let rank = - Mpi.comm_rank comm_node + Mpi.comm_rank comm let master = rank = 0 + let broadcast_generic broadcast x = + let x = + if master then Some (Lazy.force x) + else None + in + match broadcast x 0 comm with + | Some x -> x + | None -> assert false + + let broadcast x = broadcast_generic Mpi.broadcast x end diff --git a/Parallel_mpi/Parallel.mli b/Parallel_mpi/Parallel.mli index 4d76cc0..19d1f23 100644 --- a/Parallel_mpi/Parallel.mli +++ b/Parallel_mpi/Parallel.mli @@ -30,6 +30,41 @@ val broadcast_float_array : float array -> float array val broadcast_vec : Lacaml.D.vec -> Lacaml.D.vec (** Broadcasts a Lacaml vector to all processes. *) +(** {5 Intra-node operations} *) +module Node : sig + val name : string + (** Name of the current host *) + + val comm : Mpi.communicator + (** MPI Communicator on the current node *) + + val rank : Mpi.rank + (** Rank of the current process in the node *) + + val master : bool + (** If true, master process of the node *) + + val broadcast : 'a lazy_t -> 'a + (** Broadcasts data to all the processes of the current node. *) +end + +(* +(** {5 Intra-node operations} *) +module InterNode : sig + + val comm : Mpi.communicator + (** MPI Communicator among the master processes of the each node *) + + val rank : Mpi.rank + (** Rank of the current process in the inter-node communicator *) + + val master : bool + (** If true, master process of the inter-node communicator *) + + val broadcast : 'a lazy_t -> 'a + (** Broadcasts data to all the processes of the inter-node communicator. *) +end +*) (** {5 Vector operations} *) module Vec : sig diff --git a/run_parallel.ml b/run_parallel.ml index da89fb2..9231e7c 100644 --- a/run_parallel.ml +++ b/run_parallel.ml @@ -21,7 +21,7 @@ let v = Parallel.Vec.init 47 (fun i -> float_of_int i) in print_newline () -let () = +let pouet2 () = let f (a,b) = (Parallel.rank, a+b) in let input = Stream.of_list [ (1,2) ; (3,4) ; (5,6) ; (7,8) ; (9,10) @@ -34,3 +34,11 @@ let () = in Stream.iter (fun (x,y) -> Printf.printf "%d %d\n%!" x y) stream + +open Printf +let () = + printf "%d : %s\n" Parallel.rank Parallel.Node.name; + let value = Parallel.broadcast (lazy Parallel.rank) in + printf "%d : %d\n" Parallel.rank value; + let value = Parallel.Node.broadcast (lazy Parallel.rank) in + printf "%d : %d\n" Parallel.rank value