HOPS
HOPS class reference
MHO_MPIInterface.hh
Go to the documentation of this file.
1 #ifndef MHO_MPIInterface_HH__
2 #define MHO_MPIInterface_HH__
3 
4 #include "mpi.h"
5 
6 #include "MHO_Message.hh"
7 #include <string>
8 #include <vector>
9 
10 #define LOCAL_RANK_MPI
11 
12 namespace hops
13 {
14 
24 {
25  public:
26  //singleton interface
33  static MHO_MPIInterface* GetInstance();
34 
42  void Initialize(int* argc, char*** argv, bool split_mode = true);
46  void Finalize();
47 
53  bool Check() const { return (fGlobalProcessID >= 0) && (fNProcesses > 0); }
54 
60  int GetGlobalProcessID() const { return fGlobalProcessID; }
61 
67  int GetNProcesses() const { return fNProcesses; }
68 
74  int GetLocalProcessID() const { return fLocalProcessID; }
75 
81  std::string GetHostName() const { return fHostName; };
82 
83  //use to isolate a section of code, so each process completes it
84  //one at a time
92  void EndSequentialProcess();
93 
97  void GlobalBarrier() const { MPI_Barrier(MPI_COMM_WORLD); }
98 
99 
108  void PrintMessage(std::string msg);
109 
110  //broadcast a string message to all processes
116  void BroadcastString(std::string& msg);
117 
118  //routines to be used by programs which split the processes into two
119  //groups bases on even/odd local process rank
125  bool SplitMode() { return fSplitMode; };
126 
132  bool IsSplitValid() { return fValidSplit; };
133 
140 
147 
153  int GetSubGroupRank() { return fSubGroupRank; };
154 
161 
167  MPI_Group* GetSubGroup()
168  {
170  {
171  return &fEvenGroup;
172  }
173  else
174  {
175  return &fOddGroup;
176  };
177  }
178 
180  {
182  {
183  return &fEvenCommunicator;
184  }
185  else
186  {
187  return &fOddCommunicator;
188  };
189  }
190 
191  MPI_Group* GetEvenGroup() { return &fEvenGroup; };
192 
193  MPI_Group* GetOddGroup() { return &fOddGroup; };
194 
195  MPI_Comm* GetEvenCommunicator() { return &fEvenCommunicator; };
196 
197  MPI_Comm* GetOddCommunicator() { return &fOddCommunicator; };
198 
199  protected:
201  virtual ~MHO_MPIInterface();
202 
204 
208  std::string fHostName;
209  std::vector< int > fCoHostedProcessIDs;
210 
211  //groups and communicators for splitting processes into
212  //two sets, based on whether they have even/odd (local) ranks
214  MPI_Group fEvenGroup; //even process subgroup
215  MPI_Group fOddGroup; //odd process subgroup
216  MPI_Comm fEvenCommunicator; //comm for even group
217  MPI_Comm fOddCommunicator; //comm for odd group
218  bool fValidSplit; //true if the size of the subgroups is equal
219  bool fIsEvenGroupMember; //true if this process is a member of the even subgroup
220  int fSubGroupRank; //rank of this process in its subgroup
221  int fNSubGroupProcesses; //number of processes in the subgroup this process belongs to
222  int fPartnerProcessID; //global rank of partner process in other subgroup
223 
224  void DetermineLocalRank();
225  void SetupSubGroups();
226 
227  MPI_Status fStatus;
228 };
229 
230 } //end of namespace hops
231 
232 #endif
interface functions for initialization of a MPI environment
Definition: MHO_MPIInterface.hh:24
MPI_Group fEvenGroup
Definition: MHO_MPIInterface.hh:214
bool fValidSplit
Definition: MHO_MPIInterface.hh:218
MPI_Group fOddGroup
Definition: MHO_MPIInterface.hh:215
int fNProcesses
Definition: MHO_MPIInterface.hh:206
MPI_Comm * GetSubGroupCommunicator()
Definition: MHO_MPIInterface.hh:179
bool fIsEvenGroupMember
Definition: MHO_MPIInterface.hh:219
std::string GetHostName() const
Getter for host name.
Definition: MHO_MPIInterface.hh:81
MPI_Group * GetEvenGroup()
Definition: MHO_MPIInterface.hh:191
void PrintMessage(std::string msg)
Collects and prints messages from all processes in a MPI parallel environment. when called,...
Definition: MHO_MPIInterface.cc:136
int GetNSubGroupProcesses()
Getter for nsub group processes.
Definition: MHO_MPIInterface.hh:146
MPI_Comm * GetEvenCommunicator()
Definition: MHO_MPIInterface.hh:195
int fPartnerProcessID
Definition: MHO_MPIInterface.hh:222
std::string fHostName
Definition: MHO_MPIInterface.hh:208
MPI_Status fStatus
Definition: MHO_MPIInterface.hh:227
bool IsEvenGroupMember()
Checks if the current process is a member of the even subgroup.
Definition: MHO_MPIInterface.hh:139
MPI_Group * GetSubGroup()
Getter for sub group.
Definition: MHO_MPIInterface.hh:167
void EndSequentialProcess()
Sends a flag to the next process and waits for all processes to finish.
Definition: MHO_MPIInterface.cc:126
int GetNProcesses() const
Getter for N processes.
Definition: MHO_MPIInterface.hh:67
MPI_Comm * GetOddCommunicator()
Definition: MHO_MPIInterface.hh:197
void BeginSequentialProcess()
Isolates a section of code for sequential processing by each process one at a time.
Definition: MHO_MPIInterface.cc:116
int GetGlobalProcessID() const
Getter for global process id.
Definition: MHO_MPIInterface.hh:60
int fLocalProcessID
Definition: MHO_MPIInterface.hh:207
static MHO_MPIInterface * GetInstance()
Getter for instance.
Definition: MHO_MPIInterface.cc:106
int GetLocalProcessID() const
Getter for local process id.
Definition: MHO_MPIInterface.hh:74
bool IsSplitValid()
Checks if even/odd split is valid.
Definition: MHO_MPIInterface.hh:132
bool SplitMode()
Checks if processes are split into two groups based on even/odd ranks.
Definition: MHO_MPIInterface.hh:125
void BroadcastString(std::string &msg)
Broadcasts a string message to all processes from root/master process.
Definition: MHO_MPIInterface.cc:231
int fSubGroupRank
Definition: MHO_MPIInterface.hh:220
MPI_Comm fEvenCommunicator
Definition: MHO_MPIInterface.hh:216
void GlobalBarrier() const
Waits for all processes in MPI_COMM_WORLD to reach this barrier.
Definition: MHO_MPIInterface.hh:97
void Finalize()
Finalizes MPI by calling MPI_Finalize if not already finalized.
Definition: MHO_MPIInterface.cc:95
bool Check() const
Checks if global process ID is non-negative and number of processes is greater than zero.
Definition: MHO_MPIInterface.hh:53
int GetSubGroupRank()
Getter for sub group rank.
Definition: MHO_MPIInterface.hh:153
MHO_MPIInterface()
Definition: MHO_MPIInterface.cc:36
void Initialize(int *argc, char ***argv, bool split_mode=true)
Initializes MPI environment and sets up process groups/communicators.
Definition: MHO_MPIInterface.cc:49
MPI_Group * GetOddGroup()
Definition: MHO_MPIInterface.hh:193
std::vector< int > fCoHostedProcessIDs
Definition: MHO_MPIInterface.hh:209
bool fSplitMode
Definition: MHO_MPIInterface.hh:213
int fGlobalProcessID
Definition: MHO_MPIInterface.hh:205
int GetPartnerProcessID()
Getter for partner process id.
Definition: MHO_MPIInterface.hh:160
void SetupSubGroups()
Definition: MHO_MPIInterface.cc:374
MPI_Comm fOddCommunicator
Definition: MHO_MPIInterface.hh:217
static MHO_MPIInterface * fMPIInterface
Definition: MHO_MPIInterface.hh:203
void DetermineLocalRank()
Definition: MHO_MPIInterface.cc:254
int fNSubGroupProcesses
Definition: MHO_MPIInterface.hh:221
void msg(const char *string, int level,...)
Definition: msg.c:25
Definition: MHO_ChannelLabeler.hh:17