Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4MPIToolsManager.hh
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 // $Id$
27 
28 // The manager class for MPI applications.
29 
30 // Author: Ivana Hrivnacova, 25/06/2015 (ivana@ipno.in2p3.fr)
31 
32 #ifndef G4MPIToolsManager_h
33 #define G4MPIToolsManager_h 1
34 
36 #include "G4HnInformation.hh"
37 #include "G4ios.hh"
38 
39 #include <tools/impi_world>
40 #include <tools/histo/hmpi>
41 
42 #include <vector>
43 
45 {
46  public:
48  tools::histo::hmpi* hmpi)
49  : fState(state), fHmpi(hmpi) {}
50  virtual ~G4MPIToolsManager() {}
51 
52  public:
53  // methods
54  template <typename T>
55  G4bool Merge(const std::vector<T*>& htVector,
56  const std::vector<G4HnInformation*>& hnVector);
57  private:
58  // methods
59  template <typename T>
60  G4bool Send(G4int nofActiveT,
61  const std::vector<T*>& htVector,
62  const std::vector<G4HnInformation*>& hnVector);
63 
64  template <typename T>
65  G4bool Receive(G4int nofActiveT,
66  const std::vector<T*>& htVector,
67  const std::vector<G4HnInformation*>& hnVector);
68 
69  // data members
70  const G4AnalysisManagerState& fState;
71  tools::histo::hmpi* fHmpi;
72 };
73 
74 // inline functions
75 
76 //_____________________________________________________________________________
77 template <typename T>
78 G4bool G4MPIToolsManager::Send(G4int nofActiveT,
79  const std::vector<T*>& htVector,
80  const std::vector<G4HnInformation*>& hnVector)
81 {
82  G4bool finalResult = true;
83 
84  // send object to destination rank
85  // G4cout << "Begin send for " << nofActiveT << G4endl;
86  fHmpi->beg_send(nofActiveT);
87 
88  // pack objects
89  for ( G4int i=0; i<G4int(htVector.size()); ++i ) {
90  // skip sending if activation is enabled and HT is inactivated
91  auto info = hnVector[i];
92  if ( ( fState.GetIsActivation() && ( ! info->GetActivation() ) ) ) continue;
93  // pack histogram for sending
94  // G4cout << "Packed " << i << "th T" << G4endl;
95  auto ht = htVector[i];
96  auto result = fHmpi->pack(*ht);
97  finalResult = result && finalResult;
98  }
99 
100  //G4cout << "Go to send all " << G4endl;
101  if ( ! fHmpi->send(fHmpi->rank()) ) {
102  G4ExceptionDescription description;
103  description << " Rank: " << fHmpi->rank() << " : can't send histos.";
104  G4Exception("G4H1ToolsManager::Receieve",
105  "Analysis_W031", JustWarning, description);
106  return false;
107  }
108 
109  return finalResult;
110 }
111 
112 //_____________________________________________________________________________
113 template <typename T>
114 G4bool G4MPIToolsManager::Receive(G4int nofActiveT,
115  const std::vector<T*>& htVector,
116  const std::vector<G4HnInformation*>& hnVector)
117 {
118  G4int commSize;
119  G4bool result = fHmpi->comm_size(commSize);
120  if ( ! result ) {
121  G4ExceptionDescription description;
122  description
123  << " Failed to get MPI commander size." << G4endl
124  << " Merging will not be performed.";
125  G4Exception("G4H1ToolsManager::Merge",
126  "Analysis_W031", JustWarning, description);
127  return false;
128  }
129 
130  // get objects from source ranks
131  for (G4int srank = 0; srank < commSize; ++srank) {
132 
133  // skip destination rank
134  if ( srank == fHmpi->rank() ) continue;
135 
136  // get objects from this source rank
137  //G4cout << "Go to wait_histos " << rank << G4endl;
138  using class_pointer = std::pair<std::string,void*>;
139  std::vector<class_pointer> hs;
140  if ( ! fHmpi->wait_histos(srank, hs) ) {
141  G4ExceptionDescription description;
142  description << " wait_histos from " << srank << " : failed.";
143  G4Exception("G4H1ToolsManager::Receieve",
144  "Analysis_W031", JustWarning, description);
145  return false;
146  }
147 
148  // check that we got the right number of objects
149  if ( G4int(hs.size()) != nofActiveT ) {
150  G4ExceptionDescription description;
151  description << " srank: " << srank << " : got " << hs.size() << " objects, "
152  << "while " << nofActiveT << " were exepected." << G4endl;
153  G4Exception("G4H1ToolsManager::Receieve",
154  "Analysis_W031", JustWarning, description);
155  return false;
156  }
157 
158  // merge the objects to destination rank
159  G4int counter = 0;
160  for ( G4int i=0; i<G4int(htVector.size()); ++i ) {
161  // skip sending if activation is enabled and HT is inactivated
162  auto info = hnVector[i];
163  if ( ( fState.GetIsActivation() && ( ! info->GetActivation() ) ) ) continue;
164  // merge histograms
165  auto ht = htVector[i];
166  auto newHt = static_cast<T*>(hs[counter++].second);
167  ht->add(*newHt);
168  }
169  }
170  return true;
171 }
172 
173 
174 //_____________________________________________________________________________
175 template <typename T>
176 inline G4bool G4MPIToolsManager::Merge(const std::vector<T*>& htVector,
177  const std::vector<G4HnInformation*>& hnVector)
178 {
179  if ( ! htVector.size() ) return true;
180 
181  // Get number of objects to be sent
182  G4int nofActiveT = 0;
183  if ( fState.GetIsActivation() ) {
184  // only activated histograms will be treated
185  for ( G4int i=0; i<G4int(htVector.size()); ++i ) {
186  auto activation = hnVector[i]->GetActivation();
187  if ( activation ) ++nofActiveT;
188  }
189  } else {
190  nofActiveT = G4int(htVector.size());
191  }
192 
193  if ( ! nofActiveT ) return true;
194 
195  G4int commRank;
196  if ( ! fHmpi->comm_rank(commRank) ) {
197  G4ExceptionDescription description;
198  description
199  << " Failed to get MPI commander rank." << G4endl
200  << " Merging will not be performed.";
201  G4Exception("G4H1ToolsManager::Merge",
202  "Analysis_W031", JustWarning, description);
203  return false;
204  }
205 
206  G4bool finalResult = true;
207 
208  if ( commRank != fHmpi->rank() ) {
209 
210 #ifdef G4VERBOSE
211  if ( fState.GetVerboseL3() ) {
212  G4ExceptionDescription description;
213  description << "on rank " << commRank
214  << " destination rank: " << fHmpi->rank();
215  fState.GetVerboseL4()->Message("mpi send", "Hn|Pn", description);
216  }
217 #endif
218 
219  auto result = Send(nofActiveT, htVector, hnVector);
220 
221  finalResult = result && finalResult;
222 
223 #ifdef G4VERBOSE
224  if ( fState.GetVerboseL1() ) {
225  G4ExceptionDescription description;
226  description << "on rank " << commRank
227  << " destination rank: " << fHmpi->rank();
228  fState.GetVerboseL1()->Message("send", "Hn|Pn", description);
229  }
230 #endif
231 
232  } else {
233 
234 #ifdef G4VERBOSE
235  if ( fState.GetVerboseL3() ) {
236  G4ExceptionDescription description;
237  description << "on rank " << commRank
238  << " destination rank: " << fHmpi->rank();
239  fState.GetVerboseL4()->Message("mpi wait_histos", "Hn|Pn", description);
240  }
241 #endif
242 
243  auto result = Receive(nofActiveT, htVector, hnVector);
244 
245  finalResult = result && finalResult;
246 
247 #ifdef G4VERBOSE
248  if ( fState.GetVerboseL1() ) {
249  G4ExceptionDescription description;
250  description << "on rank " << commRank
251  << " destination rank: " << fHmpi->rank();
252  fState.GetVerboseL1()->Message("mpi wait_histos", "Hn|Pn", description);
253  }
254 #endif
255  }
256  return finalResult;
257 }
258 
259 #endif
G4double G4ParticleHPJENDLHEData::G4double result
const XML_Char XML_Encoding * info
Definition: expat.h:530
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
int G4int
Definition: G4Types.hh:78
const G4AnalysisVerbose * GetVerboseL3() const
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
virtual ~G4MPIToolsManager()
G4MPIToolsManager(const G4AnalysisManagerState &state, tools::histo::hmpi *hmpi)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
#define G4endl
Definition: G4ios.hh:61
const G4AnalysisVerbose * GetVerboseL1() const
G4bool Merge(const std::vector< T * > &htVector, const std::vector< G4HnInformation * > &hnVector)