queso-0.57.0
MpiComm.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2017 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #ifndef UQ_MPI_COMM_H
26 #define UQ_MPI_COMM_H
27 
28 #include <queso/Defines.h>
29 
30 #ifdef QUESO_HAS_MPI
31 #include <mpi.h>
32 #endif
33 
34 #ifdef QUESO_HAS_TRILINOS
35 class Epetra_Comm;
36 #endif
37 
38 namespace QUESO {
39 
40 #ifdef QUESO_HAS_MPI
41 typedef MPI_Comm RawType_MPI_Comm ;
42 typedef MPI_Group RawType_MPI_Group ;
43 typedef MPI_Datatype RawType_MPI_Datatype ;
44 typedef MPI_Datatype data_type ;
45 typedef MPI_Op RawType_MPI_Op ;
46 typedef MPI_Status RawType_MPI_Status ;
47 #define RawValue_MPI_COMM_SELF MPI_COMM_SELF
48 #define RawValue_MPI_ANY_SOURCE MPI_ANY_SOURCE
49 #define RawValue_MPI_CHAR MPI_CHAR
50 #define RawValue_MPI_INT MPI_INT
51 #define RawValue_MPI_DOUBLE MPI_DOUBLE
52 #define RawValue_MPI_UNSIGNED MPI_UNSIGNED
53 #define RawValue_MPI_MIN MPI_MIN
54 #define RawValue_MPI_MAX MPI_MAX
55 #define RawValue_MPI_SUM MPI_SUM
56 #else
57 typedef int RawType_MPI_Comm;
58 typedef int RawType_MPI_Group;
59 typedef int RawType_MPI_Datatype;
60 struct data_type { };
61 typedef int RawType_MPI_Op;
62 typedef int RawType_MPI_Status;
63 #define RawValue_MPI_COMM_SELF 0
64 #define RawValue_MPI_ANY_SOURCE -1
65 #define RawValue_MPI_CHAR 0
66 #define RawValue_MPI_INT 1
67 #define RawValue_MPI_DOUBLE 2
68 #define RawValue_MPI_UNSIGNED 3
69 #define RawValue_MPI_MIN 0
70 #define RawValue_MPI_MAX 1
71 #define RawValue_MPI_SUM 2
72 #endif
73 
77 class DataType
78 {
79 public:
80  DataType () : _datatype() {}
81 
82  DataType (const DataType &other) :
83  _datatype(other._datatype)
84  {}
85 
87  _datatype(type)
88  {}
89 
90 #ifdef QUESO_HAS_MPI
91  DataType (const DataType &other, unsigned int count)
92  {
93  // FIXME - if we nest an inner type here will we run into bug
94  // https://github.com/libMesh/libmesh/issues/631 again?
95  MPI_Type_contiguous(count, other._datatype, &_datatype);
96  this->commit();
97  }
98 #else
99  DataType (const DataType &, unsigned int)
100  {
101  }
102 #endif
103 
104  DataType & operator = (const DataType &other)
105  { _datatype = other._datatype; return *this; }
106 
108  { _datatype = type; return *this; }
109 
110  operator const RawType_MPI_Datatype & () const
111  { return _datatype; }
112 
113  operator RawType_MPI_Datatype & ()
114  { return _datatype; }
115 
116  void commit ()
117  {
118 #ifdef QUESO_HAS_MPI
119  MPI_Type_commit (&_datatype);
120 #endif
121  }
122 
123  void free ()
124  {
125 #ifdef QUESO_HAS_MPI
126  MPI_Type_free (&_datatype);
127 #endif
128  }
129 
130 protected:
132 };
133 
141 template <typename T>
142 class StandardType : public DataType
143 {
144 #ifdef QUESO_HAS_CXX11 // This macro isn't defined (yet)
145  // Get a slightly better compiler diagnostic if we have C++11
146  static_assert(dependent_false<T>::value,
147  "Only specializations of StandardType may be used, did you forget to include a header file (e.g. parallel_algebra.h)?");
148 #endif
149 
150  /*
151  * The unspecialized class is useless, so we make its constructor
152  * private to catch mistakes at compile-time rather than link-time.
153  * Specializations should have a public constructor of the same
154  * form.
155  */
156 private:
157  StandardType(const T* example = NULL);
158 };
159 
160 #ifdef QUESO_HAS_MPI
161 
162 #define QUESO_STANDARD_TYPE(cxxtype,mpitype) \
163  template<> \
164  class StandardType<cxxtype> : public DataType \
165  { \
166  public: \
167  explicit \
168  StandardType(const cxxtype* = NULL) : DataType(mpitype) {} \
169  }
170 
171 #else
172 
173 #define QUESO_STANDARD_TYPE(cxxtype,mpitype) \
174  template<> \
175  class StandardType<cxxtype> : public DataType \
176  { \
177  public: \
178  explicit \
179  StandardType(const cxxtype* = NULL) : DataType() {} \
180  }
181 
182 #endif
183 
184 QUESO_STANDARD_TYPE(char,MPI_CHAR);
185 QUESO_STANDARD_TYPE(int,MPI_INT);
186 QUESO_STANDARD_TYPE(unsigned int,MPI_UNSIGNED);
187 QUESO_STANDARD_TYPE(double,MPI_DOUBLE);
188 
189 class BaseEnvironment;
190 
203 class MpiComm
204 {
205 public:
207 
208 
210 
217  MpiComm(const BaseEnvironment& env, RawType_MPI_Comm inputRawComm);
218 
220 
226  MpiComm(const BaseEnvironment& env);
227 
229 
230  MpiComm(const MpiComm& src);
231 
233  ~MpiComm();
235 
237 
238  MpiComm& operator= (const MpiComm& rhs);
241 
242 
244 
245 #ifdef QUESO_HAS_MPI
246  RawType_MPI_Comm Comm () const;
248 #endif // QUESO_HAS_MPI
249 
251  int MyPID () const;
252 
254  int NumProc () const;
256 
258 
259 
268  void Allreduce(void* sendbuf, void* recvbuf, int count, RawType_MPI_Datatype datatype,
269  RawType_MPI_Op op, const char* whereMsg, const char* whatMsg) const;
270 
272 
278  template <typename T>
279  void Allreduce(const T * sendbuf, T * recvbuf, int count, RawType_MPI_Op op,
280  const char* whereMsg, const char* whatMsg) const;
281 
283 
285  void Barrier () const; // const char* whereMsg, const char* whatMsg) const;
286 
288 
293  void Bcast (void* buffer, int count, RawType_MPI_Datatype datatype, int root,
294  const char* whereMsg, const char* whatMsg) const;
295 
297 
307  void Gather (void *sendbuf, int sendcnt, RawType_MPI_Datatype sendtype,
308  void *recvbuf, int recvcount, RawType_MPI_Datatype recvtype,
309  int root,
310  const char* whereMsg, const char* whatMsg) const;
311 
313 
320  template <typename T>
321  void Gather(const T * sendbuf, int sendcnt, T * recvbuf, int recvcount, int root,
322  const char * whereMsg, const char * whatMsg) const;
323 
325 
334  void Gatherv (void *sendbuf, int sendcnt, RawType_MPI_Datatype sendtype,
335  void *recvbuf, int *recvcnts, int *displs, RawType_MPI_Datatype recvtype,
336  int root,
337  const char* whereMsg, const char* whatMsg) const;
338 
340 
350  template <typename T>
351  void Gatherv(const T * sendbuf, int sendcnt, T * recvbuf, int * recvcnts,
352  int * displs, int root, const char * whereMsg,
353  const char * whatMsg) const;
354 
356 
362  void Recv (void *buf, int count, RawType_MPI_Datatype datatype, int source, int tag, RawType_MPI_Status *status,
363  const char* whereMsg, const char* whatMsg) const;
364 
366 
371  void Send (void *buf, int count, RawType_MPI_Datatype datatype, int dest, int tag,
372  const char* whereMsg, const char* whatMsg) const;
374 
376 
377  void syncPrintDebugMsg(const char* msg, unsigned int msgVerbosity, unsigned int numUSecs) const;
379 
380 #ifdef QUESO_HAS_TRILINOS
381  const Epetra_Comm& epetraMpiComm() const;
383 #endif
384 
386 private:
388 
389  MpiComm();
390 
392  void copy (const MpiComm& src);
393 
394  // QUESO environment
396 
397 #ifdef QUESO_HAS_TRILINOS
398  // Epetra communicator
399  Epetra_Comm* m_epetraComm;
400 #endif
401 
404 
407 
409  int m_myPid;
410 
411  // Total number of processes
413 };
414 
415 } // End namespace QUESO
416 
417 #endif // UQ_MPI_COMM_H
MPI_Comm RawType_MPI_Comm
Definition: MpiComm.h:41
const Epetra_Comm & epetraMpiComm() const
Extract MPI Communicator from a Epetra_MpiComm object.
Definition: MpiComm.C:360
DataType(const DataType &, unsigned int)
Definition: MpiComm.h:99
MpiComm()
Default Constructor.
Definition: MpiComm.C:91
The QUESO MPI Communicator Class.
Definition: MpiComm.h:203
void Gather(void *sendbuf, int sendcnt, RawType_MPI_Datatype sendtype, void *recvbuf, int recvcount, RawType_MPI_Datatype recvtype, int root, const char *whereMsg, const char *whatMsg) const
Gather values from each process to collect on all processes.
Definition: MpiComm.C:202
void syncPrintDebugMsg(const char *msg, unsigned int msgVerbosity, unsigned int numUSecs) const
Synchronizes all the processes and print debug message.
Definition: MpiComm.C:333
void copy(const MpiComm &src)
Copies from an existing MpiComm instance.
Definition: MpiComm.C:368
void Recv(void *buf, int count, RawType_MPI_Datatype datatype, int source, int tag, RawType_MPI_Status *status, const char *whereMsg, const char *whatMsg) const
Blocking receive of data from this process to another process.
Definition: MpiComm.C:307
MPI_Datatype data_type
Definition: MpiComm.h:44
~MpiComm()
Destructor.
DataType & operator=(const DataType &other)
Definition: MpiComm.h:104
RawType_MPI_Datatype _datatype
Definition: MpiComm.h:131
and that you are informed that you can do these things To protect your we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it For example
Definition: License.txt:45
int m_myPid
Process ID of this process.
Definition: MpiComm.h:409
DataType(const DataType &other, unsigned int count)
Definition: MpiComm.h:91
QUESO_STANDARD_TYPE(char, MPI_CHAR)
void commit()
Definition: MpiComm.h:116
RawType_MPI_Comm m_rawComm
Embedded wrapped opaque MPI_Comm object.
Definition: MpiComm.h:403
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:143
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:174
StandardType(const T *example=NULL)
MPI_Op RawType_MPI_Op
Definition: MpiComm.h:45
MPI_Datatype RawType_MPI_Datatype
Definition: MpiComm.h:43
const BaseEnvironment & m_env
Definition: MpiComm.h:395
void Send(void *buf, int count, RawType_MPI_Datatype datatype, int dest, int tag, const char *whereMsg, const char *whatMsg) const
Possibly blocking send of data from this process to another process.
Definition: MpiComm.C:320
void free()
Definition: MpiComm.h:123
MPI_Status RawType_MPI_Status
Definition: MpiComm.h:46
MPI_Group RawType_MPI_Group
Definition: MpiComm.h:42
int MyPID() const
Return my process ID.
Definition: MpiComm.C:124
DataType(const DataType &other)
Definition: MpiComm.h:82
Epetra_Comm * m_epetraComm
Definition: MpiComm.h:399
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:133
int m_worldRank
World rank.
Definition: MpiComm.h:406
void Gatherv(void *sendbuf, int sendcnt, RawType_MPI_Datatype sendtype, void *recvbuf, int *recvcnts, int *displs, RawType_MPI_Datatype recvtype, int root, const char *whereMsg, const char *whatMsg) const
Gathers into specified locations from all processes in a group.
Definition: MpiComm.C:254
RawType_MPI_Comm Comm() const
Extract MPI Communicator from a MpiComm object.
Definition: MpiComm.C:111
void Bcast(void *buffer, int count, RawType_MPI_Datatype datatype, int root, const char *whereMsg, const char *whatMsg) const
Broadcast values from the root process to the slave processes.
Definition: MpiComm.C:191
MpiComm & operator=(const MpiComm &rhs)
Assignment operator.
Definition: MpiComm.C:102
DataType(const RawType_MPI_Datatype &type)
Definition: MpiComm.h:86
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:198

Generated on Sat Apr 22 2017 14:04:35 for queso-0.57.0 by  doxygen 1.8.5