Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4TaskRunManager Class Reference

#include <G4TaskRunManager.hh>

Inheritance diagram for G4TaskRunManager:

Public Types

using InitializeSeedsCallback = std::function<G4bool(G4int, G4int&, G4int&)>
using RunTaskGroup = G4TaskGroup<void>
Public Types inherited from G4MTRunManager
enum class  WorkerActionRequest { UNDEFINED , NEXTITERATION , PROCESSUI , ENDWORKER }
using masterWorlds_t = std::map<G4int, G4VPhysicalVolume*>
Public Types inherited from G4RunManager
enum  RMType {
  sequentialRM , masterRM , workerRM , subEventMasterRM ,
  subEventWorkerRM
}
Public Types inherited from PTL::TaskRunManager
using pointer = TaskRunManager*

Public Member Functions

 G4TaskRunManager (G4bool useTBB=G4GetEnv< G4bool >("G4USE_TBB", false))
 G4TaskRunManager (G4VUserTaskQueue *taskQueue, G4bool useTBB=G4GetEnv< G4bool >("G4USE_TBB", false), G4int evtGrainsize=0)
 ~G4TaskRunManager () override
void SetGrainsize (G4int n)
G4int GetGrainsize () const
G4int GetNumberOfTasks () const
G4int GetNumberOfEventsPerTask () const
void SetNumberOfThreads (G4int n) override
G4int GetNumberOfThreads () const override
size_t GetNumberActiveThreads () const override
void Initialize () override
void InitializeEventLoop (G4int n_event, const char *macroFile=nullptr, G4int n_select=-1) override
void InitializeThreadPool () override
G4bool ThreadPoolIsInitialized () const
void Initialize (uint64_t nthreads) override
void TerminateOneEvent () override
void ProcessOneEvent (G4int i_event) override
void ConstructScoringWorlds () override
void RunTermination () override
G4bool SetUpAnEvent (G4Event *, G4long &s1, G4long &s2, G4long &s3, G4bool reseedRequired=true) override
G4int SetUpNEvents (G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true) override
void MergeScores (const G4ScoringManager *localScoringManager) override
void MergeRun (const G4Run *localRun) override
void RequestWorkersProcessCommandsStack () override
void ThisWorkerProcessCommandsStackDone () override
void WaitForReadyWorkers () override
void WaitForEndEventLoopWorkers () override
void ThisWorkerReady () override
void ThisWorkerEndEventLoop () override
WorkerActionRequest ThisWorkerWaitForNextAction () override
void SetInitializeSeedsCallback (InitializeSeedsCallback f)
void AbortRun (G4bool softAbort=false) override
void AbortEvent () override
Public Member Functions inherited from G4MTRunManager
 G4MTRunManager ()
 ~G4MTRunManager () override
void SetPinAffinity (G4int n=1)
G4int GetPinAffinity () const
std::vector< G4StringGetCommandStack ()
const CLHEP::HepRandomEnginegetMasterRandomEngine () const
void SetUserInitialization (G4VUserPhysicsList *userPL) override
void SetUserInitialization (G4VUserDetectorConstruction *userDC) override
void SetUserInitialization (G4UserWorkerInitialization *userInit) override
void SetUserInitialization (G4UserWorkerThreadInitialization *userInit) override
void SetUserInitialization (G4VUserActionInitialization *userInit) override
void SetUserAction (G4UserRunAction *userAction) override
void SetUserAction (G4VUserPrimaryGeneratorAction *userAction) override
void SetUserAction (G4UserEventAction *userAction) override
void SetUserAction (G4UserStackingAction *userAction) override
void SetUserAction (G4UserTrackingAction *userAction) override
void SetUserAction (G4UserSteppingAction *userAction) override
void SetEventModulo (G4int i=1)
G4int GetEventModulo () const
Public Member Functions inherited from G4RunManager
 G4RunManager ()
virtual ~G4RunManager ()
 G4RunManager (const G4RunManager &)=delete
