80 ,
PTL::TaskRunManager(useTBB)
81 , eventGrainsize(grainsize)
82 , numberOfEventsPerTask(-1)
84 , masterRNGEngine(nullptr)
85 , workTaskGroup(nullptr)
96 if(numberOfStaticAllocators > 0)
99 msg1 <<
"There are " << numberOfStaticAllocators
100 <<
" static G4Allocator objects detected.\n"
101 <<
"In multi-threaded mode, all G4Allocator objects must "
102 <<
"be dynamicly instantiated.";
111 masterRNGEngine = G4Random::getTheEngine();
119 G4String _nthread_env = G4GetEnv<G4String>(
"G4FORCENUMBEROFTHREADS",
"");
120 for(
auto& itr : _nthread_env)
121 itr = (char)std::tolower(itr);
123 if(_nthread_env ==
"max")
125 else if(!_nthread_env.empty())
127 std::stringstream ss;
128 G4int _nthread_val = -1;
142 G4int _useTBB = G4GetEnv<G4int>(
"G4FORCE_TBB", (
G4int) useTBB);
149 msg <<
"TBB was requested but Geant4 was not built with TBB support";
150 G4Exception(
"G4TaskRunManager::G4TaskRunManager(...)",
"Run0131",
201 std::ostringstream os;
203 G4Random::saveEngineStatus(os.str().c_str());
215 msg <<
"\n### Number of threads is forced to " <<
forcedNwokers
216 <<
" by G4FORCENUMBEROFTHREADS environment variable. G4TaskRunManager::"
217 << __FUNCTION__ <<
"(" << n <<
") ignored ###";
218 G4Exception(
"G4TaskRunManager::SetNumberOfThreads(G4int)",
"Run0132",
230 std::stringstream ss;
231 ss <<
"\n### Thread-pool already initialized. Resizing to " <<
nworkers
263 G4Exception(
"G4TaskRunManager::InitializeThreadPool",
"Run1040",
264 JustWarning,
"Threadpool already initialized. Ignoring...");
277 std::stringstream ss;
279 ss << std::setw(90) <<
"";
288 G4cout <<
"G4TaskRunManager :: Using G4ThreadPool..." <<
G4endl;
313 G4int grainSize = (eventGrainsize == 0)
316 G4GetEnv<G4int>(
"G4FORCE_GRAINSIZE", grainSize,
"Forcing grainsize...");
340 msgd <<
"Event modulo is reduced to " <<
eventModulo <<
" (was " << oldMod
342 <<
" to distribute events to all threads.";
343 G4Exception(
"G4TaskRunManager::InitializeEventLoop()",
"Run10035",
349 nEvtsPerTask = G4GetEnv<G4int>(
350 "G4FORCE_EVENTS_PER_TASK", nEvtsPerTask,
351 "Forcing number of events per task (overrides grainsize)...");
353 nEvtsPerTask = G4GetEnv<G4int>(
"G4FORCE_EVENTS_PER_TASK", nEvtsPerTask);
359 numberOfEventsPerTask = nEvtsPerTask;
364 std::stringstream msg;
365 msg <<
"--> G4TaskRunManager::ComputeNumberOfTasks() --> " << numberOfTasks
366 <<
" tasks with " << numberOfEventsPerTask <<
" events/task...";
368 std::stringstream ss;
370 ss << std::setw((
G4int)msg.str().length()) <<
"";
388 static bool initializeStarted =
false;
394 if(initializeStarted)
397 if(!initCmdStack.empty())
400 for(
auto& itr : initCmdStack)
408 std::stringstream msg;
409 msg <<
"--> G4TaskRunManager::CreateAndStartWorkers() --> "
410 <<
"Initializing workers...";
412 std::stringstream ss;
414 ss << std::setw((
G4int)msg.str().length()) <<
"";
425 initializeStarted =
true;
430 if(!initCmdStack.empty())
433 for(
auto& itr : initCmdStack)
444 std::stringstream msg;
445 msg <<
"--> G4TaskRunManager::CreateAndStartWorkers() --> "
446 <<
"Creating " << numberOfTasks <<
" tasks with "
447 << numberOfEventsPerTask <<
" events/task...";
449 std::stringstream ss;
451 ss << std::setw((
G4int)msg.str().length()) <<
"";
460 for(
G4int nt = 0; nt < numberOfTasks + 1; ++nt)
464 remaining -= numberOfEventsPerTask;
475 G4cout <<
"Adding task " << nt <<
" to task-group..." <<
G4endl;
525 if(macroFile !=
nullptr)
552 if(_overload ==
false && _functor ==
false)
569 <<
"> of seedOncePerCommunication is invalid. It is reset "
571 G4Exception(
"G4TaskRunManager::InitializeEventLoop()",
"Run10036",
623 std::vector<G4VPhysicalVolume*>::iterator itrW =
625 for(
G4int iWorld = 0; iWorld < nWorlds; ++iWorld)
663 s1 = helper->
GetSeed(idx_rndm);
664 s2 = helper->
GetSeed(idx_rndm + 1);
666 s3 = helper->
GetSeed(idx_rndm + 2);
685 G4int nevt = numberOfEventsPerTask;
700 for(
G4int i = 0; i < nevRnd; ++i)
747 G4cerr <<
"Run is not in progress. AbortRun() ignored." <<
G4endl;
777 auto process_commands_stack = []() {
782 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
G4RunManagerKernel * kernel
virtual void BeamOn(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1)
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()
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()
std::size_t GetNoWorlds() const
G4int ApplyCommand(const char *aCommand)
void SetMasterUIManager(G4bool val)
static G4UImanager * GetUIpointer()
virtual void DoWork() override
static G4WorkerTaskRunManager * GetWorkerRunManager()
enable_if_t< std::is_void< Up >::value, void > exec(Func func, Args... args)
virtual void Initialize(uint64_t n=std::thread::hardware_concurrency())
ThreadPool * GetThreadPool() const
void resize(size_type _n)
bool is_tbb_threadpool() const
void execute_on_all_threads(FuncT &&_func)
size_type destroy_threadpool()
static void set_use_tbb(bool _v)
G4int G4GetNumberOfCores()