queso-0.56.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-2015 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_TRILINOS
31 #include <Epetra_MpiComm.h>
32 #endif
33 
34 #ifdef QUESO_HAS_MPI
35 #include <mpi.h>
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;
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  RawType_MPI_Comm Comm () const;
247 
249  int MyPID () const;
250 
252  int NumProc () const;
254 
256 
257 
266  void Allreduce(void* sendbuf, void* recvbuf, int count, RawType_MPI_Datatype datatype,
267  RawType_MPI_Op op, const char* whereMsg, const char* whatMsg) const;
268 
270 
276  template <typename T>
277  void Allreduce(const T * sendbuf, T * recvbuf, int count, RawType_MPI_Op op,
278  const char* whereMsg, const char* whatMsg) const;
279 
281 
283  void Barrier () const; // const char* whereMsg, const char* whatMsg) const;
284 
286 
291  void Bcast (void* buffer, int count, RawType_MPI_Datatype datatype, int root,
292  const char* whereMsg, const char* whatMsg) const;
293 
295 
305  void Gather (void *sendbuf, int sendcnt, RawType_MPI_Datatype sendtype,
306  void *recvbuf, int recvcount, RawType_MPI_Datatype recvtype,
307  int root,
308  const char* whereMsg, const char* whatMsg) const;
309 
311 
318  template <typename T>
319  void Gather(const T * sendbuf, int sendcnt, T * recvbuf, int recvcount, int root,
320  const char * whereMsg, const char * whatMsg) const;
321 
323 
332  void Gatherv (void *sendbuf, int sendcnt, RawType_MPI_Datatype sendtype,
333  void *recvbuf, int *recvcnts, int *displs, RawType_MPI_Datatype recvtype,
334  int root,
335  const char* whereMsg, const char* whatMsg) const;
336 
338 
348  template <typename T>
349  void Gatherv(const T * sendbuf, int sendcnt, T * recvbuf, int * recvcnts,
350  int * displs, int root, const char * whereMsg,
351  const char * whatMsg) const;
352 
354 
360  void Recv (void *buf, int count, RawType_MPI_Datatype datatype, int source, int tag, RawType_MPI_Status *status,
361  const char* whereMsg, const char* whatMsg) const;
362 
364 
369  void Send (void *buf, int count, RawType_MPI_Datatype datatype, int dest, int tag,
370  const char* whereMsg, const char* whatMsg) const;
372 
374 
375  void syncPrintDebugMsg(const char* msg, unsigned int msgVerbosity, unsigned int numUSecs) const;
377 
378 #ifdef QUESO_HAS_TRILINOS
379  const Epetra_MpiComm& epetraMpiComm() const;
381 #endif
382 
384 private:
386 
387  MpiComm();
388 
390  void copy (const MpiComm& src);
391 
392  // QUESO environment
394 #ifdef QUESO_HAS_TRILINOS
395 
396  // Epetra MPI communicator
397  Epetra_MpiComm* m_epetraMpiComm;
398 #endif
401 
404 
406  int m_myPid;
407 
408  // Total number of processes
410 };
411 
412 } // End namespace QUESO
413 
414 #endif // UQ_MPI_COMM_H
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:123
RawType_MPI_Comm Comm() const
Extract MPI Communicator from a MpiComm object.
Definition: MpiComm.C:105
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:164
int RawType_MPI_Status
Definition: MpiComm.h:62
~MpiComm()
Destructor.
Definition: MpiComm.C:86
int RawType_MPI_Comm
Definition: MpiComm.h:57
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:197
int MyPID() const
Return my process ID.
Definition: MpiComm.C:114
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:310
DataType(const RawType_MPI_Datatype &type)
Definition: MpiComm.h:86
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:244
void commit()
Definition: MpiComm.h:116
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 RawType_MPI_Op
Definition: MpiComm.h:61
MpiComm()
Default Constructor.
StandardType(const T *example=NULL)
int m_worldRank
World rank.
Definition: MpiComm.h:403
int m_myPid
Process ID of this process.
Definition: MpiComm.h:406
void free()
Definition: MpiComm.h:123
QUESO_STANDARD_TYPE(char, MPI_CHAR)
DataType(const DataType &, unsigned int)
Definition: MpiComm.h:99
RawType_MPI_Comm m_rawComm
Embedded wrapped opaque MPI_Comm object.
Definition: MpiComm.h:400
void syncPrintDebugMsg(const char *msg, unsigned int msgVerbosity, unsigned int numUSecs) const
Synchronizes all the processes and print debug message.
Definition: MpiComm.C:323
The QUESO MPI Communicator Class.
Definition: MpiComm.h:203
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:297
MpiComm & operator=(const MpiComm &rhs)
Assignment operator.
Definition: MpiComm.C:97
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:192
RawType_MPI_Datatype _datatype
Definition: MpiComm.h:131
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:133
void copy(const MpiComm &src)
Copies from an existing MpiComm instance.
Definition: MpiComm.C:358
int RawType_MPI_Group
Definition: MpiComm.h:58
const BaseEnvironment & m_env
Definition: MpiComm.h:393
DataType & operator=(const DataType &other)
Definition: MpiComm.h:104
DataType(const DataType &other)
Definition: MpiComm.h:82
int RawType_MPI_Datatype
Definition: MpiComm.h:59
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:181

Generated on Tue Nov 29 2016 10:53:10 for queso-0.56.0 by  doxygen 1.8.5