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 {} 39 39 40 //____________________________________________ 40 //_____________________________________________________________________________ 41 G4RootMpiAnalysisManager* G4RootMpiAnalysisMan << 41 G4RootMpiAnalysisManager::~G4RootMpiAnalysisManager() 42 { << 42 {} 43 return fgInstance; << 43 44 } << 44 // >> 45 // private methods >> 46 // 45 47 46 //____________________________________________ 48 //_____________________________________________________________________________ 47 G4RootMpiAnalysisManager::G4RootMpiAnalysisMan << 49 void G4RootMpiAnalysisManager::CreateMpiNtupleManagers( >> 50 tools::impi* impi, G4int mpiRank, G4int mpiSize) 48 { 51 { 49 fgInstance = this; << 52 #ifdef G4VERBOSE >> 53 if ( fState.GetVerboseL4() ) >> 54 fState.GetVerboseL4()->Message("create", "mpi ntuple managers", ""); >> 55 #endif >> 56 >> 57 switch ( fMpiNtupleMergeMode ) >> 58 { >> 59 case G4MpiNtupleMergeMode::kNone: >> 60 fNtupleManager = new G4RootNtupleManager(fState, 0, fNtupleRowWise); >> 61 fNtupleManager->SetFileManager(fFileManager); >> 62 SetNtupleManager(fNtupleManager); >> 63 break; >> 64 >> 65 case G4MpiNtupleMergeMode::kMain: { >> 66 fNtupleManager >> 67 = new G4RootMpiNtupleManager(fState, fNtupleRowWise, impi, mpiSize); >> 68 fNtupleManager->SetFileManager(fFileManager); >> 69 SetNtupleManager(fNtupleManager); >> 70 break; >> 71 } 50 72 51 // Reset the ntuple file manager << 73 case G4MpiNtupleMergeMode::kSlave: { 52 fNtupleFileManager.reset(); << 74 auto destinationRank = mpiSize; >> 75 fMpiSlaveNtupleManager >> 76 = new G4RootMpiPNtupleManager(fState, fNtupleRowWise, impi, mpiRank, destinationRank); >> 77 SetNtupleManager(fMpiSlaveNtupleManager); >> 78 break; >> 79 } >> 80 } 53 81 54 // Ntuple file manager << 82 #ifdef G4VERBOSE 55 fNtupleFileManager = std::make_shared<G4Root << 83 if ( fState.GetVerboseL3() ) 56 SetNtupleFileManager(fNtupleFileManager); << 84 fState.GetVerboseL3()->Message("create", "mpi ntuple managers", ""); 57 fNtupleFileManager->SetFileManager(fFileMana << 85 #endif 58 fNtupleFileManager->SetBookingManager(fNtupl << 59 } 86 } 60 87 61 //____________________________________________ 88 //_____________________________________________________________________________ 62 G4RootMpiAnalysisManager::~G4RootMpiAnalysisMa << 89 void G4RootMpiAnalysisManager::SetMpiNtupleMergingMode( >> 90 G4int mpiRank, G4int mpiSize, >> 91 G4int nofNtupleFiles) 63 { 92 { 64 fgInstance = 0; << 93 #ifdef G4VERBOSE >> 94 if ( fState.GetVerboseL1() ) >> 95 fState.GetVerboseL1() >> 96 ->Message("set", "mpi ntuple merging mode", ""); >> 97 #endif >> 98 >> 99 auto canMerge = true; >> 100 >> 101 // Illegal situations >> 102 if ( mpiSize < 2 ) { >> 103 G4ExceptionDescription description; >> 104 description >> 105 << " " << "Merging ntuples is not applicable on a single rank." >> 106 << G4endl >> 107 << " " << "Setting was ignored."; >> 108 G4Exception("G4RootMpiAnalysisManager::SetMpiNtupleMergingMode()", >> 109 "Analysis_W013", JustWarning, description); >> 110 canMerge = false; >> 111 } >> 112 >> 113 G4String mergingMode; >> 114 if ( ! canMerge ) { >> 115 fMpiNtupleMergeMode = G4MpiNtupleMergeMode::kNone; >> 116 mergingMode = "G4MpiNtupleMergeMode::kNone"; >> 117 } >> 118 else { >> 119 // Set the number of reduced ntuple files >> 120 // (multiple output files are not yet supported) >> 121 fNofNtupleFiles = nofNtupleFiles; >> 122 >> 123 // Forced merging mode >> 124 // MPI >> 125 if ( mpiRank >= mpiSize ) { >> 126 // the extra worker >> 127 fMpiNtupleMergeMode = G4MpiNtupleMergeMode::kMain; >> 128 mergingMode = "G4MpiNtupleMergeMode::kMain"; >> 129 } else { >> 130 // processing worker >> 131 fMpiNtupleMergeMode = G4MpiNtupleMergeMode::kSlave; >> 132 mergingMode = "G4MpiNtupleMergeMode::kSlave"; >> 133 } >> 134 } >> 135 >> 136 #ifdef G4VERBOSE >> 137 if ( fState.GetVerboseL2() ) >> 138 fState.GetVerboseL2() >> 139 ->Message("set", "ntuple merging mode", mergingMode); >> 140 #endif 65 } 141 } 66 142 67 // << 68 // public methods << 69 // << 70 143 71 //____________________________________________ 144 //_____________________________________________________________________________ 72 void G4RootMpiAnalysisManager::SetMpiNtupleMer << 145 void G4RootMpiAnalysisManager::SetMpiNtupleMerging(tools::impi* impi, 73 << 146 G4int mpiRank, G4int mpiSize, >> 147 G4int nofNtupleFiles, >> 148 G4bool rowWise, >> 149 unsigned int basketSize) 74 { 150 { 75 // G4cout << "SetMpiNtupleMerging: " 151 // G4cout << "SetMpiNtupleMerging: " 76 // << impi << ", " 152 // << impi << ", " 77 // << mpiRank << "," 153 // << mpiRank << "," 78 // << mpiSize << "," 154 // << mpiSize << "," 79 // << nofNtupleFiles << G4endl; << 155 // << nofNtupleFiles << "," >> 156 // << rowWise << "," >> 157 // << basketSize << G4endl; >> 158 >> 159 // fImpi = impi; 80 160 81 std::static_pointer_cast<G4RootMpiNtupleFile << 161 // Keep basketSize in file manager 82 ->SetMpiNtupleMerging(impi, mpiRank, mpiSi << 162 fFileManager->SetBasketSize(basketSize); >> 163 fNtupleRowWise = rowWise; >> 164 >> 165 // Set ntuple merging mode >> 166 SetMpiNtupleMergingMode(mpiRank, mpiSize, nofNtupleFiles); >> 167 >> 168 // Clear existing managers >> 169 ClearNtupleManagers(); >> 170 >> 171 // Re-create managers >> 172 CreateMpiNtupleManagers(impi, mpiRank, mpiSize); 83 } 173 } 84 174 85 // << 175 // 86 // protected methods 176 // protected methods 87 // 177 // 88 178 89 //____________________________________________ 179 //_____________________________________________________________________________ 90 G4bool G4RootMpiAnalysisManager::OpenFileImpl( 180 G4bool G4RootMpiAnalysisManager::OpenFileImpl(const G4String& fileName) 91 { 181 { 92 if (fNtupleFileManager->GetMergeMode() == G4 << 182 // No MPI merging, call base class 93 return G4ToolsAnalysisManager::OpenFileImp << 183 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) { >> 184 return G4RootAnalysisManager::OpenFileImpl(fileName); 94 } 185 } 95 186 96 // Create ntuple manager(s) << 187 auto finalResult = true; 97 // and set it to base class which takes then << 188 auto result = fFileManager->SetFileName(fileName); 98 if (!fVNtupleManager) { << 189 finalResult = finalResult && result; 99 SetNtupleManager(fNtupleFileManager->Creat << 190 >> 191 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kMain ) { >> 192 >> 193 #ifdef G4VERBOSE >> 194 G4String name = fFileManager->GetFullFileName(); >> 195 if ( fState.GetVerboseL4() ) >> 196 fState.GetVerboseL4()->Message("open", "main ntuple file", name); >> 197 #endif >> 198 >> 199 fFileManager->SetNofNtupleFiles(fNofNtupleFiles); >> 200 result = fFileManager->OpenFile(fileName); >> 201 finalResult = finalResult && result; >> 202 >> 203 fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory()); >> 204 >> 205 G4cout << "Main: Go to create ntuples from booking " << G4endl; >> 206 fNtupleManager->CreateNtuplesFromBooking(); >> 207 >> 208 #ifdef G4VERBOSE >> 209 if ( fState.GetVerboseL1() ) >> 210 fState.GetVerboseL1()->Message("open", "main ntuple file", name, finalResult); >> 211 #endif 100 } 212 } 101 213 102 auto result = true; << 214 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) { 103 215 104 // Open file << 216 #ifdef G4VERBOSE 105 // In difference from base class a file is o << 217 G4String name = fFileManager->GetFullFileName(); 106 result &= fFileManager->OpenFile(fileName); << 218 if ( fState.GetVerboseL4() ) >> 219 fState.GetVerboseL4()->Message("open", "file", name); >> 220 #endif >> 221 result = fFileManager->OpenFile(fileName); >> 222 finalResult = finalResult && result; >> 223 >> 224 // fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory()); >> 225 >> 226 G4cout << "Slave: Go to create ntuples from booking" << G4endl; >> 227 // No file is open by Slave manager >> 228 fMpiSlaveNtupleManager->CreateNtuplesFromBooking(); >> 229 >> 230 #ifdef G4VERBOSE >> 231 if ( fState.GetVerboseL1() ) >> 232 fState.GetVerboseL1()->Message("open", "file", name, finalResult); >> 233 #endif >> 234 } 107 235 108 // Open ntuple file(s) and create ntuples fr << 236 return finalResult; 109 result &= fNtupleFileManager->ActionAtOpenFi << 237 } 110 238 111 return result; << 112 } << 113 239 114 //____________________________________________ 240 //_____________________________________________________________________________ 115 G4bool G4RootMpiAnalysisManager::WriteImpl() << 241 G4bool G4RootMpiAnalysisManager::CloseFileImpl(G4bool reset) 116 { 242 { 117 auto result = true; << 243 // No MPI merging, call base class >> 244 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) { >> 245 return G4RootAnalysisManager::CloseFileImpl(reset); >> 246 } 118 247 119 // Call base class method << 248 auto finalResult = true; 120 result &= G4ToolsAnalysisManager::WriteImpl( << 121 249 122 // Write file also on Slave << 250 // reset data 123 // (skipped in base class) << 251 if ( reset ) { 124 if (fNtupleFileManager->GetMergeMode() == G4 << 252 auto result = Reset(); 125 // write all open files << 253 if ( ! result ) { 126 result &= fFileManager->WriteFiles(); << 254 G4ExceptionDescription description; >> 255 description << " " << "Resetting data failed"; >> 256 G4Exception("G4RootAnalysisManager::Write()", >> 257 "Analysis_W021", JustWarning, description); >> 258 } >> 259 finalResult = finalResult && result; 127 } 260 } 128 261 129 Message(kVL2, "write", "slave files", "", re << 262 // close file >> 263 fFileManager->CloseFile(); 130 264 131 return result; << 265 // MT not yet supported - no files clean-up >> 266 return finalResult; 132 } 267 } 133 268 134 //____________________________________________ 269 //_____________________________________________________________________________ 135 G4bool G4RootMpiAnalysisManager::CloseFileImpl << 270 G4bool G4RootMpiAnalysisManager::WriteNtuple() 136 { 271 { 137 // Cannot call base class function, as we ne << 272 // No MPI merging, call base class 138 // on slave; an option in the base class can << 273 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) { 139 << 274 return G4RootAnalysisManager::WriteNtuple(); 140 Message(kVL4, "close", "files"); << 141 << 142 auto result = true; << 143 if (fVNtupleFileManager) { << 144 result &= fVNtupleFileManager->ActionAtClo << 145 } 275 } >> 276 >> 277 auto finalResult = true; 146 278 147 // close file also on Slave << 279 G4String ntupleType; 148 // - the conditoon used in the base class is << 280 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kMain ) ntupleType = "main ntuples"; 149 // if ( (fVNtupleFileManager == nullptr) || << 281 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) ntupleType = "slave ntuples"; 150 // (fVNtupleFileManager->GetMergeMode() << 282 151 if (!fVFileManager->CloseFiles()) { << 283 #ifdef G4VERBOSE 152 Warn("Closing files failed", fkClass, "Clo << 284 if ( fState.GetVerboseL4() ) 153 result = false; << 285 fState.GetVerboseL4()->Message("merge", ntupleType, ""); >> 286 #endif >> 287 >> 288 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kMain ) { >> 289 auto result = fNtupleManager->Merge(); >> 290 finalResult = result && finalResult; >> 291 } >> 292 >> 293 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) { >> 294 auto result = fMpiSlaveNtupleManager->Merge(); >> 295 finalResult = result && finalResult; 154 } 296 } 155 // } << 156 297 157 // delete empty files << 298 #ifdef G4VERBOSE 158 if (!fVFileManager->DeleteEmptyFiles()) { << 299 if ( fState.GetVerboseL1() ) 159 Warn("Deleting empty files failed", fkClas << 300 fState.GetVerboseL1()->Message("merge", ntupleType, ""); 160 result = false; << 301 #endif 161 } << 162 302 163 // reset histograms << 303 return finalResult; 164 if (reset) { << 304 } 165 if (!Reset()) { << 166 Warn("Resetting data failed", fkClass, " << 167 result = false; << 168 } << 169 } << 170 305 171 Message(kVL3, "close", "files", "", result); << 306 //_____________________________________________________________________________ 172 G4cout << "### Done G4RootMpiAnalysisManager << 307 G4bool G4RootMpiAnalysisManager::Reset() >> 308 { >> 309 // Reset histograms and ntuple 173 310 174 return result; << 311 // No MPI merging, call base class >> 312 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kNone ) { >> 313 return G4RootAnalysisManager::Reset(); >> 314 } >> 315 >> 316 auto finalResult = true; >> 317 >> 318 auto result = G4ToolsAnalysisManager::Reset(); >> 319 finalResult = finalResult && result; >> 320 >> 321 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kMain ) { >> 322 result = fNtupleManager->Reset(false); >> 323 finalResult = result && finalResult; >> 324 } >> 325 >> 326 if ( fMpiNtupleMergeMode == G4MpiNtupleMergeMode::kSlave ) { >> 327 result = fMpiSlaveNtupleManager->Reset(false); >> 328 finalResult = result && finalResult; >> 329 } >> 330 >> 331 return finalResult; 175 } 332 } >> 333 >> 334 176 335