G4RunManageroperator= (const G4RunManager &)=delete
virtual void BeamOn (G4int n_event, const char *macroFile=nullptr, G4int n_select=-1)
virtual void DefineWorldVolume (G4VPhysicalVolume *worldVol, G4bool topologyIsChanged=true)
virtual void InitializeGeometry ()
virtual void InitializePhysics ()
virtual G4bool ConfirmBeamOnCondition ()
virtual void RunInitialization ()
virtual void DoEventLoop (G4int n_event, const char *macroFile=nullptr, G4int n_select=-1)
virtual void TerminateEventLoop ()
virtual G4EventGenerateEvent (G4int i_event)
virtual void AnalyzeEvent (G4Event *anEvent)
void DumpRegion (const G4String &rname) const
void DumpRegion (G4Region *region=nullptr) const
void GeometryHasBeenModified (G4bool prop=true)
void ReinitializeGeometry (G4bool destroyFirst=false, G4bool prop=true)
void PhysicsHasBeenModified ()
void CutOffHasBeenModified ()
void ReOptimizeMotherOf (G4VPhysicalVolume *)
void ReOptimize (G4LogicalVolume *)
void SetGeometryToBeOptimized (G4bool vl)
G4bool GetGeometryToBeOptimized ()
void GeometryDirectlyUpdated (G4bool val=true)
virtual void RestoreRandomNumberStatus (const G4String &fileN)
virtual void RestoreRndmEachEvent (G4bool)
const G4VUserDetectorConstructionGetUserDetectorConstruction () const
const G4VUserPhysicsListGetUserPhysicsList () const
const G4VUserActionInitializationGetUserActionInitialization () const
G4VUserActionInitializationGetNonConstUserActionInitialization () const
const G4UserWorkerInitializationGetUserWorkerInitialization () const
const G4UserWorkerThreadInitializationGetUserWorkerThreadInitialization () const
const G4UserRunActionGetUserRunAction () const
const G4VUserPrimaryGeneratorActionGetUserPrimaryGeneratorAction () const
const G4UserEventActionGetUserEventAction () const
const G4UserStackingActionGetUserStackingAction () const
const G4UserTrackingActionGetUserTrackingAction () const
const G4UserSteppingActionGetUserSteppingAction () const
void SetNumberOfAdditionalWaitingStacks (G4int iAdd)
void SetDefaultClassification (G4TrackStatus ts, G4ClassificationOfNewTrack val, G4ExceptionSeverity es=G4ExceptionSeverity::IgnoreTheIssue)
void SetDefaultClassification (const G4ParticleDefinition *pd, G4ClassificationOfNewTrack val, G4ExceptionSeverity es=G4ExceptionSeverity::IgnoreTheIssue)
const G4StringGetVersionString () const
void SetPrimaryTransformer (G4PrimaryTransformer *pt)
void StoreRandomNumberStatusToG4Event (G4int vl)
G4int GetFlagRandomNumberStatusToG4Event () const
void SetRandomNumberStore (G4bool flag)
G4bool GetRandomNumberStore () const
void SetRandomNumberStoreDir (const G4String &dir)
const G4StringGetRandomNumberStoreDir () const
const G4StringGetRandomNumberStatusForThisRun () const
const G4StringGetRandomNumberStatusForThisEvent () const
void SetRandomNumberStorePerEvent (G4bool flag)
G4bool GetRandomNumberStorePerEvent () const
void SetVerboseLevel (G4int vl)
G4int GetVerboseLevel () const
G4int GetPrintProgress ()
void SetPrintProgress (G4int i)
void SetNumberOfEventsToBeStored (G4int val)
const G4RunGetCurrentRun () const
G4RunGetNonConstCurrentRun () const
const G4EventGetCurrentEvent () const
const G4EventGetPreviousEvent (G4int i) const
void SetRunIDCounter (G4int i)
G4int GetNumberOfParallelWorld () const
void SetNumberOfEventsToBeProcessed (G4int val)
G4int GetNumberOfEventsToBeProcessed () const
G4int GetNumberOfSelectEvents () const
const G4StringGetSelectMacro () const
void SetDCtable (G4DCtable *DCtbl)
RMType GetRunManagerType () const
virtual void RegisterSubEventType (G4int, G4int)
virtual void MergeTrajectories (const G4SubEvent *, const G4Event *)
virtual void UpdateScoringForSubEvent (const G4SubEvent *, const G4Event *)
virtual const G4SubEventGetSubEvent (G4int, G4bool &, G4long &, G4long &, G4long &, G4bool)
virtual void SubEventFinished (const G4SubEvent *, const G4Event *)
virtual G4int GetSubEventType () const
virtual void SetSubEventType (G4int)
virtual std::size_t GetMaxNTrack () const
virtual void TrajectoriesToBeMerged (G4bool)
virtual void ReportEventDeletion (const G4Event *evt)
void ResetNavigatorAtInitialization (G4bool val=true)
Public Member Functions inherited from PTL::TaskRunManager
 TaskRunManager (bool useTBB=false)
virtual ~TaskRunManager ()
virtual void Terminate ()
ThreadPoolGetThreadPool () const
TaskManagerGetTaskManager () const
bool IsInitialized () const

Static Public Member Functions

static G4TaskRunManagerGetMasterRunManager ()
static G4TaskRunManagerKernelGetMTMasterRunManagerKernel ()
static G4ThreadId GetMasterThreadId ()
Static Public Member Functions inherited from G4MTRunManager
static G4ThreadId GetMasterThreadId ()
static G4ScoringManagerGetMasterScoringManager ()
static masterWorlds_tGetMasterWorlds ()
static void addWorld (G4int counter, G4VPhysicalVolume *w)
static G4MTRunManagerGetMasterRunManager ()
static G4RunManagerKernelGetMasterRunManagerKernel ()
static G4MTRunManagerKernelGetMTMasterRunManagerKernel ()
static G4int SeedOncePerCommunication ()
static void SetSeedOncePerCommunication (G4int val)
Static Public Member Functions inherited from G4RunManager
static G4RunManagerGetRunManager ()
static G4bool IfGeometryHasBeenDestroyed ()
Static Public Member Functions inherited from PTL::TaskRunManager
static TaskRunManagerGetInstance (bool useTBB=false)
static TaskRunManagerGetMasterRunManager (bool useTBB=false)

Protected Member Functions

virtual void ComputeNumberOfTasks ()
G4bool InitializeSeeds (G4int) override
void RefillSeeds () override
void StoreRNGStatus (const G4String &filenamePrefix) override
void CreateAndStartWorkers () override
void TerminateWorkers () override
void NewActionRequest (WorkerActionRequest) override
virtual void AddEventTask (G4int)
Protected Member Functions inherited from G4MTRunManager
virtual void PrepareCommandsStack ()
void rndmSaveThisRun () override
void rndmSaveThisEvent () override
Protected Member Functions inherited from G4RunManager
 G4RunManager (RMType rmType)
virtual void StackPreviousEvent (G4Event *anEvent)
virtual void CleanUpPreviousEvents ()
virtual void CleanUpUnnecessaryEvents (G4int keepNEvents)
void UpdateScoring (const G4Event *evt=nullptr)
virtual void DeleteUserInitializations ()

Protected Attributes

