61 void InitializeWindowsConditions();
85 nworkers(2),forcedNwokers(-1),pinAffinity(0),
87 nextActionRequest(UNDEFINED),
88 eventModuloDef(0),eventModulo(1),
89 nSeedsUsed(0),nSeedsFilled(0),
90 nSeedsMax(10000),nSeedsPerEvent(2)
95 "Another instance of a G4MTRunManager already exists.");
99 #ifndef G4MULTITHREADED
101 msg <<
"Geant4 code is compiled without multi-threading support"
102 <<
"(-DG4MULTITHREADED is set to off).\n";
103 msg <<
"G4MTRunManager can only be used in multi-threaded applications.";
108 if(numberOfStaticAllocators>0)
111 msg1 <<
"There are " << numberOfStaticAllocators
112 <<
" static G4Allocator objects detected.\n"
113 <<
"In multi-threaded mode, all G4Allocator objects must be dynamicly instantiated.";
126 InitializeWindowsConditions();
132 char* env = getenv(
"G4FORCENUMBEROFTHREADS");
136 if(envS==
"MAX"||envS==
"max")
140 std::istringstream is(env);
148 msg2 <<
"Environment variable G4FORCENUMBEROFTHREADS has an invalid value <"
149 << envS <<
">. It has to be an integer or a word \"max\".\n"
150 <<
"G4FORCENUMBEROFTHREADS is ignored.";
158 <<
" by Environment variable G4FORCENUMBEROFTHREADS." <<
G4endl;
175 std::ostringstream os;
177 G4Random::saveEngineStatus(os.str().c_str());
185 msg <<
"Number of threads cannot be changed at this moment \n"
186 <<
"(old threads are still alive). Method ignored.";
187 G4Exception(
"G4MTRunManager::SetNumberOfThreads(G4int)",
194 <<
" by G4FORCENUMBEROFWORKERS shell variable.\n"
195 <<
"Method ignored.";
196 G4Exception(
"G4MTRunManager::SetNumberOfThreads(G4int)",
232 for ( std::vector<G4String>::const_iterator it = cmdCopy->begin() ;
233 it != cmdCopy->end(); ++it )
305 msgd <<
"Event modulo is reduced to " <<
eventModulo
306 <<
" to distribute events to all threads.";
307 G4Exception(
"G4MTRunManager::InitializeEventLoop()",
333 <<
"> of seedOncePerCommunication is invalid. It is reset to 0." ;
334 G4Exception(
"G4MTRunManager::InitializeEventLoop()",
336 seedOncePerCommunication = 0;
407 std::vector<G4VPhysicalVolume*>::iterator itrW
409 for(
size_t iWorld=0;iWorld<nWorlds;iWorld++)
452 "For multi-threaded version, define G4VUserPrimaryGeneratorAction in G4VUserActionInitialization.");
458 "For multi-threaded version, define G4UserEventAction in G4VUserActionInitialization.");
464 "For multi-threaded version, define G4UserStackingAction in G4VUserActionInitialization.");
470 "For multi-threaded version, define G4UserTrackingAction in G4VUserActionInitialization.");
476 "For multi-threaded version, define G4UserSteppingAction in G4VUserActionInitialization.");
501 s1 = helper->
GetSeed(idx_rndm);
502 s2 = helper->
GetSeed(idx_rndm+1);
527 for(
int i=0;i<nevRnd;i++)
547 #ifdef G4MULTITHREADED //protect here to prevent warning in compilation
583 G4cerr <<
"Run is not in progress. AbortRun() ignored." <<
G4endl;
650 #ifdef G4MULTITHREADED
668 G4int numberOfReadyWorkers = 0;
671 G4int numberOfEndOfEventLoopWorkers = 0;
675 G4int numberOfReadyWorkersForNewAction = 0;
678 CRITICAL_SECTION cs1;
679 CRITICAL_SECTION cs2;
680 CRITICAL_SECTION cs3;
686 void InitializeWindowsConditions()
688 #ifdef G4MULTITHREADED
689 InitializeConditionVariable( &beginEventLoopCondition );
690 InitializeConditionVariable( &endEventLoopCondition );
691 InitializeConditionVariable( &numWorkersBeginEventLoopChangeCondition );
692 InitializeConditionVariable( &numWorkersEndEventLoopChangedCondition );
693 InitializeConditionVariable( &requestChangeActionForWorker);
694 InitializeConditionVariable( &numberOfReadyWorkersForNewActionChangedCondition );
696 InitializeCriticalSection( &cs1 );
697 InitializeCriticalSection( &cs2 );
698 InitializeCriticalSection( &cs3 );
708 G4AutoLock lockLoop(&numberOfReadyWorkersMutex);
710 EnterCriticalSection( &cs1 );
714 if (numberOfReadyWorkers == activethreads )
723 LeaveCriticalSection( &cs1 );
726 &numberOfReadyWorkersMutex);
751 G4AutoLock l(&numberOfEndOfEventLoopWorkersMutex);
752 numberOfEndOfEventLoopWorkers = 0;
763 G4AutoLock lockLoop(&numberOfReadyWorkersMutex);
765 EnterCriticalSection( &cs1 );
767 ++numberOfReadyWorkers;
773 LeaveCriticalSection( &cs1 );
794 G4AutoLock l(&numberOfEndOfEventLoopWorkersMutex);
796 EnterCriticalSection( &cs2 );
799 if ( numberOfEndOfEventLoopWorkers == activethreads )
806 LeaveCriticalSection( &cs2 );
809 &numberOfEndOfEventLoopWorkersMutex);
815 numberOfReadyWorkers = 0;
817 G4AutoLock l2(&numberOfEndOfEventLoopWorkersMutex);
825 G4AutoLock l(&numberOfEndOfEventLoopWorkersMutex);
827 EnterCriticalSection( &cs2 );
829 ++numberOfEndOfEventLoopWorkers;
835 LeaveCriticalSection( &cs2 );
837 G4CONDITIONWAIT(&endEventLoopCondition,&numberOfEndOfEventLoopWorkersMutex);
847 G4AutoLock l(&numberOfReadyWorkersForNewActionMutex);
849 EnterCriticalSection( &cs3 );
853 if ( numberOfReadyWorkersForNewAction == activethreads )
862 LeaveCriticalSection( &cs3 );
865 &numberOfReadyWorkersForNewActionMutex);
873 G4AutoLock l2(&numberOfReadyWorkersForNewActionMutex);
874 numberOfReadyWorkersForNewAction = 0;
887 G4AutoLock l(&numberOfReadyWorkersForNewActionMutex);
889 EnterCriticalSection( &cs3 );
891 ++numberOfReadyWorkersForNewAction;
897 LeaveCriticalSection( &cs3 );
899 G4CONDITIONWAIT(&requestChangeActionForWorker,&numberOfReadyWorkersForNewActionMutex);
914 "Pin affinity must be >0 or <0.");
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()
virtual G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true)
std::vector< G4String > GetCommandStack()
virtual void PrepareCommandsStack()
virtual void SetUserInitialization(G4VUserDetectorConstruction *userInit)
virtual void Merge(const G4Run *)
std::map< G4int, G4VPhysicalVolume * > masterWorlds_t
static G4int seedOncePerCommunication
std::vector< G4String > uiCmdsForWorkers
void Fill(double *dbl, int nev, int nev_tot, int nrpe)
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()
static G4MTRunManager * GetMasterRunManager()
G4int G4GetNumberOfCores()
void SetPinAffinity(G4int n=1)
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 G4bool SetUpAnEvent(G4Event *, long &s1, long &s2, long &s3, G4bool reseedRequired=true)
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