Skip to content

Commit

Permalink
Merge pull request #2282 from wddgit/generatorRandomInterface3
Browse files Browse the repository at this point in the history
Multithreading fixes -- Migrate Generators to New Random Service Interface
  • Loading branch information
ktf committed Feb 4, 2014
2 parents e858790 + cd28f66 commit fe34280
Show file tree
Hide file tree
Showing 100 changed files with 716 additions and 538 deletions.
1 change: 1 addition & 0 deletions FWCore/Concurrency/interface/SharedResourceNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace edm {
static const std::string kPhotos;
static const std::string kTauola;
static const std::string kEvtGen;
static const std::string kHerwig6;
};

// Each time the following function is called, it returns a different
Expand Down
1 change: 1 addition & 0 deletions FWCore/Concurrency/src/SharedResourceNames.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const std::string edm::SharedResourceNames::kPythia8 = "Pythia8";
const std::string edm::SharedResourceNames::kPhotos = "Photos";
const std::string edm::SharedResourceNames::kTauola = "Tauola";
const std::string edm::SharedResourceNames::kEvtGen = "EvtGen";
const std::string edm::SharedResourceNames::kHerwig6 = "Herwig6";

static std::atomic<unsigned int> counter;

Expand Down
17 changes: 10 additions & 7 deletions FWCore/ServiceRegistry/interface/RandomEngineSentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,45 @@ namespace edm {
template <class T> class RandomEngineSentry {
public:

explicit RandomEngineSentry(T* t, CLHEP::HepRandomEngine* engine): t_(t) {
explicit RandomEngineSentry(T* t, CLHEP::HepRandomEngine* engine): t_(t), engine_(engine) {
if(t) {
t->setRandomEngine(engine);
}
}

explicit RandomEngineSentry(T* t, StreamID const& streamID): t_(t) {
explicit RandomEngineSentry(T* t, StreamID const& streamID): t_(t), engine_(nullptr) {
if(t) {
Service<RandomNumberGenerator> rng;
if (!rng.isAvailable()) {
throw cms::Exception("Configuration")
<< "Attempt to get a random engine when the RandomNumberGeneratorService is not configured.\n"
"You must configure the service if you want an engine.\n";
}
CLHEP::HepRandomEngine& engine = rng->getEngine(streamID);
t->setRandomEngine(&engine);
engine_ = &rng->getEngine(streamID);
t->setRandomEngine(engine_);
}
}

explicit RandomEngineSentry(T* t, LuminosityBlockIndex const& lumi): t_(t) {
explicit RandomEngineSentry(T* t, LuminosityBlockIndex const& lumi): t_(t), engine_(nullptr) {
if(t) {
Service<RandomNumberGenerator> rng;
if (!rng.isAvailable()) {
throw cms::Exception("Configuration")
<< "Attempt to get a random engine when the RandomNumberGeneratorService is not configured.\n"
"You must configure the service if you want an engine.\n";
}
CLHEP::HepRandomEngine& engine = rng->getEngine(lumi);
t->setRandomEngine(&engine);
engine_ = &rng->getEngine(lumi);
t->setRandomEngine(engine_);
}
}

~RandomEngineSentry() { if(t_) t_->setRandomEngine(nullptr); }

CLHEP::HepRandomEngine* randomEngine() const { return engine_; }

private:
T* t_;
CLHEP::HepRandomEngine* engine_;
};
}
#endif
7 changes: 5 additions & 2 deletions GeneratorInterface/AMPTInterface/interface/AMPTHadronizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "GeneratorInterface/Core/interface/BaseHadronizer.h"
#include "CLHEP/Random/RandomEngine.h"

#include <map>
#include <string>
Expand All @@ -16,7 +15,9 @@ namespace HepMC {
class GenVertex;
}

extern CLHEP::HepRandomEngine* _amptRandomEngine;
namespace CLHEP {
class HepRandomEngine;
}

namespace gen
{
Expand Down Expand Up @@ -50,6 +51,8 @@ namespace gen

private:

virtual void doSetRandomEngine(CLHEP::HepRandomEngine* v) override;

void add_heavy_ion_rec(HepMC::GenEvent *evt);
HepMC::GenParticle* build_ampt( int index, int barcode );
HepMC::GenVertex* build_ampt_vertex(int i, int id);
Expand Down
20 changes: 9 additions & 11 deletions GeneratorInterface/AMPTInterface/src/AMPTHadronizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "GeneratorInterface/Core/interface/RNDMEngineAccess.h"

#include "GeneratorInterface/AMPTInterface/interface/AMPTHadronizer.h"
#include "GeneratorInterface/AMPTInterface/interface/AMPTWrapper.h"
Expand All @@ -30,14 +26,14 @@ using namespace edm;
using namespace std;
using namespace gen;

CLHEP::HepRandomEngine* _amptRandomEngine;
static CLHEP::HepRandomEngine* amptRandomEngine;

extern "C"
{
float gen::ranart_(int *idummy)
{
if(0) idummy = idummy;
float rannum = _amptRandomEngine->flat();
float rannum = amptRandomEngine->flat();
return rannum;
}
}
Expand All @@ -47,7 +43,7 @@ extern "C"
float gen::ran1_(int *idummy)
{
if(0) idummy = idummy;
return _amptRandomEngine->flat();
return amptRandomEngine->flat();
}
}

Expand Down Expand Up @@ -100,17 +96,19 @@ AMPTHadronizer::AMPTHadronizer(const ParameterSet &pset) :
cosphi0_(1.),
rotate_(pset.getParameter<bool>("rotateEventPlane"))
{
// Default constructor
edm::Service<RandomNumberGenerator> rng;
_amptRandomEngine = &(rng->getEngine());
}


//_____________________________________________________________________
AMPTHadronizer::~AMPTHadronizer()
{
}

//_____________________________________________________________________
void AMPTHadronizer::doSetRandomEngine(CLHEP::HepRandomEngine* v)
{
amptRandomEngine = v;
}

//_____________________________________________________________________
void AMPTHadronizer::add_heavy_ion_rec(HepMC::GenEvent *evt)
{
Expand Down
17 changes: 14 additions & 3 deletions GeneratorInterface/BeamHaloGenerator/interface/BeamHaloProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@

#include "FWCore/Framework/interface/one/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

namespace CLHEP {
class HepRandomEngine;
}

namespace edm
{
class BeamHaloProducer : public one::EDProducer<EndRunProducer> {
class BeamHaloProducer : public one::EDProducer<EndRunProducer, one::WatchLuminosityBlocks> {
public:

/// Constructor
BeamHaloProducer(const ParameterSet &);
/// Destructor
virtual ~BeamHaloProducer();

void setRandomEngine(CLHEP::HepRandomEngine* v);

private:
bool call_ki_bhg_init(long& seed);
bool call_bh_set_parameters(int* ival, float* fval,const std::string cval_string);
Expand All @@ -31,8 +38,10 @@ namespace edm

private:

virtual void produce(Event & e, const EventSetup & es);
virtual void endRunProduce(Run & r, const EventSetup & es);
virtual void produce(Event & e, const EventSetup & es) override;
virtual void endRunProduce(Run & r, const EventSetup & es) override;
virtual void beginLuminosityBlock(LuminosityBlock const&, EventSetup const&) override;
virtual void endLuminosityBlock(LuminosityBlock const&, EventSetup const&) override { }

void clear();

Expand All @@ -46,6 +55,8 @@ namespace edm
float EG_MIN_;
float EG_MAX_;
std::string G3FNAME_;

bool isInitialized_;
};

}
Expand Down
36 changes: 22 additions & 14 deletions GeneratorInterface/BeamHaloGenerator/src/BeamHaloProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/ServiceRegistry/interface/RandomEngineSentry.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"

#include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
Expand Down Expand Up @@ -58,7 +57,8 @@ BeamHaloProducer::~BeamHaloProducer() {


BeamHaloProducer::BeamHaloProducer( const ParameterSet & pset) :
evt(0)
evt(0),
isInitialized_(false)
{

int iparam[8];
Expand All @@ -83,17 +83,6 @@ BeamHaloProducer::BeamHaloProducer( const ParameterSet & pset) :
cparam = pset.getUntrackedParameter<std::string>("G3FNAME","input.txt");
call_bh_set_parameters(iparam,fparam,cparam);


// -- Seed for randomnumbers
Service<RandomNumberGenerator> rng;
_BeamHalo_randomEngine = &(rng->getEngine());
long seed = (long)(rng->mySeed());


// -- initialisation
call_ki_bhg_init(seed);


produces<HepMCProduct>();
produces<GenEventInfoProduct>();
produces<GenRunInfoProduct, InRun>();
Expand All @@ -106,7 +95,26 @@ void BeamHaloProducer::clear()
{
}

void BeamHaloProducer::setRandomEngine(CLHEP::HepRandomEngine* v) {
_BeamHalo_randomEngine = v;
}

void BeamHaloProducer::beginLuminosityBlock(LuminosityBlock const& lumi, EventSetup const&)
{
if(!isInitialized_) {
isInitialized_ = true;
RandomEngineSentry<BeamHaloProducer> randomEngineSentry(this, lumi.index());

// -- initialisation
long seed = 1; // This seed is not actually used
call_ki_bhg_init(seed);
}
}

void BeamHaloProducer::produce(Event & e, const EventSetup & es) {

RandomEngineSentry<BeamHaloProducer> randomEngineSentry(this, e.streamID());

// cout << "in produce " << endl;

// auto_ptr<HepMCProduct> bare_product(new HepMCProduct());
Expand Down
1 change: 1 addition & 0 deletions GeneratorInterface/CascadeInterface/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<use name="boost"/>
<use name="FWCore/Concurrency"/>
<use name="FWCore/Framework"/>
<use name="SimDataFormats/GeneratorProducts"/>
<use name="GeneratorInterface/Core"/>
Expand Down
39 changes: 16 additions & 23 deletions GeneratorInterface/CascadeInterface/plugins/Cascade2Hadronizer.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "GeneratorInterface/CascadeInterface/plugins/Cascade2Hadronizer.h"

#include "GeneratorInterface/Core/interface/RNDMEngineAccess.h"


#include "HepMC/GenEvent.h"
#include "HepMC/PdfInfo.h"
#include "HepMC/PythiaWrapper6_4.h"
Expand All @@ -12,17 +9,16 @@
#include "HepMC/IO_HEPEVT.h"
#include "HepMC/IO_GenEvent.h"

#include "FWCore/Concurrency/interface/SharedResourceNames.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "GeneratorInterface/Core/interface/FortranCallback.h"
#include "GeneratorInterface/Core/interface/FortranInstance.h"

HepMC::IO_HEPEVT hepevtio;

#include "HepPID/ParticleIDTranslations.hh"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
#include "CLHEP/Random/RandFlat.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/EDMException.h"

//-- Pythia6 routines and functionalities to pass around Pythia6 params

Expand All @@ -31,20 +27,22 @@ HepMC::IO_HEPEVT hepevtio;

#include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"

#include "CLHEP/Random/RandomEngine.h"

using namespace edm;
using namespace std;

#define debug 0

CLHEP::RandFlat* fFlat_extern;
static CLHEP::HepRandomEngine* cascade2RandomEngine;

extern "C" {

double dcasrn_(int *idummy) {

static int call = 0;

double rdm_nb = fFlat_extern->fire();
double rdm_nb = cascade2RandomEngine->flat();

if(debug && ++call < 100) cout<<"dcasrn from c++, call: "<<call<<" random number: "<<rdm_nb<<endl;

Expand All @@ -54,7 +52,7 @@ extern "C" {


namespace gen {

class Pythia6ServiceWithCallback : public Pythia6Service {

public:
Expand Down Expand Up @@ -82,16 +80,12 @@ namespace gen {
double p[5][pyjets_maxn], v[5][pyjets_maxn];
} pyjets_local;

const std::vector<std::string> Cascade2Hadronizer::theSharedResources = { edm::SharedResourceNames::kPythia6,
gen::FortranInstance::kFortranInstance };

Cascade2Hadronizer::Cascade2Hadronizer(edm::ParameterSet const& pset)
: BaseHadronizer(pset),
fPy6Service(new Pythia6ServiceWithCallback(pset)), //-- this will store py6 parameters for further settings

//-- fRandomEngine(&getEngineReference()),

//-- defined in GeneratorInterface/Core/src/RNDMEngineAccess.cc
//-- CLHEP::HepRandomEngine& gen::getEngineReference()
//-- { edm::Service<edm::RandomNumberGenerator> rng;
//-- return rng->getEngine(); }

fComEnergy(pset.getParameter<double>("comEnergy")),
fextCrossSection(pset.getUntrackedParameter<double>("crossSection",-1.)),
Expand All @@ -107,12 +101,6 @@ namespace gen {

fParameters = pset.getParameter<ParameterSet>("Cascade2Parameters");

edm::Service<edm::RandomNumberGenerator> rng;
if(debug) cout<<"seed: "<<rng->mySeed()<<endl;
CLHEP::HepRandomEngine& engine = rng->getEngine();
fFlat = new CLHEP::RandFlat(engine);
fFlat_extern = fFlat;

fConvertToPDG = false;
if(pset.exists("doPDGConvert"))
fConvertToPDG = pset.getParameter<bool>("doPDGConvert");
Expand Down Expand Up @@ -143,6 +131,11 @@ namespace gen {
if(fPy6Service != 0) delete fPy6Service;
}

void Cascade2Hadronizer::doSetRandomEngine(CLHEP::HepRandomEngine* v) {
cascade2RandomEngine = v;
fPy6Service->setRandomEngine(v);
}

void Cascade2Hadronizer::flushTmpStorage(){

pyjets_local.n = 0 ;
Expand Down
Loading

0 comments on commit fe34280

Please sign in to comment.