RunTaskGroupworkTaskGroup = nullptr
G4boolpoolInitialized = PTL::TaskRunManager::m_is_initialized
G4ThreadPool *& threadPool = PTL::TaskRunManager::m_thread_pool
G4VUserTaskQueue *& taskQueue = PTL::TaskRunManager::m_task_queue
G4TaskManager *& taskManager = PTL::TaskRunManager::m_task_manager
InitializeSeedsCallback initSeedsCallback
G4bool workersStarted = false
G4int eventGrainsize = 0
G4int numberOfEventsPerTask = -1
G4int numberOfTasks = -1
CLHEP::HepRandomEnginemasterRNGEngine = nullptr
G4TaskRunManagerKernelMTkernel = nullptr
Protected Attributes inherited from G4MTRunManager
G4int nworkers = 2
G4int forcedNwokers = -1
G4int numberOfEventToBeProcessed = 0
WorkerActionRequest nextActionRequest = WorkerActionRequest::UNDEFINED
G4int eventModuloDef = 0
G4int eventModulo = 1
G4int nSeedsUsed = 0
G4int nSeedsFilled = 0
G4int nSeedsMax = 10000
G4int nSeedsPerEvent = 2
G4doublerandDbl = nullptr
G4MTBarrier beginOfEventLoopBarrier
G4MTBarrier endOfEventLoopBarrier
G4MTBarrier nextActionRequestBarrier
G4MTBarrier processUIBarrier
G4int pinAffinity = 0
G4ThreadsList threads
std::vector< G4StringuiCmdsForWorkers
CLHEP::HepRandomEnginemasterRNGEngine = nullptr
G4MTRunManagerKernelMTkernel = nullptr
Protected Attributes inherited from G4RunManager
G4RunManagerKernelkernel = nullptr
G4EventManagereventManager = nullptr
G4VUserDetectorConstructionuserDetector = nullptr
G4VUserPhysicsListphysicsList = nullptr
G4VUserActionInitializationuserActionInitialization = nullptr
G4UserWorkerInitializationuserWorkerInitialization = nullptr
G4UserWorkerThreadInitializationuserWorkerThreadInitialization = nullptr
G4UserRunActionuserRunAction = nullptr
G4VUserPrimaryGeneratorActionuserPrimaryGeneratorAction = nullptr
G4UserEventActionuserEventAction = nullptr
G4UserStackingActionuserStackingAction = nullptr
G4UserTrackingActionuserTrackingAction = nullptr
G4UserSteppingActionuserSteppingAction = nullptr
G4bool geometryInitialized = false
G4bool physicsInitialized = false
G4bool runAborted = false
G4bool initializedAtLeastOnce = false
G4bool geometryToBeOptimized = true
G4int runIDCounter = 0
G4int verboseLevel = 0
G4int printModulo = -1
G4Timertimer = nullptr
G4DCtableDCtable = nullptr
G4RuncurrentRun = nullptr
G4EventcurrentEvent = nullptr
std::list< G4Event * > * previousEvents = nullptr
G4int n_perviousEventsToBeStored = 0
G4int numberOfEventToBeProcessed = 0
G4bool storeRandomNumberStatus = false
G4int storeRandomNumberStatusToG4Event = 0
G4String randomNumberStatusDir = "./"
G4String randomNumberStatusForThisRun = ""
G4String randomNumberStatusForThisEvent = ""
G4bool rngStatusEventsFlag = false
G4VPhysicalVolumecurrentWorld = nullptr
G4int nParallelWorlds = 0
G4String msgText = " "
G4int n_select_msg = -1
G4int numberOfEventProcessed = 0
G4String selectMacro = ""
G4bool fakeRun = false
G4bool isScoreNtupleWriter = false
G4bool geometryDirectlyUpdated = false
RMType runManagerType
Protected Attributes inherited from PTL::TaskRunManager
bool m_is_initialized = false
uint64_t m_workers = 0
bool m_use_tbb = false
VUserTaskQueuem_task_queue = nullptr
ThreadPoolm_thread_pool = nullptr
TaskManagerm_task_manager = nullptr

Friends

class G4RunManagerFactory

Additional Inherited Members

Protected Types inherited from G4MTRunManager
using G4ThreadsList = std::list<G4Thread*>
Static Protected Attributes inherited from G4MTRunManager
static G4MTRUN_DLL G4ScoringManagermasterScM = nullptr
static G4MTRUN_DLL G4MTRunManagerfMasterRM = nullptr
static G4ThreadId masterThreadId = G4ThisThread::get_id()
static G4int seedOncePerCommunication = 0
Static Protected Attributes inherited from G4RunManager
static G4RUN_DLL G4bool fGeometryHasBeenDestroyed = false

Detailed Description

Definition at line 65 of file G4TaskRunManager.hh.

Member Typedef Documentation

◆ InitializeSeedsCallback

Definition at line 70 of file G4TaskRunManager.hh.

◆ RunTaskGroup

Definition at line 71 of file G4TaskRunManager.hh.

Constructor & Destructor Documentation

◆ G4TaskRunManager() [1/2]

G4TaskRunManager::G4TaskRunManager ( G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false))

Definition at line 141 of file G4TaskRunManager.cc.

141: G4TaskRunManager(nullptr, useTBB, 0) {}
G4TaskRunManager(G4bool useTBB=G4GetEnv< G4bool >("G4USE_TBB", false))

Referenced by G4SubEvtRunManager::G4SubEvtRunManager(), G4TaskRunManager(), and GetMasterRunManager().

◆ G4TaskRunManager() [2/2]

G4TaskRunManager::G4TaskRunManager ( G4VUserTaskQueue * taskQueue,
G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false),
G4int evtGrainsize = 0 )

Definition at line 76 of file G4TaskRunManager.cc.

