CMS 3D CMS Logo

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

#include <OscarMTMasterThread.h>

Public Member Functions

void beginRun (const edm::EventSetup &iSetup) const
 
void callConsumes (edm::ConsumesCollector &&iC) 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 Attributes

edm::ESGetToken< cms::DDCompactView, IdealGeometryRecordm_DD4Hep
 
edm::ESGetToken< DDCompactView, IdealGeometryRecordm_DDD
 
bool m_firstRun = true
 
bool m_hasToken = false
 
bool m_mainCanProceed = false
 
bool m_masterCanProceed = false
 
std::thread m_masterThread
 
ThreadState m_masterThreadState
 
std::condition_variable m_notifyMainCv
 
std::condition_variable m_notifyMasterCv
 
const cms::DDCompactViewm_pDD4Hep = nullptr
 
const DDCompactViewm_pDDD = nullptr
 
edm::ESGetToken< HepPDT::ParticleDataTable, PDTRecordm_PDT
 
const bool m_pGeoFromDD4hep
 
std::mutex m_protectMutex
 
const HepPDT::ParticleDataTablem_pTable = nullptr
 
std::shared_ptr< RunManagerMTm_runManagerMaster
 
bool m_stopped = false
 
std::mutex m_threadMutex
 

Detailed Description

Definition at line 37 of file OscarMTMasterThread.h.

Member Enumeration Documentation

◆ ThreadState

Enumerator
NotExist 
BeginRun 
EndRun 
Destruct 

Definition at line 52 of file OscarMTMasterThread.h.

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

Constructor & Destructor Documentation

◆ OscarMTMasterThread()

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

Definition at line 12 of file OscarMTMasterThread.cc.

13  : m_pGeoFromDD4hep(iConfig.getParameter<bool>("g4GeometryDD4hepSource")),
15  // Lock the mutex
16  std::unique_lock<std::mutex> lk(m_threadMutex);
17 
18  edm::LogVerbatim("SimG4CoreApplication")
19  << "OscarMTMasterThread: creating master thread DD4Hep: " << m_pGeoFromDD4hep;
20 
21  // Create Geant4 master thread
22  m_masterThread = std::thread([&]() {
24  // Initialization
25  std::unique_ptr<CustomUIsession> uiSession;
26 
27  // Lock the mutex (i.e. wait until the creating thread has called cv.wait()
28  std::unique_lock<std::mutex> lk2(m_threadMutex);
29 
30  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: initializing RunManagerMT";
31 
32  //UIsession manager for message handling
33  uiSession = std::make_unique<CustomUIsession>();
34 
35  // Create the master run manager, and share it to the CMSSW thread
36  m_runManagerMaster = std::make_shared<RunManagerMT>(iConfig);
37 
39  // State loop
40  bool isG4Alive = false;
41  while (true) {
42  // Signal main thread that it can proceed
43  m_mainCanProceed = true;
44  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: State loop, notify main thread";
45  m_notifyMainCv.notify_one();
46 
47  // Wait until the main thread sends signal
48  m_masterCanProceed = false;
49  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: State loop, starting wait";
50  m_notifyMasterCv.wait(lk2, [&] { return m_masterCanProceed; });
51  //m_notifyMasterCv.wait(lk2, [&] { return false; });
52 
53  // Act according to the state
54  edm::LogVerbatim("OscarMTMasterThread")
55  << "Master thread: Woke up, state is " << static_cast<int>(m_masterThreadState);
57  // Initialize Geant4
58  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Initializing Geant4";
60  isG4Alive = true;
62  // Stop Geant4
63  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Stopping Geant4";
64  m_runManagerMaster->stopG4();
65  isG4Alive = false;
67  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Breaking out of state loop";
68  if (isG4Alive)
70  << "Geant4 is still alive, master thread state must be set to EndRun before Destruct";
71  break;
72  } else {
74  << "OscarMTMasterThread: Illegal master thread state " << static_cast<int>(m_masterThreadState);
75  }
76  }
77 
79  // Cleanup
80  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: start RunManagerMT destruction";
81 
82  // must be done in this thread, segfault otherwise
83  m_runManagerMaster.reset();
84  G4PhysicalVolumeStore::Clean();
85 
86  edm::LogVerbatim("OscarMTMasterThread") << "Master thread: Reseted shared_ptr";
87  lk2.unlock();
88  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Master thread is finished";
89  });
90 
91  // Start waiting a signal from the condition variable (releases the lock temporarily)
92  // First for initialization
93  m_mainCanProceed = false;
94  LogDebug("OscarMTMasterThread") << "Main thread: Signal master for initialization";
95  m_notifyMainCv.wait(lk, [&]() { return m_mainCanProceed; });
96 
97  lk.unlock();
98  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Master thread is constructed";
99 }

