78 :
PTL::TaskRunManager(useTBB), eventGrainsize(grainsize)
80 if (task_queue !=
nullptr)
taskQueue = task_queue;
88 if (numberOfStaticAllocators > 0) {
90 msg1 <<
"There are " << numberOfStaticAllocators <<
" static G4Allocator objects detected.\n"
91 <<
"In multi-threaded mode, all G4Allocator objects must "
92 <<
"be dynamicly instantiated.";
100 masterRNGEngine = G4Random::getTheEngine();
109 for (
auto& itr : _nthread_env)
110 itr = (char)std::tolower(itr);
112 if (_nthread_env ==
"max")
114 else if (!_nthread_env.empty()) {
115 std::stringstream ss;
116 G4int _nthread_val = -1;
129 if (_useTBB > 0) useTBB =
true;
133 msg <<
"TBB was requested but Geant4 was not built with TBB support";
181 std::ostringstream os;
183 G4Random::saveEngineStatus(os.str().c_str());
193 msg <<
"\n### Number of threads is forced to " <<
forcedNwokers
194 <<
" by G4FORCENUMBEROFTHREADS environment variable. G4TaskRunManager::" << __FUNCTION__
195 <<
"(" << n <<
") ignored ###";
204 std::stringstream ss;
205 ss <<
"\n### Thread-pool already initialized. Resizing to " <<
nworkers <<
"threads ###";
243 "Threadpool already initialized. Ignoring...");
256 std::stringstream ss;
258 ss << std::setw(90) <<
"";
265 G4cout <<
"G4TaskRunManager :: Using G4ThreadPool..." <<
G4endl;
291 grainSize =
G4GetEnv<G4int>(
"G4FORCE_GRAINSIZE", grainSize,
"Forcing grainsize...");
292 if (grainSize == 0) grainSize = 1;
309 msgd <<
"Event modulo is reduced to " <<
eventModulo <<
" (was " << oldMod <<
")"
310 <<
" to distribute events to all threads.";
316 nEvtsPerTask =
G4GetEnv<G4int>(
"G4FORCE_EVENTS_PER_TASK", nEvtsPerTask,
317 "Forcing number of events per task (overrides grainsize)...");
319 nEvtsPerTask =
G4GetEnv<G4int>(
"G4FORCE_EVENTS_PER_TASK", nEvtsPerTask);
321 if (nEvtsPerTask < 1) nEvtsPerTask = 1;
324 numberOfEventsPerTask = nEvtsPerTask;
328 std::stringstream msg;
329 msg <<
"--> G4TaskRunManager::ComputeNumberOfTasks() --> " << numberOfTasks <<
" tasks with "
330 << numberOfEventsPerTask <<
" events/task...";
332 std::stringstream ss;
334 ss << std::setw((
G4int)msg.str().length()) <<
"";
335 G4cout <<
"\n" << ss.str() <<
"\n" << msg.str() <<
"\n" << ss.str() <<
"\n" <<
G4endl;
348 static bool initializeStarted =
false;
353 if (initializeStarted) {
355 if (!initCmdStack.empty()) {
357 for (
auto& itr : initCmdStack)
364 std::stringstream msg;
365 msg <<
"--> G4TaskRunManager::CreateAndStartWorkers() --> "
366 <<
"Initializing workers...";
368 std::stringstream ss;
370 ss << std::setw((
G4int)msg.str().length()) <<
"";
371 G4cout <<
"\n" << ss.str() <<
"\n" << msg.str() <<
"\n" << ss.str() <<
"\n" <<
G4endl;
376 initializeStarted =
true;
380 if (!initCmdStack.empty()) {
382 for (
auto& itr : initCmdStack)
392 std::stringstream msg;
393 msg <<
"--> G4TaskRunManager::CreateAndStartWorkers() --> "
394 <<
"Creating " << numberOfTasks <<
" tasks with " << numberOfEventsPerTask
395 <<
" events/task...";
397 std::stringstream ss;
399 ss << std::setw((
G4int)msg.str().length()) <<
"";
400 G4cout <<
"\n" << ss.str() <<
"\n" << msg.str() <<
"\n" << ss.str() <<
"\n" <<
G4endl;
404 for (
G4int nt = 0; nt < numberOfTasks + 1; ++nt) {
406 remaining -= numberOfEventsPerTask;
459 if (macroFile !=
nullptr) {
481 if (!_overload && !_functor) {
496 <<
"> of seedOncePerCommunication is invalid. It is reset "
548 for (
G4int iWorld = 0; iWorld < nWorlds; ++iWorld) {
578 if (reseedRequired) {
581 s1 = helper->
GetSeed(idx_rndm);
582 s2 = helper->
GetSeed(idx_rndm + 1);
599 G4int nevt = numberOfEventsPerTask;
607 if (reseedRequired) {
611 for (
G4int i = 0; i < nevRnd; ++i) {
650 G4cerr <<
"Run is not in progress. AbortRun() ignored." <<
G4endl;
679 auto process_commands_stack = []() {
681 if (mrm !=
nullptr) {
683 for (
const auto& itr : cmds)
_Tp G4GetEnv(const std::string &env_id, _Tp _default=_Tp())
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()
G4int GetNumberOfThreads() const override
void ConstructScoringWorlds() override
void InitializeThreadPool() override
void CreateAndStartWorkers() override
G4bool InitializeSeeds(G4int) override
static G4TaskRunManager * GetMasterRunManager()
void ThisWorkerProcessCommandsStackDone() override
G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true) override
G4VUserTaskQueue *& taskQueue
void SetNumberOfThreads(G4int n) override
void AbortRun(G4bool softAbort=false) override
InitializeSeedsCallback initSeedsCallback
void RefillSeeds() override
void RequestWorkersProcessCommandsStack() override
virtual void AddEventTask(G4int)
static G4ThreadId GetMasterThreadId()
void TerminateOneEvent() override
RunTaskGroup * workTaskGroup
void TerminateWorkers() override
void AbortEvent() override
~G4TaskRunManager() override
void InitializeEventLoop(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1) override
void Initialize() override
void StoreRNGStatus(const G4String &filenamePrefix) override
void RunTermination() override
void MergeScores(const G4ScoringManager *localScoringManager)
void WaitForEndEventLoopWorkers() override
G4TaskGroup< void > RunTaskGroup
G4ThreadPool *& threadPool
static G4TaskRunManagerKernel * GetMTMasterRunManagerKernel()
void ProcessOneEvent(G4int i_event) override
void MergeRun(const G4Run *localRun)
virtual void ComputeNumberOfTasks()
G4TaskRunManager(G4bool useTBB=G4GetEnv< G4bool >("G4USE_TBB", false))
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()
static G4WorkerTaskRunManager * GetWorkerRunManager()
enable_if_t< std::is_void< Up >::value, void > exec(Func func, Args... args)
virtual int GetNumberOfThreads() const
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()