77 : PTL::TaskRunManager(useTBB), eventGrainsize(grainsize)
78{
79 if (task_queue != nullptr) taskQueue = task_queue;
80
81 // override default of 2 from G4MTRunManager
83 fMasterRM = this;
84 MTkernel = static_cast<G4TaskRunManagerKernel*>(kernel);
85
86 G4int numberOfStaticAllocators = kernel->GetNumberOfStaticAllocators();
87 if (numberOfStaticAllocators > 0) {
89 msg1 << "There are " << numberOfStaticAllocators << " static G4Allocator objects detected.\n"
90 << "In multi-threaded mode, all G4Allocator objects must "
91 << "be dynamicly instantiated.";
92 G4Exception("G4TaskRunManager::G4TaskRunManager", "Run1035", FatalException, msg1);
93 }
94
97
98 // use default RandomNumberGenerator if created by user, or create default
99 masterRNGEngine = G4Random::getTheEngine();
100
103
104 //------------------------------------------------------------------------//
105 // handle threading
106 //------------------------------------------------------------------------//
107 auto _nthread_env = G4GetEnv<G4String>("G4FORCENUMBEROFTHREADS", "");
108 for (auto& itr : _nthread_env)
109 itr = (char)std::tolower(itr);
110
111 if (_nthread_env == "max")
113 else if (!_nthread_env.empty()) {
114 std::stringstream ss;
115 G4int _nthread_val = -1;
116 ss << _nthread_env;
117 ss >> _nthread_val;
118 if (_nthread_val > 0) forcedNwokers = _nthread_val;
119
121 }
122
123 //------------------------------------------------------------------------//
124 // option for forcing TBB
125 //------------------------------------------------------------------------//
126#ifdef GEANT4_USE_TBB
127 G4int _useTBB = G4GetEnv<G4int>("G4FORCE_TBB", (G4int)useTBB);
128 if (_useTBB > 0) useTBB = true;
129#else
130 if (useTBB) {
132 msg << "TBB was requested but Geant4 was not built with TBB support";
133 G4Exception("G4TaskRunManager::G4TaskRunManager(...)", "Run0131", JustWarning, msg);
134 }
135 useTBB = false;
136#endif
137}
_Tp G4GetEnv(const std::string &env_id, _Tp _default=_Tp())
@ JustWarning
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
double G4double
Definition G4Types.hh:83
int G4int
Definition G4Types.hh:85
G4int numberOfEventToBeProcessed
static G4MTRUN_DLL G4ScoringManager * masterScM
static G4MTRUN_DLL G4MTRunManager * fMasterRM
G4RunManagerKernel * kernel
static G4ScoringManager * GetScoringManagerIfExist()
G4VUserTaskQueue *& taskQueue
CLHEP::HepRandomEngine * masterRNGEngine
G4TaskRunManagerKernel * MTkernel
void SetMasterUIManager(G4bool val)
static G4UImanager * GetUIpointer()
G4int G4GetNumberOfCores()

◆ ~G4TaskRunManager()

G4TaskRunManager::~G4TaskRunManager ( )
override

Definition at line 145 of file G4TaskRunManager.cc.

146{
147 // terminate all the workers
149
150 // trigger all G4AutoDelete instances
152
153 // delete the task-group
154 delete workTaskGroup;
155 workTaskGroup = nullptr;
156
157 // destroy the thread-pool
158 if (threadPool != nullptr) threadPool->destroy_threadpool();
159
161}
RunTaskGroup * workTaskGroup
void TerminateWorkers() override
G4ThreadPool *& threadPool
virtual void Terminate()

Member Function Documentation

◆ AbortEvent()

void G4TaskRunManager::AbortEvent ( )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 653 of file G4TaskRunManager.cc.

654{
655 // nothing to do in the master thread
656}

◆ AbortRun()

void G4TaskRunManager::AbortRun ( G4bool softAbort = false)
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 638 of file G4TaskRunManager.cc.

639{
640 // This method is valid only for GeomClosed or EventProc state
642 if (currentState == G4State_GeomClosed || currentState == G4State_EventProc) {
643 runAborted = true;
644 MTkernel->BroadcastAbortRun(softAbort);
645 }
646 else {
647 G4cerr << "Run is not in progress. AbortRun() ignored." << G4endl;
648 }
649}
G4ApplicationState
@ G4State_EventProc
@ G4State_GeomClosed
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition G4ios.hh:67
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()

◆ AddEventTask()

void G4TaskRunManager::AddEventTask ( G4int nt)
protectedvirtual

Reimplemented in G4SubEvtRunManager.

Definition at line 408 of file G4TaskRunManager.cc.

409{
410 if (verboseLevel > 3) G4cout << "Adding task " << nt << " to task-group..." << G4endl;
411 workTaskGroup->exec([this, nt]() {
412 if (verboseLevel > 3) G4cout << "Starting task " << nt << "..." << G4endl;
414 });
415}
G4GLOB_DLL std::ostream G4cout

Referenced by CreateAndStartWorkers().

◆ ComputeNumberOfTasks()

void G4TaskRunManager::ComputeNumberOfTasks ( )
protectedvirtual

Reimplemented in G4SubEvtRunManager.

Definition at line 280 of file G4TaskRunManager.cc.

281{
282 G4int grainSize = (eventGrainsize == 0) ? (G4int)threadPool->size() : eventGrainsize;
283 grainSize = G4GetEnv<G4int>("G4FORCE_GRAINSIZE", grainSize, "Forcing grainsize...");
284 if (grainSize == 0) grainSize = 1;
285
286 G4int nEvtsPerTask =
287 (numberOfEventToBeProcessed > grainSize) ? (numberOfEventToBeProcessed / grainSize) : 1;
288
289 if (eventModuloDef > 0) {
291 }
292 else {
294 if (eventModulo < 1) eventModulo = 1;
295 }
296 if (eventModulo > nEvtsPerTask) {
297 G4int oldMod = eventModulo;
298 eventModulo = nEvtsPerTask;
299
301 msgd << "Event modulo is reduced to " << eventModulo << " (was " << oldMod << ")"
302 << " to distribute events to all threads.";
303 G4Exception("G4TaskRunManager::InitializeEventLoop()", "Run10035", JustWarning, msgd);
304 }
305 nEvtsPerTask = eventModulo;
306
307 if (fakeRun)
308 nEvtsPerTask = G4GetEnv<G4int>("G4FORCE_EVENTS_PER_TASK", nEvtsPerTask,
309 "Forcing number of events per task (overrides grainsize)...");
310 else
311 nEvtsPerTask = G4GetEnv<G4int>("G4FORCE_EVENTS_PER_TASK", nEvtsPerTask);
312
313 if (nEvtsPerTask < 1) nEvtsPerTask = 1;
314
316 numberOfEventsPerTask = nEvtsPerTask;
318
319 if (fakeRun && verboseLevel > 2) {
320 std::stringstream msg;
321 msg << "--> G4TaskRunManager::ComputeNumberOfTasks() --> " << numberOfTasks << " tasks with "
322 << numberOfEventsPerTask << " events/task...";
323
324 std::stringstream ss;
325 ss.fill('=');
326 ss << std::setw((G4int)msg.str().length()) << "";
327 G4cout << "\n" << ss.str() << "\n" << msg.str() << "\n" << ss.str() << "\n" << G4endl;
328 }
329}

