Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 // 26 // G4WorkerThread implementation 27 // 28 // Authors: X.Dong, A.Dotti, 2013 29 // ------------------------------------------- 30 31 #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" 44 45 // ------------------------------------------- 46 void G4WorkerThread::SetThreadId(G4int tid) 47 { 48 threadId = tid; 49 } 50 51 // ------------------------------------------- 52 G4int G4WorkerThread::GetThreadId() const 53 { 54 return threadId; 55 } 56 57 // ------------------------------------------- 58 void G4WorkerThread::SetNumberThreads(G4int nw 59 { 60 numThreads = nw; 61 } 62 63 // ------------------------------------------- 64 G4int G4WorkerThread::GetNumberThreads() const 65 { 66 return numThreads; 67 } 68 69 // ------------------------------------------- 70 void G4WorkerThread::BuildGeometryAndPhysicsVe 71 { 72 // Initialise all split classes 73 // with copy of data from master thread 74 75 G4GeometryWorkspace::GetPool()->CreateAndUse 76 G4SolidsWorkspace::GetPool()->CreateAndUseWo 77 G4ParticlesWorkspace::GetPool()->CreateAndUs 78 G4PhysicsListWorkspace::GetPool()->CreateAnd 79 } 80 81 // ------------------------------------------- 82 void G4WorkerThread::DestroyGeometryAndPhysics 83 { 84 // Clear all split classes 85 86 G4GeometryWorkspace::GetPool()->CleanUpAndDe 87 G4SolidsWorkspace::GetPool()->CleanUpAndDest 88 G4ParticlesWorkspace::GetPool()->CleanUpAndD 89 G4PhysicsListWorkspace::GetPool()->CleanUpAn 90 } 91 92 // ------------------------------------------- 93 void G4WorkerThread::UpdateGeometryAndPhysicsV 94 { 95 // ========================================= 96 // Step-0: keep sensitive detector and field 97 // ========================================= 98 // First remember SD and Filed Associated wi 99 // in order to re-use it 100 // (note that all the stuff after this will 101 using LV2SDFM = std::map<G4LogicalVolume*, s 102 LV2SDFM lvmap; 103 104 using R2FSM = std::map<G4Region*, std::pair< 105 R2FSM rgnmap; 106 107 G4LogicalVolumeStore* mLogVolStore = G4Logic 108 for (auto lv : *mLogVolStore) { 109 // The following needs an explanation. 110 // Consider the case in which the user add 111 // the runs. The problem is that the threa 112 // of the G4LogicalVolume object is not in 113 // because the initialization is done once 114 // (see G4MTRunManagerKernel::StartThread 115 // G4WorkerThread::BuildGeometryAndPhysics 116 // The problem is that pointers of SD and 117 // may be invalid pointers (because never 118 // this behavior in our testing). If now w 119 // them in Step-4 below we set invalid poi 120 // Thus we need a way to know if for a giv 121 // or not the SD and FM pointers. 122 // To solve this problem: We assume that t 123 // called also by Master thread, thus for 124 // pointers of SD and Fields are correct. 125 // (LIMITATION: this assumption may be too 126 // memory could instantiate SD only for wo 127 // not to happen!). 128 // Thus if a SD and FieldMgr are needed fo 129 // shadow are !=0 it means that user wants 130 // associated with LV, we get the values a 131 // 132 G4VSensitiveDetector* sd = nullptr; 133 G4FieldManager* fmgr = nullptr; 134 if (lv->GetMasterSensitiveDetector() != nu 135 sd = lv->GetSensitiveDetector(); 136 } 137 if (lv->GetMasterFieldManager() != nullptr 138 fmgr = lv->GetFieldManager(); 139 } 140 if (sd != nullptr || fmgr != nullptr) { 141 lvmap[lv] = std::make_pair(sd, fmgr); 142 } 143 } 144 G4RegionStore* mRegStore = G4RegionStore::Ge 145 for (auto reg : *mRegStore) { 146 G4FastSimulationManager* fsm = reg->GetFas 147 G4UserSteppingAction* usa = reg->GetRegion 148 if (reg != nullptr || usa != nullptr) { 149 rgnmap[reg] = std::make_pair(fsm, usa); 150 } 151 } 152 153 //=========================== 154 // Step-1: Clean the workspace 155 //=========================== 156 G4GeometryWorkspace* geomWorkspace = G4Geome 157 geomWorkspace->DestroyWorkspace(); 158 G4SolidsWorkspace* solidWorkspace = G4Solids 159 solidWorkspace->DestroyWorkspace(); 160 161 //=========================== 162 // Step-2: Re-create and initialize workspac 163 //=========================== 164 geomWorkspace->InitialiseWorkspace(); 165 solidWorkspace->InitialiseWorkspace(); 166 167 //========================================== 168 // Step-4: Restore sensitive detector and fi 169 //========================================== 170 for (const auto& it : lvmap) { 171 G4LogicalVolume* lv = it.first; 172 G4VSensitiveDetector* sd = (it.second).fir 173 G4FieldManager* fmgr = (it.second).second; 174 if (fmgr != nullptr) // What should be th 175 { // We use always false for MT mode 176 lv->SetFieldManager(fmgr, false); 177 } 178 if (sd != nullptr) { 179 lv->SetSensitiveDetector(sd); 180 } 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 } 190 191 // ------------------------------------------- 192 void G4WorkerThread::SetPinAffinity(G4int affi 193 { 194 if (affinity == 0) return; 195 196 #if !defined(WIN32) 197 G4cout << "AFFINITY SET" << G4endl; 198 // Assign this thread to cpus in a round rob 199 G4int offset = affinity; 200 G4int cpuindex = 0; 201 if (std::abs(offset) > G4Threading::G4GetNum 202 G4Exception("G4WorkerThread::SetPinAffinit 203 "Cannot set thread affinity, a 204 "number of cores"); 205 return; 206 } 207 if (offset > 0) // Start assigning affinity 208 { 209 --offset; 210 cpuindex = (GetThreadId() + offset) % G4Th 211 // Round robin 212 } 213 else // Exclude the given CPU 214 { 215 offset *= -1; 216 --offset; 217 G4int myidx = GetThreadId() % (G4Threading 218 cpuindex = myidx + static_cast<G4int>(myid 219 } 220 G4cout << "Setting affinity to:" << cpuindex 221 222 # if defined(G4MULTITHREADED) 223 // Avoid compilation warning in C90 standard 224 G4NativeThread t = pthread_self(); 225 # else 226 G4NativeThread t; 227 # endif 228 G4bool success = G4Threading::G4SetPinAffini 229 if (!success) { 230 G4Exception("G4MTRunManagerKernel::StarThr 231 "Cannot set thread affinity.") 232 } 233 #endif 234 } 235