Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2011-2019 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 2019776 : template <typename T> Data(T*p,int s): pointer(p), size(s), type(getMPIType<T>()) {}
71 : /// Init from reference
72 5829875 : template <typename T> explicit Data(T&p): pointer(&p), size(1), type(getMPIType<T>()) {}
73 : /// Init from pointer to VectorGeneric
74 11128 : 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 20133 : 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 1983568 : template <typename T> explicit Data(std::vector<T>&v) {
83 3967136 : if(v.size()>0) { Data d(&v[0],v.size()); pointer=d.pointer; size=d.size; type=d.type; }
84 0 : else { pointer=NULL; size=0; }
85 11144 : }
86 : /// Init from reference to PLMD::Matrix
87 19 : template <typename T> explicit Data(Matrix<T>&m ) {
88 38 : if(m.nrows()*m.ncols()>0) { Data d(&m(0,0),m.nrows()*m.ncols()); pointer=d.pointer; size=d.size; type=d.type; }
89 0 : else { pointer=NULL; size=0; }
90 : }
91 : /// Init from reference to std::string
92 : explicit Data(std::string&s) {
93 : if(s.size()>0) { Data d(&s[0],s.size()); pointer=d.pointer; size=d.size; type=d.type; }
94 : else { pointer=NULL; size=0; }
95 : }
96 : };
97 : /// Const version of Communicator::Data
98 : /// See Communicator::Data documentation
99 : struct ConstData {
100 : const void*pointer;
101 : int size;
102 : MPI_Datatype type;
103 17217 : template <typename T> explicit ConstData(const T*p,int s): pointer(p), size(s), type(getMPIType<T>()) {}
104 206 : template <typename T> explicit ConstData(const T&p): pointer(&p), size(1), type(getMPIType<T>()) {}
105 : template <unsigned n> explicit ConstData(const VectorGeneric<n> *p,int s): pointer(p), size(n*s), type(getMPIType<double>()) {}
106 : template <unsigned n> explicit ConstData(const VectorGeneric<n> &p): pointer(&p), size(n), type(getMPIType<double>()) {}
107 : template <unsigned n,unsigned m> explicit ConstData(const TensorGeneric<n,m> *p,int s): pointer(p), size(n*m*s), type(getMPIType<double>()) {}
108 : template <unsigned n,unsigned m> explicit ConstData(const TensorGeneric<n,m> &p): pointer(&p), size(n*m), type(getMPIType<double>()) {}
109 144 : template <typename T> explicit ConstData(const std::vector<T>&v) {
110 288 : if(v.size()>0) { ConstData d(&v[0],v.size()); pointer=d.pointer; size=d.size; type=d.type; }
111 0 : else { pointer=NULL; size=0; }
112 : }
113 : template <typename T> explicit ConstData(const Matrix<T>&m ) {
114 : if(m.nrows()*m.ncols()>0) { ConstData d(&m(0,0),m.nrows()*m.ncols()); pointer=d.pointer; size=d.size; type=d.type; }
115 : else { pointer=NULL; size=0; }
116 : }
117 42 : explicit ConstData(const std::string&s) {
118 84 : if(s.size()>0) { ConstData d(&s[0],s.size()); pointer=d.pointer; size=d.size; type=d.type; }
119 0 : else { pointer=NULL; size=0; }
120 : }
121 : };
122 : public:
123 : /// Wrapper class for MPI_Status
124 : class Status {
125 : int Get_count(MPI_Datatype)const;
126 : public:
127 : MPI_Status s;
128 : template <class T>
129 7280 : int Get_count()const {return Get_count(getMPIType<T>());}
130 : };
131 : /// Special status used when status should be ignored.
132 : /// E.g. `Recv(a,0,1,Communicator::StatusIgnore);`
133 : /// Notice that this is the default for Recv, so this is equivalent to
134 : /// `Recv(a,0,1);`
135 : static Status StatusIgnore;
136 : /// Wrapper class for MPI_Request
137 : class Request {
138 : public:
139 : MPI_Request r;
140 : void wait(Status&s=StatusIgnore);
141 : };
142 : /// Default constructor
143 : Communicator();
144 : /// Copy constructor.
145 : /// It effectively "clones" the communicator, providing a new one acting on the same group
146 : Communicator(const Communicator&);
147 : /// Assignment operator.
148 : /// It effectively "clones" the communicator, providing a new one acting on the same group
149 : Communicator& operator=(const Communicator&);
150 : /// Destructor
151 : virtual ~Communicator();
152 : /// Obtain the rank of the present process
153 : int Get_rank()const;
154 : /// Obtain the number of processes
155 : int Get_size()const;
156 : /// Set from a real MPI communicator.
157 : /// \param comm MPI communicator
158 : void Set_comm(MPI_Comm comm);
159 : /// Reference to MPI communicator
160 : MPI_Comm & Get_comm();
161 : /// Set from a pointer to a real MPI communicator (C).
162 : /// \param comm Pointer to a C MPI communicator
163 : void Set_comm(void*comm);
164 : /// Set from a pointer to a real MPI communicator (FORTRAN).
165 : /// \param comm Pointer to a FORTRAN MPI communicator (INTEGER)
166 : void Set_fcomm(void*comm);
167 : /// Wrapper to MPI_Abort.
168 : /// \param code Error code
169 : void Abort(int code);
170 : /// Wrapper to MPI_Barrier
171 : void Barrier()const;
172 : /// Tests if MPI library is initialized
173 : static bool initialized();
174 :
175 : /// Returns MPI_COMM_WORLD if MPI is initialized, otherwise the default communicator
176 : static Communicator & Get_world();
177 :
178 : /// Wrapper for MPI_Allreduce with MPI_SUM (data struct)
179 : void Sum(Data);
180 : /// Wrapper for MPI_Allreduce with MPI_SUM (pointer)
181 60604 : template <class T> void Sum(T*buf,int count) {Sum(Data(buf,count));}
182 : /// Wrapper for MPI_Allreduce with MPI_SUM (reference)
183 7750346 : template <class T> void Sum(T&buf) {Sum(Data(buf));}
184 :
185 : /// Wrapper for MPI_Bcast (data struct)
186 : void Bcast(Data,int);
187 : /// Wrapper for MPI_Bcast (pointer)
188 : template <class T> void Bcast(T*buf,int count,int root) {Bcast(Data(buf,count),root);}
189 : /// Wrapper for MPI_Bcast (reference)
190 7904916 : template <class T> void Bcast(T&buf,int root) {Bcast(Data(buf),root);}
191 :
192 : /// Wrapper for MPI_Isend (data struct)
193 : Request Isend(ConstData,int,int);
194 : /// Wrapper for MPI_Isend (pointer)
195 29120 : template <class T> Request Isend(const T*buf,int count,int source,int tag) {return Isend(ConstData(buf,count),source,tag);}
196 : /// Wrapper for MPI_Isend (reference)
197 168 : template <class T> Request Isend(const T&buf,int source,int tag) {return Isend(ConstData(buf),source,tag);}
198 :
199 : /// Wrapper for MPI_Allgatherv (data struct)
200 : void Allgatherv(ConstData in,Data out,const int*,const int*);
201 : /// Wrapper for MPI_Allgatherv (pointer)
202 2267 : template <class T,class S> void Allgatherv(const T*sendbuf,int sendcount,S*recvbuf,const int*recvcounts,const int*displs) {
203 2267 : Allgatherv(ConstData(sendbuf,sendcount),Data(recvbuf,0),recvcounts,displs);
204 2267 : }
205 : /// Wrapper for MPI_Allgatherv (reference)
206 : template <class T,class S> void Allgatherv(const T&sendbuf,S&recvbuf,const int*recvcounts,const int*displs) {
207 : Allgatherv(ConstData(sendbuf),Data(recvbuf),recvcounts,displs);
208 : }
209 :
210 : /// Wrapper for MPI_Allgather (data struct)
211 : void Allgather(ConstData in,Data out);
212 : /// Wrapper for MPI_Allgatherv (pointer)
213 204 : template <class T,class S> void Allgather(const T*sendbuf,int sendcount,S*recvbuf,int recvcount) {
214 408 : Allgather(ConstData(sendbuf,sendcount),Data(recvbuf,recvcount*Get_size()));
215 204 : }
216 : /// Wrapper for MPI_Allgatherv (reference)
217 308 : template <class T,class S> void Allgather(const T&sendbuf,S&recvbuf) {
218 308 : Allgather(ConstData(sendbuf),Data(recvbuf));
219 308 : }
220 :
221 : /// Wrapper for MPI_Recv (data struct)
222 : void Recv(Data,int,int,Status&s=StatusIgnore);
223 : /// Wrapper for MPI_Recv (pointer)
224 29120 : template <class T> void Recv(T*buf,int count,int source,int tag,Status&s=StatusIgnore) {Recv(Data(buf,count),source,tag,s);}
225 : /// Wrapper for MPI_Recv (reference)
226 168 : template <class T> void Recv(T&buf,int source,int tag,Status&s=StatusIgnore) {Recv(Data(buf),source,tag,s);}
227 :
228 : /// Wrapper to MPI_Comm_split
229 : void Split(int,int,Communicator&)const;
230 : };
231 :
232 : }
233 :
234 : #endif
|