Referenced by CreateAndStartWorkers(), and InitializeEventLoop().

◆ ConstructScoringWorlds()

void G4TaskRunManager::ConstructScoringWorlds ( )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 536 of file G4TaskRunManager.cc.

537{
539 // Call base class stuff...
541
542 GetMasterWorlds().clear();
545 for (G4int iWorld = 0; iWorld < nWorlds; ++iWorld) {
546 addWorld(iWorld, *itrW);
547 ++itrW;
548 }
549}
static void addWorld(G4int counter, G4VPhysicalVolume *w)
static masterWorlds_t & GetMasterWorlds()
virtual void ConstructScoringWorlds()
static G4TransportationManager * GetTransportationManager()
std::vector< G4VPhysicalVolume * >::iterator GetWorldsIterator()
std::size_t GetNoWorlds() const

◆ CreateAndStartWorkers()

void G4TaskRunManager::CreateAndStartWorkers ( )
overrideprotectedvirtual

Reimplemented from G4MTRunManager.

Definition at line 333 of file G4TaskRunManager.cc.

334{
335 // Now loop on requested number of workers
336 // This will also start the workers
337 // Currently we do not allow to change the
338 // number of threads: threads area created once
339 // Instead of pthread based workers, create tbbTask
340 static bool initializeStarted = false;
341
343
344 if (fakeRun) {
345 if (initializeStarted) {
346 auto initCmdStack = GetCommandStack();
347 if (!initCmdStack.empty()) {
348 threadPool->execute_on_all_threads([cmds = std::move(initCmdStack)]() {
349 for (auto& itr : cmds)
352 });
353 }
354 }
355 else {
356 if (verboseLevel > 0) {
357 std::stringstream msg;
358 msg << "--> G4TaskRunManager::CreateAndStartWorkers() --> "
359 << "Initializing workers...";
360
361 std::stringstream ss;
362 ss.fill('=');
363 ss << std::setw((G4int)msg.str().length()) << "";
364 G4cout << "\n" << ss.str() << "\n" << msg.str() << "\n" << ss.str() << "\n" << G4endl;
365 }
366
368 threadPool->execute_on_all_threads([]() { G4TaskRunManagerKernel::InitializeWorker(); });
369 }
370 initializeStarted = true;
371 }
372 else {
373 auto initCmdStack = GetCommandStack();
374 if (!initCmdStack.empty()) {
375 threadPool->execute_on_all_threads([cmds = std::move(initCmdStack)]() {
376 for (auto& itr : cmds)
378 });
379 }
380
381 // cleans up a previous run and events in case a thread
382 // does not execute any tasks
383 threadPool->execute_on_all_threads([]() { G4TaskRunManagerKernel::ExecuteWorkerInit(); });
384
385 if (verboseLevel > 0) {
386 std::stringstream msg;
387 msg << "--> G4TaskRunManager::CreateAndStartWorkers() --> "
388 << "Creating " << numberOfTasks << " tasks with " << numberOfEventsPerTask
389 << " events/task...";
390
391 std::stringstream ss;
392 ss.fill('=');
393 ss << std::setw((G4int)msg.str().length()) << "";
394 G4cout << "\n" << ss.str() << "\n" << msg.str() << "\n" << ss.str() << "\n" << G4endl;
395 }
396
398 for (G4int nt = 0; nt < numberOfTasks + 1; ++nt) {
399 if (remaining > 0) AddEventTask(nt);
400 remaining -= numberOfEventsPerTask;
401 }
402 workTaskGroup->wait();
403 }
404}
std::vector< G4String > GetCommandStack()
static std::vector< G4String > & InitCommandStack()
virtual void AddEventTask(G4int)
virtual void ComputeNumberOfTasks()
G4int ApplyCommand(const char *aCommand)
static G4WorkerTaskRunManager * GetWorkerRunManager()

Referenced by InitializeEventLoop().

◆ GetGrainsize()

G4int G4TaskRunManager::GetGrainsize ( ) const
inline

Definition at line 96 of file G4TaskRunManager.hh.

96{ return eventGrainsize; }

◆ GetMasterRunManager()

◆ GetMasterThreadId()

G4ThreadId G4TaskRunManager::GetMasterThreadId ( )
static

Definition at line 165 of file G4TaskRunManager.cc.

166{
168}
static G4ThreadId GetMasterThreadId()

◆ GetMTMasterRunManagerKernel()

G4TaskRunManagerKernel * G4TaskRunManager::GetMTMasterRunManagerKernel ( )
static

Definition at line 69 of file G4TaskRunManager.cc.

70{
72}
static G4TaskRunManager * GetMasterRunManager()

◆ GetNumberActiveThreads()

size_t G4TaskRunManager::GetNumberActiveThreads ( ) const
inlineoverridevirtual

Reimplemented from G4MTRunManager.

Definition at line 102 of file G4TaskRunManager.hh.

103 {
105 }
virtual size_t GetNumberActiveThreads() const

◆ GetNumberOfEventsPerTask()

G4int G4TaskRunManager::GetNumberOfEventsPerTask ( ) const
inline

Definition at line 98 of file G4TaskRunManager.hh.