References BeginRun, Destruct, EndRun, Exception, LogDebug, edm::errors::LogicError, m_mainCanProceed, m_masterCanProceed, m_masterThread, m_masterThreadState, m_notifyMainCv, m_notifyMasterCv, m_pDD4Hep, m_pDDD, m_pGeoFromDD4hep, m_pTable, m_runManagerMaster, and m_threadMutex.

◆ ~OscarMTMasterThread()

OscarMTMasterThread::~OscarMTMasterThread ( )

Definition at line 101 of file OscarMTMasterThread.cc.

101  {
102  if (!m_stopped) {
103  stopThread();
104  }
105 }

References m_stopped, and stopThread().

Member Function Documentation

◆ beginRun()

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

Definition at line 120 of file OscarMTMasterThread.cc.

120  {
121  std::lock_guard<std::mutex> lk(m_protectMutex);
122  std::unique_lock<std::mutex> lk2(m_threadMutex);
123  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::beginRun";
124 
125  if (m_firstRun) {
126  if (m_pGeoFromDD4hep) {
127  m_pDD4Hep = &(*iSetup.getTransientHandle(m_DD4Hep));
128  } else {
129  m_pDDD = &(*iSetup.getTransientHandle(m_DDD));
130  }
131  m_pTable = &iSetup.getData(m_PDT);
132  m_firstRun = false;
133  }
135  m_masterCanProceed = true;
136  m_mainCanProceed = false;
137  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: Signal master for BeginRun";
138  m_notifyMasterCv.notify_one();
139  m_notifyMainCv.wait(lk2, [&]() { return m_mainCanProceed; });
140 
141  lk2.unlock();
142  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread: finish BeginRun";
143 }

References BeginRun, edm::EventSetup::getData(), edm::EventSetup::getTransientHandle(), m_DD4Hep, m_DDD, m_firstRun, m_mainCanProceed, m_masterCanProceed, m_masterThreadState, m_notifyMainCv, m_notifyMasterCv, m_pDD4Hep, m_pDDD, m_PDT, m_pGeoFromDD4hep, m_protectMutex, m_pTable, and m_threadMutex.

Referenced by OscarMTProducer::globalBeginRun().

◆ callConsumes()

void OscarMTMasterThread::callConsumes ( edm::ConsumesCollector &&  iC) const

◆ endRun()

void OscarMTMasterThread::endRun ( ) const

Definition at line 145 of file OscarMTMasterThread.cc.

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

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

◆ runManagerMaster()

RunManagerMT& OscarMTMasterThread::runManagerMaster ( ) const
inline

Definition at line 48 of file OscarMTMasterThread.h.

48 { return *m_runManagerMaster; }

References m_runManagerMaster.

◆ runManagerMasterPtr()

RunManagerMT* OscarMTMasterThread::runManagerMasterPtr ( ) const
inline

Definition at line 49 of file OscarMTMasterThread.h.

49 { return m_runManagerMaster.get(); }

References m_runManagerMaster.

Referenced by OscarMTProducer::beginRun().

◆ stopThread()

