Geant4-11
G4MPIToolsManager.icc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27// The manager class for MPI applications.
28
29// Author: Ivana Hrivnacova, 25/06/2015 (ivana@ipno.in2p3.fr)
30
31using std::to_string;
32
33//_____________________________________________________________________________
34template <typename HT>
35inline
36G4bool G4MPIToolsManager::Send(G4int nofActiveT,
37 const std::vector<HT*>& htVector,
38 const std::vector<G4HnInformation*>& hnVector)
39{
40 auto result = true;
41
42 // send object to destination rank
43 // G4cout << "Begin send for " << nofActiveT << G4endl;
44 fHmpi->beg_send(nofActiveT);
45
46 // pack objects
47 for ( G4int i=0; i<G4int(htVector.size()); ++i ) {
48 // skip sending if activation is enabled and HT is inactivated
49 auto info = hnVector[i];
50 if ( ( fState.GetIsActivation() && ( ! info->GetActivation() ) ) ) continue;
51 // pack histogram for sending
52 // G4cout << "Packed " << i << "th T" << G4endl;
53 auto ht = htVector[i];
54 result &= fHmpi->pack(*ht);
55 }
56
57 //G4cout << "Go to send all " << G4endl;
58 if ( ! fHmpi->send(fHmpi->rank()) ) {
59 G4Analysis::Warn(
60 "Rank: " + to_string(fHmpi->rank()) + " : can't send histos.",
61 fkClass, "Send");
62 return false;
63 }
64
65 return result;
66}
67
68//_____________________________________________________________________________
69template <typename HT>
70inline
71G4bool G4MPIToolsManager::Receive(G4int nofActiveT,
72 const std::vector<HT*>& htVector,
73 const std::vector<G4HnInformation*>& hnVector)
74{
75 G4int commSize;
76 G4bool result = fHmpi->comm_size(commSize);
77 if ( ! result ) {
78 G4Analysis::Warn(
79 "Failed to get MPI commander size.\nMerging will not be performed.",
80 fkClass, "Receive");
81 return false;
82 }
83
84 // get objects from source ranks
85 for (G4int srank = 0; srank < commSize; ++srank) {
86
87 // skip destination rank
88 if ( srank == fHmpi->rank() ) continue;
89
90 // get objects from this source rank
91 // G4cout << "Go to wait_histos " << srank << G4endl;
92 using class_pointer = std::pair<std::string,void*>;
93 std::vector<class_pointer> hs;
94 if ( ! fHmpi->wait_histos(srank, hs) ) {
95 G4Analysis::Warn(
96 "Wait_histos from " + to_string(srank) + " : failed.",
97 fkClass, "Receive");
98 return false;
99 }
100
101 // check that we got the right number of objects
102 if ( G4int(hs.size()) != nofActiveT ) {
103 G4Analysis::Warn(
104 "srank: " + to_string(srank) + " : got " + to_string(hs.size()) +
105 " objects, while " + to_string(nofActiveT) +" were expected.",
106 fkClass, "Receive");
107 return false;
108 }
109
110 // merge the objects to destination rank
111 G4int counter = 0;
112 for ( G4int i=0; i<G4int(htVector.size()); ++i ) {
113 // skip sending if activation is enabled and HT is inactivated
114 auto info = hnVector[i];
115 if ( ( fState.GetIsActivation() && ( ! info->GetActivation() ) ) ) continue;
116 // merge histograms
117 auto ht = htVector[i];
118 auto newHt = static_cast<HT*>(hs[counter++].second);
119 ht->add(*newHt);
120 }
121 }
122 return true;
123}
124
125
126//_____________________________________________________________________________
127template <typename HT>
128inline
129G4bool G4MPIToolsManager::Merge(const std::vector<HT*>& htVector,
130 const std::vector<G4HnInformation*>& hnVector)
131{
132 if ( ! htVector.size() ) return true;
133
134 // Get number of objects to be sent
135 G4int nofActiveT = 0;
136 if ( fState.GetIsActivation() ) {
137 // only activated histograms will be treated
138 for ( G4int i=0; i<G4int(htVector.size()); ++i ) {
139 auto activation = hnVector[i]->GetActivation();
140 if ( activation ) ++nofActiveT;
141 }
142 } else {
143 nofActiveT = G4int(htVector.size());
144 }
145
146 if ( ! nofActiveT ) return true;
147
148 G4int commRank;
149 if ( ! fHmpi->comm_rank(commRank) ) {
150 G4Analysis::Warn(
151 "Failed to get MPI commander rank.\nMerging will not be performed.",
152 fkClass, "Merge");
153 return false;
154 }
155
156 auto result = true;
157
158 if ( commRank != fHmpi->rank() ) {
159 fState.Message(G4Analysis::kVL3, "mpi send", "Hn|Pn",
160 "on rank " + to_string(commRank) +
161 " destination rank: " + to_string(fHmpi->rank()));
162
163 result &= Send(nofActiveT, htVector, hnVector);
164
165 fState.Message(G4Analysis::kVL1, "mpi send", "Hn|Pn",
166 "on rank " + to_string(commRank) +
167 " destination rank: " + to_string(fHmpi->rank()));
168
169 } else {
170 fState.Message(G4Analysis::kVL3, "mpi wait_histos", "Hn|Pn",
171 "on rank " + to_string(commRank) +
172 " destination rank: " + to_string(fHmpi->rank()));
173
174 result &= Receive(nofActiveT, htVector, hnVector);
175
176 fState.Message(G4Analysis::kVL1, "mpi wait_histos", "Hn|Pn",
177 "on rank " + to_string(commRank) +
178 " destination rank: " + to_string(fHmpi->rank()));
179 }
180 return result;
181}