98{ return numberOfEventsPerTask; }

Referenced by G4WorkerTaskRunManager::DoWork().

◆ GetNumberOfTasks()

G4int G4TaskRunManager::GetNumberOfTasks ( ) const
inline

Definition at line 97 of file G4TaskRunManager.hh.

97{ return numberOfTasks; }

◆ GetNumberOfThreads()

G4int G4TaskRunManager::GetNumberOfThreads ( ) const
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 208 of file G4TaskRunManager.cc.

209{
210 // If the ThreadPool isn't initialized, it will return 0 even if we've already
211 // set nworkers
213}
virtual int GetNumberOfThreads() const

◆ Initialize() [1/2]

void G4TaskRunManager::Initialize ( )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 217 of file G4TaskRunManager.cc.

218{
219 G4bool firstTime = (threadPool == nullptr);
220 if (firstTime) InitializeThreadPool();
221
223
224 // make sure all worker threads are set up.
226 if (firstTime) G4RunManager::SetRunIDCounter(0);
227 // G4UImanager::GetUIpointer()->SetIgnoreCmdNotFound(true);
228}
bool G4bool
Definition G4Types.hh:86
virtual void Initialize()
void SetRunIDCounter(G4int i)
virtual void BeamOn(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1)
void InitializeThreadPool() override

◆ Initialize() [2/2]

void G4TaskRunManager::Initialize ( uint64_t nthreads)
inlineoverridevirtual

Reimplemented from PTL::TaskRunManager.

Definition at line 115 of file G4TaskRunManager.hh.

virtual void Initialize(uint64_t n=std::thread::hardware_concurrency())

◆ InitializeEventLoop()

void G4TaskRunManager::InitializeEventLoop ( G4int n_event,
const char * macroFile = nullptr,
G4int n_select = -1 )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 443 of file G4TaskRunManager.cc.

444{
445 MTkernel->SetUpDecayChannels();
448
449 if (!fakeRun) {
450 nSeedsUsed = 0;
451 nSeedsFilled = 0;
452
453 if (verboseLevel > 0) timer->Start();
454
455 n_select_msg = n_select;
456 if (macroFile != nullptr) {
457 if (n_select_msg < 0) n_select_msg = n_event;
458
459 msgText = "/control/execute ";
460 msgText += macroFile;
461 selectMacro = macroFile;
462 }
463 else {
464 n_select_msg = -1;
465 selectMacro = "";
466 }
467
469
470 // initialize seeds
471 // If user did not implement InitializeSeeds,
472 // use default: nSeedsPerEvent seeds per event
473
474 if (n_event > 0) {
475 G4bool _overload = InitializeSeeds(n_event);
476 G4bool _functor = false;
477 if (!_overload) _functor = initSeedsCallback(n_event, nSeedsPerEvent, nSeedsFilled);
478 if (!_overload && !_functor) {
480 switch (SeedOncePerCommunication()) {
481 case 0:
482 nSeedsFilled = n_event;
483 break;
484 case 1:
486 break;
487 case 2:
488 nSeedsFilled = n_event / eventModulo + 1;
489 break;
490 default:
492 msgd << "Parameter value <" << SeedOncePerCommunication()
493 << "> of seedOncePerCommunication is invalid. It is reset "
494 "to 0.";
495 G4Exception("G4TaskRunManager::InitializeEventLoop()", "Run10036", JustWarning, msgd);
497 nSeedsFilled = n_event;
498 }
499
500 // Generates up to nSeedsMax seed pairs only.
503 helper->Fill(randDbl, nSeedsFilled, n_event, nSeedsPerEvent);
504 }
505 }
506 }
507
508 // Now initialize workers. Check if user defined a WorkerThreadInitialization
509 if (userWorkerThreadInitialization == nullptr)
510 userWorkerThreadInitialization = new G4UserTaskThreadInitialization();
511
512 // Prepare UI commands for threads
514
515 // Start worker threads
517}
G4TemplateRNGHelper< G4long > G4RNGHelper
static G4int SeedOncePerCommunication()
static void SetSeedOncePerCommunication(G4int val)
virtual void PrepareCommandsStack()
G4Timer * timer
G4UserWorkerThreadInitialization * userWorkerThreadInitialization
G4int numberOfEventProcessed
G4String msgText
G4String selectMacro
void CreateAndStartWorkers() override
G4bool InitializeSeeds(G4int) override
InitializeSeedsCallback initSeedsCallback
static G4TemplateRNGHelper< G4long > * GetInstance()
void Fill(G4double *dbl, G4int nev, G4int nev_tot, G4int nrpe)

◆ InitializeSeeds()

G4bool G4TaskRunManager::InitializeSeeds ( G4int )
inlineoverrideprotectedvirtual

Reimplemented from G4MTRunManager.

Definition at line 179 of file G4TaskRunManager.hh.

179{ return false; }

Referenced by InitializeEventLoop().

◆ InitializeThreadPool()

void G4TaskRunManager::InitializeThreadPool ( )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 232 of file G4TaskRunManager.cc.

233{
234 if (poolInitialized && (threadPool != nullptr) && (workTaskGroup != nullptr)) {
235 G4Exception("G4TaskRunManager::InitializeThreadPool", "Run1040", JustWarning,
236 "Threadpool already initialized. Ignoring...");
237 return;
238 }
239
241
242 // create the joiners
243 if (workTaskGroup == nullptr) {
245 }
246
247 if (verboseLevel > 0) {
248 std::stringstream ss;
249 ss.fill('=');
250 ss << std::setw(90) << "";
251 G4cout << "\n" << ss.str() << G4endl;
252
253 if (threadPool->is_tbb_threadpool()) {
254 G4cout << "G4TaskRunManager :: Using TBB..." << G4endl;
255 }
256 else {
257 G4cout << "G4TaskRunManager :: Using G4ThreadPool..." << G4endl;
258 }
259
260 G4cout << ss.str() << "\n" << G4endl;
261 }
262}
G4TaskGroup< void > RunTaskGroup

