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

◆ ThreadState

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::OscarMTMasterThread ( const edm::ParameterSet iConfig)
explicit

Definition at line 22 of file OscarMTMasterThread.cc.

23  : m_pGeoFromDD4hep(iConfig.getParameter<bool>("g4GeometryDD4hepSource")),
24  m_pDD(nullptr),
25  m_pDD4hep(nullptr),
26  m_pTable(nullptr),
28  m_masterCanProceed(false),
29  m_mainCanProceed(false),
30  m_firstRun(true),
31  m_stopped(false) {
32  // Lock the mutex
33  std::unique_lock<std::mutex> lk(m_threadMutex);
34 
35  edm::LogVerbatim("SimG4CoreApplication") << "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") << "OscarMTMasterThread: initializing RunManagerMT";
49 
50  //UIsession manager for message handling
51  uiSession = std::make_unique<CustomUIsession>();
52 
53  // Create the master run manager, and share it to the CMSSW thread
54  runManagerMaster = std::make_shared<RunManagerMT>(iConfig);
56 
57  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: initialization of RunManagerMT finished";
58 
60  // State loop
61  bool isG4Alive = false;
62  while (true) {
63  // Signal main thread that it can proceed
64  m_mainCanProceed = true;
65  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: State loop, notify main thread";
66  m_notifyMainCv.notify_one();
67 
68  // Wait until the main thread sends signal
69  m_masterCanProceed = false;
70  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: State loop, starting wait";
71  m_notifyMasterCv.wait(lk2, [&] { return m_masterCanProceed; });
72 
73  // Act according to the state
74  edm::LogVerbatim("OscarMTMasterThread")
75  << "Master thread: Woke up, state is " << static_cast<int>(m_masterThreadState);
77  // Initialize Geant4
78  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Initializing Geant4";
80  isG4Alive = true;
82  // Stop Geant4
83  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Stopping Geant4";
85  isG4Alive = false;
87  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Breaking out of state loop";
88  if (isG4Alive)
90  << "Geant4 is still alive, master thread state must be set to EndRun before Destruct";
91  break;
92  } else {
94  << "OscarMTMasterThread: Illegal master thread state " << static_cast<int>(m_masterThreadState);
95  }
96  }
97 
99  // Cleanup
100  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: start RunManagerMT destruction";
101  LogDebug("OscarMTMasterThread") << "Master thread: Am I unique owner of runManagerMaster? "
102  << runManagerMaster.unique();
103 
104  // must be done in this thread, segfault otherwise
105  runManagerMaster.reset();
106  G4PhysicalVolumeStore::Clean();
107 
108  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Reseted shared_ptr";
109  lk2.unlock();
110  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Master thread is finished";
111  });
112 
113  // Start waiting a signal from the condition variable (releases the lock temporarily)
114  // First for initialization
115  m_mainCanProceed = false;
116  LogDebug("OscarMTMasterThread") << "Main thread: Signal master for initialization";
117  m_notifyMainCv.wait(lk, [&]() { return m_mainCanProceed; });
118 
119  lk.unlock();
120  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Master thread is constructed";
121 }

References BeginRun, Destruct, EndRun, Exception, RunManagerMT::initG4(), 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, runManagerMaster(), and RunManagerMT::stopG4().

◆ ~OscarMTMasterThread()

OscarMTMasterThread::~OscarMTMasterThread ( )

Definition at line 123 of file OscarMTMasterThread.cc.

123  {
124  if (!m_stopped) {
125  stopThread();
126  }
127 }

References m_stopped, and stopThread().

Member Function Documentation

◆ beginRun()

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

Definition at line 129 of file OscarMTMasterThread.cc.

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

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

Referenced by OscarMTProducer::globalBeginRun().

◆ endRun()

void OscarMTMasterThread::endRun ( ) const

Definition at line 148 of file OscarMTMasterThread.cc.

148  {
149  std::lock_guard<std::mutex> lk(m_protectMutex);
150 
151  std::unique_lock<std::mutex> lk2(m_threadMutex);
153  m_mainCanProceed = false;
154  m_masterCanProceed = true;
155  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Signal master for EndRun";
156  m_notifyMasterCv.notify_one();
157  m_notifyMainCv.wait(lk2, [&]() { return m_mainCanProceed; });
158  lk2.unlock();
159  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: finish EndRun";
160 }

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

