LCOV - code coverage report
Current view: top level - tools - Communicator.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 32 35 91.4 %
Date: 2021-11-18 15:22:58 Functions: 35 38 92.1 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-2020 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #ifndef __PLUMED_tools_Communicator_h
      23             : #define __PLUMED_tools_Communicator_h
      24             : #ifdef __PLUMED_HAS_MPI
      25             : #include <mpi.h>
      26             : #endif
      27             : #include <cstdlib>
      28             : #include "Exception.h"
      29             : #include <vector>
      30             : #include <string>
      31             : #include "Vector.h"
      32             : #include "Tensor.h"
      33             : #include "Matrix.h"
      34             : 
      35             : namespace PLMD {
      36             : 
      37             : #ifndef  __PLUMED_HAS_MPI
      38             : /// Surrogate of MPI_Comm when MPI library is not available
      39             : class MPI_Comm {};
      40             : /// Surrogate of MPI_Datatype when MPI library is not available
      41             : class MPI_Datatype {};
      42             : /// Surrogate of MPI_Status when MPI library is not available
      43             : class MPI_Status {};
      44             : /// Surrogate of MPI_Request when MPI library is not available
      45             : class MPI_Request {};
      46             : #endif
      47             : 
      48             : /// \ingroup TOOLBOX
      49             : /// Class containing wrappers to MPI.
      50             : /// All the MPI related stuff is relegated here.
      51             : class Communicator {
      52             : /// Communicator
      53             :   MPI_Comm communicator;
      54             : /// Function returning the MPI type.
      55             : /// You can use it to access to the MPI type of a C++ type, e.g.
      56             : /// `MPI_Datatype type=getMPIType<double>();`
      57             :   template <class T>
      58             :   static MPI_Datatype getMPIType();
      59             : /// Structure defining a buffer for MPI.
      60             : /// It contains info on the pointed data and its type and size. It is useful to
      61             : /// allow wrapper of MPI functions where the triplet (buffer,type,size)
      62             : /// is grouped into a single object. It can be built starting from
      63             : /// different kinds of data. To implement compatibility of MPI wrappers
      64             : /// with e.g. vectors, add constructors here.
      65             :   struct Data {
      66             :     void*pointer;
      67             :     int size;
      68             :     MPI_Datatype type;
      69             : /// Init from pointer and size
      70     2049472 :     template <typename T> Data(T*p,int s): pointer(p), size(s), type(getMPIType<T>()) {}
      71             : /// Init from reference
      72     5967695 :     template <typename T> explicit Data(T&p): pointer(&p), size(1), type(getMPIType<T>()) {}
      73             : /// Init from pointer to VectorGeneric
      74       11179 :     template <unsigned n> explicit Data(VectorGeneric<n> *p,int s): pointer(p), size(n*s), type(getMPIType<double>()) {}
      75             : /// Init from reference to VectorGeneric
      76             :     template <unsigned n> explicit Data(VectorGeneric<n> &p): pointer(&p), size(n), type(getMPIType<double>()) {}
      77             : /// Init from pointer to TensorGeneric
      78          16 :     template <unsigned n,unsigned m> explicit Data(TensorGeneric<n,m> *p,int s): pointer(p), size(n*m*s), type(getMPIType<double>()) {}
      79             : /// Init from reference to TensorGeneric
      80       25681 :     template <unsigned n,unsigned m> explicit Data(TensorGeneric<n,m> &p): pointer(&p), size(n*m), type(getMPIType<double>()) {}
      81             : /// Init from reference to std::vector
      82     1999545 :     template <typename T> explicit Data(std::vector<T>&v) {
      83     3987895 :       Data d(v.data(),v.size()); pointer=d.pointer; size=d.size; type=d.type;
      84             :     }
      85             : /// Init from reference to PLMD::Matrix
      86          17 :     template <typename T> explicit Data(Matrix<T>&m ) {
      87          34 :       if(m.nrows()*m.ncols()>0) { Data d(&m(0,0),m.nrows()*m.ncols()); pointer=d.pointer; size=d.size; type=d.type; }
      88           0 :       else { pointer=NULL; size=0; }
      89             :     }
      90             : /// Init from reference to std::string
      91             :     explicit Data(std::string&s) {
      92             :       if(s.size()>0) { Data d(&s[0],s.size()); pointer=d.pointer; size=d.size; type=d.type; }
      93             :       else { pointer=NULL; size=0; }
      94             :     }
      95             :   };
      96             : /// Const version of Communicator::Data
      97             : /// See Communicator::Data documentation
      98             :   struct ConstData {
      99             :     const void*pointer;
     100             :     int size;
     101             :     MPI_Datatype type;
     102       17806 :     template <typename T> explicit ConstData(const T*p,int s): pointer(p), size(s), type(getMPIType<T>()) {}
     103        4487 :     template <typename T> explicit ConstData(const T&p): pointer(&p), size(1), type(getMPIType<T>()) {}
     104             :     template <unsigned n> explicit ConstData(const VectorGeneric<n> *p,int s): pointer(p), size(n*s), type(getMPIType<double>()) {}
     105             :     template <unsigned n> explicit ConstData(const VectorGeneric<n> &p): pointer(&p), size(n), type(getMPIType<double>()) {}
     106             :     template <unsigned n,unsigned m> explicit ConstData(const TensorGeneric<n,m> *p,int s): pointer(p), size(n*m*s), type(getMPIType<double>()) {}
     107             :     template <unsigned n,unsigned m> explicit ConstData(const TensorGeneric<n,m> &p): pointer(&p), size(n*m), type(getMPIType<double>()) {}
     108         198 :     template <typename T> explicit ConstData(const std::vector<T>&v) {
     109         396 :       ConstData d(v.data(),v.size()); pointer=d.pointer; size=d.size; type=d.type;
     110             :     }
     111             :     template <typename T> explicit ConstData(const Matrix<T>&m ) {
     112             :       if(m.nrows()*m.ncols()>0) { ConstData d(&m(0,0),m.nrows()*m.ncols()); pointer=d.pointer; size=d.size; type=d.type; }
     113             :       else { pointer=NULL; size=0; }
     114             :     }
     115          57 :     explicit ConstData(const std::string&s) {
     116         114 :       if(s.size()>0) { ConstData d(&s[0],s.size()); pointer=d.pointer; size=d.size; type=d.type; }
     117           0 :       else { pointer=NULL; size=0; }
     118             :     }
     119             :   };
     120             : public:
     121             : /// Wrapper class for MPI_Status
     122             :   class Status {
     123             :     int Get_count(MPI_Datatype)const;
     124             :   public:
     125             :     MPI_Status s;
     126             :     template <class T>
     127        7540 :     int Get_count()const {return Get_count(getMPIType<T>());}
     128             :   };
     129             : /// Special status used when status should be ignored.
     130             : /// E.g. `Recv(a,0,1,Communicator::StatusIgnore);`
     131             : /// Notice that this is the default for Recv, so this is equivalent to
     132             : /// `Recv(a,0,1);`
     133             :   static Status StatusIgnore;
     134             : /// Wrapper class for MPI_Request
     135             :   class Request {
     136             :   public:
     137             :     MPI_Request r;
     138             :     void wait(Status&s=StatusIgnore);
     139             :   };
     140             : /// Default constructor
     141             :   Communicator();
     142             : /// Copy constructor.
     143             : /// It effectively "clones" the communicator, providing a new one acting on the same group
     144             :   Communicator(const Communicator&);
     145             : /// Assignment operator.
     146             : /// It effectively "clones" the communicator, providing a new one acting on the same group
     147             :   Communicator& operator=(const Communicator&);
     148             : /// Destructor
     149             :   virtual ~Communicator();
     150             : /// Obtain the rank of the present process
     151             :   int Get_rank()const;
     152             : /// Obtain the number of processes
     153             :   int Get_size()const;
     154             : /// Set from a real MPI communicator.
     155             : /// \param comm MPI communicator
     156             :   void Set_comm(MPI_Comm comm);
     157             : /// Reference to MPI communicator
     158             :   MPI_Comm & Get_comm();
     159             : /// Set from a pointer to a real MPI communicator (C).
     160             : /// \param comm Pointer to a C MPI communicator
     161             :   void Set_comm(void*comm);
     162             : /// Set from a pointer to a real MPI communicator (FORTRAN).
     163             : /// \param comm Pointer to a FORTRAN MPI communicator (INTEGER)
     164             :   void Set_fcomm(void*comm);
     165             : /// Wrapper to MPI_Abort.
     166             : /// \param code Error code
     167             :   void Abort(int code);
     168             : /// Wrapper to MPI_Barrier
     169             :   void Barrier()const;
     170             : /// Tests if MPI library is initialized
     171             :   static bool initialized();
     172             : /// Wrapper for MPI_Allreduce with MPI_SUM (data struct)
     173             :   void Sum(Data);
     174             : /// Wrapper for MPI_Allreduce with MPI_SUM (pointer)
     175       87108 :   template <class T> void Sum(T*buf,int count) {Sum(Data(buf,count));}
     176             : /// Wrapper for MPI_Allreduce with MPI_SUM (reference)
     177     7768900 :   template <class T> void Sum(T&buf) {Sum(Data(buf));}
     178             : /// Wrapper for MPI_Allreduce with MPI_PROD (data struct)
     179             :   void Prod(Data);
     180             : /// Wrapper for MPI_Allreduce with MPI_PROD (pointer)
     181             :   template <class T> void Prod(T*buf,int count) {Prod(Data(buf,count));}
     182             : /// Wrapper for MPI_Allreduce with MPI_PROD (reference)
     183             :   template <class T> void Prod(T&buf) {Prod(Data(buf));}
     184             : /// Wrapper for MPI_Allreduce with MPI_MAX (data struct)
     185             :   void Max(Data);
     186             : /// Wrapper for MPI_Allreduce with MPI_MAX (pointer)
     187             :   template <class T> void Max(T*buf,int count) {Max(Data(buf,count));}
     188             : /// Wrapper for MPI_Allreduce with MPI_MAX (reference)
     189             :   template <class T> void Max(T&buf) {Max(Data(buf));}
     190             : /// Wrapper for MPI_Allreduce with MPI_MIN (data struct)
     191             :   void Min(Data);
     192             : /// Wrapper for MPI_Allreduce with MPI_MIN (pointer)
     193             :   template <class T> void Min(T*buf,int count) {Min(Data(buf,count));}
     194             : /// Wrapper for MPI_Allreduce with MPI_MIN (reference)
     195           0 :   template <class T> void Min(T&buf) {Min(Data(buf));}
     196             : 
     197             : /// Wrapper for MPI_Bcast (data struct)
     198             :   void Bcast(Data,int);
     199             : /// Wrapper for MPI_Bcast (pointer)
     200             :   template <class T> void Bcast(T*buf,int count,int root) {Bcast(Data(buf,count),root);}
     201             : /// Wrapper for MPI_Bcast (reference)
     202     8207492 :   template <class T> void Bcast(T&buf,int root) {Bcast(Data(buf),root);}
     203             : 
     204             : /// Wrapper for MPI_Isend (data struct)
     205             :   Request Isend(ConstData,int,int);
     206             : /// Wrapper for MPI_Isend (pointer)
     207       30160 :   template <class T> Request Isend(const T*buf,int count,int source,int tag) {return Isend(ConstData(buf,count),source,tag);}
     208             : /// Wrapper for MPI_Isend (reference)
     209         228 :   template <class T> Request Isend(const T&buf,int source,int tag) {return Isend(ConstData(buf),source,tag);}
     210             : 
     211             : /// Wrapper for MPI_Allgatherv (data struct)
     212             :   void Allgatherv(ConstData in,Data out,const int*,const int*);
     213             : /// Wrapper for MPI_Allgatherv (pointer)
     214        2267 :   template <class T,class S> void Allgatherv(const T*sendbuf,int sendcount,S*recvbuf,const int*recvcounts,const int*displs) {
     215        2267 :     Allgatherv(ConstData(sendbuf,sendcount),Data(recvbuf,0),recvcounts,displs);
     216        2267 :   }
     217             : /// Wrapper for MPI_Allgatherv (reference)
     218             :   template <class T,class S> void Allgatherv(const T&sendbuf,S&recvbuf,const int*recvcounts,const int*displs) {
     219             :     Allgatherv(ConstData(sendbuf),Data(recvbuf),recvcounts,displs);
     220             :   }
     221             : 
     222             : /// Wrapper for MPI_Allgather (data struct)
     223             :   void Allgather(ConstData in,Data out);
     224             : /// Wrapper for MPI_Allgatherv (pointer)
     225         204 :   template <class T,class S> void Allgather(const T*sendbuf,int sendcount,S*recvbuf,int recvcount) {
     226         408 :     Allgather(ConstData(sendbuf,sendcount),Data(recvbuf,recvcount*Get_size()));
     227         204 :   }
     228             : /// Wrapper for MPI_Allgatherv (reference)
     229        4628 :   template <class T,class S> void Allgather(const T&sendbuf,S&recvbuf) {
     230        4628 :     Allgather(ConstData(sendbuf),Data(recvbuf));
     231        4628 :   }
     232             : 
     233             : /// Wrapper for MPI_Recv (data struct)
     234             :   void Recv(Data,int,int,Status&s=StatusIgnore);
     235             : /// Wrapper for MPI_Recv (pointer)
     236       30160 :   template <class T> void Recv(T*buf,int count,int source,int tag,Status&s=StatusIgnore) {Recv(Data(buf,count),source,tag,s);}
     237             : /// Wrapper for MPI_Recv (reference)
     238         228 :   template <class T> void Recv(T&buf,int source,int tag,Status&s=StatusIgnore) {Recv(Data(buf),source,tag,s);}
     239             : 
     240             : /// Wrapper to MPI_Comm_split
     241             :   void Split(int,int,Communicator&)const;
     242             : };
     243             : 
     244             : }
     245             : 
     246             : #endif

Generated by: LCOV version 1.14