Referenced by G4SubEvtRunManager::Initialize(), and Initialize().

◆ MergeRun()

void G4TaskRunManager::MergeRun ( const G4Run * localRun)
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 561 of file G4TaskRunManager.cc.

562{
563 G4AutoLock l(&runMergerMutex);
564 if (currentRun != nullptr) currentRun->Merge(localRun);
565}
G4TemplateAutoLock< G4Mutex > G4AutoLock
G4Run * currentRun

◆ MergeScores()

void G4TaskRunManager::MergeScores ( const G4ScoringManager * localScoringManager)
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 553 of file G4TaskRunManager.cc.

554{
555 G4AutoLock l(&scorerMergerMutex);
556 if (masterScM != nullptr) masterScM->Merge(localScoringManager);
557}

◆ NewActionRequest()

void G4TaskRunManager::NewActionRequest ( WorkerActionRequest )
inlineoverrideprotectedvirtual

Reimplemented from G4MTRunManager.

Definition at line 189 of file G4TaskRunManager.hh.

189{}

◆ ProcessOneEvent()

void G4TaskRunManager::ProcessOneEvent ( G4int i_event)
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 266 of file G4TaskRunManager.cc.

267{
268 // Nothing to do
269}

◆ RefillSeeds()

void G4TaskRunManager::RefillSeeds ( )
overrideprotectedvirtual

Reimplemented from G4MTRunManager.

Definition at line 419 of file G4TaskRunManager.cc.

420{
422 G4int nFill = 0;
423 switch (SeedOncePerCommunication()) {
424 case 0:
426 break;
427 case 1:
428 nFill = numberOfTasks - nSeedsFilled;
429 break;
430 case 2:
431 default:
433 }
434 // Generates up to nSeedsMax seed pairs only.
435 if (nFill > nSeedsMax) nFill = nSeedsMax;
436 masterRNGEngine->flatArray(nSeedsPerEvent * nFill, randDbl);
437 helper->Refill(randDbl, nFill);
438 nSeedsFilled += nFill;
439}
void Refill(G4double *dbl, G4int nev)

Referenced by SetUpAnEvent(), and SetUpNEvents().

◆ RequestWorkersProcessCommandsStack()

void G4TaskRunManager::RequestWorkersProcessCommandsStack ( )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 672 of file G4TaskRunManager.cc.

673{
675
676 auto process_commands_stack = []() {
678 if (mrm != nullptr) {
679 auto cmds = mrm->GetCommandStack();
680 for (const auto& itr : cmds)
681 G4UImanager::GetUIpointer()->ApplyCommand(itr); // TLS instance
683 }
684 };
685
686 if (threadPool != nullptr) threadPool->execute_on_all_threads(process_commands_stack);
687}
virtual void ThisWorkerProcessCommandsStackDone()

Referenced by TerminateWorkers().

◆ RunTermination()

void G4TaskRunManager::RunTermination ( )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 521 of file G4TaskRunManager.cc.

522{
523 // Wait for all worker threads to have finished the run
524 // i.e. wait for them to return from RunTermination()
525 // This guarantee that userrunaction for workers has been called
526
527 // Wait now for all threads to finish event-loop
529 // Now call base-class methof
532}
virtual void RunTermination()
virtual void TerminateEventLoop()
void WaitForEndEventLoopWorkers() override

◆ SetGrainsize()

void G4TaskRunManager::SetGrainsize ( G4int n)
inline

Definition at line 95 of file G4TaskRunManager.hh.

◆ SetInitializeSeedsCallback()

void G4TaskRunManager::SetInitializeSeedsCallback ( InitializeSeedsCallback f)
inline

Definition at line 168 of file G4TaskRunManager.hh.

168{ initSeedsCallback = f; }

◆ SetNumberOfThreads()

void G4TaskRunManager::SetNumberOfThreads ( G4int n)
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 181 of file G4TaskRunManager.cc.

182{
183 if (forcedNwokers > 0) {
184 if (verboseLevel > 0) {
186 msg << "\n### Number of threads is forced to " << forcedNwokers
187 << " by G4FORCENUMBEROFTHREADS environment variable. G4TaskRunManager::" << __FUNCTION__
188 << "(" << n << ") ignored ###";
189 G4Exception("G4TaskRunManager::SetNumberOfThreads(G4int)", "Run0132", JustWarning, msg);
190 }
192 }
193 else {
194 nworkers = n;
195 if (poolInitialized) {
196 if (verboseLevel > 0) {
197 std::stringstream ss;
198 ss << "\n### Thread-pool already initialized. Resizing to " << nworkers << "threads ###";
199 G4cout << ss.str() << "\n" << G4endl;
200 }
201 GetThreadPool()->resize(n);
202 }
203 }
204}
ThreadPool * GetThreadPool() const
void resize(size_type _n)

◆ SetUpAnEvent()

G4bool G4TaskRunManager::SetUpAnEvent ( G4Event * evt,
G4long & s1,
G4long & s2,
G4long & s3,
G4bool reseedRequired = true )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 569 of file G4TaskRunManager.cc.

571{
572 G4AutoLock l(&setUpEventMutex);
575 if (reseedRequired) {
577 G4int idx_rndm = nSeedsPerEvent * nSeedsUsed;
578 s1 = helper->GetSeed(idx_rndm);
579 s2 = helper->GetSeed(idx_rndm + 1);
580 if (nSeedsPerEvent == 3) s3 = helper->GetSeed(idx_rndm + 2);
581 ++nSeedsUsed;
583 }
585 return true;
586 }
587 return false;
588}
void SetEventID(G4int i)
Definition G4Event.hh:81
void RefillSeeds() override
virtual const T GetSeed(const G4int &sdId)

◆ SetUpNEvents()

