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 // G4WorkerThread implementation << 27 // << 28 // Authors: X.Dong, A.Dotti, 2013 << 29 // ------------------------------------------- << 30 << 31 #include "G4WorkerThread.hh" 26 #include "G4WorkerThread.hh" 32 << 33 #include "G4GeometryWorkspace.hh" << 34 #include "G4LogicalVolume.hh" << 35 #include "G4LogicalVolumeStore.hh" << 36 #include "G4MTRunManager.hh" << 37 #include "G4ParticlesWorkspace.hh" << 38 #include "G4PhysicalVolumeStore.hh" << 39 #include "G4PhysicsListWorkspace.hh" << 40 #include "G4Region.hh" << 41 #include "G4RegionStore.hh" << 42 #include "G4SolidsWorkspace.hh" << 43 #include "G4WorkerRunManager.hh" 27 #include "G4WorkerRunManager.hh" >> 28 #include "G4MTRunManager.hh" 44 29 45 // ------------------------------------------- << 46 void G4WorkerThread::SetThreadId(G4int tid) 30 void G4WorkerThread::SetThreadId(G4int tid) 47 { 31 { 48 threadId = tid; << 32 threadId = tid; 49 } 33 } 50 34 51 // ------------------------------------------- << 52 G4int G4WorkerThread::GetThreadId() const 35 G4int G4WorkerThread::GetThreadId() const 53 { 36 { 54 return threadId; << 37 return threadId; 55 } 38 } 56 39 57 // ------------------------------------------- << 58 void G4WorkerThread::SetNumberThreads(G4int nw 40 void G4WorkerThread::SetNumberThreads(G4int nw) 59 { 41 { 60 numThreads = nw; << 42 numThreads = nw; 61 } 43 } 62 44 63 // ------------------------------------------- << 64 G4int G4WorkerThread::GetNumberThreads() const 45 G4int G4WorkerThread::GetNumberThreads() const 65 { 46 { 66 return numThreads; << 47 return numThreads; 67 } 48 } 68 49 69 // ------------------------------------------- << 50 #include "G4GeometryWorkspace.hh" >> 51 #include "G4GeometryWorkspacePool.hh" >> 52 #include "G4SolidsWorkspace.hh" >> 53 #include "G4SolidsWorkspacePool.hh" >> 54 #include "G4ParticlesWorkspace.hh" >> 55 #include "G4PhysicsListWorkspace.hh" >> 56 70 void G4WorkerThread::BuildGeometryAndPhysicsVe 57 void G4WorkerThread::BuildGeometryAndPhysicsVector() 71 { 58 { 72 // Initialise all split classes << 59 // Initialise all split classes in the geometry with copy of data from master thread 73 // with copy of data from master thread << 60 G4GeometryWorkspacePool::GetInstance()->CreateAndUseWorkspace(); 74 << 61 G4SolidsWorkspacePool::GetInstance()->CreateAndUseWorkspace(); 75 G4GeometryWorkspace::GetPool()->CreateAndUse << 62 76 G4SolidsWorkspace::GetPool()->CreateAndUseWo << 63 G4ParticlesWorkspace::GetPool()->CreateAndUseWorkspace(); 77 G4ParticlesWorkspace::GetPool()->CreateAndUs << 64 G4PhysicsListWorkspace::GetPool()->CreateAndUseWorkspace(); 78 G4PhysicsListWorkspace::GetPool()->CreateAnd << 65 // const_cast<G4VUPLManager&>(G4VUserPhysicsList::GetSubInstanceManager()).NewSubInstances(); >> 66 // const_cast<G4VPCManager&>(G4VPhysicsConstructor::GetSubInstanceManager()).NewSubInstances(); >> 67 // const_cast<G4VMPLManager&>(G4VModularPhysicsList::GetSubInstanceManager()).WorkerCopySubInstanceArray(); 79 } 68 } 80 69 81 // ------------------------------------------- << 82 void G4WorkerThread::DestroyGeometryAndPhysics 70 void G4WorkerThread::DestroyGeometryAndPhysicsVector() 83 { 71 { 84 // Clear all split classes << 72 #if 0 85 << 73 // Manage Geometry Workspace explicitly 86 G4GeometryWorkspace::GetPool()->CleanUpAndDe << 74 fGeometryWrk->ReleaseAndDestroyWorkspace(); 87 G4SolidsWorkspace::GetPool()->CleanUpAndDest << 75 delete fGeometryWrk; 88 G4ParticlesWorkspace::GetPool()->CleanUpAndD << 76 fGeometryWrk=0; 89 G4PhysicsListWorkspace::GetPool()->CleanUpAn << 77 #else >> 78 // Alternative: >> 79 // Initialise all split classes in the geometry with copy of data from master thread >> 80 // G4GeometryWorkspacePool::GetInstance()->ReleaseAndDestroyMyWorkspace(); >> 81 G4GeometryWorkspacePool::GetInstance()->GetWorkspace()->DestroyWorkspace(); >> 82 G4SolidsWorkspacePool::GetInstance()->GetWorkspace()->DestroyWorkspace(); >> 83 G4ParticlesWorkspace::GetPool()->GetWorkspace()->DestroyWorkspace(); >> 84 >> 85 G4PhysicsListWorkspace::GetPool()->GetWorkspace()->DestroyWorkspace(); >> 86 #endif >> 87 >> 88 // const_cast<G4VUPLManager&>(G4VUserPhysicsList::GetSubInstanceManager()).FreeWorker(); >> 89 // const_cast<G4VPCManager&>(G4VPhysicsConstructor::GetSubInstanceManager()).FreeWorker(); >> 90 // const_cast<G4VMPLManager&>(G4VModularPhysicsList::GetSubInstanceManager()).FreeWorker(); 90 } 91 } 91 92 92 // ------------------------------------------- << 93 #include "G4Region.hh" >> 94 #include "G4RegionStore.hh" >> 95 #include "G4LogicalVolume.hh" >> 96 #include "G4Region.hh" >> 97 #include "G4PhysicalVolumeStore.hh" >> 98 #include "G4LogicalVolumeStore.hh" >> 99 >> 100 93 void G4WorkerThread::UpdateGeometryAndPhysicsV 101 void G4WorkerThread::UpdateGeometryAndPhysicsVectorFromMaster() 94 { 102 { 95 // ========================================= << 103 //================================================= 96 // Step-0: keep sensitive detector and field << 104 //Step-0: keep sensitive detector and field manager 97 // ========================================= << 105 //================================================= 98 // First remember SD and Filed Associated wi << 106 // First remember SD and Filed Associated with worker 99 // in order to re-use it << 107 // in order to re-use it 100 // (note that all the stuff after this will << 108 // (note that all the stuff after this will reset SD and Field 101 using LV2SDFM = std::map<G4LogicalVolume*, s << 109 typedef std::map<G4LogicalVolume*,std::pair<G4VSensitiveDetector*,G4FieldManager*> > LV2SDFM; 102 LV2SDFM lvmap; << 110 LV2SDFM lvmap; 103 << 111 // typedef std::map<G4LogicalVolume*,std::pair<G4Region*,G4bool> > LV2Region; 104 using R2FSM = std::map<G4Region*, std::pair< << 112 // LV2Region lv2rmap; 105 R2FSM rgnmap; << 113 typedef std::map<G4Region*,std::pair<G4FastSimulationManager*,G4UserSteppingAction*> > R2FSM; 106 << 114 R2FSM rgnmap; 107 G4LogicalVolumeStore* mLogVolStore = G4Logic << 115 G4LogicalVolumeStore* mLogVolStore = G4LogicalVolumeStore::GetInstance(); 108 for (auto lv : *mLogVolStore) { << 116 for(size_t ip=0; ip<mLogVolStore->size(); ip++) 109 // The following needs an explanation. << 117 { 110 // Consider the case in which the user add << 118 G4LogicalVolume *lv = (*mLogVolStore)[ip]; 111 // the runs. The problem is that the threa << 119 //The following needs an explanation. 112 // of the G4LogicalVolume object is not in << 120 //Consider the case in which the user adds one LogVolume between the runs. The problem is that the thread-local part 113 // because the initialization is done once << 121 //(split class) of the G4LogicalVolume object is not initialized for workers because the initialization is done once when the 114 // (see G4MTRunManagerKernel::StartThread << 122 //thread starts (see G4MTRunManagerKernel::StartThread Step-2 that calls G4WorkerThread::BuildGeometryAndPhysicsVector in this class) 115 // G4WorkerThread::BuildGeometryAndPhysics << 123 //The problem is that pointers of SD and FM for these newly added LV 116 // The problem is that pointers of SD and << 124 //may be invalid pointers (because never initialized, we have seen this behavior in our testing). If now we remember 117 // may be invalid pointers (because never << 125 //them and re-use them in Step-4 below we set invalid pointers to LV for this thread. 118 // this behavior in our testing). If now w << 126 //Thus we need a way to know if for a given LV we need to remember or not the SD and FM pointers. 119 // them in Step-4 below we set invalid poi << 127 //To solve this problem: We assume that the ConstructSDandField is called also by Master thread 120 // Thus we need a way to know if for a giv << 128 //thus for newly added LV the shadow pointers of SD and Fields are correct. 121 // or not the SD and FM pointers. << 129 // (LIMITATION: this assumption may be too stringent, a user to save memory could instantiate SD only 122 // To solve this problem: We assume that t << 130 // for workers, but we require this not to happen!). 123 // called also by Master thread, thus for << 131 // Thus is a SD and FieldMgr are needed for this particular LV, and shadow are !=0 it means that user 124 // pointers of SD and Fields are correct. << 132 // wants an SD and FM to be associated with LV, we get the values and we remember them. 125 // (LIMITATION: this assumption may be too << 133 G4VSensitiveDetector* sd = 0; 126 // memory could instantiate SD only for wo << 134 G4FieldManager* fmgr = 0; 127 // not to happen!). << 135 if ( lv->GetMasterSensitiveDetector() != 0 ) sd = lv->GetSensitiveDetector(); 128 // Thus if a SD and FieldMgr are needed fo << 136 if ( lv->GetMasterFieldManager() != 0 ) fmgr = lv->GetFieldManager(); 129 // shadow are !=0 it means that user wants << 137 if ( sd || fmgr ) lvmap[lv] = std::make_pair(sd,fmgr); 130 // associated with LV, we get the values a << 138 // G4Region* rgn = lv->GetRegion(); 131 // << 139 // G4bool isRoot = lv->IsRootRegion(); 132 G4VSensitiveDetector* sd = nullptr; << 140 // if ( rgn || isRoot ) lv2rmap[lv] = std::make_pair(rgn,isRoot); 133 G4FieldManager* fmgr = nullptr; << 141 } 134 if (lv->GetMasterSensitiveDetector() != nu << 142 G4RegionStore* mRegStore = G4RegionStore::GetInstance(); 135 sd = lv->GetSensitiveDetector(); << 143 for(size_t ir=0; ir<mRegStore->size(); ir++) 136 } << 144 { 137 if (lv->GetMasterFieldManager() != nullptr << 145 G4Region* reg = (*mRegStore)[ir]; 138 fmgr = lv->GetFieldManager(); << 146 G4FastSimulationManager* fsm = reg->GetFastSimulationManager(); 139 } << 147 G4UserSteppingAction* usa = reg->GetRegionalSteppingAction(); 140 if (sd != nullptr || fmgr != nullptr) { << 148 if ( reg || usa ) rgnmap[reg] = std::make_pair(fsm,usa); 141 lvmap[lv] = std::make_pair(sd, fmgr); << 149 } 142 } << 150 143 } << 151 //=========================== 144 G4RegionStore* mRegStore = G4RegionStore::Ge << 152 //Step-1: Clean the workspace 145 for (auto reg : *mRegStore) { << 153 //=========================== 146 G4FastSimulationManager* fsm = reg->GetFas << 154 G4GeometryWorkspace* geomWorkspace= G4GeometryWorkspacePool::GetInstance()->GetWorkspace(); 147 G4UserSteppingAction* usa = reg->GetRegion << 155 geomWorkspace->DestroyWorkspace(); 148 if (reg != nullptr || usa != nullptr) { << 156 G4SolidsWorkspace* solidWorkspace = G4SolidsWorkspacePool::GetInstance()->GetWorkspace(); 149 rgnmap[reg] = std::make_pair(fsm, usa); << 157 solidWorkspace->DestroyWorkspace(); 150 } << 158 151 } << 159 //=========================== >> 160 //Step-2: Re-create and initialize workspace >> 161 //=========================== >> 162 geomWorkspace->InitialiseWorkspace(); >> 163 solidWorkspace->InitialiseWorkspace(); 152 164 153 //=========================== << 165 // Alternative 154 // Step-1: Clean the workspace << 166 // 155 //=========================== << 167 // To wipe, release and get a new one ... 156 G4GeometryWorkspace* geomWorkspace = G4Geome << 168 // G4GeometryWorkspacePool *fWorkspaceMgr= G4GeometryWorkspaceOutlet::GetInstance(); 157 geomWorkspace->DestroyWorkspace(); << 169 // fWorkspaceMgr->ReleaseAndDestroyMyWorkspace(); 158 G4SolidsWorkspace* solidWorkspace = G4Solids << 170 // Now re-create 159 solidWorkspace->DestroyWorkspace(); << 171 // fWorkspaceMgr->CreateAndUseWorkspace(); 160 << 172 161 //=========================== << 173 162 // Step-2: Re-create and initialize workspac << 174 //=================================================== 163 //=========================== << 175 //Step-4: Restore sensitive detector and field manaer 164 geomWorkspace->InitialiseWorkspace(); << 176 //=================================================== 165 solidWorkspace->InitialiseWorkspace(); << 177 for ( LV2SDFM::const_iterator it = lvmap.begin() ; it != lvmap.end() ; ++it ) 166 << 178 { 167 //========================================== << 179 G4LogicalVolume* lv = it->first; 168 // Step-4: Restore sensitive detector and fi << 180 G4VSensitiveDetector* sd = (it->second).first; 169 //========================================== << 181 G4FieldManager* fmgr = (it->second).second; 170 for (const auto& it : lvmap) { << 182 if(fmgr) lv->SetFieldManager(fmgr, false); //What should be the second parameter? We use always false for MT mode 171 G4LogicalVolume* lv = it.first; << 183 if(sd) lv->SetSensitiveDetector(sd); 172 G4VSensitiveDetector* sd = (it.second).fir << 184 } 173 G4FieldManager* fmgr = (it.second).second; << 185 174 if (fmgr != nullptr) // What should be th << 186 // for ( LV2Region::const_iterator it2 = lv2rmap.begin() ; it2 != lv2rmap.end() ; it2++ ) 175 { // We use always false for MT mode << 187 // { 176 lv->SetFieldManager(fmgr, false); << 188 // G4LogicalVolume* lv2 = it2->first; 177 } << 189 // G4Region* rgn = (it2->second).first; 178 if (sd != nullptr) { << 190 // if(rgn) lv2->SetRegion(rgn); 179 lv->SetSensitiveDetector(sd); << 191 // G4bool isRoot = (it2->second).second; >> 192 // lv2->SetRegionRootFlag(isRoot); >> 193 // } >> 194 >> 195 >> 196 for ( R2FSM::const_iterator it3 = rgnmap.begin() ; it3 != rgnmap.end() ; it3++ ) >> 197 { >> 198 G4Region* reg = it3->first; >> 199 G4FastSimulationManager* fsm = (it3->second).first; >> 200 if(fsm) reg->SetFastSimulationManager(fsm); >> 201 G4UserSteppingAction* usa = (it3->second).second; >> 202 if(usa) reg->SetRegionalSteppingAction(usa); 180 } 203 } 181 } << 182 for (const auto& it3 : rgnmap) { << 183 G4Region* reg = it3.first; << 184 G4FastSimulationManager* fsm = (it3.second << 185 if (fsm != nullptr) reg->SetFastSimulation << 186 G4UserSteppingAction* usa = (it3.second).s << 187 if (usa != nullptr) reg->SetRegionalSteppi << 188 } << 189 } 204 } 190 205 191 // ------------------------------------------- << 192 void G4WorkerThread::SetPinAffinity(G4int affi 206 void G4WorkerThread::SetPinAffinity(G4int affinity) const 193 { 207 { 194 if (affinity == 0) return; << 208 if ( affinity == 0 ) return; 195 << 196 #if !defined(WIN32) 209 #if !defined(WIN32) 197 G4cout << "AFFINITY SET" << G4endl; << 210 G4cout<<"AFFINITY SET"<<G4endl; 198 // Assign this thread to cpus in a round rob << 211 //Assign this thread to cpus in a round robin way 199 G4int offset = affinity; 212 G4int offset = affinity; 200 G4int cpuindex = 0; 213 G4int cpuindex = 0; 201 if (std::abs(offset) > G4Threading::G4GetNum << 214 if ( std::abs(offset)>G4Threading::G4GetNumberOfCores() ) { 202 G4Exception("G4WorkerThread::SetPinAffinit << 215 G4Exception("G4WorkerThread::SetPinAffinity","Run0035", 203 "Cannot set thread affinity, a << 216 JustWarning, 204 "number of cores"); << 217 "Cannot set thread affinity, affinity parameter larger than number of cores"); 205 return; << 218 return; 206 } << 219 } 207 if (offset > 0) // Start assigning affinity << 220 if (offset>0) { //Start assigning affinity to given CPU 208 { << 221 --offset; 209 --offset; << 222 cpuindex = (GetThreadId()+offset) % G4Threading::G4GetNumberOfCores(); //Round robin 210 cpuindex = (GetThreadId() + offset) % G4Th << 223 } else {//Exclude the given CPU 211 // Round robin << 224 offset *= -1; 212 } << 225 --offset; 213 else // Exclude the given CPU << 226 G4int myidx = GetThreadId()%(G4Threading::G4GetNumberOfCores()-1); 214 { << 227 cpuindex = myidx + (myidx>=offset); 215 offset *= -1; << 228 } 216 --offset; << 229 G4cout<<"Setting affinity to:"<<cpuindex<<G4endl; 217 G4int myidx = GetThreadId() % (G4Threading << 230 //Avoid compilation warning in C90 standard w/o MT 218 cpuindex = myidx + static_cast<G4int>(myid << 231 #if defined(G4MULTITHREADED) 219 } << 232 G4Thread t = G4THREADSELF(); 220 G4cout << "Setting affinity to:" << cpuindex << 233 #else 221 << 234 G4Thread t; 222 # if defined(G4MULTITHREADED) << 235 #endif 223 // Avoid compilation warning in C90 standard << 236 G4bool success = G4Threading::G4SetPinAffinity(cpuindex,t); 224 G4NativeThread t = pthread_self(); << 237 if ( ! success ) { 225 # else << 238 G4Exception("G4MTRunManagerKernel::StarThread","Run0035",JustWarning,"Cannot set thread affinity."); 226 G4NativeThread t; << 227 # endif << 228 G4bool success = G4Threading::G4SetPinAffini << 229 if (!success) { << 230 G4Exception("G4MTRunManagerKernel::StarThr << 231 "Cannot set thread affinity.") << 232 } 239 } 233 #endif 240 #endif 234 } 241 } 235 242