File indexing completed on 2025-01-18 09:58:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 using std::to_string;
0032
0033
0034 template <typename HT>
0035 inline
0036 G4bool G4MPIToolsManager::Send(
0037 G4int nofActiveT, const std::vector<std::pair<HT*, G4HnInformation*>>& hnVector)
0038 {
0039 auto result = true;
0040
0041
0042
0043 fHmpi->beg_send(nofActiveT);
0044
0045
0046 for (const auto& [ht, info] : hnVector) {
0047
0048
0049 if ( ( fState.GetIsActivation() && ( ! info->GetActivation() ) ) ||
0050 ( info->GetDeleted() ) ) continue;
0051
0052
0053 result &= fHmpi->pack(*ht);
0054 }
0055
0056
0057 if ( ! fHmpi->send(fHmpi->rank()) ) {
0058 G4Analysis::Warn(
0059 "Rank: " + to_string(fHmpi->rank()) + " : can't send histos.",
0060 fkClass, "Send");
0061 return false;
0062 }
0063
0064 return result;
0065 }
0066
0067
0068 template <typename HT>
0069 inline
0070 G4bool G4MPIToolsManager::Receive(
0071 G4int nofActiveT, const std::vector<std::pair<HT*, G4HnInformation*>>& hnVector)
0072 {
0073 G4int commSize;
0074 G4bool result = fHmpi->comm_size(commSize);
0075 if ( ! result ) {
0076 G4Analysis::Warn(
0077 "Failed to get MPI commander size.\nMerging will not be performed.",
0078 fkClass, "Receive");
0079 return false;
0080 }
0081
0082
0083 for (G4int srank = 0; srank < commSize; ++srank) {
0084
0085
0086 if ( srank == fHmpi->rank() ) continue;
0087
0088
0089
0090 using class_pointer = std::pair<std::string,void*>;
0091 std::vector<class_pointer> hs;
0092 if ( ! fHmpi->wait_histos(srank, hs) ) {
0093 G4Analysis::Warn(
0094 "Wait_histos from " + to_string(srank) + " : failed.",
0095 fkClass, "Receive");
0096 return false;
0097 }
0098
0099
0100 if ( G4int(hs.size()) != nofActiveT ) {
0101 G4Analysis::Warn(
0102 "srank: " + to_string(srank) + " : got " + to_string(hs.size()) +
0103 " objects, while " + to_string(nofActiveT) +" were expected.",
0104 fkClass, "Receive");
0105 return false;
0106 }
0107
0108
0109 G4int counter = 0;
0110 for (const auto& [ht, info] : hnVector) {
0111
0112 if ( ( fState.GetIsActivation() && ( ! info->GetActivation() ) ) ) continue;
0113
0114 auto newHt = static_cast<HT*>(hs[counter++].second);
0115 ht->add(*newHt);
0116 }
0117 }
0118 return true;
0119 }
0120
0121
0122
0123 template <typename HT>
0124 inline
0125 G4bool G4MPIToolsManager::Merge(
0126 const std::vector<std::pair<HT*, G4HnInformation*>>& hnVector)
0127 {
0128 if ( hnVector.empty() ) return true;
0129
0130
0131 G4int nofActiveT = 0;
0132 if ( fState.GetIsActivation() ) {
0133
0134 for (const auto& htn : hnVector) {
0135 auto activation = htn.second->GetActivation();
0136 if ( activation ) ++nofActiveT;
0137 }
0138 } else {
0139 nofActiveT = G4int(hnVector.size());
0140 }
0141
0142 if ( ! nofActiveT ) return true;
0143
0144 G4int commRank;
0145 if ( ! fHmpi->comm_rank(commRank) ) {
0146 G4Analysis::Warn(
0147 "Failed to get MPI commander rank.\nMerging will not be performed.",
0148 fkClass, "Merge");
0149 return false;
0150 }
0151
0152 auto result = true;
0153
0154 if ( commRank != fHmpi->rank() ) {
0155 fState.Message(G4Analysis::kVL3, "mpi send", "Hn|Pn",
0156 "on rank " + to_string(commRank) +
0157 " destination rank: " + to_string(fHmpi->rank()));
0158
0159 result &= Send(nofActiveT, hnVector);
0160
0161 fState.Message(G4Analysis::kVL1, "mpi send", "Hn|Pn",
0162 "on rank " + to_string(commRank) +
0163 " destination rank: " + to_string(fHmpi->rank()));
0164
0165 } else {
0166 fState.Message(G4Analysis::kVL3, "mpi wait_histos", "Hn|Pn",
0167 "on rank " + to_string(commRank) +
0168 " destination rank: " + to_string(fHmpi->rank()));
0169
0170 result &= Receive(nofActiveT, hnVector);
0171
0172 fState.Message(G4Analysis::kVL1, "mpi wait_histos", "Hn|Pn",
0173 "on rank " + to_string(commRank) +
0174 " destination rank: " + to_string(fHmpi->rank()));
0175 }
0176 return result;
0177 }