Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OscarMTProducer initialisation #31022

Merged
merged 2 commits into from Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions SimG4Core/Application/plugins/OscarMTProducer.cc
Expand Up @@ -4,7 +4,8 @@
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "SimG4Core/Application/interface/OscarMTProducer.h"
#include "OscarMTProducer.h"

#include "SimG4Core/Application/interface/RunManagerMT.h"
#include "SimG4Core/Application/interface/RunManagerMTWorker.h"
#include "SimG4Core/Notification/interface/G4SimEvent.h"
Expand Down Expand Up @@ -62,11 +63,12 @@ namespace {
};
} // namespace

OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMasterThread*) {
OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMasterThread* ms) {
// Random number generation not allowed here
StaticRandomEngineSetUnset random(nullptr);

m_runManagerWorker = std::make_unique<RunManagerMTWorker>(p, consumesCollector());
m_masterThread = ms;

produces<edm::SimTrackContainer>().setBranchAlias("SimTracks");
produces<edm::SimVertexContainer>().setBranchAlias("SimVertices");
Expand Down Expand Up @@ -164,12 +166,18 @@ void OscarMTProducer::globalEndJob(OscarMTMasterThread* masterThread) {
masterThread->stopThread();
}

void OscarMTProducer::beginRun(const edm::Run&, const edm::EventSetup& es) {
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::beginRun";
m_runManagerWorker->initializeG4(m_masterThread->runManagerMasterPtr(), es);
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::beginRun done";
}

void OscarMTProducer::endRun(const edm::Run&, const edm::EventSetup&) {
// Random number generation not allowed here
StaticRandomEngineSetUnset random(nullptr);
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::EndRun";
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::endRun";
m_runManagerWorker->endRun();
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::EndRun done";
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::endRun done";
}

void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
Expand Down
Expand Up @@ -31,11 +31,13 @@ class OscarMTProducer : public edm::stream::EDProducer<edm::GlobalCache<OscarMTM
static void globalEndRun(const edm::Run& iRun, const edm::EventSetup& iSetup, const RunContext* iContext);
static void globalEndJob(OscarMTMasterThread* masterThread);

void beginRun(const edm::Run& r, const edm::EventSetup& c) override;
void endRun(const edm::Run& r, const edm::EventSetup& c) override;
void produce(edm::Event& e, const edm::EventSetup& c) override;

private:
std::unique_ptr<RunManagerMTWorker> m_runManagerWorker;
const OscarMTMasterThread* m_masterThread = nullptr;
};

#endif
2 changes: 1 addition & 1 deletion SimG4Core/Application/plugins/OscarProducer.cc
Expand Up @@ -4,7 +4,7 @@
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "SimG4Core/Application/interface/OscarProducer.h"
#include "OscarProducer.h"
#include "SimG4Core/Notification/interface/G4SimEvent.h"

#include "SimDataFormats/Track/interface/SimTrackContainer.h"
Expand Down
8 changes: 6 additions & 2 deletions SimG4Core/Application/src/RunManagerMTWorker.cc
Expand Up @@ -243,6 +243,8 @@ void RunManagerMTWorker::initializeTLS() {
void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm::EventSetup& es) {
// I guess everything initialized here should be in thread_local storage
initializeTLS();
if (m_tls->threadInitialized)
return;

int thisID = getThreadIndex();
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeThread " << thisID << " is started";
Expand Down Expand Up @@ -359,6 +361,7 @@ void RunManagerMTWorker::initializeG4(RunManagerMT* runManagerMaster, const edm:
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeThread done for the thread " << thisID;

G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
m_tls->threadInitialized = true;
}

void RunManagerMTWorker::initializeUserActions() {
Expand Down Expand Up @@ -467,8 +470,9 @@ std::unique_ptr<G4SimEvent> RunManagerMTWorker::produce(const edm::Event& inpevt
// per-run initialization here by ourselves.

if (!(m_tls && m_tls->threadInitialized)) {
edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::produce(): stream " << inpevt.streamID()
<< " thread " << getThreadIndex() << " initializing";
edm::LogWarning("SimG4CoreApplication")
<< "RunManagerMTWorker::produce(): stream " << inpevt.streamID() << " thread " << getThreadIndex()
<< " initializing in the produce(..) method - there is a problem";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to initializeG4() in produce() should not be an indication of a problem. The framework does not guarantee that all TBB worker threads would call the beginRun(). Therefore it is expected that for some threads the produce() is the first function in OscarMTProducer they call (which is why the code was crafted that way).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@makortel , thanks for explanation. In the next PR I will substitute Warning by Verbatim. One of the reason to move initialisation at beginRun(..) is to take out initialisation from the event loop, so this will not affect CPU measurements. For the case, when streams do not coincide with threads we will still have initialisation at the 1st event.

initializeG4(&runManagerMaster, es);
m_tls->threadInitialized = true;
}
Expand Down