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 cms::DDCompactViewm_pDD4hep
 
const bool m_pGeoFromDD4hep
 
std::mutex m_protectMutex
 
const HepPDT::ParticleDataTablem_pTable
 
std::shared_ptr< RunManagerMTm_runManagerMaster
 
bool m_stopped
 
std::mutex m_threadMutex
 

Detailed Description

Definition at line 32 of file OscarMTMasterThread.h.

Member Enumeration Documentation

Enumerator
NotExist 
BeginRun 
EndRun 
Destruct 

Definition at line 47 of file OscarMTMasterThread.h.

47 { NotExist = 0, BeginRun = 1, EndRun = 2, Destruct = 3 };

Constructor & Destructor Documentation

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

Definition at line 20 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_pDD4hep, m_pTable, m_runManagerMaster, m_threadMutex, and runManagerMaster().

21  : m_pGeoFromDD4hep(iConfig.getParameter<bool>("g4GeometryDD4hepSource")),
22  m_pDD(nullptr),
23  m_pDD4hep(nullptr),
24  m_pTable(nullptr),
26  m_masterCanProceed(false),
27  m_mainCanProceed(false),
28  m_firstRun(true),
29  m_stopped(false) {
30  // Lock the mutex
31  std::unique_lock<std::mutex> lk(m_threadMutex);
32 
33  edm::LogInfo("SimG4CoreApplication") << "OscarMTMasterThread: creating master thread";
34 
35  // Create Genat4 master thread
36  m_masterThread = std::thread([&]() {
38  // Initialization
39 
40  std::shared_ptr<RunManagerMT> runManagerMaster;
41  std::unique_ptr<CustomUIsession> uiSession;
42 
43  // Lock the mutex (i.e. wait until the creating thread has called cv.wait()
44  std::unique_lock<std::mutex> lk2(m_threadMutex);
45 
46  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: initializing RunManagerMT";
47 
48  //UIsession manager for message handling
49  uiSession.reset(new CustomUIsession());
50 
51  // Create the master run manager, and share it to the CMSSW thread
52  runManagerMaster = std::make_shared<RunManagerMT>(iConfig);
54 
55  LogDebug("SimG4CoreApplication") << "OscarMTMasterThread: initialization of RunManagerMT finished";
56 
58  // State loop
59  bool isG4Alive = false;
60  while (true) {
61  // Signal main thread that it can proceed
62  m_mainCanProceed = true;
63  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: State loop, notify main thread";
64  m_notifyMainCv.notify_one();
65 
66  // Wait until the main thread sends signal
67  m_masterCanProceed = false;
68  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: State loop, starting wait";
69  m_notifyMasterCv.wait(lk2, [&] { return m_masterCanProceed; });
70 
71  // Act according to the state
72  edm::LogInfo("OscarMTMasterThread")
73  << "Master thread: Woke up, state is " << static_cast<int>(m_masterThreadState);
75  // Initialize Geant4
76  edm::LogInfo("OscarMTMasterThread") << "Master thread: Initializing Geant4";
77  runManagerMaster->initG4(m_pDD, m_pDD4hep, m_pTable);
78  isG4Alive = true;
80  // Stop Geant4
81  LogDebug("OscarMTMasterThread") << "Master thread: Stopping Geant4";
82  runManagerMaster->stopG4();
83  isG4Alive = false;
85  LogDebug("OscarMTMasterThread") << "Master thread: Breaking out of state loop";
86  if (isG4Alive)
88  << "Geant4 is still alive, master thread state must be set to EndRun before Destruct";
89  break;
90  } else {
92  << "OscarMTMasterThread: Illegal master thread state " << static_cast<int>(m_masterThreadState);
93  }
94  }
95 
97  // Cleanup
98  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: start RunManagerMT destruction";
99  LogDebug("OscarMTMasterThread") << "Master thread: Am I unique owner of runManagerMaster? "
100  << runManagerMaster.unique();
101 
102  // must be done in this thread, segfault otherwise
103  runManagerMaster.reset();
104  G4PhysicalVolumeStore::Clean();
105 
106  LogDebug("OscarMTMasterThread") << "Master thread: Reseted shared_ptr";
107  lk2.unlock();
108  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Master thread is finished";
109  });
110 
111  // Start waiting a signal from the condition variable (releases the lock temporarily)
112  // First for initialization
113  m_mainCanProceed = false;
114  LogDebug("OscarMTMasterThread") << "Main thread: Signal master for initialization";
115  m_notifyMainCv.wait(lk, [&]() { return m_mainCanProceed; });
116 
117  lk.unlock();
118  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Master thread is constructed";
119 }
#define LogDebug(id)
T getParameter(std::string const &) const
std::shared_ptr< RunManagerMT > m_runManagerMaster
ThreadState m_masterThreadState
const cms::DDCompactView * m_pDD4hep
RunManagerMT & runManagerMaster() const
std::condition_variable m_notifyMasterCv
const DDCompactView * m_pDD
const HepPDT::ParticleDataTable * m_pTable
std::condition_variable m_notifyMainCv
OscarMTMasterThread::~OscarMTMasterThread ( )

Definition at line 121 of file OscarMTMasterThread.cc.

References m_stopped, and stopThread().

121  {
122  if (!m_stopped) {
123  stopThread();
124  }
125 }

Member Function Documentation

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

Definition at line 127 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().