void OscarMTMasterThread::stopThread ( )

Definition at line 159 of file OscarMTMasterThread.cc.

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

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

◆ m_DD4Hep

edm::ESGetToken<cms::DDCompactView, IdealGeometryRecord> OscarMTMasterThread::m_DD4Hep
mutableprivate

Definition at line 64 of file OscarMTMasterThread.h.

Referenced by beginRun(), and callConsumes().

◆ m_DDD

edm::ESGetToken<DDCompactView, IdealGeometryRecord> OscarMTMasterThread::m_DDD
mutableprivate

Definition at line 63 of file OscarMTMasterThread.h.

Referenced by beginRun(), and callConsumes().

◆ m_firstRun

bool OscarMTMasterThread::m_firstRun = true
mutableprivate

Definition at line 78 of file OscarMTMasterThread.h.

Referenced by beginRun().

◆ m_hasToken

bool OscarMTMasterThread::m_hasToken = false
mutableprivate

Definition at line 75 of file OscarMTMasterThread.h.

Referenced by callConsumes().

◆ m_mainCanProceed

bool OscarMTMasterThread::m_mainCanProceed = false
mutableprivate

Definition at line 77 of file OscarMTMasterThread.h.

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

◆ m_masterCanProceed

bool OscarMTMasterThread::m_masterCanProceed = false
mutableprivate

Definition at line 76 of file OscarMTMasterThread.h.

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

◆ m_masterThread

std::thread OscarMTMasterThread::m_masterThread
private

Definition at line 57 of file OscarMTMasterThread.h.

Referenced by OscarMTMasterThread(), and stopThread().

◆ m_masterThreadState

ThreadState OscarMTMasterThread::m_masterThreadState
mutableprivate

Definition at line 73 of file OscarMTMasterThread.h.

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

◆ m_notifyMainCv

std::condition_variable OscarMTMasterThread::m_notifyMainCv
mutableprivate

Definition at line 71 of file OscarMTMasterThread.h.

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

◆ m_notifyMasterCv

std::condition_variable OscarMTMasterThread::m_notifyMasterCv
mutableprivate

Definition at line 70 of file OscarMTMasterThread.h.

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

◆ m_pDD4Hep

const cms::DDCompactView* OscarMTMasterThread::m_pDD4Hep = nullptr
mutableprivate

Definition at line 61 of file OscarMTMasterThread.h.

Referenced by beginRun(), and OscarMTMasterThread().

◆ m_pDDD

const DDCompactView* OscarMTMasterThread::m_pDDD = nullptr
mutableprivate

Definition at line 60 of file OscarMTMasterThread.h.

Referenced by beginRun(), and OscarMTMasterThread().

◆ m_PDT

edm::ESGetToken<HepPDT::ParticleDataTable, PDTRecord> OscarMTMasterThread::m_PDT
mutableprivate

Definition at line 65 of file OscarMTMasterThread.h.

Referenced by beginRun(), and callConsumes().

◆ m_pGeoFromDD4hep

const bool OscarMTMasterThread::m_pGeoFromDD4hep
private

Definition at line 54 of file OscarMTMasterThread.h.

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

◆ m_protectMutex

std::mutex OscarMTMasterThread::m_protectMutex
mutableprivate

Definition at line 68 of file OscarMTMasterThread.h.

Referenced by beginRun(), and endRun().

◆ m_pTable

const HepPDT::ParticleDataTable* OscarMTMasterThread::m_pTable = nullptr
mutableprivate

Definition at line 62 of file OscarMTMasterThread.h.

Referenced by beginRun(), and OscarMTMasterThread().

◆ m_runManagerMaster

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

◆ m_stopped

bool OscarMTMasterThread::m_stopped = false
mutableprivate

Definition at line 79 of file OscarMTMasterThread.h.

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

◆ m_threadMutex

std::mutex OscarMTMasterThread::m_threadMutex
mutableprivate

