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 "G4RootMpiAnalysisManager.hh" 29 #include "G4RootMpiAnalysisManager.hh" 30 << 31 #include "G4AnalysisUtilities.hh" << 32 #include "G4RootMpiNtupleFileManager.hh" << 33 #include "G4RootMpiNtupleManager.hh" 30 #include "G4RootMpiNtupleManager.hh" 34 #include "G4RootMpiPNtupleManager.hh" 31 #include "G4RootMpiPNtupleManager.hh" 35 32 36 #include <tools/impi> 33 #include <tools/impi> 37 34 38 using namespace G4Analysis; << 35 //_____________________________________________________________________________ >> 36 G4RootMpiAnalysisManager::G4RootMpiAnalysisManager(G4bool isMaster) >> 37 : G4RootAnalysisManager(isMaster), >> 38 fMpiNtupleMergeMode(G4MpiNtupleMergeMode::kNone), >> 39 fMpiSlaveNtupleManager(nullptr) >> 40 {} 39 41 40 //____________________________________________ 42 //_____________________________________________________________________________ 41 G4RootMpiAnalysisManager* G4RootMpiAnalysisMan << 43 G4RootMpiAnalysisManager::~G4RootMpiAnalysisManager() 42 { << 44 {} 43 return fgInstance; << 45 44 } << 46 // >> 47 // private methods >> 48 // 45 49 46 //____________________________________________ 50 //_____________________________________________________________________________ 47 G4RootMpiAnalysisManager::G4RootMpiAnalysisMan << 51 void G4RootMpiAnalysisManager::CreateMpiNtupleManagers( >> 52 tools::impi* impi, G4int mpiRank, G4int mpiSize) 48 { 53 { 49 fgInstance = this; << 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: >> 62 fNtupleManager >> 63 = new G4RootNtupleManager(fState, 0, fNtupleRowWise, fNtupleRowMode); >> 64 fNtupleManager->SetFileManager(fFileManager); >> 65 SetNtupleManager(fNtupleManager); >> 66 break; >> 67 >> 68 case G4MpiNtupleMergeMode::kMain: { >> 69 fNtupleManager >> 70 = new G4RootMpiNtupleManager(fState, fNtupleRowWise, fNtupleRowMode, >> 71 impi, mpiSize); >> 72 fNtupleManager->SetFileManager(fFileManager); >> 73 SetNtupleManager(fNtupleManager); >> 74 break; >> 75 } 50 76 51 // Reset the ntuple file manager << 77 case G4MpiNtupleMergeMode::kSlave: { 52 fNtupleFileManager.reset(); << 78 auto destinationRank = mpiSize; >> 79 fMpiSlaveNtupleManager >> 80 = new G4RootMpiPNtupleManager(fState, impi, mpiRank, destinationRank); >> 81 SetNtupleManager(fMpiSlaveNtupleManager); >> 82 break; >> 83 } >> 84 } 53 85 54 // Ntuple file manager << 86 #ifdef G4VERBOSE 55 fNtupleFileManager = std::make_shared<G4Root << 87 if ( fState.GetVerboseL3() ) 56 SetNtupleFileManager(fNtupleFileManager); << 88 fState.GetVerboseL3()->Message("create", "mpi ntuple managers", ""); 57 fNtupleFileManager->SetFileManager(fFileMana << 89 #endif 58 fNtupleFileManager->SetBookingManager(fNtupl << 59 } 90 } 60 91 61 //____________________________________________ 92 //_____________________________________________________________________________ 62 G4RootMpiAnalysisManager::~G4RootMpiAnalysisMa << 93 void G4RootMpiAnalysisManager::SetMpiNtupleMergingMode( >> 94 G4int mpiRank, G4int mpiSize, >> 95 G4int nofNtupleFiles) 63 { 96 { 64 fgInstance = 0; << 97 #ifdef G4VERBOSE >> 98 if ( fState.GetVerboseL1() ) >> 99 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() ) >> 142 fState.GetVerboseL2() >> 143 ->Message("set", "ntuple merging mode", mergingMode); >> 144 #endif 65 } 145 } 66 146 67 // << 68 // public methods << 69 // << 70 147 71 //____________________________________________ 148 //_____________________________________________________________________________ 72 void G4RootMpiAnalysisManager::SetMpiNtupleMer << 149 void G4RootMpiAnalysisManager::SetMpiNtupleMerging(tools::impi* impi, 73 << 150 G4int mpiRank, G4int mpiSize, >> 151 G4int nofNtupleFiles) 74 { 152 { 75 // G4cout << "SetMpiNtupleMerging: " 153 // G4cout << "SetMpiNtupleMerging: " 76 // << impi << ", " 154 // << impi << ", " 77 // << mpiRank << "," 155 // << mpiRank << "," 78 // << mpiSize << "," 156 // << mpiSize << "," 79 // << nofNtupleFiles << G4endl; 157 // << nofNtupleFiles << G4endl; 80 158 81 std::static_pointer_cast<G4RootMpiNtupleFile << 159 // fImpi = impi; 82 ->SetMpiNtupleMerging(impi, mpiRank, mpiSi << 160 >> 161 // Set ntuple merging mode >> 162 SetMpiNtupleMergingMode(mpiRank, mpiSize, nofNtupleFiles); >> 163 >> 164 // Clear existing managers >> 165 ClearNtupleManagers(); >> 166 >> 167 // Re-create managers >> 168 CreateMpiNtupleManagers(impi, mpiRank, mpiSize); 83 } 169 } 84 170 85 // << 171 // 86 // protected methods 172 // protected methods 87 // 173 // 88 174 89 //____________________________________________ 175 //_____________________________________________________________________________ 90 G4bool G4RootMpiAnalysisManager::OpenFileImpl( 176 G4bool G4RootMpiAnalysisManager::OpenFileImpl(const G4String& fileName) 91 { 177 { 92 if (fNtupleFileManager->GetMergeMode() == G4 << 178 // No MPI merging, call base class 93 return G4ToolsAnalysisManager::OpenFileImp << 179 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) { >> 180 return G4RootAnalysisManager::OpenFileImpl(fileName); 94 } 181 } 95 182 96 // Create ntuple manager(s) << 183 auto finalResult = true; 97 // and set it to base class which takes then << 184 auto result = fFileManager->SetFileName(fileName); 98 if (!fVNtupleManager) { << 185 finalResult = finalResult && result; 99 SetNtupleManager(fNtupleFileManager->Creat << 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; >> 202 fNtupleManager->CreateNtuplesFromBooking(); >> 203 >> 204 #ifdef G4VERBOSE >> 205 if ( fState.GetVerboseL1() ) >> 206 fState.GetVerboseL1()->Message("open", "main ntuple file", name, finalResult); >> 207 #endif 100 } 208 } 101 209 102 auto result = true; << 210 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) { 103 211 104 // Open file << 212 #ifdef G4VERBOSE 105 // In difference from base class a file is o << 213 G4String name = fFileManager->GetFullFileName(); 106 result &= fFileManager->OpenFile(fileName); << 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 >> 224 fMpiSlaveNtupleManager->CreateNtuplesFromBooking(); >> 225 >> 226 #ifdef G4VERBOSE >> 227 if ( fState.GetVerboseL1() ) >> 228 fState.GetVerboseL1()->Message("open", "file", name, finalResult); >> 229 #endif >> 230 } 107 231 108 // Open ntuple file(s) and create ntuples fr << 232 return finalResult; 109 result &= fNtupleFileManager->ActionAtOpenFi << 233 } 110 234 111 return result; << 112 } << 113 235 114 //____________________________________________ 236 //_____________________________________________________________________________ 115 G4bool G4RootMpiAnalysisManager::WriteImpl() << 237 G4bool G4RootMpiAnalysisManager::CloseFileImpl(G4bool reset) 116 { 238 { 117 auto result = true; << 239 // No MPI merging, call base class >> 240 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) { >> 241 return G4RootAnalysisManager::CloseFileImpl(reset); >> 242 } 118 243 119 // Call base class method << 244 auto finalResult = true; 120 result &= G4ToolsAnalysisManager::WriteImpl( << 121 245 122 // Write file also on Slave << 246 // reset data 123 // (skipped in base class) << 247 if ( reset ) { 124 if (fNtupleFileManager->GetMergeMode() == G4 << 248 auto result = Reset(); 125 // write all open files << 249 if ( ! result ) { 126 result &= fFileManager->WriteFiles(); << 250 G4ExceptionDescription description; >> 251 description << " " << "Resetting data failed"; >> 252 G4Exception("G4RootAnalysisManager::Write()", >> 253 "Analysis_W021", JustWarning, description); >> 254 } >> 255 finalResult = finalResult && result; 127 } 256 } 128 257 129 Message(kVL2, "write", "slave files", "", re << 258 // close file >> 259 fFileManager->CloseFile(); 130 260 131 return result; << 261 // MT not yet supported - no files clean-up >> 262 return finalResult; 132 } 263 } 133 264 134 //____________________________________________ 265 //_____________________________________________________________________________ 135 G4bool G4RootMpiAnalysisManager::CloseFileImpl << 266 G4bool G4RootMpiAnalysisManager::WriteNtuple() 136 { 267 { 137 // Cannot call base class function, as we ne << 268 // No MPI merging, call base class 138 // on slave; an option in the base class can << 269 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) { 139 << 270 return G4RootAnalysisManager::WriteNtuple(); 140 Message(kVL4, "close", "files"); << 141 << 142 auto result = true; << 143 if (fVNtupleFileManager) { << 144 result &= fVNtupleFileManager->ActionAtClo << 145 } 271 } >> 272 >> 273 auto finalResult = true; 146 274 147 // close file also on Slave << 275 G4String ntupleType; 148 // - the conditoon used in the base class is << 276 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kMain ) ntupleType = "main ntuples"; 149 // if ( (fVNtupleFileManager == nullptr) || << 277 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) ntupleType = "slave ntuples"; 150 // (fVNtupleFileManager->GetMergeMode() << 278 151 if (!fVFileManager->CloseFiles()) { << 279 #ifdef G4VERBOSE 152 Warn("Closing files failed", fkClass, "Clo << 280 if ( fState.GetVerboseL4() ) 153 result = false; << 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; 154 } 292 } 155 // } << 156 293 157 // delete empty files << 294 #ifdef G4VERBOSE 158 if (!fVFileManager->DeleteEmptyFiles()) { << 295 if ( fState.GetVerboseL1() ) 159 Warn("Deleting empty files failed", fkClas << 296 fState.GetVerboseL1()->Message("merge", ntupleType, ""); 160 result = false; << 297 #endif 161 } << 162 298 163 // reset histograms << 299 return finalResult; 164 if (reset) { << 300 } 165 if (!Reset()) { << 166 Warn("Resetting data failed", fkClass, " << 167 result = false; << 168 } << 169 } << 170 301 171 Message(kVL3, "close", "files", "", result); << 302 //_____________________________________________________________________________ 172 G4cout << "### Done G4RootMpiAnalysisManager << 303 G4bool G4RootMpiAnalysisManager::Reset() >> 304 { >> 305 // Reset histograms and ntuple 173 306 174 return result; << 307 // No MPI merging, call base class >> 308 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) { >> 309 return G4RootAnalysisManager::Reset(); >> 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; 175 } 328 } 176 329