CMS 3D CMS Logo

List of all members | Public Member Functions | Private Types | Private Member Functions | Private Attributes
OscarMTMasterThread Class Reference

#include <OscarMTMasterThread.h>

Public Member Functions

void beginRun (const edm::EventSetup &iSetup) const
 
void endRun () const
 
 OscarMTMasterThread (const edm::ParameterSet &iConfig)
 
RunManagerMTrunManagerMaster () const
 
RunManagerMTrunManagerMasterPtr () const
 
void stopThread ()
 
 ~OscarMTMasterThread ()
 

Private Types

enum  ThreadState { ThreadState::NotExist =0, ThreadState::BeginRun =1, ThreadState::EndRun =2, ThreadState::Destruct =3 }
 

Private Member Functions

void readES (const edm::EventSetup &iSetup) const
 

Private Attributes

edm::ESWatcher< IdealGeometryRecordidealGeomRcdWatcher_
 
edm::ESWatcher< IdealMagneticFieldRecordidealMagRcdWatcher_
 
bool m_firstRun
 
bool m_mainCanProceed
 
bool m_masterCanProceed
 
std::thread m_masterThread
 
ThreadState m_masterThreadState
 
std::condition_variable m_notifyMainCv
 
std::condition_variable m_notifyMasterCv
 
const DDCompactViewm_pDD
 
const MagneticFieldm_pMF
 
std::mutex m_protectMutex
 
const HepPDT::ParticleDataTablem_pTable
 
const bool m_pUseMagneticField
 
std::shared_ptr< RunManagerMTm_runManagerMaster
 
bool m_stopped
 
std::mutex m_threadMutex
 

Detailed Description

Definition at line 28 of file OscarMTMasterThread.h.

Member Enumeration Documentation

Enumerator
NotExist 
BeginRun 
EndRun 
Destruct 

Definition at line 43 of file OscarMTMasterThread.h.

43  {
44  NotExist=0, BeginRun=1, EndRun=2, Destruct=3
45  };

Constructor & Destructor Documentation

OscarMTMasterThread::OscarMTMasterThread ( const edm::ParameterSet iConfig)
explicit

Definition at line 22 of file OscarMTMasterThread.cc.

References BeginRun, g4SimHits_cfi::CustomUIsession, Destruct, EndRun, Exception, LogDebug, edm::errors::LogicError, m_mainCanProceed, m_masterCanProceed, m_masterThread, m_masterThreadState, m_notifyMainCv, m_notifyMasterCv, m_pDD, m_pMF, m_pTable, m_runManagerMaster, m_threadMutex, and runManagerMaster().

22  :
23  m_pUseMagneticField(iConfig.getParameter<bool>("UseMagneticField")),
24  m_pDD(nullptr), m_pMF(nullptr), m_pTable(nullptr),
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 }
#define LogDebug(id)
T getParameter(std::string const &) const
std::shared_ptr< RunManagerMT > m_runManagerMaster
ThreadState m_masterThreadState
RunManagerMT & runManagerMaster() const
std::condition_variable m_notifyMasterCv
const DDCompactView * m_pDD
const MagneticField * m_pMF
const HepPDT::ParticleDataTable * m_pTable
std::condition_variable m_notifyMainCv
OscarMTMasterThread::~OscarMTMasterThread ( )

Definition at line 133 of file OscarMTMasterThread.cc.

References m_stopped, and stopThread().

133  {
134  if(!m_stopped) {
135  stopThread();
136  }
137 }

Member Function Documentation

void OscarMTMasterThread::beginRun ( const edm::EventSetup iSetup) const

Definition at line 139 of file OscarMTMasterThread.cc.

References BeginRun, m_mainCanProceed, m_masterCanProceed, m_masterThreadState, m_notifyMainCv, m_notifyMasterCv, m_protectMutex, m_threadMutex, and readES().

Referenced by OscarMTProducer::globalBeginRun().

139  {
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 }
ThreadState m_masterThreadState
std::condition_variable m_notifyMasterCv
void readES(const edm::EventSetup &iSetup) const
std::condition_variable m_notifyMainCv
void OscarMTMasterThread::endRun ( ) const

Definition at line 160 of file OscarMTMasterThread.cc.

References EndRun, m_mainCanProceed, m_masterCanProceed, m_masterThreadState, m_notifyMainCv, m_notifyMasterCv, m_protectMutex, and m_threadMutex.

160  {
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 }
ThreadState m_masterThreadState
std::condition_variable m_notifyMasterCv
std::condition_variable m_notifyMainCv
void OscarMTMasterThread::readES ( const edm::EventSetup iSetup) const
private

Definition at line 204 of file OscarMTMasterThread.cc.

