Geant4_10
G4MTRunManagerKernel.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 
27 #include "G4MTRunManagerKernel.hh"
28 #include "G4RegionStore.hh"
29 #include "G4StateManager.hh"
30 #include "G4AutoLock.hh"
31 
32 std::vector<G4WorkerRunManager*>* G4MTRunManagerKernel::workerRMvector = 0;
33 
34 namespace {
35  G4Mutex workerRMMutex = G4MUTEX_INITIALIZER;
36 }
37 
39 {
40  //This version of the constructor should never be called in sequential mode!
41 #ifndef G4MULTITHREADED
43  msg<<"Geant4 code is compiled without multi-threading support (-DG4MULTITHREADED is set to off).";
44  msg<<" This type of RunManager can only be used in mult-threaded applications.";
45  G4Exception("G4RunManagerKernel::G4RunManagerKernel()","Run0035",FatalException,msg);
46 #endif
47  if(!workerRMvector) workerRMvector = new std::vector<G4WorkerRunManager*>;
48 }
49 
51 {
52  if(!workerRMvector)
53  {
54  if(workerRMvector->size()>0)
55  {
57  msg<<"G4MTRunManagerKernel is to be deleted while "
58  <<workerRMvector->size()<<" G4WorkerRunManager are still alive.";
59  G4Exception("G4RunManagerKernel::~G4RunManagerKernel()",
60  "Run10035",FatalException,msg);
61  }
62  delete workerRMvector;
63  workerRMvector = 0;
64  }
65 }
66 
68 {
69  //Behavior is the same as base class (sequential mode)
70  //ShadowProcess pointer == process poitner
72 }
73 
74 #include "G4WorkerRunManager.hh"
78 #include "G4WorkerThread.hh"
79 #include "G4UImanager.hh"
80 #include "G4LogicalVolume.hh"
81 #include "G4VPhysicalVolume.hh"
82 #include "G4PVReplica.hh"
83 #include "G4Region.hh"
84 #include "G4Material.hh"
85 #include "G4PhysicsVector.hh"
86 #include "G4VDecayChannel.hh"
87 #include "G4PhysicalVolumeStore.hh"
88 #include "G4MaterialTable.hh"
89 #include "G4PolyconeSide.hh"
90 #include "G4PolyhedraSide.hh"
91 #include "G4PVParameterised.hh"
92 #include "G4VUserPhysicsList.hh"
93 #include "G4VPhysicsConstructor.hh"
94 #include "G4VModularPhysicsList.hh"
95 
96 G4ThreadLocal G4WorkerThread* G4MTRunManagerKernel::wThreadContext = 0;
98 { return wThreadContext; }
99 
101 {
105  // Here is not sequential anymore and G4UserWorkerThreadInitialization is
106  // a shared user initialization class
107  // This means this method cannot use data memebers of G4RunManagerKernel
108  // unless they are invariant ("read-only") and can be safely shared.
109  // All the rest that is not invariant should be incapsualted into
110  // the context (or, as for wThreadContext be G4ThreadLocal)
112 //#ifdef G4MULTITHREADED
113 // turnontpmalloc();
114 //#endif
115 
116  wThreadContext = static_cast<G4WorkerThread*>(context);
118 
119  //============================
120  //Step-0: Thread ID
121  //============================
122  //Initliazie per-thread stream-output
123  //The following line is needed before we actually do IO initialization
124  //becasue the constructor of UI manager resets the IO destination.
125  G4int thisID = wThreadContext->GetThreadId();
128 
129  //============================
130  //Step-1: Random number engine
131  //============================
132  //RNG Engine needs to be initialized by "cloning" the master one.
133  const CLHEP::HepRandomEngine* masterEngine = masterRM->getMasterRandomEngine();
134  masterRM->GetUserWorkerThreadInitialization()->SetupRNGEngine(masterEngine);
135 
136  //============================
137  //Step-2: Initialize worker thread
138  //============================
139  if(masterRM->GetUserWorkerInitialization())
141  if(masterRM->GetUserActionInitialization())
142  {
144  if ( sv ) { G4VSteppingVerbose::SetInstance(sv); }
145  }
146  //Now initialize worker part of shared objects (geometry/physics)
147  wThreadContext->BuildGeometryAndPhysicsVector();
148  G4WorkerRunManager* wrm
150  wrm->SetWorkerThread(wThreadContext);
151  G4AutoLock wrmm(&workerRMMutex);
152  workerRMvector->push_back(wrm);
153  wrmm.unlock();
154 
155  //================================
156  //Step-3: Setup worker run manager
157  //================================
158  // Set the detector and physics list to the worker thread. Share with master
159  const G4VUserDetectorConstruction* detector = masterRM->GetUserDetectorConstruction();
160  wrm->G4RunManager::SetUserInitialization(const_cast<G4VUserDetectorConstruction*>(detector));
161  const G4VUserPhysicsList* physicslist = masterRM->GetUserPhysicsList();
162  wrm->SetUserInitialization(const_cast<G4VUserPhysicsList*>(physicslist));
163 
164  //================================
165  //Step-4: Initialize worker run manager
166  //================================
167  if(masterRM->GetUserActionInitialization())
168  { masterRM->GetNonConstUserActionInitialization()->Build(); }
169  if(masterRM->GetUserWorkerInitialization())
170  { masterRM->GetUserWorkerInitialization()->WorkerStart(); }
171  wrm->Initialize();
172 
173  //================================
174  //Step5: Loop over requests from the master thread
175  //================================
177  while( nextAction != G4MTRunManager::ENDWORKER )
178  {
179  if( nextAction == G4MTRunManager::NEXTITERATION ) // start the next run
180  {
181  //The following code deals with changing materials between runs
182  static G4ThreadLocal G4bool skipInitialization = true;
183  if(skipInitialization)
184  {
185  // re-initialization is not necessary for the first run
186  skipInitialization = false;
187  }
188  else
189  {
190 // ReinitializeGeometry();
191  wThreadContext->UpdateGeometryAndPhysicsVectorFromMaster();
192  }
193 
194  // Execute UI commands stored in the masther UI manager
195  std::vector<G4String> cmds = masterRM->GetCommandStack();
196  G4UImanager* uimgr = G4UImanager::GetUIpointer(); //TLS instance
197  std::vector<G4String>::const_iterator it = cmds.begin();
198  for(;it!=cmds.end();it++)
199  { uimgr->ApplyCommand(*it); }
200  //Start this run
201  G4int numevents = masterRM->GetNumberOfEventsToBeProcessed();
202  G4String macroFile = masterRM->GetSelectMacro();
203  G4int numSelect = masterRM->GetNumberOfSelectEvents();
204  if ( macroFile == "" || macroFile == " " )
205  {
206  wrm->BeamOn(numevents,0,numSelect);
207  }
208  else
209  {
210  wrm->BeamOn(numevents,macroFile,numSelect);
211  }
212  }
213  else
214  {
216  d<<"Cannot continue, this worker has been requested an unknwon action: "
217  <<nextAction<<" expecting: ENDWORKER(=" <<G4MTRunManager::ENDWORKER
218  <<") or NEXTITERATION(="<<G4MTRunManager::NEXTITERATION<<")";
219  G4Exception("G4MTRunManagerKernel::StartThread","Run0035",FatalException,d);
220  }
221 
222  //Now wait for master thread to signal new action to be performed
223  nextAction = masterRM->ThisWorkerWaitForNextAction();
224  } //No more actions to perform
225 
226  //===============================
227  //Step-6: Terminate worker thread
228  //===============================
229  if(masterRM->GetUserWorkerInitialization())
230  { masterRM->GetUserWorkerInitialization()->WorkerStop(); }
231 
232  wrmm.lock();
233  std::vector<G4WorkerRunManager*>::iterator itrWrm = workerRMvector->begin();
234  for(;itrWrm!=workerRMvector->end();itrWrm++)
235  {
236  if((*itrWrm)==wrm)
237  {
238  workerRMvector->erase(itrWrm);
239  break;
240  }
241  }
242  wrmm.unlock();
243  delete wrm;
244  wThreadContext->DestroyGeometryAndPhysicsVector();
245  wThreadContext = 0;
246 
247  return static_cast<void*>(0);
248 }
249 
250 //Now moved to G4WorkerThread
251 //void G4MTRunManagerKernel::ReinitializeGeometry()
252 //{
253 // G4AutoLock wrmm(&workerRMMutex);
254 // //=================================================
255 // //Step-0: keep sensitive detector and field manager
256 // //=================================================
257 // typedef std::map<G4LogicalVolume*,std::pair<G4VSensitiveDetector*,G4FieldManager*> > LV2SDFM;
258 // LV2SDFM lvmap;
259 // G4PhysicalVolumeStore* mphysVolStore = G4PhysicalVolumeStore::GetInstance();
260 // for(size_t ip=0;ip<mphysVolStore->size();ip++)
261 // {
262 // G4VPhysicalVolume* pv = (*mphysVolStore)[ip];
263 // G4LogicalVolume *lv = pv->GetLogicalVolume();
264 // G4VSensitiveDetector* sd = lv->GetSensitiveDetector();
265 // G4FieldManager* fm = lv->GetFieldManager();
266 // if(sd||fm) lvmap[lv] = std::make_pair(sd,fm);
267 // }
268 //
269 // //===========================
270 // //Step-1: Clean the instances
271 // //===========================
272 // const_cast<G4LVManager&>(G4LogicalVolume::GetSubInstanceManager()).FreeSlave();
273 // const_cast<G4PVManager&>(G4VPhysicalVolume::GetSubInstanceManager()).FreeSlave();
274 // const_cast<G4PVRManager&>(G4PVReplica::GetSubInstanceManager()).FreeSlave();
275 // const_cast<G4RegionManager&>(G4Region::GetSubInstanceManager()).FreeSlave();
276 // const_cast<G4PlSideManager&>(G4PolyconeSide::GetSubInstanceManager()).FreeSlave();
277 // const_cast<G4PhSideManager&>(G4PolyhedraSide::GetSubInstanceManager()).FreeSlave();
278 //
279 // //===========================
280 // //Step-2: Re-create instances
281 // //===========================
282 // const_cast<G4LVManager&>(G4LogicalVolume::GetSubInstanceManager()).SlaveCopySubInstanceArray();
283 // const_cast<G4PVManager&>(G4VPhysicalVolume::GetSubInstanceManager()).SlaveCopySubInstanceArray();
284 // const_cast<G4PVRManager&>(G4PVReplica::GetSubInstanceManager()).SlaveCopySubInstanceArray();
285 // const_cast<G4RegionManager&>(G4Region::GetSubInstanceManager()).SlaveInitializeSubInstance();
286 // const_cast<G4PlSideManager&>(G4PolyconeSide::GetSubInstanceManager()).SlaveInitializeSubInstance();
287 // const_cast<G4PhSideManager&>(G4PolyhedraSide::GetSubInstanceManager()).SlaveInitializeSubInstance();
288 //
289 // //===============================
290 // //Step-3: Re-initialize instances
291 // //===============================
292 // for(size_t ip=0;ip<mphysVolStore->size();ip++)
293 // {
294 // G4VPhysicalVolume* physVol = (*mphysVolStore)[ip];
295 // G4LogicalVolume* g4LogicalVolume = physVol->GetLogicalVolume();
296 // G4VSolid* g4VSolid = g4LogicalVolume->GetMasterSolid(); // shadow pointer
297 // G4PVReplica* g4PVReplica = 0;
298 // g4PVReplica = dynamic_cast<G4PVReplica*>(physVol);
299 // if(g4PVReplica) // if the volume is a replica
300 // {
301 // G4VSolid *slaveg4VSolid = g4VSolid->Clone();
302 // g4LogicalVolume->InitialiseWorker(g4LogicalVolume,slaveg4VSolid,0);
303 // }
304 // else
305 // { g4LogicalVolume->InitialiseWorker(g4LogicalVolume,g4VSolid,0); }
306 // }
307 //
308 // //===================================================
309 // //Step-4: Restore sensitive detector and field manaer
310 // //===================================================
311 // LV2SDFM::const_iterator it = lvmap.begin();
312 // for(; it!=lvmap.end() ; ++it )
313 // {
314 // G4LogicalVolume* lv = it->first;
315 // G4VSensitiveDetector* sd = (it->second).first;
316 // G4FieldManager* fm = (it->second).second;
317 // lv->SetFieldManager(fm, false);
318 // lv->SetSensitiveDetector(sd);
319 // }
320 // wrmm.unlock();
321 //}
322 
323 #include "G4ParticleDefinition.hh"
324 #include "G4ParticleTable.hh"
326 #include "G4DecayTable.hh"
327 #include "G4VDecayChannel.hh"
328 
330 {
333  pItr->reset();
334  while((*pItr)())
335  {
336  G4DecayTable* dt = pItr->value()->GetDecayTable();
337  if(dt)
338  {
339  G4int nCh = dt->entries();
340  for(G4int i=0;i<nCh;i++)
341  { dt->GetDecayChannel(i)->GetDaughter(0); }
342  }
343  }
344 }
345 
347 {
348  G4AutoLock wrmm(&workerRMMutex);
349  std::vector<G4WorkerRunManager*>::iterator itr = workerRMvector->begin();
350  for(;itr!=workerRMvector->end();itr++)
351  { (*itr)->AbortRun(softAbort); }
352 }
353 
static void * StartThread(void *context)
std::vector< G4String > GetCommandStack()
virtual void SetupShadowProcess() const
void SetUpForAThread(G4int tId)
Definition: G4UImanager.cc:700
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
virtual void SetupRNGEngine(const CLHEP::HepRandomEngine *aRNGEngine) const
Float_t d
Definition: plot.C:237
static void UpdateGeometryAndPhysicsVectorFromMaster()
G4String GetSelectMacro() const
const G4VUserDetectorConstruction * GetUserDetectorConstruction() const
G4ParticleDefinition * GetDaughter(G4int anIndex)
static void BuildGeometryAndPhysicsVector()
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:105
virtual void BeamOn(G4int n_event, const char *macroFile=0, G4int n_select=-1)
G4VDecayChannel * GetDecayChannel(G4int index) const
const G4UserWorkerInitialization * GetUserWorkerInitialization() const
virtual G4WorkerRunManager * CreateWorkerRunManager() const
#define G4ThreadLocal
Definition: tls.hh:52
virtual void SetUserInitialization(G4VUserPhysicsList *userInit)
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:158
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:58
G4int entries() const
void SetupShadowProcess() const
void reset(G4bool ifSkipIon=true)
bool G4bool
Definition: G4Types.hh:79
static G4WorkerThread * GetWorkerThread()
static G4MTRunManager * GetMasterRunManager()
virtual WorkerActionRequest ThisWorkerWaitForNextAction()
virtual void Build() const =0
G4int GetThreadId() const
G4int GetNumberOfEventsToBeProcessed() const
const G4VUserActionInitialization * GetUserActionInitialization() const
const XML_Char * context
Definition: expat.h:434
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int GetNumberOfSelectEvents() const
void SetWorkerThread(G4WorkerThread *wc)
Sets the worker context.
const G4UserWorkerThreadInitialization * GetUserWorkerThreadInitialization() const
G4int G4Mutex
Definition: G4Threading.hh:156
static void SetInstance(G4VSteppingVerbose *Instance)
static G4ParticleTable * GetParticleTable()
G4VUserActionInitialization * GetNonConstUserActionInitialization() const
const CLHEP::HepRandomEngine * getMasterRandomEngine() const
virtual G4VSteppingVerbose * InitializeSteppingVerbose() const
static void DestroyGeometryAndPhysicsVector()
virtual void Initialize()
const G4VUserPhysicsList * GetUserPhysicsList() const
void BroadcastAbortRun(G4bool softAbort)
G4PTblDicIterator * GetIterator() const
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:419