queso-0.53.0
MpiComm.C
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 #include <unistd.h>
26 #include <queso/MpiComm.h>
27 #include <queso/Environment.h>
28 
29 namespace QUESO {
30 
31 // QUESO MpiComm MPI Constructor ------------------
33  :
34  m_env (env),
35 #ifdef QUESO_HAS_TRILINOS
36  m_epetraMpiComm( new Epetra_MpiComm(inputRawComm) ),
37 #endif
38  m_rawComm (inputRawComm),
39  m_worldRank (-1),
40  m_myPid (-1),
41  m_numProc (-1)
42 {
43  int mpiRC = MPI_Comm_rank(inputRawComm,&m_worldRank);
44  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "failed MPI_Comm_rank() on full rank");
45 
46  mpiRC = MPI_Comm_rank(inputRawComm,&m_myPid);
47  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "failed MPI_Comm_rank() on inputRawComm");
48 
49  mpiRC = MPI_Comm_size(inputRawComm,&m_numProc);
50  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "failed MPI_Comm_size() on inputRawComm");
51 }
52 
53 // Copy constructor ---------------------------------
55  :
56  m_env (src.m_env)
57 #ifdef QUESO_HAS_TRILINOS
58  ,
59  m_epetraMpiComm(NULL)
60 #endif
61 {
62  this->copy(src);
63 }
64 
65 // Destructor ---------------------------------------
67 {
68 #ifdef QUESO_HAS_TRILINOS
69  delete m_epetraMpiComm;
70  m_epetraMpiComm = NULL;
71 #endif
72 }
73 
74 // --------------------------------------------------
75 // Set methodos -------------------------------------
76 MpiComm&
78 {
79  this->copy(rhs);
80  return *this;
81 }
82 
83 // Attribute access methods -------------------------
86 {
87 #ifdef QUESO_HAS_TRILINOS
88  return m_epetraMpiComm->Comm();
89 #endif
90  return m_rawComm;
91 }
92 // --------------------------------------------------
93 int
95 {
96 #ifdef QUESO_HAS_TRILINOS
97  return m_epetraMpiComm->MyPID();
98 #endif
99  return m_myPid;
100 }
101 // --------------------------------------------------
102 int
104 {
105 #ifdef QUESO_HAS_TRILINOS
106  return m_epetraMpiComm->NumProc();
107 #endif
108  return m_numProc;
109 }
110 // Methods overridden from Comm ---------------------
111 
112 void
113 MpiComm::Allreduce(void* sendbuf, void* recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char* whereMsg, const char* whatMsg) const
114 {
115  int mpiRC = MPI_Allreduce(sendbuf, recvbuf, count, datatype, op, m_rawComm);
116  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, whatMsg);
117 
118  return;
119 }
120 //--------------------------------------------------
121 void
122 MpiComm::Barrier() const // const char* whereMsg, const char* whatMsg) const
123 {
124 #ifdef QUESO_HAS_TRILINOS
125  return m_epetraMpiComm->Barrier();
126 #endif
127  int mpiRC = MPI_Barrier(m_rawComm);
128  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "mpiRC indicates failure"); // whatMsg);
129  return;
130 }
131 //--------------------------------------------------
132 void
133 MpiComm::Bcast(void* buffer, int count, RawType_MPI_Datatype datatype, int root, const char* whereMsg, const char* whatMsg) const
134 {
135  int mpiRC = MPI_Bcast(buffer, count, datatype, root, m_rawComm);
136  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, whatMsg);
137  return;
138 }
139 //--------------------------------------------------
140 void
142  void* sendbuf, int sendcnt, RawType_MPI_Datatype sendtype,
143  void* recvbuf, int recvcount, RawType_MPI_Datatype recvtype,
144  int root,
145  const char* whereMsg, const char* whatMsg) const
146 {
147  //int MPI_Gather (void *sendbuf, int sendcnt, MPI_Datatype sendtype,
148  // void *recvbuf, int recvcount, MPI_Datatype recvtype,
149  // int root, MPI_Comm comm )
150  int mpiRC = MPI_Gather(sendbuf, sendcnt, sendtype,
151  recvbuf, recvcount, recvtype,
152  root, m_rawComm);
153  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, whatMsg);
154  return;
155 }
156 //--------------------------------------------------
157 void
159  void* sendbuf, int sendcnt, RawType_MPI_Datatype sendtype,
160  void* recvbuf, int* recvcnts, int* displs, RawType_MPI_Datatype recvtype,
161  int root,
162  const char* whereMsg, const char* whatMsg) const
163 {
164  //int MPI_Gatherv(void *sendbuf, int sendcnt, MPI_Datatype sendtype,
165  // void *recvbuf, int *recvcnts, int *displs, MPI_Datatype recvtype,
166  // int root, MPI_Comm comm )
167  int mpiRC = MPI_Gatherv(sendbuf, sendcnt, sendtype,
168  recvbuf, recvcnts, displs, recvtype,
169  root, m_rawComm);
170  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, whatMsg);
171  return;
172 }
173 //--------------------------------------------------
174 void
176  void* buf, int count, RawType_MPI_Datatype datatype, int source, int tag, RawType_MPI_Status* status,
177  const char* whereMsg, const char* whatMsg) const
178 {
179  int mpiRC = MPI_Recv(buf, count, datatype, source, tag, m_rawComm, status);
180  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, whatMsg);
181  return;
182 }
183 //--------------------------------------------------
184 void
186  void* buf, int count, RawType_MPI_Datatype datatype, int dest, int tag,
187  const char* whereMsg, const char* whatMsg) const
188 {
189  int mpiRC = MPI_Send(buf, count, datatype, dest, tag, m_rawComm);
190  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, whatMsg);
191  return;
192 }
193 // Misc methods ------------------------------------
194 void
195 MpiComm::syncPrintDebugMsg(const char* msg, unsigned int msgVerbosity, unsigned int numUSecs) const
196 {
197  if (m_env.syncVerbosity() >= msgVerbosity) {
198  this->Barrier();
199  for (int i = 0; i < this->NumProc(); ++i) {
200  if (i == this->MyPID()) {
201  std::cout << msg
202  << ": fullRank " << m_env.fullRank()
203  << ", subEnvironment " << m_env.subId()
204  << ", subRank " << m_env.subRank()
205  << ", inter0Rank " << m_env.inter0Rank()
206  << std::endl;
207  }
208  usleep(numUSecs);
209  this->Barrier();
210  }
211  //if (this->fullRank() == 0) std::cout << "Sleeping " << numUSecs << " microseconds..."
212  // << std::endl;
213  //usleep(numUSecs);
214  this->Barrier();
215  }
216 
217  return;
218 }
219 // -------------------------------------------------
220 #ifdef QUESO_HAS_TRILINOS
221 const Epetra_MpiComm&
222 MpiComm::epetraMpiComm() const
223 {
224  return *m_epetraMpiComm;
225 }
226 #endif
227 
228 // Private methods----------------------------------
229 void
231 {
232 #ifdef QUESO_HAS_TRILINOS
233  delete m_epetraMpiComm;
234  m_epetraMpiComm = new Epetra_MpiComm(*src.m_epetraMpiComm);
235 #endif
236  m_rawComm = src.m_rawComm;
237  m_worldRank = src.m_worldRank;
238  m_myPid = src.m_myPid;
239  m_numProc = src.m_numProc;
240 
241  return;
242 }
243 // -------------------------------------------------
244 
245 } // End namespace QUESO
MPI_Comm RawType_MPI_Comm
Definition: MpiComm.h:38
int subRank() const
Access function for sub-rank.
Definition: Environment.C:241
int MyPID() const
Return my process ID.
Definition: MpiComm.C:94
MPI_Status RawType_MPI_Status
Definition: MpiComm.h:42
MPI_Op RawType_MPI_Op
Definition: MpiComm.h:41
RawType_MPI_Comm m_rawComm
Embedded wrapped opaque MPI_Comm object.
Definition: MpiComm.h:213
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:122
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:103
int fullRank() const
Returns the process full rank.
Definition: Environment.C:222
const BaseEnvironment & m_env
Definition: MpiComm.h:206
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:185
MPI_Datatype RawType_MPI_Datatype
Definition: MpiComm.h:40
unsigned int syncVerbosity() const
Access function to private attribute m_syncVerbosity.
Definition: Environment.C:403
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
The QUESO MPI Communicator Class.
Definition: MpiComm.h:75
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
int m_worldRank
World rank.
Definition: MpiComm.h:216
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:158
RawType_MPI_Comm Comm() const
Extract MPI Communicator from a MpiComm object.
Definition: MpiComm.C:85
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:193
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:133
MpiComm & operator=(const MpiComm &rhs)
Assignment operator.
Definition: MpiComm.C:77
MpiComm()
Default Constructor.
unsigned int subId() const
Access function to the number of each sub-environment Id: m_subId.
Definition: Environment.C:295
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:175
~MpiComm()
Destructor.
Definition: MpiComm.C:66
int m_myPid
Process ID of this process.
Definition: MpiComm.h:219
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:141
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:113
void syncPrintDebugMsg(const char *msg, unsigned int msgVerbosity, unsigned int numUSecs) const
Synchronizes all the processes and print debug message.
Definition: MpiComm.C:195
void copy(const MpiComm &src)
Copies from an existing MpiComm instance.
Definition: MpiComm.C:230

Generated on Thu Jun 11 2015 13:52:31 for queso-0.53.0 by  doxygen 1.8.5