127  {
128  std::lock_guard<std::mutex> lk(m_protectMutex);
129 
130  std::unique_lock<std::mutex> lk2(m_threadMutex);
131 
132  // Reading from ES must be done in the main (CMSSW) thread
133  readES(iSetup);
134 
136  m_masterCanProceed = true;
137  m_mainCanProceed = false;
138  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Signal master for BeginRun";
139  m_notifyMasterCv.notify_one();
140  m_notifyMainCv.wait(lk2, [&]() { return m_mainCanProceed; });
141 
142  lk2.unlock();
143  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: finish BeginRun";
144 }
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 146 of file OscarMTMasterThread.cc.

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

146  {
147  std::lock_guard<std::mutex> lk(m_protectMutex);
148 
149  std::unique_lock<std::mutex> lk2(m_threadMutex);
151  m_mainCanProceed = false;
152  m_masterCanProceed = true;
153  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Signal master for EndRun";
154  m_notifyMasterCv.notify_one();
155  m_notifyMainCv.wait(lk2, [&]() { return m_mainCanProceed; });
156  lk2.unlock();
157  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: finish EndRun";
158 }
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 185 of file OscarMTMasterThread.cc.

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

Referenced by beginRun().

185  {
186  bool geomChanged = idealGeomRcdWatcher_.check(iSetup);
187  if (geomChanged && (!m_firstRun)) {
189  << "[SimG4Core OscarMTMasterThread]\n"
190  << "The Geometry configuration is changed during the job execution\n"
191  << "this is not allowed, the geometry must stay unchanged";
192  }
193  // Don't read from ES if not the first run, just as in
194  if (!m_firstRun)
195  return;
196 
197  // DDDWorld: get the DDCV from the ES and use it to build the World
198  if (m_pGeoFromDD4hep) {
200  iSetup.get<IdealGeometryRecord>().get(pDD);
201  m_pDD4hep = pDD.product();
202  } else {
204  iSetup.get<IdealGeometryRecord>().get(pDD);
205  m_pDD = pDD.product();
206  }
207 
209  iSetup.get<PDTRecord>().get(fTable);
210  m_pTable = fTable.product();
211 
212  m_firstRun = false;
213 }
edm::ESWatcher< IdealGeometryRecord > idealGeomRcdWatcher_
const cms::DDCompactView * m_pDD4hep
const DDCompactView * m_pDD
T const * product() const
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:52
T get() const
Definition: EventSetup.h:73
T const * product() const
Definition: ESHandle.h:86
const HepPDT::ParticleDataTable * m_pTable
RunManagerMT& OscarMTMasterThread::runManagerMaster ( ) const
inline

Definition at line 41 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread().

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

Definition at line 42 of file OscarMTMasterThread.h.

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

Definition at line 160 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().

160  {
161  if (m_stopped) {
162  return;
163  }
164  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: stop main thread";
165 
166  // Release our instance of the shared master run manager, so that
167  // the G4 master thread can do the cleanup. Then notify the master
168  // thread, and join it.
169  std::unique_lock<std::mutex> lk2(m_threadMutex);
170  m_runManagerMaster.reset();
171  LogDebug("OscarMTMasterThread") << "Main thread: reseted shared_ptr";
172 
174  m_masterCanProceed = true;
175  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: notify";
176  m_notifyMasterCv.notify_one();
177  lk2.unlock();
178 
179  LogDebug("OscarMTMasterThread") << "Main thread: joining master thread";
180  m_masterThread.join();
181  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: main thread finished";
182  m_stopped = true;
183 }
#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 55 of file OscarMTMasterThread.h.

Referenced by readES().

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

Definition at line 56 of file OscarMTMasterThread.h.

bool OscarMTMasterThread::m_firstRun
mutableprivate

Definition at line 70 of file OscarMTMasterThread.h.

Referenced by readES().

bool OscarMTMasterThread::m_mainCanProceed
mutableprivate

Definition at line 69 of file OscarMTMasterThread.h.

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

bool OscarMTMasterThread::m_masterCanProceed
mutableprivate

Definition at line 68 of file OscarMTMasterThread.h.

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

std::thread OscarMTMasterThread::m_masterThread
private

Definition at line 52 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and stopThread().

ThreadState OscarMTMasterThread::m_masterThreadState
mutableprivate

Definition at line 66 of file OscarMTMasterThread.h.

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

std::condition_variable OscarMTMasterThread::m_notifyMainCv
mutableprivate

Definition at line 64 of file OscarMTMasterThread.h.

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

std::condition_variable OscarMTMasterThread::m_notifyMasterCv
mutableprivate

Definition at line 63 of file OscarMTMasterThread.h.

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

const DDCompactView* OscarMTMasterThread::m_pDD
mutableprivate

Definition at line 57 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and readES().

const cms::DDCompactView* OscarMTMasterThread::m_pDD4hep
mutableprivate

Definition at line 58 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and readES().

const bool OscarMTMasterThread::m_pGeoFromDD4hep
private

Definition at line 49 of file OscarMTMasterThread.h.

Referenced by readES().

std::mutex OscarMTMasterThread::m_protectMutex
mutableprivate

Definition at line 61 of file OscarMTMasterThread.h.

Referenced by beginRun(), and endRun().

const HepPDT::ParticleDataTable* OscarMTMasterThread::m_pTable
mutableprivate

Definition at line 59 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and readES().

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

Definition at line 51 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and stopThread().

bool OscarMTMasterThread::m_stopped
mutableprivate

Definition at line 71 of file OscarMTMasterThread.h.

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

std::mutex OscarMTMasterThread::m_threadMutex
mutableprivate

Definition at line 62 of file OscarMTMasterThread.h.

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