Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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
28#include "G4AutoLock.hh"
29#include "G4RegionStore.hh"
30#include "G4StateManager.hh"
31
32std::vector<G4WorkerRunManager*>* G4MTRunManagerKernel::workerRMvector = 0;
33
34namespace
35{
36 G4Mutex workerRMMutex = G4MUTEX_INITIALIZER;
37}
38
40 : G4RunManagerKernel(masterRMK)
41{
42 // This version of the constructor should never be called in sequential mode!
43#ifndef G4MULTITHREADED
45 msg << "Geant4 code is compiled without multi-threading support "
46 "(-DG4MULTITHREADED "
47 "is set to off).";
48 msg << " This type of RunManager can only be used in mult-threaded "
49 "applications.";
50 G4Exception("G4RunManagerKernel::G4RunManagerKernel()", "Run0109",
51 FatalException, msg);
52#endif
53 G4AutoLock l(&workerRMMutex);
54 if(!workerRMvector)
55 workerRMvector = new std::vector<G4WorkerRunManager*>;
56 l.unlock();
57 // Set flag that a MT-type kernel has been instantiated
59}
60
62{
63 G4AutoLock l(&workerRMMutex);
64 if(workerRMvector)
65 {
66 if(workerRMvector->size() > 0)
67 {
69 msg << "G4MTRunManagerKernel is to be deleted while "
70 << workerRMvector->size() << " G4WorkerRunManager are still alive.";
71 G4Exception("G4RunManagerKernel::~G4RunManagerKernel()", "Run10035",
72 FatalException, msg);
73 }
74 delete workerRMvector;
75 workerRMvector = 0;
76 }
77}
78
80{
81 // Behavior is the same as base class (sequential mode)
82 // ShadowProcess pointer == process poitner
84}
85
86#include "G4LogicalVolume.hh"
87#include "G4Material.hh"
88#include "G4MaterialTable.hh"
89#include "G4PVParameterised.hh"
90#include "G4PVReplica.hh"
92#include "G4PhysicsVector.hh"
93#include "G4PolyconeSide.hh"
94#include "G4PolyhedraSide.hh"
95#include "G4Region.hh"
96#include "G4UImanager.hh"
99#include "G4VDecayChannel.hh"
101#include "G4VPhysicalVolume.hh"
104#include "G4VUserPhysicsList.hh"
105#include "G4WorkerRunManager.hh"
106#include "G4WorkerThread.hh"
107
108G4ThreadLocal G4WorkerThread* G4MTRunManagerKernel::wThreadContext = 0;
110{
111 return wThreadContext;
112}
113
115{
116 //!!!!!!!!!!!!!!!!!!!!!!!!!!
117 //!!!!!! IMPORTANT !!!!!!!!!
118 //!!!!!!!!!!!!!!!!!!!!!!!!!!
119 // Here is not sequential anymore and G4UserWorkerThreadInitialization is
120 // a shared user initialization class
121 // This means this method cannot use data memebers of G4RunManagerKernel
122 // unless they are invariant ("read-only") and can be safely shared.
123 // All the rest that is not invariant should be incapsualted into
124 // the context (or, as for wThreadContext be G4ThreadLocal)
125 //!!!!!!!!!!!!!!!!!!!!!!!!!!
126 //#ifdef G4MULTITHREADED
127 // turnontpmalloc();
128 //#endif
130 wThreadContext = context;
132
133 //============================
134 // Step-0: Thread ID
135 //============================
136 // Initliazie per-thread stream-output
137 // The following line is needed before we actually do IO initialization
138 // becasue the constructor of UI manager resets the IO destination.
139 G4int thisID = wThreadContext->GetThreadId();
142
143 //============================
144 // Optimization: optional
145 //============================
146 // Enforce thread affinity if requested
147 wThreadContext->SetPinAffinity(masterRM->GetPinAffinity());
148
149 //============================
150 // Step-1: Random number engine
151 //============================
152 // RNG Engine needs to be initialized by "cloning" the master one.
153 const CLHEP::HepRandomEngine* masterEngine =
154 masterRM->getMasterRandomEngine();
155 masterRM->GetUserWorkerThreadInitialization()->SetupRNGEngine(masterEngine);
156
157 //============================
158 // Step-2: Initialize worker thread
159 //============================
160 if(masterRM->GetUserWorkerInitialization())
161 {
163 }
164 if(masterRM->GetUserActionInitialization())
165 {
168 if(sv)
169 {
171 }
172 }
173 // Now initialize worker part of shared objects (geometry/physics)
174 wThreadContext->BuildGeometryAndPhysicsVector();
175 G4WorkerRunManager* wrm =
177 wrm->SetWorkerThread(wThreadContext);
178 G4AutoLock wrmm(&workerRMMutex);
179 workerRMvector->push_back(wrm);
180 wrmm.unlock();
181
182 //================================
183 // Step-3: Setup worker run manager
184 //================================
185 // Set the detector and physics list to the worker thread. Share with master
186 const G4VUserDetectorConstruction* detector =
187 masterRM->GetUserDetectorConstruction();
188 wrm->G4RunManager::SetUserInitialization(
189 const_cast<G4VUserDetectorConstruction*>(detector));
190 const G4VUserPhysicsList* physicslist = masterRM->GetUserPhysicsList();
191 wrm->SetUserInitialization(const_cast<G4VUserPhysicsList*>(physicslist));
192
193 //================================
194 // Step-4: Initialize worker run manager
195 //================================
196 if(masterRM->GetUserActionInitialization())
197 {
199 }
200 if(masterRM->GetUserWorkerInitialization())
201 {
203 }
204 wrm->Initialize();
205
206 //================================
207 // Step5: Loop over requests from the master thread
208 //================================
209 // This function should enter a loop processing new runs and actions
210 // requests from master. It should block until thread is ready
211 // to terminate
212 wrm->DoWork();
213
214 //===============================
215 // Step-6: Terminate worker thread
216 //===============================
217 if(masterRM->GetUserWorkerInitialization())
218 {
220 }
221
222 wrmm.lock();
223 std::vector<G4WorkerRunManager*>::iterator itrWrm = workerRMvector->begin();
224 for(; itrWrm != workerRMvector->end(); itrWrm++)
225 {
226 if((*itrWrm) == wrm)
227 {
228 workerRMvector->erase(itrWrm);
229 break;
230 }
231 }
232 wrmm.unlock();
233 delete wrm;
234
235 //===============================
236 // Step-7: Cleanup split classes
237 //===============================
238 wThreadContext->DestroyGeometryAndPhysicsVector();
239 wThreadContext = 0;
240
242}
243
244#include "G4DecayTable.hh"
246#include "G4ParticleTable.hh"
248#include "G4VDecayChannel.hh"
249
251{
254 pItr->reset();
255 while((*pItr)())
256 {
257 G4DecayTable* dt = pItr->value()->GetDecayTable();
258 if(dt)
259 {
260 G4int nCh = dt->entries();
261 for(G4int i = 0; i < nCh; i++)
262 {
263 dt->GetDecayChannel(i)->GetDaughter(0);
264 }
265 }
266 }
267}
268
270{
271 G4AutoLock wrmm(&workerRMMutex);
272 std::vector<G4WorkerRunManager*>::iterator itr = workerRMvector->begin();
273 for(; itr != workerRMvector->end(); itr++)
274 {
275 (*itr)->AbortRun(softAbort);
276 }
277}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:85
std::mutex G4Mutex
Definition: G4Threading.hh:81
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4VDecayChannel * GetDecayChannel(G4int index) const
G4int entries() const
void BroadcastAbortRun(G4bool softAbort)
static G4WorkerThread * GetWorkerThread()
static void StartThread(G4WorkerThread *context)
G4int GetPinAffinity() const
const CLHEP::HepRandomEngine * getMasterRandomEngine() const
static G4MTRunManager * GetMasterRunManager()
void reset(G4bool ifSkipIon=true)
G4PTblDicIterator * GetIterator() const
static G4ParticleTable * GetParticleTable()
virtual void SetupShadowProcess() const
const G4UserWorkerInitialization * GetUserWorkerInitialization() const
virtual void Initialize()
const G4VUserDetectorConstruction * GetUserDetectorConstruction() const
const G4VUserActionInitialization * GetUserActionInitialization() const
G4VUserActionInitialization * GetNonConstUserActionInitialization() const
const G4VUserPhysicsList * GetUserPhysicsList() const
const G4UserWorkerThreadInitialization * GetUserWorkerThreadInitialization() const
void SetUpForAThread(G4int tId)
Definition: G4UImanager.cc:832
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
virtual void SetupRNGEngine(const CLHEP::HepRandomEngine *aRNGEngine) const
virtual G4WorkerRunManager * CreateWorkerRunManager() const
G4ParticleDefinition * GetDaughter(G4int anIndex)
static void SetInstance(G4VSteppingVerbose *Instance)
virtual void Build() const =0
virtual G4VSteppingVerbose * InitializeSteppingVerbose() const
void SetWorkerThread(G4WorkerThread *wc)
Sets the worker context.
virtual void SetUserInitialization(G4VUserPhysicsList *userInit)
G4int GetThreadId() const
static void BuildGeometryAndPhysicsVector()
static void DestroyGeometryAndPhysicsVector()
void SetPinAffinity(G4int aff) const
G4int WorkerThreadJoinsPool()
Definition: G4Threading.cc:132
G4int WorkerThreadLeavesPool()
Definition: G4Threading.cc:131
void SetMultithreadedApplication(G4bool value)
Definition: G4Threading.cc:129
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:125
#define G4ThreadLocal
Definition: tls.hh:77