1 #ifndef MHO_MPIInterface_HH__
2 #define MHO_MPIInterface_HH__
10 #define LOCAL_RANK_MPI
60 void Initialize(
int* argc,
char*** argv,
bool split_mode =
true);
221 template<
typename T > std::map< std::string, T >
MergeMap(
const std::map< std::string, T >& local_map)
224 std::vector< int > key_lengths;
225 std::string concatenated_keys;
226 std::vector< T > values;
228 for(
const auto& kv : local_map)
230 key_lengths.push_back(
static_cast< int >(kv.first.size()));
231 concatenated_keys += kv.first;
232 values.push_back(kv.second);
235 int local_entry_count =
static_cast< int >(local_map.size());
236 int local_char_count =
static_cast< int >(concatenated_keys.size());
239 MPI_Gather(&local_entry_count, 1, MPI_INT, entry_counts.data(), 1, MPI_INT, 0, MPI_COMM_WORLD);
243 MPI_Gather(&local_char_count, 1, MPI_INT, char_counts.data(), 1, MPI_INT, 0, MPI_COMM_WORLD);
245 std::map< std::string, T > merged;
256 entry_displs[i] = entry_displs[i - 1] + entry_counts[i - 1];
257 char_displs[i] = char_displs[i - 1] + char_counts[i - 1];
264 std::vector< int > all_key_lengths(total_entries);
265 std::vector< char > all_chars(total_chars);
266 std::vector< T > all_values(total_entries);
269 MPI_Gatherv(key_lengths.data(), local_entry_count, MPI_INT, all_key_lengths.data(), entry_counts.data(),
270 entry_displs.data(), MPI_INT, 0, MPI_COMM_WORLD);
273 MPI_Gatherv(concatenated_keys.data(), local_char_count, MPI_CHAR, all_chars.data(), char_counts.data(),
274 char_displs.data(), MPI_CHAR, 0, MPI_COMM_WORLD);
277 MPI_Gatherv(values.data(), local_entry_count, mpi_type_for< T >(), all_values.data(), entry_counts.data(),
278 entry_displs.data(), mpi_type_for< T >(), 0, MPI_COMM_WORLD);
282 for(
int i = 0; i < total_entries; ++i)
284 int len = all_key_lengths[i];
285 std::string key(all_chars.begin() + pos, all_chars.begin() + pos + len);
287 merged[key] = all_values[i];
293 MPI_Gatherv(key_lengths.data(), local_entry_count, MPI_INT,
nullptr,
nullptr,
nullptr, MPI_INT, 0,
296 MPI_Gatherv(concatenated_keys.data(), local_char_count, MPI_CHAR,
nullptr,
nullptr,
nullptr, MPI_CHAR, 0,
299 MPI_Gatherv(values.data(), local_entry_count, mpi_type_for< T >(),
nullptr,
nullptr,
nullptr,
300 mpi_type_for< T >(), 0, MPI_COMM_WORLD);
311 std::set< std::string >
MergeStringSet(
const std::set< std::string >& local_set);
interface functions for initialization of a MPI environment
Definition: MHO_MPIInterface.hh:42
MPI_Group fEvenGroup
Definition: MHO_MPIInterface.hh:326
bool fValidSplit
Definition: MHO_MPIInterface.hh:330
MPI_Group fOddGroup
Definition: MHO_MPIInterface.hh:327
int fNProcesses
Definition: MHO_MPIInterface.hh:318
MPI_Comm * GetSubGroupCommunicator()
Definition: MHO_MPIInterface.hh:196
bool fIsEvenGroupMember
Definition: MHO_MPIInterface.hh:331
std::string GetHostName() const
Getter for host name.
Definition: MHO_MPIInterface.hh:99
MPI_Group * GetEvenGroup()
Definition: MHO_MPIInterface.hh:208
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:163
MPI_Comm * GetEvenCommunicator()
Definition: MHO_MPIInterface.hh:212
int fPartnerProcessID
Definition: MHO_MPIInterface.hh:334
std::string fHostName
Definition: MHO_MPIInterface.hh:320
MPI_Status fStatus
Definition: MHO_MPIInterface.hh:339
bool IsEvenGroupMember()
Checks if the current process is a member of the even subgroup.
Definition: MHO_MPIInterface.hh:156
MPI_Group * GetSubGroup()
Getter for sub group.
Definition: MHO_MPIInterface.hh:184
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:85
MPI_Comm * GetOddCommunicator()
Definition: MHO_MPIInterface.hh:214
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:78
int fLocalProcessID
Definition: MHO_MPIInterface.hh:319
static MHO_MPIInterface * GetInstance()
Getter for instance.
Definition: MHO_MPIInterface.cc:104
int GetLocalProcessID() const
Getter for local process id.
Definition: MHO_MPIInterface.hh:92
bool IsSplitValid()
Checks if even/odd split is valid.
Definition: MHO_MPIInterface.hh:149
bool SplitMode()
Checks if processes are split into two groups based on even/odd ranks.
Definition: MHO_MPIInterface.hh:142
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:221
int fSubGroupRank
Definition: MHO_MPIInterface.hh:332
MPI_Comm fEvenCommunicator
Definition: MHO_MPIInterface.hh:328
void GlobalBarrier() const
Waits for all processes in MPI_COMM_WORLD to reach this barrier.
Definition: MHO_MPIInterface.hh:115
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:71
int GetSubGroupRank()
Getter for sub group rank.
Definition: MHO_MPIInterface.hh:170
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:210
std::vector< int > fCoHostedProcessIDs
Definition: MHO_MPIInterface.hh:321
bool fSplitMode
Definition: MHO_MPIInterface.hh:325
int fGlobalProcessID
Definition: MHO_MPIInterface.hh:317
int GetPartnerProcessID()
Getter for partner process id.
Definition: MHO_MPIInterface.hh:177
void SetupSubGroups()
Definition: MHO_MPIInterface.cc:368
MPI_Comm fOddCommunicator
Definition: MHO_MPIInterface.hh:329
void DetermineLocalRank()
Definition: MHO_MPIInterface.cc:248
int fNSubGroupProcesses
Definition: MHO_MPIInterface.hh:333
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:28
MPI_Datatype mpi_type_for< int >()
Definition: MHO_MPIInterface.hh:18
MPI_Datatype mpi_type_for< double >()
Definition: MHO_MPIInterface.hh:23