125 "Another instance of a G4MTRunManager already exists.");
130#ifndef G4MULTITHREADED
132 msg <<
"Geant4 code is compiled without multi-threading support"
133 <<
"(-DG4MULTITHREADED is set to off).\n";
134 msg <<
"G4MTRunManager can only be used in multi-threaded applications.";
139 if (numberOfStaticAllocators > 0) {
141 msg1 <<
"There are " << numberOfStaticAllocators <<
" static G4Allocator objects detected.\n"
142 <<
"In multi-threaded mode, all G4Allocator objects must be dynamically "
154 masterRNGEngine = G4Random::getTheEngine();
158 char* env = std::getenv(
"G4FORCENUMBEROFTHREADS");
159 if (env !=
nullptr) {
161 if (envS ==
"MAX" || envS ==
"max") {
165 std::istringstream is(env);
173 msg2 <<
"Environment variable G4FORCENUMBEROFTHREADS has an invalid "
175 << envS <<
">. It has to be an integer or a word \"max\".\n"
176 <<
"G4FORCENUMBEROFTHREADS is ignored.";
184 <<
" by Environment variable G4FORCENUMBEROFTHREADS." <<
G4endl;
204 std::ostringstream os;
206 G4Random::saveEngineStatus(os.str().c_str());
215 G4cerr <<
"Warning from G4RunManager::rndmSaveThisRun():"
216 <<
" Random number status was not stored prior to this run." <<
G4endl
217 <<
"/random/setSavingFlag command must be issued. "
218 <<
"Command ignored." <<
G4endl;
224 std::ostringstream os;
225 os <<
"run" << runNumber <<
".rndm" <<
'\0';
230 G4cout << fileIn <<
" is copied to " << fileOut <<
G4endl;
238 "This method shall not be invoked !!");
244 if (!threads.empty()) {
246 msg <<
"Number of threads cannot be changed at this moment \n"
247 <<
"(old threads are still alive). Method ignored.";
253 <<
" by G4FORCENUMBEROFTHREADS shell variable.\n"
254 <<
"Method ignored.";
288 uiCmdsForWorkers.clear();
290 for (
const auto& it : *cmdCopy)
291 uiCmdsForWorkers.push_back(it);
300 return uiCmdsForWorkers;
310 if (threads.empty()) {
313 std::stringstream msg;
314 msg <<
"--> G4MTRunManager::CreateAndStartWorkers() --> "
315 <<
"Initializing workers...";
317 std::stringstream ss;
319 ss << std::setw(
G4int(msg.str().length())) <<
"";
320 G4cout <<
"\n" << ss.str() <<
"\n" << msg.str() <<
"\n" << ss.str() <<
"\n" <<
G4endl;
327 context->SetThreadId(nw);
329 threads.push_back(thread);
352 if (macroFile !=
nullptr) {
372 msgd <<
"Event modulo is reduced to " <<
eventModulo
373 <<
" to distribute events to all threads.";
396 <<
"> of seedOncePerCommunication is invalid. It is reset to 0.";
472 for (
G4int iWorld = 0; iWorld < nWorlds; ++iWorld) {
514 if (userAction !=
nullptr) userAction->
SetMaster();
521 "For multi-threaded version, define G4VUserPrimaryGeneratorAction in "
522 "G4VUserActionInitialization.");
529 "For multi-threaded version, define G4UserEventAction in "
530 "G4VUserActionInitialization.");
537 "For multi-threaded version, define G4UserStackingAction in "
538 "G4VUserActionInitialization.");
545 "For multi-threaded version, define G4UserTrackingAction in "
546 "G4VUserActionInitialization.");
553 "For multi-threaded version, define G4UserSteppingAction in "
554 "G4VUserActionInitialization.");
578 if (reseedRequired) {
581 s1 = helper->
GetSeed(idx_rndm);
582 s2 = helper->
GetSeed(idx_rndm + 1);
603 if (reseedRequired) {
607 for (
G4int i = 0; i < nevRnd; ++i) {
631#ifdef G4MULTITHREADED
632 while (!threads.empty()) {
652 G4cerr <<
"Run is not in progress. AbortRun() ignored." <<
G4endl;
727 "Pin affinity must be >0 or <0.");
Helper function for copying random state files in G4run.
G4bool G4CopyRandomState(const G4fs::path &source, const G4fs::path &dest, const G4String &callsite)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
std::queue< G4long > G4SeedsQueue
#define G4MUTEX_INITIALIZER
G4GLOB_DLL std::ostream G4cerr
G4GLOB_DLL std::ostream G4cout
virtual void flatArray(const int size, double *vect)=0
virtual void WaitForReadyWorkers()
void SetActiveThreads(unsigned int val)
void BroadcastAbortRun(G4bool softAbort)
void SetUpDecayChannels()
static G4int SeedOncePerCommunication()
virtual WorkerActionRequest ThisWorkerWaitForNextAction()
static void SetSeedOncePerCommunication(G4int val)
void AbortEvent() override
G4int numberOfEventToBeProcessed
void TerminateOneEvent() override
void SetUserAction(G4UserRunAction *userAction) override
void RunTermination() override
void SetNumberOfThreads(G4int n) override
virtual G4bool InitializeSeeds(G4int)
virtual void WaitForReadyWorkers()
G4MTBarrier beginOfEventLoopBarrier
virtual void CreateAndStartWorkers()
virtual void ThisWorkerProcessCommandsStackDone()
~G4MTRunManager() override
virtual G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true)
static void addWorld(G4int counter, G4VPhysicalVolume *w)
virtual void PrepareCommandsStack()
static G4ScoringManager * GetMasterScoringManager()
static G4MTRUN_DLL G4ScoringManager * masterScM
static G4MTRUN_DLL G4MTRunManager * fMasterRM
virtual void ThisWorkerReady()
void Initialize() override
static G4MTRunManagerKernel * GetMTMasterRunManagerKernel()
static G4int seedOncePerCommunication
void ProcessOneEvent(G4int i_event) override
static G4MTRUN_DLL masterWorlds_t masterWorlds
virtual size_t GetNumberActiveThreads() const
static G4MTRunManager * GetMasterRunManager()
virtual void WaitForEndEventLoopWorkers()
void ConstructScoringWorlds() override
WorkerActionRequest nextActionRequest
void SetUserInitialization(G4VUserPhysicsList *userPL) override
std::map< G4int, G4VPhysicalVolume * > masterWorlds_t
virtual void RefillSeeds()
static G4ThreadId masterThreadId
G4MTBarrier processUIBarrier
static G4ThreadId GetMasterThreadId()
void MergeRun(const G4Run *localRun)
G4MTBarrier endOfEventLoopBarrier
G4MTBarrier nextActionRequestBarrier
void AbortRun(G4bool softAbort=false) override
virtual void TerminateWorkers()
static masterWorlds_t & GetMasterWorlds()
virtual void RequestWorkersProcessCommandsStack()
void InitializeEventLoop(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1) override
static G4RunManagerKernel * GetMasterRunManagerKernel()
void MergeScores(const G4ScoringManager *localScoringManager)
void rndmSaveThisRun() override
virtual void NewActionRequest(WorkerActionRequest newRequest)
void SetPinAffinity(G4int n=1)
void rndmSaveThisEvent() override
void StoreRNGStatus(const G4String &filenamePrefix) override
std::vector< G4String > GetCommandStack()
virtual G4bool SetUpAnEvent(G4Event *, G4long &s1, G4long &s2, G4long &s3, G4bool reseedRequired=true)
virtual void ThisWorkerEndEventLoop()
G4int GetNumberOfStaticAllocators() const
G4UserWorkerInitialization * userWorkerInitialization
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()
G4VUserActionInitialization * userActionInitialization
G4String randomNumberStatusDir
virtual void TerminateEventLoop()
virtual void SetUserAction(G4UserRunAction *userAction)
virtual void SetUserInitialization(G4VUserDetectorConstruction *userInit)
G4bool storeRandomNumberStatus
virtual void ConstructScoringWorlds()
virtual void Merge(const G4Run *)
void Merge(const G4ScoringManager *scMan)
static G4ScoringManager * GetScoringManagerIfExist()
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
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
std::vector< G4String > * GetCommandStack()
void SetMasterUIManager(G4bool val)
static G4UImanager * GetUIpointer()
virtual void SetMaster(G4bool val=true)
virtual void JoinWorker(G4Thread *aThread)
virtual G4Thread * CreateAndStartWorker(G4WorkerThread *workerThreadContext)
virtual void BuildForMaster() const
void SetNumberThreads(G4int numnberThreads)
G4int G4GetNumberOfCores()