CMS 3D CMS Logo

OscarMTMasterThread.cc
Go to the documentation of this file.
1 #include <memory>
2 
4 
7 
12 
16 
17 #include "HepPDT/ParticleDataTable.hh"
19 
20 #include "G4PhysicalVolumeStore.hh"
21 
23  : m_pGeoFromDD4hep(iConfig.getParameter<bool>("g4GeometryDD4hepSource")),
24  m_pDD(nullptr),
25  m_pDD4hep(nullptr),
26  m_pTable(nullptr),
27  m_masterThreadState(ThreadState::NotExist),
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  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 = std::make_unique<CustomUIsession>();
50 
51  // Create the master run manager, and share it to the CMSSW thread
52  m_runManagerMaster = std::make_shared<RunManagerMT>(iConfig);
53 
55  // State loop
56  bool isG4Alive = false;
57  while (true) {
58  // Signal main thread that it can proceed
59  m_mainCanProceed = true;
60  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: State loop, notify main thread";
61  m_notifyMainCv.notify_one();
62 
63  // Wait until the main thread sends signal
64  m_masterCanProceed = false;
65  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: State loop, starting wait";
66  m_notifyMasterCv.wait(lk2, [&] { return m_masterCanProceed; });
67 
68  // Act according to the state
69  edm::LogVerbatim("OscarMTMasterThread")
70  << "Master thread: Woke up, state is " << static_cast<int>(m_masterThreadState);
72  // Initialize Geant4
73  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Initializing Geant4";
75  isG4Alive = true;
77  // Stop Geant4
78  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Stopping Geant4";
79  m_runManagerMaster->stopG4();
80  isG4Alive = false;
82  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Breaking out of state loop";
83  if (isG4Alive)
85  << "Geant4 is still alive, master thread state must be set to EndRun before Destruct";
86  break;
87  } else {
89  << "OscarMTMasterThread: Illegal master thread state " << static_cast<int>(m_masterThreadState);
90  }
91  }
92 
94  // Cleanup
95  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: start RunManagerMT destruction";
96 
97  // must be done in this thread, segfault otherwise
98  m_runManagerMaster.reset();
99  G4PhysicalVolumeStore::Clean();
100 
101  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Reseted shared_ptr";
102  lk2.unlock();
103  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Master thread is finished";
104  });
105 
106  // Start waiting a signal from the condition variable (releases the lock temporarily)
107  // First for initialization
108  m_mainCanProceed = false;
109  LogDebug("OscarMTMasterThread") << "Main thread: Signal master for initialization";
110  m_notifyMainCv.wait(lk, [&]() { return m_mainCanProceed; });
111 
112  lk.unlock();
113  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Master thread is constructed";
114 }
115 
117  if (!m_stopped) {
118  stopThread();
119  }
120 }
121 
123  std::lock_guard<std::mutex> lk(m_protectMutex);
124 
125  std::unique_lock<std::mutex> lk2(m_threadMutex);
126 
127  // Reading from ES must be done in the main (CMSSW) thread
128  readES(iSetup);
129 
131  m_masterCanProceed = true;
132  m_mainCanProceed = false;
133  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Signal master for BeginRun";
134  m_notifyMasterCv.notify_one();
135  m_notifyMainCv.wait(lk2, [&]() { return m_mainCanProceed; });
136 
137  lk2.unlock();
138  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: finish BeginRun";
139 }
140 
142  std::lock_guard<std::mutex> lk(m_protectMutex);
143 
144  std::unique_lock<std::mutex> lk2(m_threadMutex);
146  m_mainCanProceed = false;
147  m_masterCanProceed = true;
148  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Signal master for EndRun";
149  m_notifyMasterCv.notify_one();
150  m_notifyMainCv.wait(lk2, [&]() { return m_mainCanProceed; });
151  lk2.unlock();
152  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: finish EndRun";
153 }
154 
156  if (m_stopped) {
157  return;
158  }
159  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: stop main thread";
160 
161  // Release our instance of the shared master run manager, so that
162  // the G4 master thread can do the cleanup. Then notify the master
163  // thread, and join it.
164  std::unique_lock<std::mutex> lk2(m_threadMutex);
165  m_runManagerMaster.reset();
166  LogDebug("OscarMTMasterThread") << "Main thread: reseted shared_ptr";
167 
169  m_masterCanProceed = true;
170  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: notify";
171  m_notifyMasterCv.notify_one();
172  lk2.unlock();
173 
174  LogDebug("OscarMTMasterThread") << "Main thread: joining master thread";
175  m_masterThread.join();
176  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: main thread finished";
177  m_stopped = true;
178 }
179 
180 void OscarMTMasterThread::readES(const edm::EventSetup& iSetup) const {
181  bool geomChanged = idealGeomRcdWatcher_.check(iSetup);
182  if (geomChanged && (!m_firstRun)) {
184  << "[SimG4Core OscarMTMasterThread]\n"
185  << "The Geometry configuration is changed during the job execution\n"
186  << "this is not allowed, the geometry must stay unchanged";
187  }
188  // Don't read from ES if not the first run, just as in
189  if (!m_firstRun)
190  return;
191 
192  // DDDWorld: get the DDCV from the ES and use it to build the World
193  if (m_pGeoFromDD4hep) {
195  iSetup.get<IdealGeometryRecord>().get(pDD);
196  m_pDD4hep = pDD.product();
197  } else {
199  iSetup.get<IdealGeometryRecord>().get(pDD);
200  m_pDD = pDD.product();
201  }
202 
204  iSetup.get<PDTRecord>().get(fTable);
205  m_pTable = fTable.product();
206 
207  m_firstRun = false;
208 }
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
electrons_cff.bool
bool
Definition: electrons_cff.py:366
OscarMTMasterThread::m_masterThread
std::thread m_masterThread
Definition: OscarMTMasterThread.h:52
ESTransientHandle.h
funct::false
false
Definition: Factorize.h:29
edm::ESTransientHandle::product
T const * product() const
Definition: ESTransientHandle.h:51
ESHandle.h
edm::errors::LogicError
Definition: EDMException.h:37
OscarMTMasterThread::m_firstRun
bool m_firstRun
Definition: OscarMTMasterThread.h:70
OscarMTMasterThread::OscarMTMasterThread
OscarMTMasterThread(const edm::ParameterSet &iConfig)
Definition: OscarMTMasterThread.cc:22
OscarMTMasterThread.h
OscarMTMasterThread::ThreadState::BeginRun
OscarMTMasterThread::endRun
void endRun() const
Definition: OscarMTMasterThread.cc:141
DDCompactView.h
EDMException.h
OscarMTMasterThread::ThreadState
ThreadState
Definition: OscarMTMasterThread.h:47
OscarMTMasterThread::ThreadState::EndRun
OscarMTMasterThread::m_masterThreadState
ThreadState m_masterThreadState
Definition: OscarMTMasterThread.h:66
edm::EventSetup::get
T get() const
Definition: EventSetup.h:87
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
CustomUIsession.h
DDCompactView.h
funct::true
true
Definition: Factorize.h:173
OscarMTMasterThread::stopThread
void stopThread()
Definition: OscarMTMasterThread.cc:155
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
edm::ParameterSet
Definition: ParameterSet.h:47
OscarMTMasterThread::m_protectMutex
std::mutex m_protectMutex
Definition: OscarMTMasterThread.h:61
RunManagerMT.h
OscarMTMasterThread::~OscarMTMasterThread
~OscarMTMasterThread()
Definition: OscarMTMasterThread.cc:116
OscarMTMasterThread::m_stopped
bool m_stopped
Definition: OscarMTMasterThread.h:71
IdealGeometryRecord.h
edm::EventSetup
Definition: EventSetup.h:58
get
#define get
OscarMTMasterThread::m_pDD
const DDCompactView * m_pDD
Definition: OscarMTMasterThread.h:57
edm::ESTransientHandle
Definition: ESTransientHandle.h:41
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
Exception
Definition: hltDiff.cc:245
OscarMTMasterThread::m_pDD4hep
const cms::DDCompactView * m_pDD4hep
Definition: OscarMTMasterThread.h:58
EventSetup.h
OscarMTMasterThread::beginRun
void beginRun(const edm::EventSetup &iSetup) const
Definition: OscarMTMasterThread.cc:122
OscarMTMasterThread::m_pTable
const HepPDT::ParticleDataTable * m_pTable
Definition: OscarMTMasterThread.h:59
PDTRecord.h
OscarMTMasterThread::readES
void readES(const edm::EventSetup &iSetup) const
Definition: OscarMTMasterThread.cc:180
OscarMTMasterThread::idealGeomRcdWatcher_
edm::ESWatcher< IdealGeometryRecord > idealGeomRcdWatcher_
Definition: OscarMTMasterThread.h:55
OscarMTMasterThread::m_threadMutex
std::mutex m_threadMutex
Definition: OscarMTMasterThread.h:62
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