From be47e1c9c3ebaa9e850fe4e7b89d5e89f5d136ac Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Tue, 16 Oct 2018 19:09:00 +0200 Subject: [PATCH] Working on parallel --- INSTALL.md | 9 +++++ Makefile.include | 2 +- Parallel/Parallel.ml | 74 +++++++++++++++++++++++++++++++++++++++-- Parallel/Parallel.mli | 77 ++++++++++++++++++++++++++++++++++++++++++- README.rst | 4 +++ opam | 24 -------------- run_parallel.ml | 6 ++-- 7 files changed, 166 insertions(+), 30 deletions(-) delete mode 100644 opam diff --git a/INSTALL.md b/INSTALL.md index b8afc44..e3ddcf0 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -21,6 +21,15 @@ export LACAML_LIBS="-L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_rt -lpthr opam install lacaml ``` +To use Intel MPI + +```bash +export MPI_INC_DIR=${I_MPI_ROOT}/include64/ +export MPI_LIB_DIR=${I_MPI_ROOT}/lib64/ +export MPI_BIN_PATH=${I_MPI_ROOT}/bin64/ +opam install mpi +``` + # odoc-ltxhtml This plugin allows to embed equations in the documentation generated by Ocamldoc. diff --git a/Makefile.include b/Makefile.include index e7a5e3c..c31a2f8 100644 --- a/Makefile.include +++ b/Makefile.include @@ -3,7 +3,7 @@ INCLUDE_DIRS=Parallel,Nuclei,Utils,Basis,SCF,MOBasis,CI LIBS= PKGS= -OCAMLBUILD=ocamlbuild -j 0 -cflags $(ocamlcflags) -lflags $(ocamlcflags) $(ocamldocflags) -Is $(INCLUDE_DIRS) -ocamlopt $(ocamloptflags) +OCAMLBUILD=ocamlbuild -j 0 -cflags $(ocamlcflags) -lflags $(ocamllflags) $(ocamldocflags) -Is $(INCLUDE_DIRS) -ocamlopt $(ocamloptflags) MLLFILES=$(wildcard */*.mll) $(wildcard *.mll) Utils/math_functions.c MLYFILES=$(wildcard */*.mly) $(wildcard *.mly) diff --git a/Parallel/Parallel.ml b/Parallel/Parallel.ml index 351ddeb..cfb1d89 100644 --- a/Parallel/Parallel.ml +++ b/Parallel/Parallel.ml @@ -1,6 +1,76 @@ (** Module for handling distributed parallelism *) -let size = Mpi.comm_size Mpi.comm_world -let rank = Mpi.comm_rank Mpi.comm_world +let size = + let result = + Mpi.comm_size Mpi.comm_world + in + assert (result > 0); + result +let rank = + let result = + Mpi.comm_rank Mpi.comm_world + in + assert (result >= 0); + result + + +let barrier () = + Mpi.barrier Mpi.comm_world + + +module Vec = struct + + type t = + { + global_first : int ; (* Lower index in the global array *) + global_last : int ; (* Higher index in the global array *) + local_first : int ; (* Lower index in the local array *) + local_last : int ; (* Higher index in the local array *) + data : Lacaml.D.vec ; (* Lacaml vector containing the data *) + } + + let pp ppf v = + Format.fprintf ppf "@[<2>"; + Format.fprintf ppf "@[ gf : %d@]@;" v.global_first; + Format.fprintf ppf "@[ gl : %d@]@;" v.global_last; + Format.fprintf ppf "@[ lf : %d@]@;" v.local_first; + Format.fprintf ppf "@[ ll : %d@]@;" v.local_last; + Format.fprintf ppf "@[ data : %a@]@;" (Lacaml.Io.pp_lfvec ()) v.data; + Format.fprintf ppf "@]@."; + () + + let create n = + let step = n / size + 1 in + let local_first = step * rank + 1 in + let local_last = min (local_first + step - 1) n in + { + global_first = 1 ; + global_last = n ; + local_first ; + local_last ; + data = Lacaml.D.Vec.create (local_last - local_first + 1) + } + + + let make n x = + let result = create n in + { result with data = + Lacaml.D.Vec.make + (Lacaml.D.Vec.dim result.data) + x + } + + + let make0 n = + make n 0. + + let init n f = + let result = create n in + { result with data = + Lacaml.D.Vec.init + (Lacaml.D.Vec.dim result.data) + (fun i -> f (i+result.local_first-1)) + } +end diff --git a/Parallel/Parallel.mli b/Parallel/Parallel.mli index 4fbac42..f18de90 100644 --- a/Parallel/Parallel.mli +++ b/Parallel/Parallel.mli @@ -4,7 +4,82 @@ val size : int (** Number of distributed processes. *) val rank : Mpi.rank -(** Rannk of the current distributed processe. *) +(** Rank of the current distributed processe. *) + +val barrier : unit -> unit +(** Wait for all processes to reach this point. *) + +(** {5 Vector operations} *) +module Vec : sig + + type t = + { + global_first : int ; (* Lower index in the global array *) + global_last : int ; (* Higher index in the global array *) + local_first : int ; (* Lower index in the local array *) + local_last : int ; (* Higher index in the local array *) + data : Lacaml.D.vec ; (* Lacaml vector containing the data *) + } + + val pp : Format.formatter -> t -> unit + + (** {6 Creation/conversion of vectors and dimension accessor} *) + + val create : int -> t + (** [create n] @return a distributed vector with [n] rows (not initialized). *) + + val make : int -> float -> t + (** [make n x] @return a distributed vector with [n] rows initialized with value [x]. *) + + val make0 : int -> t + (** [make0 n x] @return a distributed vector with [n] rows initialized with the zero + element. *) + + val init : int -> (int -> float) -> t + (** [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]. *) + + val of_vec : Lacaml.D.vec -> t + (** [of_vec vec] @return a distributed vector initialized from Lacaml vector [vec]. *) + + val to_vec : t -> Lacaml.D.vec + (** [to_vec v] @return a Lacaml vector initialized from vector [v]. *) +*) + +end + + +(* +module Mat : sig + + type t = + { + global_first_row : int ; (* Lower row index in the global array *) + global_last_row : int ; (* Higher row index in the global array *) + global_first_col : int ; (* Lower column index in the global array *) + global_last_col : int ; (* Higher column index in the global array *) + local_first_row : int ; (* Lower row index in the local array *) + local_last_row : int ; (* Higher row index in the local array *) + local_first_col : int ; (* Lower column index in the local array *) + local_last_col : int ; (* Higher column index in the local array *) + data : Lacaml.D.mat ; (* Lacaml matrix containing the data *) + } + +end + +val gemm : Mat.t -> Mat.t -> Mat.t +(* Distributed matrix-matrix product. The result is a distributed matrix. *) + +val dot : Vec.t -> Vec.t-> float +(* Dot product between distributed vectors. *) +*) diff --git a/README.rst b/README.rst index 6783226..15fcb18 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,7 @@ Requirements ------------ +* MPI : Message Passing Interface * BLAS/LAPACK : Linear algebra * LaCaml : LAPACK OCaml interface * Zarith : Arbitrary-precision integers @@ -8,3 +9,6 @@ Requirements * odoc-ltxhtml : https://github.com/akabe/odoc-ltxhtml * Alcotest : Lightweight testing framework +Troubleshooting +--------------- + diff --git a/opam b/opam deleted file mode 100644 index 1b18f49..0000000 --- a/opam +++ /dev/null @@ -1,24 +0,0 @@ -opam-version: "1.2" -name: "QCaml" -version: "0.1" -maintainer: "Anthony Scemama " -authors: "Anthony Scemama " -homepage: "http://github.com/scemama/QCaml" -#bug-reports: "" -#license: "" -dev-repo: "http://github.com/scemama/QCaml" - -build: [ - ["./configure" "-prefix" "%{prefix}%"] - [make] -] - -install: [make "install"] -remove: [ - ["./configure" "-prefix" "%{prefix}%"] - [make "uninstall"] - ["ocamlfind" "remove" "QCaml"] -] -depends: [ - "ocamlfind" "lacaml" "alcotest" {build} -] diff --git a/run_parallel.ml b/run_parallel.ml index f3e415f..40fd09f 100644 --- a/run_parallel.ml +++ b/run_parallel.ml @@ -1,5 +1,7 @@ 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 v = Parallel.Vec.init 47 (fun i -> float_of_int i) in + Format.printf "%a" Parallel.Vec.pp v;