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 G4FORCENUMBEROFTHREADS 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
572 G4cerr <<
"Run is not in progress. AbortRun() ignored." <<
G4endl;
639 #ifdef G4MULTITHREADED
657 G4int numberOfReadyWorkers = 0;
660 G4int numberOfEndOfEventLoopWorkers = 0;
664 G4int numberOfReadyWorkersForNewAction = 0;
667 CRITICAL_SECTION cs1;
668 CRITICAL_SECTION cs2;
669 CRITICAL_SECTION cs3;
675 void InitializeWindowsConditions()
677 #ifdef G4MULTITHREADED
678 InitializeConditionVariable( &beginEventLoopCondition );
679 InitializeConditionVariable( &endEventLoopCondition );
680 InitializeConditionVariable( &numWorkersBeginEventLoopChangeCondition );
681 InitializeConditionVariable( &numWorkersEndEventLoopChangedCondition );
682 InitializeConditionVariable( &requestChangeActionForWorker);
683 InitializeConditionVariable( &numberOfReadyWorkersForNewActionChangedCondition );
685 InitializeCriticalSection( &cs1 );
686 InitializeCriticalSection( &cs2 );
687 InitializeCriticalSection( &cs3 );
697 G4AutoLock lockLoop(&numberOfReadyWorkersMutex);
699 EnterCriticalSection( &cs1 );
703 if (numberOfReadyWorkers == activethreads )
712 LeaveCriticalSection( &cs1 );
715 &numberOfReadyWorkersMutex);
740 G4AutoLock l(&numberOfEndOfEventLoopWorkersMutex);
741 numberOfEndOfEventLoopWorkers = 0;
752 G4AutoLock lockLoop(&numberOfReadyWorkersMutex);
754 EnterCriticalSection( &cs1 );
756 ++numberOfReadyWorkers;
762 LeaveCriticalSection( &cs1 );
783 G4AutoLock l(&numberOfEndOfEventLoopWorkersMutex);
785 EnterCriticalSection( &cs2 );
788 if ( numberOfEndOfEventLoopWorkers == activethreads )
795 LeaveCriticalSection( &cs2 );
798 &numberOfEndOfEventLoopWorkersMutex);
804 numberOfReadyWorkers = 0;
806 G4AutoLock l2(&numberOfEndOfEventLoopWorkersMutex);
814 G4AutoLock l(&numberOfEndOfEventLoopWorkersMutex);
816 EnterCriticalSection( &cs2 );
818 ++numberOfEndOfEventLoopWorkers;
824 LeaveCriticalSection( &cs2 );
826 G4CONDITIONWAIT(&endEventLoopCondition,&numberOfEndOfEventLoopWorkersMutex);
836 G4AutoLock l(&numberOfReadyWorkersForNewActionMutex);
838 EnterCriticalSection( &cs3 );
842 if ( numberOfReadyWorkersForNewAction == activethreads )
851 LeaveCriticalSection( &cs3 );
854 &numberOfReadyWorkersForNewActionMutex);
862 G4AutoLock l2(&numberOfReadyWorkersForNewActionMutex);
863 numberOfReadyWorkersForNewAction = 0;
876 G4AutoLock l(&numberOfReadyWorkersForNewActionMutex);
878 EnterCriticalSection( &cs3 );
880 ++numberOfReadyWorkersForNewAction;
886 LeaveCriticalSection( &cs3 );
888 G4CONDITIONWAIT(&requestChangeActionForWorker,&numberOfReadyWorkersForNewActionMutex);
903 "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)
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
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