79 if (task_queue !=
nullptr)
taskQueue = task_queue;
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.";
108 for (
auto& itr : _nthread_env)
109 itr = (char)std::tolower(itr);
111 if (_nthread_env ==
"max")
113 else if (!_nthread_env.empty()) {
114 std::stringstream ss;
115 G4int _nthread_val = -1;
128 if (_useTBB > 0) useTBB =
true;
132 msg <<
"TBB was requested but Geant4 was not built with TBB support";
151 G4ThreadLocalSingleton<void>::Clear();
174 std::ostringstream os;
176 G4Random::saveEngineStatus(os.str().c_str());
186 msg <<
"\n### Number of threads is forced to " <<
forcedNwokers
187 <<
" by G4FORCENUMBEROFTHREADS environment variable. G4TaskRunManager::" << __FUNCTION__
188 <<
"(" << n <<
") ignored ###";
197 std::stringstream ss;
198 ss <<
"\n### Thread-pool already initialized. Resizing to " <<
nworkers <<
"threads ###";
236 "Threadpool already initialized. Ignoring...");
248 std::stringstream ss;
250 ss << std::setw(90) <<
"";
257 G4cout <<
"G4TaskRunManager :: Using G4ThreadPool..." <<
G4endl;
283 grainSize =
G4GetEnv<G4int>(
"G4FORCE_GRAINSIZE", grainSize,
"Forcing grainsize...");
284 if (grainSize == 0) grainSize = 1;
301 msgd <<
"Event modulo is reduced to " <<
eventModulo <<
" (was " << oldMod <<
")"
302 <<
" to distribute events to all threads.";
308 nEvtsPerTask =
G4GetEnv<G4int>(
"G4FORCE_EVENTS_PER_TASK", nEvtsPerTask,
309 "Forcing number of events per task (overrides grainsize)...");
311 nEvtsPerTask =
G4GetEnv<G4int>(
"G4FORCE_EVENTS_PER_TASK", nEvtsPerTask);
313 if (nEvtsPerTask < 1) nEvtsPerTask = 1;
320 std::stringstream msg;
321 msg <<
"--> G4TaskRunManager::ComputeNumberOfTasks() --> " <<
numberOfTasks <<
" tasks with "
324 std::stringstream ss;
326 ss << std::setw((
G4int)msg.str().length()) <<
"";
327 G4cout <<
"\n" << ss.str() <<
"\n" << msg.str() <<
"\n" << ss.str() <<
"\n" <<
G4endl;
340 static bool initializeStarted =
false;
345 if (initializeStarted) {
347 if (!initCmdStack.empty()) {
348 threadPool->execute_on_all_threads([cmds = std::move(initCmdStack)]() {
349 for (
auto& itr : cmds)
356 std::stringstream msg;
357 msg <<
"--> G4TaskRunManager::CreateAndStartWorkers() --> "
358 <<
"Initializing workers...";
360 std::stringstream ss;
362 ss << std::setw((
G4int)msg.str().length()) <<
"";
363 G4cout <<
"\n" << ss.str() <<
"\n" << msg.str() <<
"\n" << ss.str() <<
"\n" <<
G4endl;
368 initializeStarted =
true;
372 if (!initCmdStack.empty()) {
373 threadPool->execute_on_all_threads([cmds = std::move(initCmdStack)]() {
374 for (
auto& itr : cmds)
384 std::stringstream msg;
385 msg <<
"--> G4TaskRunManager::CreateAndStartWorkers() --> "
387 <<
" events/task...";
389 std::stringstream ss;
391 ss << std::setw((
G4int)msg.str().length()) <<
"";
392 G4cout <<
"\n" << ss.str() <<
"\n" << msg.str() <<
"\n" << ss.str() <<
"\n" <<
G4endl;
454 if (macroFile !=
nullptr) {
476 if (!_overload && !_functor) {
491 <<
"> of seedOncePerCommunication is invalid. It is reset "
543 for (
G4int iWorld = 0; iWorld < nWorlds; ++iWorld) {
573 if (reseedRequired) {
576 s1 = helper->
GetSeed(idx_rndm);
577 s2 = helper->
GetSeed(idx_rndm + 1);
602 if (reseedRequired) {
606 for (
G4int i = 0; i < nevRnd; ++i) {
642 MTkernel->BroadcastAbortRun(softAbort);
645 G4cerr <<
"Run is not in progress. AbortRun() ignored." <<
G4endl;
674 auto process_commands_stack = []() {
676 if (mrm !=
nullptr) {
678 for (
const auto& itr : cmds)
G4TemplateAutoLock< G4Mutex > G4AutoLock
_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
G4TemplateRNGHelper< G4long > G4RNGHelper
PTL::VUserTaskQueue G4VUserTaskQueue
G4GLOB_DLL std::ostream G4cerr
G4GLOB_DLL std::ostream G4cout
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 G4MTRunManager * GetMasterRunManager()
static G4ThreadId GetMasterThreadId()
static masterWorlds_t & GetMasterWorlds()
std::vector< G4String > GetCommandStack()
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()
static G4ScoringManager * GetScoringManagerIfExist()
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
static void ExecuteWorkerTask()
static void InitializeWorker()
static void TerminateWorker()
static void TerminateWorkerRunEventLoop()
static void ExecuteWorkerInit()
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
void MergeRun(const G4Run *localRun) 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
CLHEP::HepRandomEngine * masterRNGEngine
void TerminateWorkers() override
void AbortEvent() override
~G4TaskRunManager() override
G4int numberOfEventsPerTask
void InitializeEventLoop(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1) override
void Initialize() override
void MergeScores(const G4ScoringManager *localScoringManager) override
void StoreRNGStatus(const G4String &filenamePrefix) override
G4TaskRunManagerKernel * MTkernel
void RunTermination() override
void WaitForEndEventLoopWorkers() override
G4TaskGroup< void > RunTaskGroup
G4ThreadPool *& threadPool
static G4TaskRunManagerKernel * GetMTMasterRunManagerKernel()
void ProcessOneEvent(G4int i_event) override
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< G4long > * 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()
G4int ApplyCommand(const char *aCommand)
void SetMasterUIManager(G4bool val)
static G4UImanager * GetUIpointer()
static G4WorkerTaskRunManager * GetWorkerRunManager()
virtual int GetNumberOfThreads() const
TaskRunManager(bool useTBB=false)
virtual void Initialize(uint64_t n=std::thread::hardware_concurrency())
ThreadPool * GetThreadPool() const
void resize(size_type _n)
G4int G4GetNumberOfCores()
Backports of C++ language features for use with C++11 compilers.