Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 26 27 // Author: Ivana Hrivnacova, 21/11/2018 (ivana 27 // Author: Ivana Hrivnacova, 21/11/2018 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4RootMpiNtupleFileManager.hh" 29 #include "G4RootMpiNtupleFileManager.hh" 30 << 31 #include "G4AnalysisUtilities.hh" << 32 #include "G4RootMpiNtupleManager.hh" 30 #include "G4RootMpiNtupleManager.hh" 33 #include "G4RootMpiPNtupleManager.hh" 31 #include "G4RootMpiPNtupleManager.hh" >> 32 #include "G4AnalysisUtilities.hh" 34 33 35 #include <tools/impi> 34 #include <tools/impi> 36 35 37 using namespace G4Analysis; 36 using namespace G4Analysis; 38 using std::make_shared; 37 using std::make_shared; 39 38 40 //____________________________________________ 39 //_____________________________________________________________________________ 41 G4RootMpiNtupleFileManager::G4RootMpiNtupleFil 40 G4RootMpiNtupleFileManager::G4RootMpiNtupleFileManager(const G4AnalysisManagerState& state) 42 : G4RootNtupleFileManager(state), << 41 : G4RootNtupleFileManager(state), 43 fImpi(nullptr), << 42 fImpi(nullptr), 44 fMpiRank(-1), << 43 fMpiRank(-1), 45 fMpiSize(0), << 44 fMpiSize(0), 46 fMpiSlaveNtupleManager(nullptr), << 45 fMpiSlaveNtupleManager(nullptr), 47 fNtupleBooked(false) << 46 fNtupleBooked(false) 48 {} 47 {} 49 48 50 //____________________________________________ 49 //_____________________________________________________________________________ 51 G4RootMpiNtupleFileManager::~G4RootMpiNtupleFi << 50 G4RootMpiNtupleFileManager::~G4RootMpiNtupleFileManager() >> 51 {} 52 52 53 // << 53 // 54 // private methods 54 // private methods 55 // 55 // 56 56 57 //____________________________________________ 57 //_____________________________________________________________________________ 58 void G4RootMpiNtupleFileManager::SetMpiNtupleM << 58 void G4RootMpiNtupleFileManager::SetMpiNtupleMergingMode( >> 59 G4int nofNtupleFiles) 59 { 60 { 60 Message(kVL2, "set", "mpi ntuple merging mod 61 Message(kVL2, "set", "mpi ntuple merging mode"); 61 62 62 auto canMerge = true; 63 auto canMerge = true; 63 64 64 // Illegal situations 65 // Illegal situations 65 if (fMpiSize < 2) { << 66 if ( fMpiSize < 2 ) { 66 G4ExceptionDescription description; 67 G4ExceptionDescription description; 67 description << "Merging ntuples is not app << 68 description 68 << "Setting was ignored."; << 69 << "Merging ntuples is not applicable on a single rank." << G4endl 69 G4Exception("G4RootMpiNtupleFileManager::S << 70 << "Setting was ignored."; 70 JustWarning, description); << 71 G4Exception("G4RootMpiNtupleFileManager::SetMpiNtupleMergingMode()", 71 canMerge = false; << 72 "Analysis_W013", JustWarning, description); >> 73 canMerge = false; 72 } 74 } 73 75 74 G4String mergingMode; 76 G4String mergingMode; 75 if (!canMerge) { << 77 if ( ! canMerge ) { 76 fNtupleMergeMode = G4NtupleMergeMode::kNon 78 fNtupleMergeMode = G4NtupleMergeMode::kNone; 77 mergingMode = "G4NtupleMergeMode::kNone"; << 79 mergingMode = "G4NtupleMergeMode::kNone"; 78 } 80 } 79 else { 81 else { 80 // Set the number of reduced ntuple files 82 // Set the number of reduced ntuple files 81 // (multiple output files are not yet supp 83 // (multiple output files are not yet supported) 82 fNofNtupleFiles = nofNtupleFiles; 84 fNofNtupleFiles = nofNtupleFiles; 83 << 85 84 // Forced merging mode 86 // Forced merging mode 85 // MPI 87 // MPI 86 if (fMpiRank >= fMpiSize) { << 88 if ( fMpiRank >= fMpiSize ) { 87 // the extra worker 89 // the extra worker 88 fNtupleMergeMode = G4NtupleMergeMode::kM 90 fNtupleMergeMode = G4NtupleMergeMode::kMain; 89 mergingMode = "G4NtupleMergeMode::kMain" 91 mergingMode = "G4NtupleMergeMode::kMain"; 90 } << 92 } else { 91 else { << 92 // processing worker 93 // processing worker 93 fNtupleMergeMode = G4NtupleMergeMode::kS 94 fNtupleMergeMode = G4NtupleMergeMode::kSlave; 94 mergingMode = "G4NtupleMergeMode::kSlave 95 mergingMode = "G4NtupleMergeMode::kSlave"; 95 } 96 } 96 } 97 } 97 98 98 Message(kVL1, "set", "mpi ntuple merging mod 99 Message(kVL1, "set", "mpi ntuple merging mode", mergingMode); 99 } 100 } 100 101 101 // << 102 >> 103 // 102 // public methods 104 // public methods 103 // 105 // 104 106 105 //____________________________________________ 107 //_____________________________________________________________________________ 106 void G4RootMpiNtupleFileManager::SetMpiNtupleM << 108 void G4RootMpiNtupleFileManager::SetMpiNtupleMerging(tools::impi* impi, 107 << 109 G4int mpiRank, G4int mpiSize, >> 110 G4int nofNtupleFiles) 108 { 111 { 109 if (fIsInitialized) { << 112 if ( fIsInitialized ) { 110 G4ExceptionDescription description; << 113 G4ExceptionDescription description; 111 description << "Cannot change merging mode << 114 description 112 << "The function must be calle << 115 << "Cannot change merging mode." << G4endl 113 G4Exception("G4RootMpiNtupleFileManager::S << 116 << "The function must be called before OpenFile()."; 114 description); << 117 G4Exception("G4RootMpiNtupleFileManager::SetMpiNtupleMerging", 115 return; << 118 "Analysis_W013", JustWarning, description); >> 119 return; 116 } 120 } 117 121 118 // Save MPI merging parameters 122 // Save MPI merging parameters 119 fImpi = impi; 123 fImpi = impi; 120 fMpiRank = mpiRank; 124 fMpiRank = mpiRank; 121 fMpiSize = mpiSize; 125 fMpiSize = mpiSize; 122 126 123 // Set ntuple merging mode << 127 // Set ntuple merging mode 124 SetMpiNtupleMergingMode(nofNtupleFiles); 128 SetMpiNtupleMergingMode(nofNtupleFiles); 125 } 129 } 126 130 127 //____________________________________________ 131 //_____________________________________________________________________________ 128 std::shared_ptr<G4VNtupleManager> G4RootMpiNtu 132 std::shared_ptr<G4VNtupleManager> G4RootMpiNtupleFileManager::CreateNtupleManager() 129 { 133 { 130 Message(kVL4, "create", "mpi ntuple manager" 134 Message(kVL4, "create", "mpi ntuple manager"); 131 135 132 std::shared_ptr<G4VNtupleManager> activeNtup 136 std::shared_ptr<G4VNtupleManager> activeNtupleManager = nullptr; 133 switch (fNtupleMergeMode) { << 137 switch ( fNtupleMergeMode ) >> 138 { 134 case G4NtupleMergeMode::kNone: 139 case G4NtupleMergeMode::kNone: 135 fNtupleManager = make_shared<G4RootNtupl << 140 fNtupleManager 136 << 141 = make_shared<G4RootNtupleManager>(fState, fBookingManager, >> 142 0, 0, fNtupleRowWise, fNtupleRowMode); 137 fNtupleManager->SetFileManager(fFileMana 143 fNtupleManager->SetFileManager(fFileManager); 138 activeNtupleManager = fNtupleManager; 144 activeNtupleManager = fNtupleManager; 139 break; 145 break; 140 146 141 case G4NtupleMergeMode::kMain: { 147 case G4NtupleMergeMode::kMain: { 142 fNtupleManager = make_shared<G4RootMpiNt << 148 fNtupleManager 143 << 149 = make_shared<G4RootMpiNtupleManager>(fState, fBookingManager, >> 150 fNtupleRowWise, fNtupleRowMode, fImpi, fMpiSize); 144 fNtupleManager->SetFileManager(fFileMana 151 fNtupleManager->SetFileManager(fFileManager); 145 activeNtupleManager = fNtupleManager; 152 activeNtupleManager = fNtupleManager; 146 break; 153 break; 147 } 154 } 148 155 149 case G4NtupleMergeMode::kSlave: { 156 case G4NtupleMergeMode::kSlave: { 150 auto destinationRank = fMpiSize; 157 auto destinationRank = fMpiSize; 151 fMpiSlaveNtupleManager = << 158 fMpiSlaveNtupleManager 152 make_shared<G4RootMpiPNtupleManager>(f << 159 = make_shared<G4RootMpiPNtupleManager>(fState, fImpi, fMpiRank, destinationRank); 153 activeNtupleManager = fMpiSlaveNtupleMan 160 activeNtupleManager = fMpiSlaveNtupleManager; 154 break; 161 break; 155 } 162 } 156 } 163 } 157 164 158 G4String mergeMode; 165 G4String mergeMode; 159 switch (fNtupleMergeMode) { << 166 switch ( fNtupleMergeMode ) { 160 case G4NtupleMergeMode::kNone: 167 case G4NtupleMergeMode::kNone: 161 mergeMode = ""; 168 mergeMode = ""; 162 break; 169 break; 163 case G4NtupleMergeMode::kMain: 170 case G4NtupleMergeMode::kMain: 164 mergeMode = "main "; 171 mergeMode = "main "; 165 break; 172 break; 166 case G4NtupleMergeMode::kSlave: 173 case G4NtupleMergeMode::kSlave: 167 mergeMode = "slave "; 174 mergeMode = "slave "; 168 break; 175 break; 169 } 176 } 170 Message(kVL3, "create", mergeMode + "mpi ntu 177 Message(kVL3, "create", mergeMode + "mpi ntuple manager"); 171 178 172 fIsInitialized = true; 179 fIsInitialized = true; 173 180 174 return activeNtupleManager; 181 return activeNtupleManager; 175 } 182 } 176 183 177 //____________________________________________ 184 //_____________________________________________________________________________ 178 G4bool G4RootMpiNtupleFileManager::ActionAtOpe 185 G4bool G4RootMpiNtupleFileManager::ActionAtOpenFile(const G4String& fileName) 179 { 186 { 180 // No MPI merging, call base class 187 // No MPI merging, call base class 181 if (fNtupleMergeMode == G4NtupleMergeMode::k << 188 if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ) { 182 return G4RootNtupleFileManager::ActionAtOp 189 return G4RootNtupleFileManager::ActionAtOpenFile(fileName); 183 } 190 } 184 191 185 if (!fNtupleBooked) { << 192 if ( ! fNtupleBooked ) { >> 193 186 G4String objectType = "analysis file"; 194 G4String objectType = "analysis file"; 187 if (fNtupleMergeMode == G4NtupleMergeMode: << 195 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) { 188 objectType = "main analysis file"; 196 objectType = "main analysis file"; 189 } 197 } 190 Message(kVL4, "open", objectType, fileName 198 Message(kVL4, "open", objectType, fileName); 191 199 192 if (fNtupleMergeMode == G4NtupleMergeMode: << 200 193 // Creating files is triggered from Crea << 201 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) { 194 fNtupleManager->CreateNtuplesFromBooking << 202 // Creating files is triggered from CreateNtuple >> 203 fNtupleManager->CreateNtuplesFromBooking( >> 204 fBookingManager->GetNtupleBookingVector()); 195 } 205 } 196 206 197 if (fNtupleMergeMode == G4NtupleMergeMode: << 207 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) { 198 // G4cout << "Slave: Go to create ntuple 208 // G4cout << "Slave: Go to create ntuples from booking" << G4endl; 199 // No file is open by Slave manager 209 // No file is open by Slave manager 200 fMpiSlaveNtupleManager->CreateNtuplesFro << 210 fMpiSlaveNtupleManager->CreateNtuplesFromBooking( >> 211 fBookingManager->GetNtupleBookingVector()); 201 } 212 } 202 213 203 Message(kVL1, "open", objectType, fileName 214 Message(kVL1, "open", objectType, fileName); 204 215 205 fNtupleBooked = true; 216 fNtupleBooked = true; 206 } 217 } 207 218 208 return true; 219 return true; 209 } << 220 } 210 221 211 //____________________________________________ 222 //_____________________________________________________________________________ 212 G4bool G4RootMpiNtupleFileManager::ActionAtWri 223 G4bool G4RootMpiNtupleFileManager::ActionAtWrite() 213 { 224 { 214 // No MPI merging, call base class 225 // No MPI merging, call base class 215 if (fNtupleMergeMode == G4NtupleMergeMode::k << 226 if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ) { 216 return G4RootNtupleFileManager::ActionAtWr 227 return G4RootNtupleFileManager::ActionAtWrite(); 217 } 228 } 218 << 229 219 auto result = true; 230 auto result = true; 220 231 221 G4String ntupleType; 232 G4String ntupleType; 222 if (fNtupleMergeMode == G4NtupleMergeMode::k << 233 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) ntupleType = "main ntuples"; 223 if (fNtupleMergeMode == G4NtupleMergeMode::k << 234 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) ntupleType = "slave ntuples"; 224 235 225 Message(kVL4, "merge", ntupleType); 236 Message(kVL4, "merge", ntupleType); 226 237 227 if (fNtupleMergeMode == G4NtupleMergeMode::k << 238 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) { 228 result &= fNtupleManager->Merge(); 239 result &= fNtupleManager->Merge(); 229 } << 240 } 230 << 241 231 if (fNtupleMergeMode == G4NtupleMergeMode::k << 242 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) { 232 result &= fMpiSlaveNtupleManager->Merge(); 243 result &= fMpiSlaveNtupleManager->Merge(); 233 } 244 } 234 245 235 Message(kVL1, "merge", ntupleType, "", resul 246 Message(kVL1, "merge", ntupleType, "", result); 236 247 237 return result; 248 return result; 238 } 249 } 239 250 240 //____________________________________________ 251 //_____________________________________________________________________________ 241 G4bool G4RootMpiNtupleFileManager::ActionAtClo 252 G4bool G4RootMpiNtupleFileManager::ActionAtCloseFile() 242 { << 253 { 243 // No MPI merging, call base class 254 // No MPI merging, call base class 244 if (fNtupleMergeMode == G4NtupleMergeMode::k << 255 if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ) { 245 return G4RootNtupleFileManager::ActionAtCl 256 return G4RootNtupleFileManager::ActionAtCloseFile(); 246 } 257 } 247 258 248 if (fNtupleMergeMode == G4NtupleMergeMode::k << 259 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave) { 249 fMpiSlaveNtupleManager->SetNewCycle(false) 260 fMpiSlaveNtupleManager->SetNewCycle(false); 250 return true; 261 return true; 251 } 262 } 252 263 253 return CloseNtupleFiles(); 264 return CloseNtupleFiles(); 254 } 265 } 255 266 256 //____________________________________________ 267 //_____________________________________________________________________________ 257 G4bool G4RootMpiNtupleFileManager::Reset() 268 G4bool G4RootMpiNtupleFileManager::Reset() 258 { 269 { 259 // Reset ntuples << 270 // Reset ntuples 260 271 261 // No MPI merging, call base class 272 // No MPI merging, call base class 262 if (fNtupleMergeMode == G4NtupleMergeMode::k << 273 if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ) { 263 return G4RootNtupleFileManager::Reset(); 274 return G4RootNtupleFileManager::Reset(); 264 } 275 } 265 << 276 266 auto result = true; 277 auto result = true; 267 << 278 268 if (fNtupleMergeMode == G4NtupleMergeMode::k << 279 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) { 269 result &= fNtupleManager->Reset(); 280 result &= fNtupleManager->Reset(); 270 } << 281 } 271 282 272 if (fNtupleMergeMode == G4NtupleMergeMode::k << 283 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) { 273 result &= fMpiSlaveNtupleManager->Reset(); << 284 result &= fMpiSlaveNtupleManager->Reset(false); 274 } << 285 } 275 286 276 return result; 287 return result; 277 } 288 } 278 289