◆ readES()

void OscarMTMasterThread::readES ( const edm::EventSetup iSetup) const
private

Definition at line 187 of file OscarMTMasterThread.cc.

187  {
188  bool geomChanged = idealGeomRcdWatcher_.check(iSetup);
189  if (geomChanged && (!m_firstRun)) {
191  << "[SimG4Core OscarMTMasterThread]\n"
192  << "The Geometry configuration is changed during the job execution\n"
193  << "this is not allowed, the geometry must stay unchanged";
194  }
195  // Don't read from ES if not the first run, just as in
196  if (!m_firstRun)
197  return;
198 
199  // DDDWorld: get the DDCV from the ES and use it to build the World
200  if (m_pGeoFromDD4hep) {
202  iSetup.get<IdealGeometryRecord>().get(pDD);
203  m_pDD4hep = pDD.product();
204  } else {
206  iSetup.get<IdealGeometryRecord>().get(pDD);
207  m_pDD = pDD.product();
208  }
209 
211  iSetup.get<PDTRecord>().get(fTable);
212  m_pTable = fTable.product();
213 
214  m_firstRun = false;
215 }

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

Referenced by beginRun().

◆ runManagerMaster()

RunManagerMT& OscarMTMasterThread::runManagerMaster ( ) const
inline

Definition at line 41 of file OscarMTMasterThread.h.

41 { return *m_runManagerMaster; }

References m_runManagerMaster.

Referenced by OscarMTMasterThread().

◆ runManagerMasterPtr()

RunManagerMT* OscarMTMasterThread::runManagerMasterPtr ( ) const
inline

Definition at line 42 of file OscarMTMasterThread.h.

42 { return m_runManagerMaster.get(); }

References m_runManagerMaster.

Referenced by OscarMTProducer::beginRun().

◆ stopThread()

void OscarMTMasterThread::stopThread ( )

Definition at line 162 of file OscarMTMasterThread.cc.

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

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

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

Member Data Documentation

◆ idealGeomRcdWatcher_

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

Definition at line 55 of file OscarMTMasterThread.h.

Referenced by readES().

◆ idealMagRcdWatcher_

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

Definition at line 56 of file OscarMTMasterThread.h.

◆ m_firstRun

bool OscarMTMasterThread::m_firstRun
mutableprivate

Definition at line 70 of file OscarMTMasterThread.h.

Referenced by readES().

◆ m_mainCanProceed

bool OscarMTMasterThread::m_mainCanProceed
mutableprivate

Definition at line 69 of file OscarMTMasterThread.h.

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

◆ m_masterCanProceed

bool OscarMTMasterThread::m_masterCanProceed
mutableprivate

Definition at line 68 of file OscarMTMasterThread.h.

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

◆ m_masterThread

std::thread OscarMTMasterThread::m_masterThread
private

Definition at line 52 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and stopThread().

◆ m_masterThreadState

ThreadState OscarMTMasterThread::m_masterThreadState
mutableprivate

Definition at line 66 of file OscarMTMasterThread.h.

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

◆ m_notifyMainCv

std::condition_variable OscarMTMasterThread::m_notifyMainCv
mutableprivate

Definition at line 64 of file OscarMTMasterThread.h.

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

◆ m_notifyMasterCv

std::condition_variable OscarMTMasterThread::m_notifyMasterCv
mutableprivate

Definition at line 63 of file OscarMTMasterThread.h.

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

◆ m_pDD

const DDCompactView* OscarMTMasterThread::m_pDD
mutableprivate

Definition at line 57 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and readES().

◆ m_pDD4hep

const cms::DDCompactView* OscarMTMasterThread::m_pDD4hep
mutableprivate

Definition at line 58 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and readES().

◆ m_pGeoFromDD4hep

const bool OscarMTMasterThread::m_pGeoFromDD4hep
private

Definition at line 49 of file OscarMTMasterThread.h.

Referenced by readES().

◆ m_protectMutex

std::mutex OscarMTMasterThread::m_protectMutex
mutableprivate

Definition at line 61 of file OscarMTMasterThread.h.

Referenced by beginRun(), and endRun().

◆ m_pTable

const HepPDT::ParticleDataTable* OscarMTMasterThread::m_pTable
mutableprivate

Definition at line 59 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and readES().