G4int G4TaskRunManager::SetUpNEvents ( G4Event * evt,
G4SeedsQueue * seedsQueue,
G4bool reseedRequired = true )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 592 of file G4TaskRunManager.cc.

593{
594 G4AutoLock l(&setUpEventMutex);
597 G4int nmod = eventModulo;
601 }
603
604 if (reseedRequired) {
606 G4int nevRnd = nmod;
607 if (SeedOncePerCommunication() > 0) nevRnd = 1;
608 for (G4int i = 0; i < nevRnd; ++i) {
609 seedsQueue->push(helper->GetSeed(nSeedsPerEvent * nSeedsUsed));
610 seedsQueue->push(helper->GetSeed(nSeedsPerEvent * nSeedsUsed + 1));
611 if (nSeedsPerEvent == 3) seedsQueue->push(helper->GetSeed(nSeedsPerEvent * nSeedsUsed + 2));
612 nSeedsUsed++;
614 }
615 }
617 return nevt;
618 }
619 return 0;
620}

◆ StoreRNGStatus()

void G4TaskRunManager::StoreRNGStatus ( const G4String & filenamePrefix)
overrideprotectedvirtual

Reimplemented from G4MTRunManager.

Definition at line 172 of file G4TaskRunManager.cc.

173{
174 std::ostringstream os;
175 os << randomNumberStatusDir << "G4Master_" << fn << ".rndm";
176 G4Random::saveEngineStatus(os.str().c_str());
177}
G4String randomNumberStatusDir

◆ TerminateOneEvent()

void G4TaskRunManager::TerminateOneEvent ( )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 273 of file G4TaskRunManager.cc.

274{
275 // Nothing to do
276}

◆ TerminateWorkers()

void G4TaskRunManager::TerminateWorkers ( )
overrideprotectedvirtual

Reimplemented from G4MTRunManager.

Definition at line 624 of file G4TaskRunManager.cc.

625{
626 // Force workers to execute (if any) all UI commands left in the stack
628
629 if (workTaskGroup != nullptr) {
630 workTaskGroup->join();
631 if (!fakeRun)
632 threadPool->execute_on_all_threads([]() { G4TaskRunManagerKernel::TerminateWorker(); });
633 }
634}
void RequestWorkersProcessCommandsStack() override

Referenced by ~G4TaskRunManager().

◆ ThisWorkerEndEventLoop()

void G4TaskRunManager::ThisWorkerEndEventLoop ( )
inlineoverridevirtual

Reimplemented from G4MTRunManager.

Definition at line 161 of file G4TaskRunManager.hh.

161{}

◆ ThisWorkerProcessCommandsStackDone()

void G4TaskRunManager::ThisWorkerProcessCommandsStackDone ( )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 691 of file G4TaskRunManager.cc.

691{}

◆ ThisWorkerReady()

void G4TaskRunManager::ThisWorkerReady ( )
inlineoverridevirtual

Reimplemented from G4MTRunManager.

Definition at line 160 of file G4TaskRunManager.hh.

160{}

◆ ThisWorkerWaitForNextAction()

WorkerActionRequest G4TaskRunManager::ThisWorkerWaitForNextAction ( )
inlineoverridevirtual

Reimplemented from G4MTRunManager.

Definition at line 163 of file G4TaskRunManager.hh.

◆ ThreadPoolIsInitialized()

G4bool G4TaskRunManager::ThreadPoolIsInitialized ( ) const
inline

Definition at line 113 of file G4TaskRunManager.hh.

113{ return poolInitialized; }

◆ WaitForEndEventLoopWorkers()

void G4TaskRunManager::WaitForEndEventLoopWorkers ( )
overridevirtual

Reimplemented from G4MTRunManager.

Definition at line 660 of file G4TaskRunManager.cc.

661{
662 if (workTaskGroup != nullptr) {
663 workTaskGroup->join();
664 if (!fakeRun)
665 threadPool->execute_on_all_threads(
667 }
668}

Referenced by RunTermination().

◆ WaitForReadyWorkers()

void G4TaskRunManager::WaitForReadyWorkers ( )
inlineoverridevirtual

Reimplemented from G4MTRunManager.

Definition at line 158 of file G4TaskRunManager.hh.

158{}

◆ G4RunManagerFactory

friend class G4RunManagerFactory
friend

Definition at line 67 of file G4TaskRunManager.hh.

Referenced by G4RunManagerFactory.

Member Data Documentation

◆ eventGrainsize

G4int G4TaskRunManager::eventGrainsize = 0
protected

◆ initSeedsCallback

InitializeSeedsCallback G4TaskRunManager::initSeedsCallback
protected
Initial value:
= [](G4int, G4int&, G4int&) {
return false;
}

Definition at line 202 of file G4TaskRunManager.hh.

202 {
203 return false;
204 };

Referenced by G4SubEvtRunManager::InitializeEventLoop(), InitializeEventLoop(), and SetInitializeSeedsCallback().

◆ masterRNGEngine

CLHEP::HepRandomEngine* G4TaskRunManager::masterRNGEngine = nullptr
protected

◆ MTkernel

◆ numberOfEventsPerTask

◆ numberOfTasks

◆ poolInitialized

G4bool& G4TaskRunManager::poolInitialized = PTL::TaskRunManager::m_is_initialized
protected

◆ taskManager

G4TaskManager*& G4TaskRunManager::taskManager = PTL::TaskRunManager::m_task_manager
protected

Definition at line 200 of file G4TaskRunManager.hh.

◆ taskQueue

G4VUserTaskQueue*& G4TaskRunManager::taskQueue = PTL::TaskRunManager::m_task_queue
protected

Definition at line 199 of file G4TaskRunManager.hh.

Referenced by G4TaskRunManager().

◆ threadPool

◆ workersStarted

G4bool G4TaskRunManager::workersStarted = false
protected

Definition at line 208 of file G4TaskRunManager.hh.

◆ workTaskGroup


The documentation for this class was generated from the following files: