79 ,
PTL::TaskRunManager(useTBB)
80 , eventGrainsize(grainsize)
81 , numberOfEventsPerTask(-1)
83 , masterRNGEngine(nullptr)
84 , workTaskGroup(nullptr)
90 nworkers = std::thread::hardware_concurrency();
95 if(numberOfStaticAllocators > 0)
98 msg1 <<
"There are " << numberOfStaticAllocators
99 <<
" static G4Allocator objects detected.\n"
100 <<
"In multi-threaded mode, all G4Allocator objects must "
101 <<
"be dynamicly instantiated.";
110 masterRNGEngine = G4Random::getTheEngine();
118 G4String _nthread_env = G4GetEnv<G4String>(
"G4FORCENUMBEROFTHREADS",
"max");
119 for(
auto& itr : _nthread_env)
122 if(_nthread_env ==
"max")
126 std::stringstream ss;
127 G4int _nthread_val = -1;
141 G4int _useTBB = G4GetEnv<G4int>(
"G4FORCE_TBB", (
G4int) useTBB);
148 msg <<
"TBB was requested but Geant4 was not built with TBB support";
149 G4Exception(
"G4TaskRunManager::G4TaskRunManager(...)",
"Run0131",
192 std::ostringstream os;
194 G4Random::saveEngineStatus(os.str().c_str());
203 std::stringstream ss;
204 ss <<
"\n### Number of threads is forced to " <<
forcedNwokers
205 <<
" by G4FORCENUMBEROFTHREADS environment variable. G4TaskRunManager::"
206 << __FUNCTION__ <<
"(" << n <<
") ignored ###";
212 G4Exception(
"G4TaskRunManager::SetNumberOfThreads(G4int)",
"Run0132",
226 std::stringstream ss;
227 ss <<
"\n### Thread-pool already initialized. Resizing to " <<
nworkers
258 G4Exception(
"G4TaskRunManager::InitializeThreadPool",
"Run1040",
259 JustWarning,
"Threadpool already initialized. Ignoring...");
263 std::stringstream ss;
265 ss << std::setw(90) <<
"";
280 G4cout <<
"G4TaskRunManager :: Using G4ThreadPool..." <<
G4endl;
308 G4GetEnv<G4int>(
"G4FORCE_GRAINSIZE", grainSize,
"Forcing grainsize...");
317 nEvtsPerTask = G4GetEnv<G4int>(
318 "G4FORCE_EVENTS_PER_TASK", nEvtsPerTask,
319 "Forcing number of events per task (overrides grainsize)...");
321 nEvtsPerTask = G4GetEnv<G4int>(
"G4FORCE_EVENTS_PER_TASK", nEvtsPerTask);
327 numberOfEventsPerTask = nEvtsPerTask;
332 std::stringstream msg;
333 msg <<
"--> G4TaskRunManager::ComputeNumberOfTasks() --> " << numberOfTasks
334 <<
" tasks with " << numberOfEventsPerTask <<
" events/task...";
336 std::stringstream ss;
338 ss << std::setw(msg.str().length()) <<
"";
356 static bool initializeStarted =
false;
362 if(initializeStarted)
365 if(!initCmdStack.empty())
368 for(
auto& itr : initCmdStack)
376 std::stringstream msg;
377 msg <<
"--> G4TaskRunManager::CreateAndStartWorkers() --> "
378 <<
"Initializing workers...";
380 std::stringstream ss;
382 ss << std::setw(msg.str().length()) <<
"";
393 initializeStarted =
true;
398 if(!initCmdStack.empty())
401 for(
auto& itr : initCmdStack)
412 std::stringstream msg;
413 msg <<
"--> G4TaskRunManager::CreateAndStartWorkers() --> "
414 <<
"Creating " << numberOfTasks <<
" tasks with "
415 << numberOfEventsPerTask <<
" events/task...";
417 std::stringstream ss;
419 ss << std::setw(msg.str().length()) <<
"";
428 for(
G4int nt = 0; nt < numberOfTasks + 1; ++nt)
432 remaining -= numberOfEventsPerTask;
443 G4cout <<
"Adding task " << nt <<
" to task-group..." <<
G4endl;
493 if(macroFile !=
nullptr)
524 msgd <<
"Event modulo is reduced to " <<
eventModulo <<
" (was "
526 <<
" to distribute events to all threads.";
527 G4Exception(
"G4TaskRunManager::InitializeEventLoop()",
"Run10035",
544 if(_overload ==
false && _functor ==
false)
561 <<
"> of seedOncePerCommunication is invalid. It is reset "
563 G4Exception(
"G4TaskRunManager::InitializeEventLoop()",
"Run10036",
615 std::vector<G4VPhysicalVolume*>::iterator itrW =
617 for(
size_t iWorld = 0; iWorld < nWorlds; ++iWorld)
655 s1 = helper->
GetSeed(idx_rndm);
656 s2 = helper->
GetSeed(idx_rndm + 1);
658 s3 = helper->
GetSeed(idx_rndm + 2);
677 G4int nevt = numberOfEventsPerTask;
691 for(
G4int i = 0; i < nevRnd; ++i)
738 G4cerr <<
"Run is not in progress. AbortRun() ignored." <<
G4endl;
768 auto process_commands_stack = []() {
773 for(
const auto& itr : cmds)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
std::queue< G4long > G4SeedsQueue
G4GLOB_DLL std::ostream G4cerr
G4GLOB_DLL std::ostream G4cout
virtual void flatArray(const int size, double *vect)=0
static G4int SeedOncePerCommunication()
static void SetSeedOncePerCommunication(G4int val)
G4int numberOfEventToBeProcessed
virtual void ThisWorkerProcessCommandsStackDone()
static void addWorld(G4int counter, G4VPhysicalVolume *w)
virtual void PrepareCommandsStack()
static G4MTRUN_DLL G4ScoringManager * masterScM
static G4MTRUN_DLL G4MTRunManager * fMasterRM
static G4MTRUN_DLL masterWorlds_t masterWorlds
static G4MTRunManager * GetMasterRunManager()
static G4ThreadId GetMasterThreadId()
std::vector< G4String > GetCommandStack()
G4int GetNumberOfStaticAllocators() const
virtual void Initialize()
void SetRunIDCounter(G4int i)
G4UserWorkerThreadInitialization * userWorkerThreadInitialization
G4int numberOfEventProcessed
virtual void BeamOn(G4int n_event, const char *macroFile=0, G4int n_select=-1)
G4RunManagerKernel * kernel
virtual void RunTermination()
G4String randomNumberStatusDir
virtual void TerminateEventLoop()
virtual void ConstructScoringWorlds()
virtual void Merge(const G4Run *)
void Merge(const G4ScoringManager *scMan)
static G4ScoringManager * GetScoringManagerIfExist()
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
void BroadcastAbortRun(G4bool softAbort)
static void ExecuteWorkerTask()
static void InitializeWorker()
static void TerminateWorker()
static void TerminateWorkerRunEventLoop()
static void ExecuteWorkerInit()
void SetUpDecayChannels()
static std::vector< G4String > & InitCommandStack()
virtual void ConstructScoringWorlds() override
virtual void InitializeThreadPool() override
virtual void CreateAndStartWorkers() override
static G4TaskRunManager * GetMasterRunManager()
virtual G4bool InitializeSeeds(G4int) override
virtual void ThisWorkerProcessCommandsStackDone() override
virtual G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true) override
G4VUserTaskQueue *& taskQueue
virtual void SetNumberOfThreads(G4int n) override
virtual void AbortRun(G4bool softAbort=false) override
InitializeSeedsCallback initSeedsCallback
virtual void RefillSeeds() override
virtual void RequestWorkersProcessCommandsStack() override
virtual void AddEventTask(G4int)
static G4ThreadId GetMasterThreadId()
virtual void TerminateOneEvent() override
RunTaskGroup * workTaskGroup
virtual void TerminateWorkers() override
virtual void AbortEvent() override
virtual void InitializeEventLoop(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1) override
virtual ~G4TaskRunManager()
G4TBBTaskGroup< void > RunTaskGroupTBB
virtual void Initialize() override
virtual void StoreRNGStatus(const G4String &filenamePrefix) override
virtual void RunTermination() override
void MergeScores(const G4ScoringManager *localScoringManager)
virtual void WaitForEndEventLoopWorkers() override
G4TaskGroup< void > RunTaskGroup
G4ThreadPool *& threadPool
static G4TaskRunManagerKernel * GetMTMasterRunManagerKernel()
virtual void ProcessOneEvent(G4int i_event) override
void MergeRun(const G4Run *localRun)
virtual void ComputeNumberOfTasks()
G4TaskRunManager(G4bool useTBB=G4GetEnv< G4bool >("G4USE_TBB", false))
virtual G4bool SetUpAnEvent(G4Event *, G4long &s1, G4long &s2, G4long &s3, G4bool reseedRequired=true) override
static G4TemplateRNGHelper< T > * GetInstance()
virtual const T GetSeed(const G4int &sdId)
void Fill(G4double *dbl, G4int nev, G4int nev_tot, G4int nrpe)
void Refill(G4double *dbl, G4int nev)
static G4TransportationManager * GetTransportationManager()
std::vector< G4VPhysicalVolume * >::iterator GetWorldsIterator()
size_t GetNoWorlds() const
G4int ApplyCommand(const char *aCommand)
void SetMasterUIManager(G4bool val)
static G4UImanager * GetUIpointer()
virtual void DoWork() override
static G4WorkerTaskRunManager * GetWorkerRunManager()
void exec(Func &&func, Args... args)
virtual void Initialize(uint64_t n=std::thread::hardware_concurrency())
ThreadPool * GetThreadPool() const
void resize(size_type _n)
static void set_use_tbb(bool val)
void execute_on_all_threads(FuncT &&_func)
size_type destroy_threadpool()
G4int G4GetNumberOfCores()