◆ m_runManagerMaster

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

◆ m_stopped

bool OscarMTMasterThread::m_stopped
mutableprivate

Definition at line 71 of file OscarMTMasterThread.h.

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

◆ m_threadMutex

std::mutex OscarMTMasterThread::m_threadMutex
mutableprivate

Definition at line 62 of file OscarMTMasterThread.h.

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

edm::ESWatcher::check
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:52
edm::ESHandle::product
T const * product() const
Definition: ESHandle.h:86
OscarMTMasterThread::m_notifyMasterCv
std::condition_variable m_notifyMasterCv
Definition: OscarMTMasterThread.h:63
OscarMTMasterThread::m_masterThread
std::thread m_masterThread
Definition: OscarMTMasterThread.h:52
edm::ESTransientHandle::product
T const * product() const
Definition: ESTransientHandle.h:51
edm::errors::LogicError
Definition: EDMException.h:37
OscarMTMasterThread::m_firstRun
bool m_firstRun
Definition: OscarMTMasterThread.h:70
OscarMTMasterThread::ThreadState::BeginRun
OscarMTMasterThread::ThreadState::EndRun
OscarMTMasterThread::m_masterThreadState
ThreadState m_masterThreadState
Definition: OscarMTMasterThread.h:66
edm::EventSetup::get
T get() const
Definition: EventSetup.h:80
OscarMTMasterThread::m_mainCanProceed
bool m_mainCanProceed
Definition: OscarMTMasterThread.h:69
edm::ESHandle< HepPDT::ParticleDataTable >
OscarMTMasterThread::m_runManagerMaster
std::shared_ptr< RunManagerMT > m_runManagerMaster
Definition: OscarMTMasterThread.h:51
OscarMTMasterThread::stopThread
void stopThread()
Definition: OscarMTMasterThread.cc:162
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:223
OscarMTMasterThread::runManagerMaster
RunManagerMT & runManagerMaster() const
Definition: OscarMTMasterThread.h:41
RunManagerMT::initG4
void initG4(const DDCompactView *, const cms::DDCompactView *, const HepPDT::ParticleDataTable *)
Definition: RunManagerMT.cc:97
OscarMTMasterThread::m_protectMutex
std::mutex m_protectMutex
Definition: OscarMTMasterThread.h:61
OscarMTMasterThread::m_stopped
bool m_stopped
Definition: OscarMTMasterThread.h:71
get
#define get
OscarMTMasterThread::m_pDD
const DDCompactView * m_pDD
Definition: OscarMTMasterThread.h:57
edm::ESTransientHandle
Definition: ESTransientHandle.h:41
OscarMTMasterThread::ThreadState::NotExist
OscarMTMasterThread::m_masterCanProceed
bool m_masterCanProceed
Definition: OscarMTMasterThread.h:68
OscarMTMasterThread::m_pGeoFromDD4hep
const bool m_pGeoFromDD4hep
Definition: OscarMTMasterThread.h:49
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
edm::Transition::BeginRun
Exception
Definition: hltDiff.cc:246
OscarMTMasterThread::m_pDD4hep
const cms::DDCompactView * m_pDD4hep
Definition: OscarMTMasterThread.h:58
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
OscarMTMasterThread::m_pTable
const HepPDT::ParticleDataTable * m_pTable
Definition: OscarMTMasterThread.h:59
OscarMTMasterThread::readES
void readES(const edm::EventSetup &iSetup) const
Definition: OscarMTMasterThread.cc:187
RunManagerMT::stopG4
void stopG4()
Definition: RunManagerMT.cc:254
OscarMTMasterThread::idealGeomRcdWatcher_
edm::ESWatcher< IdealGeometryRecord > idealGeomRcdWatcher_
Definition: OscarMTMasterThread.h:55
OscarMTMasterThread::m_threadMutex
std::mutex m_threadMutex
Definition: OscarMTMasterThread.h:62
edm::Transition::EndRun
edm::errors::Configuration
Definition: EDMException.h:36
PDTRecord
Definition: PDTRecord.h:14
OscarMTMasterThread::m_notifyMainCv
std::condition_variable m_notifyMainCv
Definition: OscarMTMasterThread.h:64
OscarMTMasterThread::ThreadState::Destruct
IdealGeometryRecord
Definition: IdealGeometryRecord.h:25