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 // $Id$ 26 // 27 // 27 /// \file parallel/TBB/B2b/exampleB2b.cc 28 /// \file parallel/TBB/B2b/exampleB2b.cc 28 /// \brief Main program of the B2b example 29 /// \brief Main program of the B2b example 29 30 30 #include "B2ActionInitialization.hh" << 31 #include "B2bDetectorConstruction.hh" 31 #include "B2bDetectorConstruction.hh" 32 #include "FTFP_BERT.hh" << 32 #include "B2ActionInitialization.hh" 33 33 34 #include "G4StepLimiterPhysics.hh" << 34 //G4-TBB interfaces 35 #include "G4TaskRunManager.hh" << 35 #include "tbbUserWorkerInitialization.hh" >> 36 #include "SimpleTbbMasterRunManager.hh" 36 #include "G4Threading.hh" 37 #include "G4Threading.hh" 37 #include "G4UIExecutive.hh" << 38 38 #include "G4UImanager.hh" 39 #include "G4UImanager.hh" 39 #include "G4VisExecutive.hh" << 40 #include "FTFP_BERT.hh" >> 41 #include "G4StepLimiterPhysics.hh" >> 42 40 #include "Randomize.hh" 43 #include "Randomize.hh" 41 44 42 //....oooOO0OOooo........oooOO0OOooo........oo << 45 #ifdef G4VIS_USE >> 46 #include "G4VisExecutive.hh" >> 47 #endif 43 48 44 int main(int argc, char** argv) << 49 #ifdef G4UI_USE >> 50 #include "G4UIExecutive.hh" >> 51 #endif >> 52 >> 53 //TBB includes >> 54 #include <tbb/task_scheduler_init.h> >> 55 #include <tbb/task.h> >> 56 >> 57 >> 58 //This function is very simple: it just start tbb work. >> 59 //This is done in a seperate thread, because for G4 the >> 60 //master cannot live in the same thread where workers are >> 61 //Starting tbb work in a thread guarantees that no workers >> 62 //are created where the master lives (the main thread) >> 63 //Clearly a separate solution is to create and configure master >> 64 //in a separate thread. But this is much simpler. >> 65 G4ThreadFunReturnType startWork(G4ThreadFunArgType arg) 45 { 66 { 46 // Instantiate G4UIExecutive if there are no << 67 tbb::task_list* tasks = static_cast<tbb::task_list*>(arg); 47 G4UIExecutive* ui = 0; << 68 //We assume at least one /run/beamOn was executed, thus the tasklist is now filled, 48 if (argc == 1) { << 69 //lets start TBB 49 ui = new G4UIExecutive(argc, argv); << 70 try { 50 } << 71 std::cout<<"Now calling 'tbb::task::spawn_work_and_wait' "<<std::endl; >> 72 tbb::task::spawn_root_and_wait( *tasks ); >> 73 } catch(std::exception& e) { >> 74 std::cerr<<"Error occurred. Error info is:\""<<e.what()<<"\""<<std::endl; >> 75 } >> 76 return static_cast<G4ThreadFunReturnType>(0); >> 77 } 51 78 >> 79 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 80 >> 81 int main(int argc,char** argv) >> 82 { 52 // Choose the Random engine 83 // Choose the Random engine 53 84 54 G4Random::setTheEngine(new CLHEP::RanecuEngi 85 G4Random::setTheEngine(new CLHEP::RanecuEngine); 55 << 86 56 auto runManager = G4RunManagerFactory::Creat << 87 unsigned int numCoresAvailable= G4Threading::G4GetNumberOfCores(); 57 << 88 unsigned int numberOfCoresToUse= (numCoresAvailable > 1 ) ? 2 : 1 ; 58 //==== Geant4 specific stuff, from now up to << 89 //=== TBB engine initialization 59 // example << 90 tbb::task_scheduler_init init( numberOfCoresToUse ); >> 91 tbb::task_list tasks; >> 92 >> 93 SimpleTbbMasterRunManager* runManager = new SimpleTbbMasterRunManager; >> 94 >> 95 //Set TBB specific data to run-manager, 1 event per tbb::task (e.g. Nevents == N tasks) >> 96 //Note that a /run/beamOn command will just create tasks and add them to tasks >> 97 runManager->SetNumberEventsPerTask(1); //Not needed since 1 is however default >> 98 runManager->SetTaskList(&tasks); >> 99 //Set user-initialization that specify threading model, in this case TBB. >> 100 //This overwrites default that uses pthreads >> 101 runManager->SetUserInitialization(new tbbUserWorkerInitialization ); >> 102 >> 103 //==== Geant4 specific stuff, from now up to END-G4 comment is copy from MT example 60 // Set mandatory initialization classes 104 // Set mandatory initialization classes 61 105 62 runManager->SetUserInitialization(new B2bDet 106 runManager->SetUserInitialization(new B2bDetectorConstruction()); 63 107 64 G4VModularPhysicsList* physicsList = new FTF 108 G4VModularPhysicsList* physicsList = new FTFP_BERT; 65 physicsList->RegisterPhysics(new G4StepLimit 109 physicsList->RegisterPhysics(new G4StepLimiterPhysics()); 66 runManager->SetUserInitialization(physicsLis 110 runManager->SetUserInitialization(physicsList); 67 << 111 68 // Set user action classes 112 // Set user action classes 69 113 70 runManager->SetUserInitialization(new B2Acti 114 runManager->SetUserInitialization(new B2ActionInitialization()); 71 << 115 72 // Initialize G4 kernel 116 // Initialize G4 kernel 73 117 74 runManager->Initialize(); 118 runManager->Initialize(); 75 << 119 76 // Get the pointer to the User Interface man 120 // Get the pointer to the User Interface manager 77 G4UImanager* UImanager = G4UImanager::GetUIp 121 G4UImanager* UImanager = G4UImanager::GetUIpointer(); 78 122 79 if (!ui) // batch mode << 123 if (argc!=1) // batch mode 80 { << 124 { 81 G4String command = "/control/execute "; << 125 G4String command = "/control/execute "; 82 G4String fileName = argv[1]; << 126 G4String fileName = argv[1]; 83 UImanager->ApplyCommand(command + fileName << 127 UImanager->ApplyCommand(command+fileName); 84 } << 128 } 85 else { << 129 else 86 // interactive mode : define UI session << 130 { // interactive mode : define UI session 87 UImanager->ApplyCommand("/control/execute << 131 #if 1 88 if (ui->IsGUI()) UImanager->ApplyCommand(" << 132 G4int nEvents= 50; 89 ui->SessionStart(); << 133 runManager->BeamOn(nEvents); 90 delete ui; << 134 #else 91 } << 135 #ifdef G4UI_USE 92 << 136 G4UIExecutive* ui = new G4UIExecutive(argc, argv); 93 delete runManager; << 137 UImanager->ApplyCommand("/control/execute init.mac"); >> 138 if (ui->IsGUI()) >> 139 UImanager->ApplyCommand("/control/execute gui.mac"); >> 140 ui->SessionStart(); >> 141 delete ui; >> 142 #endif >> 143 #endif >> 144 } >> 145 //END-G4 >> 146 G4Thread aThread; >> 147 G4THREADCREATE(&aThread,startWork,static_cast<G4ThreadFunArgType>(&tasks)); >> 148 >> 149 //Wait for work to be finised >> 150 G4THREADJOIN(aThread); >> 151 >> 152 delete runManager; 94 153 95 return 0; << 154 return 0; 96 } 155 } 97 156 98 //....oooOO0OOooo........oooOO0OOooo........oo 157 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo..... 99 158