CMS 3D CMS Logo

OscarMTMasterThread.cc
Go to the documentation of this file.
2 
5 
10 
13 
16 
17 #include "HepPDT/ParticleDataTable.hh"
19 
20 #include "G4PhysicalVolumeStore.hh"
21 
23  m_pUseMagneticField(iConfig.getParameter<bool>("UseMagneticField")),
24  m_pDD(nullptr), m_pMF(nullptr), m_pTable(nullptr),
25  m_masterThreadState(ThreadState::NotExist),
26  m_masterCanProceed(false),
27  m_mainCanProceed(false),
28  m_firstRun(true),
29  m_stopped(false)
30 {
31  // Lock the mutex
32  std::unique_lock<std::mutex> lk(m_threadMutex);
33 
34  edm::LogInfo("SimG4CoreApplication")
35  << "OscarMTMasterThread: creating master thread";
36 
37  // Create Genat4 master thread
38  m_masterThread = std::thread([&](){
40  // Initialization
41 
42  std::shared_ptr<RunManagerMT> runManagerMaster;
43  std::unique_ptr<CustomUIsession> uiSession;
44 
45  // Lock the mutex (i.e. wait until the creating thread has called cv.wait()
46  std::unique_lock<std::mutex> lk2(m_threadMutex);
47 
48  edm::LogVerbatim("SimG4CoreApplication")
49  << "OscarMTMasterThread: initializing RunManagerMT";
50 
51  //UIsession manager for message handling
52  uiSession.reset(new CustomUIsession());
53 
54  // Create the master run manager, and share it to the CMSSW thread
55  runManagerMaster = std::make_shared<RunManagerMT>(iConfig);
57 
58  edm::LogVerbatim("SimG4CoreApplication")
59  << "OscarMTMasterThread: initialization of RunManagerMT finished";
60 
62  // State loop
63  bool isG4Alive = false;
64  while(true) {
65  // Signal main thread that it can proceed
66  m_mainCanProceed = true;
67  LogDebug("OscarMTMasterThread") << "Master thread: State loop, notify main thread";
68  m_notifyMainCv.notify_one();
69 
70  // Wait until the main thread sends signal
71  m_masterCanProceed = false;
72  LogDebug("OscarMTMasterThread") << "Master thread: State loop, starting wait";
73  m_notifyMasterCv.wait(lk2, [&]{return m_masterCanProceed;});
74 
75  // Act according to the state
76  LogDebug("OscarMTMasterThread") << "Master thread: Woke up, state is "
77  << static_cast<int>(m_masterThreadState);
79  // Initialize Geant4
80  LogDebug("OscarMTMasterThread") << "Master thread: Initializing Geant4";
81  runManagerMaster->initG4(m_pDD, m_pMF, m_pTable);
82  isG4Alive = true;
83  }
85  // Stop Geant4
86  LogDebug("OscarMTMasterThread") << "Master thread: Stopping Geant4";
87  runManagerMaster->stopG4();
88  isG4Alive = false;
89  }
91  LogDebug("OscarMTMasterThread") << "Master thread: Breaking out of state loop";
92  if(isG4Alive)
94  << "Geant4 is still alive, master thread state must be set to EndRun before Destruct";
95  break;
96  }
97  else {
99  << "OscarMTMasterThread: Illegal master thread state "
100  << static_cast<int>(m_masterThreadState);
101  }
102  }
103 
105  // Cleanup
106  edm::LogVerbatim("SimG4CoreApplication")
107  << "OscarMTMasterThread: start RunManagerMT destruction";
108  LogDebug("OscarMTMasterThread")
109  << "Master thread: Am I unique owner of runManagerMaster? "
110  << runManagerMaster.unique();
111 
112  // must be done in this thread, segfault otherwise
113  runManagerMaster.reset();
114  G4PhysicalVolumeStore::Clean();
115 
116  LogDebug("OscarMTMasterThread") << "Master thread: Reseted shared_ptr";
117  lk2.unlock();
118  edm::LogVerbatim("SimG4CoreApplication")
119  << "OscarMTMasterThread: Master thread is finished";
120  });
121 
122  // Start waiting a signal from the condition variable (releases the lock temporarily)
123  // First for initialization
124  m_mainCanProceed = false;
125  LogDebug("OscarMTMasterThread") << "Main thread: Signal master for initialization";
126  m_notifyMainCv.wait(lk, [&](){return m_mainCanProceed;});
127 
128  lk.unlock();
129  edm::LogVerbatim("SimG4CoreApplication")
130  << "OscarMTMasterThread: Master thread is constructed";
131 }
132 
134  if(!m_stopped) {
135  stopThread();
136  }
137 }
138 
140  std::lock_guard<std::mutex> lk(m_protectMutex);
141 
142  std::unique_lock<std::mutex> lk2(m_threadMutex);
143 
144  // Reading from ES must be done in the main (CMSSW) thread
145  readES(iSetup);
146 
148  m_masterCanProceed = true;
149  m_mainCanProceed = false;
150  edm::LogVerbatim("SimG4CoreApplication")
151  << "OscarMTMasterThread: Signal master for BeginRun";
152  m_notifyMasterCv.notify_one();
153  m_notifyMainCv.wait(lk2, [&](){return m_mainCanProceed;});
154 
155  lk2.unlock();
156  edm::LogVerbatim("SimG4CoreApplication")
157  << "OscarMTMasterThread: finish BeginRun";
158 }
159 
161  std::lock_guard<std::mutex> lk(m_protectMutex);
162 
163  std::unique_lock<std::mutex> lk2(m_threadMutex);
165  m_mainCanProceed = false;
166  m_masterCanProceed = true;
167  edm::LogVerbatim("SimG4CoreApplication")
168  << "OscarMTMasterThread: Signal master for EndRun";
169  m_notifyMasterCv.notify_one();
170  m_notifyMainCv.wait(lk2, [&](){return m_mainCanProceed;});
171  lk2.unlock();
172  edm::LogVerbatim("SimG4CoreApplication")
173  << "OscarMTMasterThread: finish EndRun";
174 }
175 
177  if(m_stopped) {
178  return;
179  }
180  edm::LogVerbatim("SimG4CoreApplication")
181  << "OscarMTMasterThread::stopTread: stop main thread";
182 
183  // Release our instance of the shared master run manager, so that
184  // the G4 master thread can do the cleanup. Then notify the master
185  // thread, and join it.
186  std::unique_lock<std::mutex> lk2(m_threadMutex);
187  m_runManagerMaster.reset();
188  LogDebug("OscarMTMasterThread") << "Main thread: reseted shared_ptr";
189 
191  m_masterCanProceed = true;
192  edm::LogVerbatim("SimG4CoreApplication")
193  << "OscarMTMasterThread::stopTread: notify";
194  m_notifyMasterCv.notify_one();
195  lk2.unlock();
196 
197  LogDebug("OscarMTMasterThread") << "Main thread: joining master thread";
198  m_masterThread.join();
199  edm::LogVerbatim("SimG4CoreApplication")
200  << "OscarMTMasterThread::stopTread: main thread finished";
201  m_stopped = true;
202 }
203 
204 void OscarMTMasterThread::readES(const edm::EventSetup& iSetup) const {
205  bool geomChanged = idealGeomRcdWatcher_.check(iSetup);
206  if (geomChanged && (!m_firstRun)) {
208  << "[SimG4Core OscarMTMasterThread]\n"
209  << "The Geometry configuration is changed during the job execution\n"
210  << "this is not allowed, the geometry must stay unchanged";
211  }
212  if (m_pUseMagneticField) {
213  bool magChanged = idealMagRcdWatcher_.check(iSetup);
214  if (magChanged && (!m_firstRun)) {
216  << "[SimG4Core OscarMTMasterThread]\n"
217  << "The MagneticField configuration is changed during the job execution\n"
218  << "this is not allowed, the MagneticField must stay unchanged";
219  }
220  }
221  // Don't read from ES if not the first run, just as in
222  if(!m_firstRun)
223  return;
224 
225  // DDDWorld: get the DDCV from the ES and use it to build the World
227  iSetup.get<IdealGeometryRecord>().get(pDD);
228  m_pDD = pDD.product();
229 
230  if(m_pUseMagneticField) {
232  iSetup.get<IdealMagneticFieldRecord>().get(pMF);
233  m_pMF = pMF.product();
234  }
235 
237  iSetup.get<PDTRecord>().get(fTable);
238  m_pTable = fTable.product();
239 
240  m_firstRun = false;
241 }
#define LogDebug(id)
std::shared_ptr< RunManagerMT > m_runManagerMaster
edm::ESWatcher< IdealGeometryRecord > idealGeomRcdWatcher_
#define nullptr
ThreadState m_masterThreadState
void beginRun(const edm::EventSetup &iSetup) const
RunManagerMT & runManagerMaster() const
std::condition_variable m_notifyMasterCv
void readES(const edm::EventSetup &iSetup) const
const DDCompactView * m_pDD
OscarMTMasterThread(const edm::ParameterSet &iConfig)
T const * product() const
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
edm::ESWatcher< IdealMagneticFieldRecord > idealMagRcdWatcher_
T get() const
Definition: EventSetup.h:68
const MagneticField * m_pMF
T const * product() const
Definition: ESHandle.h:84
const HepPDT::ParticleDataTable * m_pTable
std::condition_variable m_notifyMainCv