Geant4-11
G4WorkerTaskRunManager.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
29#include "G4MTRunManager.hh"
31#include "G4RNGHelper.hh"
32#include "G4Run.hh"
33#include "G4SDManager.hh"
34#include "G4ScoringManager.hh"
35#include "G4TiMemory.hh"
36#include "G4Timer.hh"
38#include "G4UImanager.hh"
39#include "G4UserRunAction.hh"
43#include "G4VScoringMesh.hh"
46#include "G4VUserPhysicsList.hh"
48#include "G4VVisManager.hh"
50#include "G4WorkerThread.hh"
51#include "G4AutoLock.hh"
53#include "G4TaskRunManager.hh"
54
55#include <fstream>
56#include <sstream>
57
58// namespace
59// {
60// G4Mutex ConstructScoringWorldsMutex;
61// }
62
63//============================================================================//
64
66{
68}
69
70//============================================================================//
71
74{
75 return static_cast<G4WorkerTaskRunManagerKernel*>(
77}
78
79//============================================================================//
80
83{}
84
85//============================================================================//
86
88{
89#ifdef G4MULTITHREADED
90 if(!visIsSetUp)
91 {
93 if(pVVis)
94 {
95 pVVis->SetUpForAThread();
96 visIsSetUp = true;
97 }
98 }
99#endif
100 runIsSeeded = false;
101
103 return;
104
105 // Signal this thread can start event loop.
106 // Note this will return only when all threads reach this point
108 if(fakeRun)
109 return;
110
111 const G4UserWorkerInitialization* uwi =
113
115 if(currentRun)
116 delete currentRun;
117
118 currentRun = nullptr;
119
122
123 // Call a user hook: this is guaranteed all threads are "synchronized"
124 if(uwi)
125 uwi->WorkerRunStart();
126
127 if(userRunAction)
129 if(!currentRun)
130 currentRun = new G4Run();
131
136
139 if(fSDM)
141
143 {
144 auto hce = fSDM->PrepareNewEvent();
146 delete hce;
147 }
148
149 std::ostringstream oss;
150 G4Random::saveFullState(oss);
153
154 for(G4int i_prev = 0; i_prev < n_perviousEventsToBeStored; ++i_prev)
155 previousEvents->push_back(nullptr);
156
157 if(printModulo > 0 || verboseLevel > 0)
158 {
159 G4cout << "### Run " << currentRun->GetRunID()
160 << " starts on worker thread " << G4Threading::G4GetThreadId() << "."
161 << G4endl;
162 }
163
164 if(userRunAction)
166
167#if defined(GEANT4_USE_TIMEMORY)
169#endif
170
172 {
174 }
175
177 {
178 G4String fileN = "currentRun";
180 {
181 std::ostringstream os;
182 os << "run" << currentRun->GetRunID();
183 fileN = os.str();
184 }
185 StoreRNGStatus(fileN);
186 }
187
188 runAborted = false;
190}
191
192//============================================================================//
193
194void G4WorkerTaskRunManager::DoEventLoop(G4int n_event, const char* macroFile,
195 G4int n_select)
196{
198 {
199 G4Exception("G4RunManager::GenerateEvent()", "Run0032", FatalException,
200 "G4VUserPrimaryGeneratorAction is not defined!");
201 }
202
203 // This is the same as in the sequential case, just the for-loop indexes are
204 // different
205 InitializeEventLoop(n_event, macroFile, n_select);
206
207 // Reset random number seeds queue
208 while(seedsQueue.size() > 0)
209 seedsQueue.pop();
210 // for each run, worker should receive at least one set of random number
211 // seeds.
212 // runIsSeeded = false;
213
214 // Event loop
215 eventLoopOnGoing = true;
216 G4int i_event = -1;
217 nevModulo = -1;
218 currEvID = -1;
219
220 for(G4int evt = 0; evt < n_event; ++evt)
221 {
222 ProcessOneEvent(i_event);
224 {
226 if(runAborted)
227 eventLoopOnGoing = false;
228 }
230 break;
231 }
232
233 // TerminateEventLoop();
234}
235
236//============================================================================//
237
239{
240 currentEvent = GenerateEvent(i_event);
242 {
247 {
248 G4cout << "Applying command \"" << msgText << "\" @ " << __FUNCTION__
249 << ":" << __LINE__ << G4endl;
251 }
252 }
253}
254
255//============================================================================//
256
258{
259 G4Event* anEvent = new G4Event(i_event);
260 long s1 = 0;
261 long s2 = 0;
262 long s3 = 0;
263 G4bool eventHasToBeSeeded = true;
265 eventHasToBeSeeded = false;
266
267 if(i_event < 0)
268 {
270 if(nevM == 1)
271 {
273 anEvent, s1, s2, s3, eventHasToBeSeeded);
274 runIsSeeded = true;
275 }
276 else
277 {
278 if(nevModulo <= 0)
279 {
281 anEvent, &seedsQueue, eventHasToBeSeeded);
282 if(nevToDo == 0)
283 eventLoopOnGoing = false;
284 else
285 {
286 currEvID = anEvent->GetEventID();
287 nevModulo = nevToDo - 1;
288 }
289 }
290 else
291 {
293 eventHasToBeSeeded = false;
294 anEvent->SetEventID(++currEvID);
295 nevModulo--;
296 }
297 if(eventLoopOnGoing && eventHasToBeSeeded)
298 {
299 s1 = seedsQueue.front();
300 seedsQueue.pop();
301 s2 = seedsQueue.front();
302 seedsQueue.pop();
303 }
304 }
305
307 {
308 delete anEvent;
309 return nullptr;
310 }
311 }
312 else if(eventHasToBeSeeded)
313 {
314 // Need to reseed random number generator
316 s1 = helper->GetSeed(i_event * 2);
317 s2 = helper->GetSeed(i_event * 2 + 1);
318 }
319
320 if(eventHasToBeSeeded)
321 {
322 long seeds[3] = { s1, s2, 0 };
323 G4Random::setTheSeeds(seeds, -1);
324 runIsSeeded = true;
327 }
328
329 // Read from file seed.
330 // Andrea Dotti 4 November 2015
331 // This is required for strong-reproducibility, in MT mode we have that each
332 // thread produces, for each event a status file, we want to do that.
333 // Search a random file with the format run{%d}evt{%d}.rndm
334
335 // This is the filename base constructed from run and event
336 const auto filename = [&] {
337 std::ostringstream os;
338 os << "run" << currentRun->GetRunID() << "evt" << anEvent->GetEventID();
339 return os.str();
340 };
341
342 G4bool RNGstatusReadFromFile = false;
344 {
345 // Build full path of RNG status file for this event
346 std::ostringstream os;
347 os << filename() << ".rndm";
348 const G4String& randomStatusFile = os.str();
349 std::ifstream ifile(randomStatusFile.c_str());
350 if(ifile)
351 {
352 // File valid and readable
353 RNGstatusReadFromFile = true;
354 G4Random::restoreEngineStatus(randomStatusFile.c_str());
355 }
356 }
357
360 {
361 std::ostringstream oss;
362 G4Random::saveFullState(oss);
365 }
366
367 if(storeRandomNumberStatus && !RNGstatusReadFromFile)
368 {
369 // If reading from file, avoid to rewrite the same
370 G4String fileN = "currentEvent";
372 fileN = filename();
373 StoreRNGStatus(fileN);
374 }
375
376 if(printModulo > 0 && anEvent->GetEventID() % printModulo == 0)
377 {
378 G4cout << "--> Event " << anEvent->GetEventID() << " starts";
379 if(eventHasToBeSeeded)
380 G4cout << " with initial seeds (" << s1 << "," << s2 << ")";
381 G4cout << "." << G4endl;
382 }
384 return anEvent;
385}
386
387//============================================================================//
388
390{
391 if(!fakeRun && currentRun)
392 {
393#if defined(GEANT4_USE_TIMEMORY)
394 workerRunProfiler.reset();
395#endif
397
398 // Call a user hook: note this is before the next barrier
399 // so threads execute this method asyncrhonouzly
400 //(TerminateRun allows for synch via G4RunAction::EndOfRun)
401 const G4UserWorkerInitialization* uwi =
403 if(uwi)
404 uwi->WorkerRunEnd();
405 }
406
407 if(currentRun)
408 {
410 }
411 // Signal this thread has finished envent-loop.
412 // Note this will return only whan all threads reach this point
414}
415
416//============================================================================//
417
419{
420 if(verboseLevel > 0 && !fakeRun)
421 {
422 timer->Stop();
423 // prefix with thread # info due to how TBB calls this function
425 "[thread " + std::to_string(workerContext->GetThreadId()) + "] ";
426 G4cout << prefix << "Thread-local run terminated." << G4endl;
427 G4cout << prefix << "Run Summary" << G4endl;
428 if(runAborted)
429 G4cout << prefix << " Run Aborted after " << numberOfEventProcessed
430 << " events processed." << G4endl;
431 else
432 G4cout << prefix
433 << " Number of events processed : " << numberOfEventProcessed
434 << G4endl;
435 G4cout << prefix << " " << *timer << G4endl;
436 }
437}
438
439//============================================================================//
440
442{
443 const CLHEP::HepRandomEngine* mrnge =
445 assert(mrnge); // Master has created RNG
448 uwti->SetupRNGEngine(mrnge);
449}
450
451//============================================================================//
452
454{
455 std::ostringstream os;
456 os << randomNumberStatusDir << "G4Worker" << workerContext->GetThreadId()
457 << "_" << fn << ".rndm";
458 G4Random::saveEngineStatus(os.str().c_str());
459}
460
461//============================================================================//
462
464{
466 if(!mrm)
467 return;
468
469 //------------------------------------------------------------------------//
470 // Check UI commands not already processed
471 auto command_stack = mrm->GetCommandStack();
472 bool matching = (command_stack.size() == processedCommandStack.size());
473 if(matching)
474 {
475 for(uintmax_t i = 0; i < command_stack.size(); ++i)
476 if(processedCommandStack.at(i) != command_stack.at(i))
477 {
478 matching = false;
479 break;
480 }
481 }
482
483 //------------------------------------------------------------------------//
484 // Execute UI commands stored in the master UI manager
485 if(!matching)
486 {
487 for(const auto& itr : command_stack)
489 processedCommandStack = command_stack;
490 }
491}
492
493//============================================================================//
494
496{
498 if(currentRun)
499 delete currentRun;
500 currentRun = nullptr;
501}
502
503//============================================================================//
504
506{
508 G4bool newRun = false;
509 const G4Run* run = mrm->GetCurrentRun();
510 G4ThreadLocalStatic G4int runId = -1;
511 if(run && run->GetRunID() != runId)
512 {
513 runId = run->GetRunID();
514 newRun = true;
515 if(runId > 0)
516 {
517 ProcessUI();
518 assert(workerContext != nullptr);
519 }
521 }
522
523 // Start this run
525 G4int numSelect = mrm->GetNumberOfSelectEvents();
526 G4String macroFile = mrm->GetSelectMacro();
527 bool empty_macro = (macroFile == "" || macroFile == " ");
528
529 const char* macro = (empty_macro) ? nullptr : macroFile.c_str();
530 numSelect = (empty_macro) ? -1 : numSelect;
531
532 if(newRun)
533 {
535 if(cond)
536 {
539 }
540 }
541 DoEventLoop(nevts, macro, numSelect);
542}
543
544//============================================================================//
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
void ProcessOneEvent(G4Event *anEvent)
void SetRandomNumberStatus(G4String &st)
Definition: G4Event.hh:90
G4int GetEventID() const
Definition: G4Event.hh:118
void SetEventID(G4int i)
Definition: G4Event.hh:80
static G4int SeedOncePerCommunication()
virtual G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true)
virtual G4bool SetUpAnEvent(G4Event *, long &s1, long &s2, long &s3, G4bool reseedRequired=true)
const CLHEP::HepRandomEngine * getMasterRandomEngine() const
virtual void ThisWorkerReady()
G4int GetEventModulo() const
static G4MTRunManager * GetMasterRunManager()
std::vector< G4String > GetCommandStack()
virtual void ThisWorkerEndEventLoop()
static G4ParallelWorldProcessStore * GetInstance()
G4bool RunInitialization(G4bool fakeRun=false)
G4bool isScoreNtupleWriter
void CleanUpPreviousEvents()
G4int storeRandomNumberStatusToG4Event
const G4UserWorkerInitialization * GetUserWorkerInitialization() const
std::list< G4Event * > * previousEvents
G4Timer * timer
G4int n_select_msg
G4int numberOfEventProcessed
G4int GetNumberOfEventsToBeProcessed() const
virtual void InitializeEventLoop(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1)
G4int runIDCounter
G4RunManagerKernel * kernel
G4int verboseLevel
G4Run * currentRun
virtual G4bool ConfirmBeamOnCondition()
static G4RunManager * GetRunManager()
G4DCtable * DCtable
G4String randomNumberStatusForThisRun
G4bool runAborted
G4String msgText
static G4bool IfGeometryHasBeenDestroyed()
G4UserRunAction * userRunAction
G4bool rngStatusEventsFlag
virtual void RunTermination()
const G4String & GetSelectMacro() const
G4VUserPrimaryGeneratorAction * userPrimaryGeneratorAction
const G4Run * GetCurrentRun() const
G4int numberOfEventToBeProcessed
G4String randomNumberStatusDir
G4String randomNumberStatusForThisEvent
const G4UserWorkerThreadInitialization * GetUserWorkerThreadInitialization() const
G4int n_perviousEventsToBeStored
G4bool storeRandomNumberStatus
G4EventManager * eventManager
virtual void TerminateOneEvent()
G4int GetNumberOfSelectEvents() const
virtual void AnalyzeEvent(G4Event *anEvent)
void UpdateScoring()
G4Event * currentEvent
Definition: G4Run.hh:49
void SetHCtable(G4HCtable *HCtbl)
Definition: G4Run.hh:107
G4int GetRunID() const
Definition: G4Run.hh:78
void SetNumberOfEventToBeProcessed(G4int n_ev)
Definition: G4Run.hh:103
void SetRunID(G4int id)
Definition: G4Run.hh:102
void SetDCtable(G4DCtable *DCtbl)
Definition: G4Run.hh:108
void SetRandomNumberStatus(G4String &st)
Definition: G4Run.hh:109
G4HCofThisEvent * PrepareNewEvent()
Definition: G4SDManager.cc:113
static G4SDManager * GetSDMpointerIfExist()
Definition: G4SDManager.cc:47
G4HCtable * GetHCtable() const
Definition: G4SDManager.hh:101
static G4TaskRunManager * GetMasterRunManager()
static G4TemplateRNGHelper< T > * GetInstance()
Definition: G4RNGHelper.cc:42
virtual const T GetSeed(const G4int &sdId)
Definition: G4RNGHelper.hh:64
void Stop()
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:485
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
virtual void BeginOfRunAction(const G4Run *aRun)
virtual G4Run * GenerateRun()
virtual void SetupRNGEngine(const CLHEP::HepRandomEngine *aRNGEngine) const
virtual G4bool Book(G4HCofThisEvent *hce)=0
static G4VScoreNtupleWriter * Instance()
virtual void OpenFile()=0
virtual void GeneratePrimaries(G4Event *anEvent)=0
static G4VVisManager * GetConcreteInstance()
virtual void ConstructScoringWorlds()
virtual void MergePartialResults()
G4WorkerThread * workerContext
virtual void DoEventLoop(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1) override
virtual G4Event * GenerateEvent(G4int i_event) override
virtual void StoreRNGStatus(const G4String &filenamePrefix) override
virtual void RunTermination() override
virtual void TerminateEventLoop() override
virtual void DoWork() override
G4ProfilerConfig< G4ProfileType::Run > ProfilerConfig
virtual void ProcessOneEvent(G4int i_event) override
static G4WorkerTaskRunManagerKernel * GetWorkerRunManagerKernel()
virtual void RunInitialization() override
static G4WorkerTaskRunManager * GetWorkerRunManager()
std::unique_ptr< ProfilerConfig > workerRunProfiler
G4int GetThreadId() const
static void UpdateGeometryAndPhysicsVectorFromMaster()
G4int G4GetThreadId()
Definition: G4Threading.cc:122
Definition: run.py:1
#define G4ThreadLocalStatic
Definition: tls.hh:76