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.

References BeginRun, Destruct, EndRun, Exception, LogDebug, 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.

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)
69  throw cms::Exception("LogicError") << "OscarMTMasterThread: Geant4 is still alive, master thread "
70  << "state must be set to EndRun before Destruct";
71  break;
72  } else {
73  throw cms::Exception("LogicError")
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("SimG4CoreApplication") << "OscarMTMasterThread: physics and geometry are cleaned";
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 }
Log< level::Info, true > LogVerbatim
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::shared_ptr< RunManagerMT > m_runManagerMaster
ThreadState m_masterThreadState
const cms::DDCompactView * m_pDD4hep
std::condition_variable m_notifyMasterCv
const DDCompactView * m_pDDD
const HepPDT::ParticleDataTable * m_pTable
std::condition_variable m_notifyMainCv
#define LogDebug(id)

◆ ~OscarMTMasterThread()

OscarMTMasterThread::~OscarMTMasterThread ( )

Definition at line 101 of file OscarMTMasterThread.cc.

References m_stopped, and stopThread().

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

Member Function Documentation

◆ beginRun()

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

Definition at line 120 of file OscarMTMasterThread.cc.

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().

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 }
Log< level::Info, true > LogVerbatim
edm::ESGetToken< cms::DDCompactView, IdealGeometryRecord > m_DD4hep
edm::ESGetToken< DDCompactView, IdealGeometryRecord > m_DDD
ThreadState m_masterThreadState
const cms::DDCompactView * m_pDD4hep
bool getData(T &iHolder) const
Definition: EventSetup.h:122
std::condition_variable m_notifyMasterCv
edm::ESGetToken< HepPDT::ParticleDataTable, PDTRecord > m_PDT
const DDCompactView * m_pDDD
ESTransientHandle< T > getTransientHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:162
const HepPDT::ParticleDataTable * m_pTable
std::condition_variable m_notifyMainCv

◆ callConsumes()

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

Definition at line 107 of file OscarMTMasterThread.cc.

References edm::BeginRun, m_DD4hep, m_DDD, m_hasToken, m_PDT, and m_pGeoFromDD4hep.

107  {
108  if (m_hasToken) {
109  return;
110  }
111  if (m_pGeoFromDD4hep) {
113  } else {
115  }
117  m_hasToken = true;
118 }
HepPDT::ParticleDataTable ParticleDataTable
edm::ESGetToken< cms::DDCompactView, IdealGeometryRecord > m_DD4hep
edm::ESGetToken< DDCompactView, IdealGeometryRecord > m_DDD
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
edm::ESGetToken< HepPDT::ParticleDataTable, PDTRecord > m_PDT

◆ endRun()

void OscarMTMasterThread::endRun ( ) const

Definition at line 145 of file OscarMTMasterThread.cc.

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

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 }
Log< level::Info, true > LogVerbatim
ThreadState m_masterThreadState
std::condition_variable m_notifyMasterCv
std::condition_variable m_notifyMainCv

◆ runManagerMaster()

RunManagerMT& OscarMTMasterThread::runManagerMaster ( ) const
inline

Definition at line 48 of file OscarMTMasterThread.h.

References m_runManagerMaster.

48 { return *m_runManagerMaster; }
std::shared_ptr< RunManagerMT > m_runManagerMaster

◆ runManagerMasterPtr()

RunManagerMT* OscarMTMasterThread::runManagerMasterPtr ( ) const
inline

Definition at line 49 of file OscarMTMasterThread.h.

References m_runManagerMaster.

Referenced by OscarMTProducer::beginRun().

49 { return m_runManagerMaster.get(); }
std::shared_ptr< RunManagerMT > m_runManagerMaster

◆ stopThread()

void OscarMTMasterThread::stopThread ( )

Definition at line 159 of file OscarMTMasterThread.cc.

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

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

159  {
160  if (m_stopped) {
161  return;
162  }
163  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: stop main thread";
164  std::unique_lock<std::mutex> lk2(m_threadMutex);
166  m_masterCanProceed = true;
167  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: notify";
168  m_notifyMasterCv.notify_one();
169  lk2.unlock();
170  m_masterThread.join();
171  edm::LogVerbatim("SimG4CoreApplication") << "OscarMTMasterThread::stopTread: is done";
172  m_stopped = true;
173 }
Log< level::Info, true > LogVerbatim
ThreadState m_masterThreadState
std::condition_variable m_notifyMasterCv

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().