60 void InitializeWindowsConditions();
84 nworkers(2),forcedNwokers(-1),
86 nextActionRequest(UNDEFINED),
87 eventModuloDef(0),eventModulo(1),
88 nSeedsUsed(0),nSeedsFilled(0),
89 nSeedsMax(10000),nSeedsPerEvent(2)
94 "Another instance of a G4MTRunManager already exists.");
98 #ifndef G4MULTITHREADED
100 msg <<
"Geant4 code is compiled without multi-threading support"
101 <<
"(-DG4MULTITHREADED is set to off).\n";
102 msg <<
"G4MTRunManager can only be used in multi-threaded applications.";
107 if(numberOfStaticAllocators>0)
110 msg1 <<
"There are " << numberOfStaticAllocators
111 <<
" static G4Allocator objects detected.\n"
112 <<
"In multi-threaded mode, all G4Allocator objects must be dynamicly instantiated.";
125 InitializeWindowsConditions();
131 char* env = getenv(
"G4FORCENUMBEROFTHREADS");
135 if(envS==
"MAX"||envS==
"max")
139 std::istringstream is(env);
147 msg2 <<
"Environment variable G4FORCENUMBEROFTHREADS has an invalid value <"
148 << envS <<
">. It has to be an integer or a word \"max\".\n"
149 <<
"G4FORCENUMBEROFTHREADS is ignored.";
157 <<
" by Environment variable G4FORCENUMBEROFTHREADS." <<
G4endl;
174 std::ostringstream os;
176 G4Random::saveEngineStatus(os.str().c_str());
184 msg <<
"Number of threads cannot be changed at this moment \n"
185 <<
"(old threads are still alive). Method ignored.";
186 G4Exception(
"G4MTRunManager::SetNumberOfThreads(G4int)",
193 <<
" by G4FORCENUMBEROFWORKERS shell variable.\n"
194 <<
"Method ignored.";
195 G4Exception(
"G4MTRunManager::SetNumberOfThreads(G4int)",
231 for ( std::vector<G4String>::const_iterator it = cmdCopy->begin() ;
232 it != cmdCopy->end(); ++it )
304 msgd <<
"Event modulo is reduced to " <<
eventModulo
305 <<
" to distribute events to all threads.";
306 G4Exception(
"G4MTRunManager::InitializeEventLoop()",
349 nSeedsFilled += nFill;
375 std::vector<G4VPhysicalVolume*>::iterator itrW
377 for(
size_t iWorld=0;iWorld<nWorlds;iWorld++)
420 "For multi-threaded version, define G4VUserPrimaryGeneratorAction in G4VUserActionInitialization.");
426 "For multi-threaded version, define G4UserEventAction in G4VUserActionInitialization.");
432 "For multi-threaded version, define G4UserStackingAction in G4VUserActionInitialization.");
438 "For multi-threaded version, define G4UserTrackingAction in G4VUserActionInitialization.");
444 "For multi-threaded version, define G4UserSteppingAction in G4VUserActionInitialization.");
467 s1 = helper->
GetSeed(idx_rndm);
468 s2 = helper->
GetSeed(idx_rndm+1);
488 for(
int i=0;i<nev;i++)
507 #ifdef G4MULTITHREADED //protect here to prevent warning in compilation
543 G4cerr <<
"Run is not in progress. AbortRun() ignored." <<
G4endl;
610 #ifdef G4MULTITHREADED
628 G4int numberOfReadyWorkers = 0;
631 G4int numberOfEndOfEventLoopWorkers = 0;
635 G4int numberOfReadyWorkersForNewAction = 0;
638 CRITICAL_SECTION cs1;
639 CRITICAL_SECTION cs2;
640 CRITICAL_SECTION cs3;
646 void InitializeWindowsConditions()
648 #ifdef G4MULTITHREADED
649 InitializeConditionVariable( &beginEventLoopCondition );
650 InitializeConditionVariable( &endEventLoopCondition );
651 InitializeConditionVariable( &numWorkersBeginEventLoopChangeCondition );
652 InitializeConditionVariable( &numWorkersEndEventLoopChangedCondition );
653 InitializeConditionVariable( &requestChangeActionForWorker);
654 InitializeConditionVariable( &numberOfReadyWorkersForNewActionChangedCondition );
656 InitializeCriticalSection( &cs1 );
657 InitializeCriticalSection( &cs2 );
658 InitializeCriticalSection( &cs3 );
668 G4AutoLock lockLoop(&numberOfReadyWorkersMutex);
670 EnterCriticalSection( &cs1 );
674 if (numberOfReadyWorkers == activethreads )
683 LeaveCriticalSection( &cs1 );
686 &numberOfReadyWorkersMutex);
711 G4AutoLock l(&numberOfEndOfEventLoopWorkersMutex);
712 numberOfEndOfEventLoopWorkers = 0;
723 G4AutoLock lockLoop(&numberOfReadyWorkersMutex);
725 EnterCriticalSection( &cs1 );
727 ++numberOfReadyWorkers;
733 LeaveCriticalSection( &cs1 );
754 G4AutoLock l(&numberOfEndOfEventLoopWorkersMutex);
756 EnterCriticalSection( &cs2 );
759 if ( numberOfEndOfEventLoopWorkers == activethreads )
766 LeaveCriticalSection( &cs2 );
769 &numberOfEndOfEventLoopWorkersMutex);
775 numberOfReadyWorkers = 0;
777 G4AutoLock l2(&numberOfEndOfEventLoopWorkersMutex);
785 G4AutoLock l(&numberOfEndOfEventLoopWorkersMutex);
787 EnterCriticalSection( &cs2 );
789 ++numberOfEndOfEventLoopWorkers;
795 LeaveCriticalSection( &cs2 );
797 G4CONDITIONWAIT(&endEventLoopCondition,&numberOfEndOfEventLoopWorkersMutex);
807 G4AutoLock l(&numberOfReadyWorkersForNewActionMutex);
809 EnterCriticalSection( &cs3 );
813 if ( numberOfReadyWorkersForNewAction == activethreads )
822 LeaveCriticalSection( &cs3 );
825 &numberOfReadyWorkersForNewActionMutex);
833 G4AutoLock l2(&numberOfReadyWorkersForNewActionMutex);
834 numberOfReadyWorkersForNewAction = 0;
847 G4AutoLock l(&numberOfReadyWorkersForNewActionMutex);
849 EnterCriticalSection( &cs3 );
851 ++numberOfReadyWorkersForNewAction;
857 LeaveCriticalSection( &cs3 );
859 G4CONDITIONWAIT(&requestChangeActionForWorker,&numberOfReadyWorkersForNewActionMutex);
virtual void InitializeEventLoop(G4int n_event, const char *macroFile=0, G4int n_select=-1)
void SetMaster(G4bool val=true)
static G4TemplateRNGHelper * GetInstance()
virtual void TerminateEventLoop()
std::vector< G4String > GetCommandStack()
virtual void PrepareCommandsStack()
virtual void SetUserInitialization(G4VUserDetectorConstruction *userInit)
virtual void Merge(const G4Run *)
std::map< G4int, G4VPhysicalVolume * > masterWorlds_t
std::vector< G4String > uiCmdsForWorkers
void Fill(double *dbl, int nev, int nev_tot, int nrpe)
virtual G4bool SetUpAnEvent(G4Event *, long &s1, long &s2, long &s3)
void SetUpDecayChannels()
G4int numberOfEventProcessed
static void addWorld(G4int counter, G4VPhysicalVolume *w)
WorkerActionRequest nextActionRequest
virtual void RunTermination()
std::ostringstream G4ExceptionDescription
virtual void ThisWorkerEndEventLoop()
void SetNumberOfThreads(G4int n)
std::vector< G4VPhysicalVolume * >::iterator GetWorldsIterator()
G4VUserActionInitialization * userActionInitialization
#define G4CONDTIONBROADCAST(cond)
G4UserWorkerThreadInitialization * userWorkerThreadInitialization
virtual G4bool InitializeSeeds(G4int)
virtual void ConstructScoringWorlds()
virtual void BeamOn(G4int n_event, const char *macroFile=0, G4int n_select=-1)
virtual void BuildForMaster() const
virtual void RunTermination()
virtual const T GetSeed(const G4int &sdId)
virtual void InitializePhysics()
void SetThreadId(G4int threadId)
#define G4CONDITIONWAIT(cond, mutex)
std::vector< G4String > * GetCommandStack()
virtual void SetUserAction(G4UserRunAction *userAction)
#define G4MUTEX_INITIALIZER
std::queue< long > G4SeedsQueue
virtual void WaitForEndEventLoopWorkers()
G4MTRunManagerKernel * MTkernel
virtual void TerminateOneEvent()
static G4UImanager * GetUIpointer()
virtual void TerminateWorkers()
static G4StateManager * GetStateManager()
static G4RunManagerKernel * GetMasterRunManagerKernel()
G4GLOB_DLL std::ostream G4cout
virtual void InitializePhysics()
void MergeRun(const G4Run *localRun)
static masterWorlds_t masterWorlds
static G4ScoringManager * GetScoringManagerIfExist()
void Refill(double *dbl, int nev)
static G4ScoringManager * masterScM
G4UserWorkerInitialization * userWorkerInitialization
G4String randomNumberStatusDir
virtual void ProcessOneEvent(G4int i_event)
virtual void ConstructScoringWorlds()
virtual void WaitForReadyWorkers()
virtual G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue)
static G4MTRunManager * GetMasterRunManager()
G4int G4GetNumberOfCores()
virtual WorkerActionRequest ThisWorkerWaitForNextAction()
virtual void SetUserInitialization(G4VUserPhysicsList *userPL)
virtual void AbortRun(G4bool softAbort=false)
G4ApplicationState GetCurrentState() const
virtual void NewActionRequest(WorkerActionRequest newRequest)
void Merge(const G4ScoringManager *scMan)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
static G4TransportationManager * GetTransportationManager()
virtual void Initialize()
virtual void ThisWorkerReady()
virtual void AbortEvent()
void SetRunIDCounter(G4int i)
virtual G4Thread * CreateAndStartWorker(G4WorkerThread *workerThreadContext)
virtual ~G4MTRunManager()
virtual void Initialize()
size_t GetNoWorlds() const
G4int numberOfEventToBeProcessed
G4int GetNumberOfStaticAllocators() const
G4RunManagerKernel * kernel
void BroadcastAbortRun(G4bool softAbort)
static G4MTRunManagerKernel * GetMTMasterRunManagerKernel()
virtual void JoinWorker(G4Thread *aThread)
void SetNumberThreads(G4int numnberThreads)
void MergeScores(const G4ScoringManager *localScoringManager)
CLHEP::HepRandomEngine * masterRNGEngine
void SetMasterUIManager(G4bool val)
virtual void StoreRNGStatus(const G4String &filenamePrefix)
G4GLOB_DLL std::ostream G4cerr
virtual void SetUserAction(G4UserRunAction *userAction)
virtual void CreateAndStartWorkers()
#define G4CONDITION_INITIALIZER
static G4MTRunManager * fMasterRM