References edm::ESWatcher< T >::check(), edm::errors::Configuration, Exception, edm::EventSetup::get(), idealGeomRcdWatcher_, idealMagRcdWatcher_, m_firstRun, m_pDD, m_pMF, m_pTable, m_pUseMagneticField, edm::ESTransientHandle< T >::product(), and edm::ESHandle< T >::product().

Referenced by beginRun().

204  {
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 }
edm::ESWatcher< IdealGeometryRecord > idealGeomRcdWatcher_
const DDCompactView * m_pDD
T const * product() const
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:52
edm::ESWatcher< IdealMagneticFieldRecord > idealMagRcdWatcher_
T get() const
Definition: EventSetup.h:71
const MagneticField * m_pMF
T const * product() const
Definition: ESHandle.h:86
const HepPDT::ParticleDataTable * m_pTable
RunManagerMT& OscarMTMasterThread::runManagerMaster ( ) const
inline

Definition at line 37 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread().

37 { return *m_runManagerMaster; }
std::shared_ptr< RunManagerMT > m_runManagerMaster
RunManagerMT* OscarMTMasterThread::runManagerMasterPtr ( ) const
inline

Definition at line 38 of file OscarMTMasterThread.h.

38 { return m_runManagerMaster.get(); }
std::shared_ptr< RunManagerMT > m_runManagerMaster
void OscarMTMasterThread::stopThread ( )

Definition at line 176 of file OscarMTMasterThread.cc.

References Destruct, LogDebug, m_masterCanProceed, m_masterThread, m_masterThreadState, m_notifyMasterCv, m_runManagerMaster, m_stopped, and m_threadMutex.

Referenced by OscarMTProducer::globalEndJob(), and ~OscarMTMasterThread().

176  {
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 }
#define LogDebug(id)
std::shared_ptr< RunManagerMT > m_runManagerMaster
ThreadState m_masterThreadState
std::condition_variable m_notifyMasterCv

Member Data Documentation

edm::ESWatcher<IdealGeometryRecord> OscarMTMasterThread::idealGeomRcdWatcher_
mutableprivate

Definition at line 53 of file OscarMTMasterThread.h.

Referenced by readES().

edm::ESWatcher<IdealMagneticFieldRecord> OscarMTMasterThread::idealMagRcdWatcher_
mutableprivate

Definition at line 54 of file OscarMTMasterThread.h.

Referenced by readES().

bool OscarMTMasterThread::m_firstRun
mutableprivate

Definition at line 68 of file OscarMTMasterThread.h.

Referenced by readES().

bool OscarMTMasterThread::m_mainCanProceed
mutableprivate

Definition at line 67 of file OscarMTMasterThread.h.

Referenced by beginRun(), endRun(), and OscarMTMasterThread().

bool OscarMTMasterThread::m_masterCanProceed
mutableprivate

Definition at line 66 of file OscarMTMasterThread.h.

Referenced by beginRun(), endRun(), OscarMTMasterThread(), and stopThread().

std::thread OscarMTMasterThread::m_masterThread
private

Definition at line 50 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and stopThread().

ThreadState OscarMTMasterThread::m_masterThreadState
mutableprivate

Definition at line 64 of file OscarMTMasterThread.h.

Referenced by beginRun(), endRun(), OscarMTMasterThread(), and stopThread().

std::condition_variable OscarMTMasterThread::m_notifyMainCv
mutableprivate

Definition at line 62 of file OscarMTMasterThread.h.

Referenced by beginRun(), endRun(), and OscarMTMasterThread().

std::condition_variable OscarMTMasterThread::m_notifyMasterCv
mutableprivate

Definition at line 61 of file OscarMTMasterThread.h.

Referenced by beginRun(), endRun(), OscarMTMasterThread(), and stopThread().

const DDCompactView* OscarMTMasterThread::m_pDD
mutableprivate

Definition at line 55 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and readES().

const MagneticField* OscarMTMasterThread::m_pMF
mutableprivate

Definition at line 56 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and readES().

std::mutex OscarMTMasterThread::m_protectMutex
mutableprivate

Definition at line 59 of file OscarMTMasterThread.h.

Referenced by beginRun(), and endRun().

const HepPDT::ParticleDataTable* OscarMTMasterThread::m_pTable
mutableprivate

Definition at line 57 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and readES().

const bool OscarMTMasterThread::m_pUseMagneticField
private

Definition at line 47 of file OscarMTMasterThread.h.

Referenced by readES().

std::shared_ptr<RunManagerMT> OscarMTMasterThread::m_runManagerMaster
private

Definition at line 49 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and stopThread().

bool OscarMTMasterThread::m_stopped
mutableprivate

Definition at line 69 of file OscarMTMasterThread.h.

Referenced by stopThread(), and ~OscarMTMasterThread().

std::mutex OscarMTMasterThread::m_threadMutex
mutableprivate

Definition at line 60 of file OscarMTMasterThread.h.

Referenced by beginRun(), endRun(), OscarMTMasterThread(), and stopThread().