Definition at line 69 of file OscarMTMasterThread.h.

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

OscarMTMasterThread::m_hasToken
bool m_hasToken
Definition: OscarMTMasterThread.h:75
OscarMTMasterThread::m_notifyMasterCv
std::condition_variable m_notifyMasterCv
Definition: OscarMTMasterThread.h:70
OscarMTMasterThread::m_masterThread
std::thread m_masterThread
Definition: OscarMTMasterThread.h:57
edm::errors::LogicError
Definition: EDMException.h:37
OscarMTMasterThread::m_firstRun
bool m_firstRun
Definition: OscarMTMasterThread.h:78
OscarMTMasterThread::m_DDD
edm::ESGetToken< DDCompactView, IdealGeometryRecord > m_DDD
Definition: OscarMTMasterThread.h:63
OscarMTMasterThread::ThreadState::BeginRun
edm::ConsumesCollector::esConsumes
auto esConsumes()
Definition: ConsumesCollector.h:97
OscarMTMasterThread::m_PDT
edm::ESGetToken< HepPDT::ParticleDataTable, PDTRecord > m_PDT
Definition: OscarMTMasterThread.h:65
OscarMTMasterThread::ThreadState::EndRun
OscarMTMasterThread::m_masterThreadState
ThreadState m_masterThreadState
Definition: OscarMTMasterThread.h:73
DDCompactView
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
OscarMTMasterThread::m_mainCanProceed
bool m_mainCanProceed
Definition: OscarMTMasterThread.h:77
OscarMTMasterThread::m_runManagerMaster
std::shared_ptr< RunManagerMT > m_runManagerMaster
Definition: OscarMTMasterThread.h:56
OscarMTMasterThread::stopThread
void stopThread()
Definition: OscarMTMasterThread.cc:159
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
OscarMTMasterThread::m_protectMutex
std::mutex m_protectMutex
Definition: OscarMTMasterThread.h:68
OscarMTMasterThread::m_DD4Hep
edm::ESGetToken< cms::DDCompactView, IdealGeometryRecord > m_DD4Hep
Definition: OscarMTMasterThread.h:64
OscarMTMasterThread::m_stopped
bool m_stopped
Definition: OscarMTMasterThread.h:79
OscarMTMasterThread::m_pDD4Hep
const cms::DDCompactView * m_pDD4Hep
Definition: OscarMTMasterThread.h:61
edm::EventSetup::getTransientHandle
ESTransientHandle< T > getTransientHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:166
edm::EventSetup::getData
bool getData(T &iHolder) const
Definition: EventSetup.h:127
OscarMTMasterThread::ThreadState::NotExist
OscarMTMasterThread::m_masterCanProceed
bool m_masterCanProceed
Definition: OscarMTMasterThread.h:76
OscarMTMasterThread::m_pGeoFromDD4hep
const bool m_pGeoFromDD4hep
Definition: OscarMTMasterThread.h:54
OscarMTMasterThread::m_pDDD
const DDCompactView * m_pDDD
Definition: OscarMTMasterThread.h:60
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
edm::Transition::BeginRun
Exception
Definition: hltDiff.cc:245
cms::DDCompactView
Definition: DDCompactView.h:31
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
OscarMTMasterThread::m_pTable
const HepPDT::ParticleDataTable * m_pTable
Definition: OscarMTMasterThread.h:62
OscarMTMasterThread::m_threadMutex
std::mutex m_threadMutex
Definition: OscarMTMasterThread.h:69
edm::Transition::EndRun
PDTRecord
Definition: PDTRecord.h:14
ParticleDataTable
HepPDT::ParticleDataTable ParticleDataTable
Definition: ParticleDataTable.h:8
OscarMTMasterThread::m_notifyMainCv
std::condition_variable m_notifyMainCv
Definition: OscarMTMasterThread.h:71
OscarMTMasterThread::ThreadState::Destruct
IdealGeometryRecord
Definition: IdealGeometryRecord.h:25