ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootMpiAnalysisManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4RootMpiAnalysisManager.cc
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 // Author: Ivana Hrivnacova, 21/11/2018 (ivana@ipno.in2p3.fr)
28 
32 
33 #include <tools/impi>
34 
35 //_____________________________________________________________________________
37  : G4RootAnalysisManager(isMaster),
38  fMpiNtupleMergeMode(G4MpiNtupleMergeMode::kNone),
39  fMpiSlaveNtupleManager(nullptr)
40 {}
41 
42 //_____________________________________________________________________________
44 {}
45 
46 //
47 // private methods
48 //
49 
50 //_____________________________________________________________________________
52  tools::impi* impi, G4int mpiRank, G4int mpiSize)
53 {
54 #ifdef G4VERBOSE
55  if ( fState.GetVerboseL4() )
56  fState.GetVerboseL4()->Message("create", "mpi ntuple managers", "");
57 #endif
58 
59  switch ( fMpiNtupleMergeMode )
60  {
61  case G4MpiNtupleMergeMode::kNone:
66  break;
67 
68  case G4MpiNtupleMergeMode::kMain: {
71  impi, mpiSize);
74  break;
75  }
76 
77  case G4MpiNtupleMergeMode::kSlave: {
78  auto destinationRank = mpiSize;
80  = new G4RootMpiPNtupleManager(fState, impi, mpiRank, destinationRank);
82  break;
83  }
84  }
85 
86 #ifdef G4VERBOSE
87  if ( fState.GetVerboseL3() )
88  fState.GetVerboseL3()->Message("create", "mpi ntuple managers", "");
89 #endif
90 }
91 
92 //_____________________________________________________________________________
94  G4int mpiRank, G4int mpiSize,
95  G4int nofNtupleFiles)
96 {
97 #ifdef G4VERBOSE
98  if ( fState.GetVerboseL1() )
100  ->Message("set", "mpi ntuple merging mode", "");
101 #endif
102 
103  auto canMerge = true;
104 
105  // Illegal situations
106  if ( mpiSize < 2 ) {
107  G4ExceptionDescription description;
108  description
109  << " " << "Merging ntuples is not applicable on a single rank."
110  << G4endl
111  << " " << "Setting was ignored.";
112  G4Exception("G4RootMpiAnalysisManager::SetMpiNtupleMergingMode()",
113  "Analysis_W013", JustWarning, description);
114  canMerge = false;
115  }
116 
117  G4String mergingMode;
118  if ( ! canMerge ) {
119  fMpiNtupleMergeMode = G4MpiNtupleMergeMode::kNone;
120  mergingMode = "G4MpiNtupleMergeMode::kNone";
121  }
122  else {
123  // Set the number of reduced ntuple files
124  // (multiple output files are not yet supported)
125  fNofNtupleFiles = nofNtupleFiles;
126 
127  // Forced merging mode
128  // MPI
129  if ( mpiRank >= mpiSize ) {
130  // the extra worker
131  fMpiNtupleMergeMode = G4MpiNtupleMergeMode::kMain;
132  mergingMode = "G4MpiNtupleMergeMode::kMain";
133  } else {
134  // processing worker
135  fMpiNtupleMergeMode = G4MpiNtupleMergeMode::kSlave;
136  mergingMode = "G4MpiNtupleMergeMode::kSlave";
137  }
138  }
139 
140 #ifdef G4VERBOSE
141  if ( fState.GetVerboseL2() )
143  ->Message("set", "ntuple merging mode", mergingMode);
144 #endif
145 }
146 
147 
148 //_____________________________________________________________________________
150  G4int mpiRank, G4int mpiSize,
151  G4int nofNtupleFiles)
152 {
153  // G4cout << "SetMpiNtupleMerging: "
154  // << impi << ", "
155  // << mpiRank << ","
156  // << mpiSize << ","
157  // << nofNtupleFiles << G4endl;
158 
159  // fImpi = impi;
160 
161  // Set ntuple merging mode
162  SetMpiNtupleMergingMode(mpiRank, mpiSize, nofNtupleFiles);
163 
164  // Clear existing managers
166 
167  // Re-create managers
168  CreateMpiNtupleManagers(impi, mpiRank, mpiSize);
169 }
170 
171 //
172 // protected methods
173 //
174 
175 //_____________________________________________________________________________
177 {
178  // No MPI merging, call base class
179  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) {
180  return G4RootAnalysisManager::OpenFileImpl(fileName);
181  }
182 
183  auto finalResult = true;
184  auto result = fFileManager->SetFileName(fileName);
185  finalResult = finalResult && result;
186 
187  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kMain ) {
188 
189 #ifdef G4VERBOSE
190  G4String name = fFileManager->GetFullFileName();
191  if ( fState.GetVerboseL4() )
192  fState.GetVerboseL4()->Message("open", "main ntuple file", name);
193 #endif
194 
195  fFileManager->SetNofNtupleFiles(fNofNtupleFiles);
196  result = fFileManager->OpenFile(fileName);
197  finalResult = finalResult && result;
198 
199  fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
200 
201  G4cout << "Main: Go to create ntuples from booking " << G4endl;
203 
204 #ifdef G4VERBOSE
205  if ( fState.GetVerboseL1() )
206  fState.GetVerboseL1()->Message("open", "main ntuple file", name, finalResult);
207 #endif
208  }
209 
210  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) {
211 
212 #ifdef G4VERBOSE
213  G4String name = fFileManager->GetFullFileName();
214  if ( fState.GetVerboseL4() )
215  fState.GetVerboseL4()->Message("open", "file", name);
216 #endif
217  result = fFileManager->OpenFile(fileName);
218  finalResult = finalResult && result;
219 
220  // fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
221 
222  G4cout << "Slave: Go to create ntuples from booking" << G4endl;
223  // No file is open by Slave manager
225 
226 #ifdef G4VERBOSE
227  if ( fState.GetVerboseL1() )
228  fState.GetVerboseL1()->Message("open", "file", name, finalResult);
229 #endif
230  }
231 
232  return finalResult;
233 }
234 
235 
236 //_____________________________________________________________________________
238 {
239  // No MPI merging, call base class
240  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) {
242  }
243 
244  auto finalResult = true;
245 
246  // reset data
247  if ( reset ) {
248  auto result = Reset();
249  if ( ! result ) {
250  G4ExceptionDescription description;
251  description << " " << "Resetting data failed";
252  G4Exception("G4RootAnalysisManager::Write()",
253  "Analysis_W021", JustWarning, description);
254  }
255  finalResult = finalResult && result;
256  }
257 
258  // close file
259  fFileManager->CloseFile();
260 
261  // MT not yet supported - no files clean-up
262  return finalResult;
263 }
264 
265 //_____________________________________________________________________________
267 {
268  // No MPI merging, call base class
269  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) {
271  }
272 
273  auto finalResult = true;
274 
275  G4String ntupleType;
276  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kMain ) ntupleType = "main ntuples";
277  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) ntupleType = "slave ntuples";
278 
279 #ifdef G4VERBOSE
280  if ( fState.GetVerboseL4() )
281  fState.GetVerboseL4()->Message("merge", ntupleType, "");
282 #endif
283 
284  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kMain ) {
285  auto result = fNtupleManager->Merge();
286  finalResult = result && finalResult;
287  }
288 
289  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) {
290  auto result = fMpiSlaveNtupleManager->Merge();
291  finalResult = result && finalResult;
292  }
293 
294 #ifdef G4VERBOSE
295  if ( fState.GetVerboseL1() )
296  fState.GetVerboseL1()->Message("merge", ntupleType, "");
297 #endif
298 
299  return finalResult;
300 }
301 
302 //_____________________________________________________________________________
304 {
305 // Reset histograms and ntuple
306 
307  // No MPI merging, call base class
308  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) {
310  }
311 
312  auto finalResult = true;
313 
314  auto result = G4ToolsAnalysisManager::Reset();
315  finalResult = finalResult && result;
316 
317  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kMain ) {
318  result = fNtupleManager->Reset(false);
319  finalResult = result && finalResult;
320  }
321 
322  if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) {
323  result = fMpiSlaveNtupleManager->Reset(false);
324  finalResult = result && finalResult;
325  }
326 
327  return finalResult;
328 }