1 #ifndef MHO_MPIInterface_HH__
2 #define MHO_MPIInterface_HH__
11 #define LOCAL_RANK_MPI
61 void Initialize(
int* argc,
char*** argv,
bool split_mode =
true);
222 template<
typename T > std::map< std::string, T >
MergeMap(
const std::map< std::string, T >& local_map)
225 std::vector< int > key_lengths;
226 std::string concatenated_keys;
227 std::vector< T > values;
229 for(
const auto& kv : local_map)
231 key_lengths.push_back(
static_cast< int >(kv.first.size()));
232 concatenated_keys += kv.first;
233 values.push_back(kv.second);
236 int local_entry_count =
static_cast< int >(local_map.size());
237 int local_char_count =
static_cast< int >(concatenated_keys.size());
240 MPI_Gather(&local_entry_count, 1, MPI_INT, entry_counts.data(), 1, MPI_INT, 0, MPI_COMM_WORLD);
244 MPI_Gather(&local_char_count, 1, MPI_INT, char_counts.data(), 1, MPI_INT, 0, MPI_COMM_WORLD);
246 std::map< std::string, T > merged;
257 entry_displs[i] = entry_displs[i - 1] + entry_counts[i - 1];
258 char_displs[i] = char_displs[i - 1] + char_counts[i - 1];
265 std::vector< int > all_key_lengths(total_entries);
266 std::vector< char > all_chars(total_chars);
267 std::vector< T > all_values(total_entries);
270 MPI_Gatherv(key_lengths.data(), local_entry_count, MPI_INT, all_key_lengths.data(), entry_counts.data(),
271 entry_displs.data(), MPI_INT, 0, MPI_COMM_WORLD);
274 MPI_Gatherv(concatenated_keys.data(), local_char_count, MPI_CHAR, all_chars.data(), char_counts.data(),
275 char_displs.data(), MPI_CHAR, 0, MPI_COMM_WORLD);
278 MPI_Gatherv(values.data(), local_entry_count, mpi_type_for< T >(), all_values.data(), entry_counts.data(),
279 entry_displs.data(), mpi_type_for< T >(), 0, MPI_COMM_WORLD);
283 for(
int i = 0; i < total_entries; ++i)
285 int len = all_key_lengths[i];
286 std::string key(all_chars.begin() + pos, all_chars.begin() + pos + len);
288 merged[key] = all_values[i];
294 MPI_Gatherv(key_lengths.data(), local_entry_count, MPI_INT,
nullptr,
nullptr,
nullptr, MPI_INT, 0,
297 MPI_Gatherv(concatenated_keys.data(), local_char_count, MPI_CHAR,
nullptr,
nullptr,
nullptr, MPI_CHAR, 0,
300 MPI_Gatherv(values.data(), local_entry_count, mpi_type_for< T >(),
nullptr,
nullptr,
nullptr,
301 mpi_type_for< T >(), 0, MPI_COMM_WORLD);
312 std::set< std::string >
MergeStringSet(
const std::set< std::string >& local_set);
interface functions for initialization of a MPI environment
Definition: MHO_MPIInterface.hh:43
MPI_Group fEvenGroup
Definition: MHO_MPIInterface.hh:327
bool fValidSplit
Definition: MHO_MPIInterface.hh:331
MPI_Group fOddGroup
Definition: MHO_MPIInterface.hh:328
int fNProcesses
Definition: MHO_MPIInterface.hh:319
MPI_Comm * GetSubGroupCommunicator()
Definition: MHO_MPIInterface.hh:197
bool fIsEvenGroupMember
Definition: MHO_MPIInterface.hh:332
std::string GetHostName() const
Getter for host name.
Definition: MHO_MPIInterface.hh:100
MPI_Group * GetEvenGroup()
Definition: MHO_MPIInterface.hh:209
void PrintMessage(std::string msg)
Collects and prints messages from all processes in a MPI parallel environment. when called,...
Definition: MHO_MPIInterface.cc:130
int GetNSubGroupProcesses()
Getter for nsub group processes.
Definition: MHO_MPIInterface.hh:164
MPI_Comm * GetEvenCommunicator()
Definition: MHO_MPIInterface.hh:213
int fPartnerProcessID
Definition: MHO_MPIInterface.hh:335
std::string fHostName
Definition: MHO_MPIInterface.hh:321
MPI_Status fStatus
Definition: MHO_MPIInterface.hh:340
bool IsEvenGroupMember()
Checks if the current process is a member of the even subgroup.
Definition: MHO_MPIInterface.hh:157
MPI_Group * GetSubGroup()
Getter for sub group.
Definition: MHO_MPIInterface.hh:185
void EndSequentialProcess()
Sends a flag to the next process and waits for all processes to finish.
Definition: MHO_MPIInterface.cc:120
int GetNProcesses() const
Getter for N processes.
Definition: MHO_MPIInterface.hh:86
MPI_Comm * GetOddCommunicator()
Definition: MHO_MPIInterface.hh:215
void BeginSequentialProcess()
Isolates a section of code for sequential processing by each process one at a time.
Definition: MHO_MPIInterface.cc:110
std::set< std::string > MergeStringSet(const std::set< std::string > &local_set)
merge a set of strings across all processes (collected on the root process 0)
Definition: MHO_MPIInterface.cc:484
int GetGlobalProcessID() const
Getter for global process id.
Definition: MHO_MPIInterface.hh:79
int fLocalProcessID
Definition: MHO_MPIInterface.hh:320
static MHO_MPIInterface * GetInstance()
Getter for instance.
Definition: MHO_MPIInterface.cc:104
int GetLocalProcessID() const
Getter for local process id.
Definition: MHO_MPIInterface.hh:93
bool IsSplitValid()
Checks if even/odd split is valid.
Definition: MHO_MPIInterface.hh:150
bool SplitMode()
Checks if processes are split into two groups based on even/odd ranks.
Definition: MHO_MPIInterface.hh:143
void BroadcastString(std::string &msg)
Broadcasts a string message to all processes from root/master process.
Definition: MHO_MPIInterface.cc:225
std::map< std::string, T > MergeMap(const std::map< std::string, T > &local_map)
merge a collection of maps across all processes, so that it is available for the root (0) process
Definition: MHO_MPIInterface.hh:222
int fSubGroupRank
Definition: MHO_MPIInterface.hh:333
MPI_Comm fEvenCommunicator
Definition: MHO_MPIInterface.hh:329
void GlobalBarrier() const
Waits for all processes in MPI_COMM_WORLD to reach this barrier.
Definition: MHO_MPIInterface.hh:116
virtual ~MHO_MPIInterface()
void Finalize()
Finalizes MPI by calling MPI_Finalize if not already finalized.
Definition: MHO_MPIInterface.cc:93
bool Check() const
Checks if global process ID is non-negative and number of processes is greater than zero.
Definition: MHO_MPIInterface.hh:72
int GetSubGroupRank()
Getter for sub group rank.
Definition: MHO_MPIInterface.hh:171
MHO_MPIInterface()
Definition: MHO_MPIInterface.cc:34
void Initialize(int *argc, char ***argv, bool split_mode=true)
Initializes MPI environment and sets up process groups/communicators.
Definition: MHO_MPIInterface.cc:47
MPI_Group * GetOddGroup()
Definition: MHO_MPIInterface.hh:211
std::vector< int > fCoHostedProcessIDs
Definition: MHO_MPIInterface.hh:322
bool fSplitMode
Definition: MHO_MPIInterface.hh:326
int fGlobalProcessID
Definition: MHO_MPIInterface.hh:318
int GetPartnerProcessID()
Getter for partner process id.
Definition: MHO_MPIInterface.hh:178
void SetupSubGroups()
Definition: MHO_MPIInterface.cc:368
MPI_Comm fOddCommunicator
Definition: MHO_MPIInterface.hh:330
void DetermineLocalRank()
Definition: MHO_MPIInterface.cc:248
int fNSubGroupProcesses
Definition: MHO_MPIInterface.hh:334
void msg(const char *string, int level,...)
Definition: msg.c:25
Definition: MHO_AdhocFlagging.hh:18
MPI_Datatype mpi_type_for()
MPI_Datatype mpi_type_for< float >()
Definition: MHO_MPIInterface.hh:29
MPI_Datatype mpi_type_for< int >()
Definition: MHO_MPIInterface.hh:19
MPI_Datatype mpi_type_for< double >()
Definition: MHO_MPIInterface.hh:24