Skip to content

Commit 0fd9f24

Browse files
committed
Merge pull request cms-sw#2392 from wddgit/threadedRandomNumbersDigitizers
Multithreading fixes -- Migrate Sim Code to New Random Service Interface
2 parents e863ba7 + 6696e5d commit 0fd9f24

File tree

177 files changed

+1692
-2128
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+1692
-2128
lines changed

FWCore/Sources/interface/VectorInputSource.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ VectorInputSource: Abstract interface for vector input sources.
1111
#include <string>
1212
#include <vector>
1313

14+
namespace CLHEP {
15+
class HepRandomEngine;
16+
}
17+
1418
namespace edm {
1519
class EventPrincipal;
1620
struct InputSourceDescription;
@@ -22,11 +26,11 @@ namespace edm {
2226
virtual ~VectorInputSource();
2327

2428
template<typename T>
25-
size_t loopRandom(EventPrincipal& cache, size_t number, T eventOperator);
29+
size_t loopRandom(EventPrincipal& cache, size_t number, T eventOperator, CLHEP::HepRandomEngine*);
2630
template<typename T>
2731
size_t loopSequential(EventPrincipal& cache, size_t number, T eventOperator);
2832
template<typename T>
29-
size_t loopRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id, size_t number, T eventOperator);
33+
size_t loopRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id, size_t number, T eventOperator, CLHEP::HepRandomEngine*);
3034
template<typename T>
3135
size_t loopSequentialWithID(EventPrincipal& cache, LuminosityBlockID const& id, size_t number, T eventOperator);
3236
template<typename T, typename Collection>
@@ -37,8 +41,8 @@ namespace edm {
3741
private:
3842

3943
void clearEventPrincipal(EventPrincipal& cache);
40-
virtual void readOneRandom(EventPrincipal& cache) = 0;
41-
virtual bool readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id) = 0;
44+
virtual void readOneRandom(EventPrincipal& cache, CLHEP::HepRandomEngine*) = 0;
45+
virtual bool readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id, CLHEP::HepRandomEngine*) = 0;
4246
virtual bool readOneSequential(EventPrincipal& cache) = 0;
4347
virtual bool readOneSequentialWithID(EventPrincipal& cache, LuminosityBlockID const& id) = 0;
4448
virtual void readOneSpecified(EventPrincipal& cache, EventID const& event) = 0;
@@ -47,11 +51,11 @@ namespace edm {
4751
};
4852

4953
template<typename T>
50-
size_t VectorInputSource::loopRandom(EventPrincipal& cache, size_t number, T eventOperator) {
54+
size_t VectorInputSource::loopRandom(EventPrincipal& cache, size_t number, T eventOperator, CLHEP::HepRandomEngine* engine) {
5155
size_t i = 0U;
5256
for(; i < number; ++i) {
5357
clearEventPrincipal(cache);
54-
readOneRandom(cache);
58+
readOneRandom(cache, engine);
5559
eventOperator(cache);
5660
}
5761
return i;
@@ -70,11 +74,11 @@ namespace edm {
7074
}
7175

7276
template<typename T>
73-
size_t VectorInputSource::loopRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id, size_t number, T eventOperator) {
77+
size_t VectorInputSource::loopRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id, size_t number, T eventOperator, CLHEP::HepRandomEngine* engine) {
7478
size_t i = 0U;
7579
for(; i < number; ++i) {
7680
clearEventPrincipal(cache);
77-
bool found = readOneRandomWithID(cache, id);
81+
bool found = readOneRandomWithID(cache, id, engine);
7882
if(!found) break;
7983
eventOperator(cache);
8084
}

FastSimulation/Tracking/plugins/RecoTrackAccumulator.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ void RecoTrackAccumulator::accumulate(edm::Event const& e, edm::EventSetup const
5050

5151
}
5252

53-
54-
void RecoTrackAccumulator::accumulate(PileUpEventPrincipal const& e, edm::EventSetup const& iSetup) {
53+
void RecoTrackAccumulator::accumulate(PileUpEventPrincipal const& e, edm::EventSetup const& iSetup, edm::StreamID const&) {
5554

5655
if (e.bunchCrossing()==0) {
5756
edm::Handle<reco::TrackCollection> tracks;

FastSimulation/Tracking/plugins/RecoTrackAccumulator.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace edm {
3030
namespace one {
3131
class EDProducerBase;
3232
}
33+
class StreamID;
3334
}
3435

3536

@@ -41,7 +42,7 @@ class RecoTrackAccumulator : public DigiAccumulatorMixMod
4142

4243
virtual void initializeEvent(edm::Event const& e, edm::EventSetup const& c);
4344
virtual void accumulate(edm::Event const& e, edm::EventSetup const& c);
44-
virtual void accumulate(PileUpEventPrincipal const& e, edm::EventSetup const& c);
45+
virtual void accumulate(PileUpEventPrincipal const& e, edm::EventSetup const& c, edm::StreamID const&) override;
4546
virtual void finalizeEvent(edm::Event& e, edm::EventSetup const& c);
4647

4748
private:

IOPool/Input/src/PoolSource.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,15 @@ namespace edm {
260260
}
261261

262262
void
263-
PoolSource::readOneRandom(EventPrincipal& cache) {
263+
PoolSource::readOneRandom(EventPrincipal& cache, CLHEP::HepRandomEngine* engine) {
264264
assert(!secondaryFileSequence_);
265-
primaryFileSequence_->readOneRandom(cache);
265+
primaryFileSequence_->readOneRandom(cache, engine);
266266
}
267267

268268
bool
269-
PoolSource::readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& lumiID) {
269+
PoolSource::readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& lumiID, CLHEP::HepRandomEngine* engine) {
270270
assert(!secondaryFileSequence_);
271-
return primaryFileSequence_->readOneRandomWithID(cache, lumiID);
271+
return primaryFileSequence_->readOneRandomWithID(cache, lumiID, engine);
272272
}
273273

274274
bool

IOPool/Input/src/PoolSource.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ PoolSource: This is an InputSource
2020
#include <string>
2121
#include <vector>
2222

23+
namespace CLHEP {
24+
class HepRandomEngine;
25+
}
26+
2327
namespace edm {
2428

2529
class ConfigurationDescriptions;
@@ -49,8 +53,8 @@ namespace edm {
4953
virtual void skip(int offset);
5054
virtual bool goToEvent_(EventID const& eventID);
5155
virtual void rewind_();
52-
virtual void readOneRandom(EventPrincipal& cache);
53-
virtual bool readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& lumiID);
56+
virtual void readOneRandom(EventPrincipal& cache, CLHEP::HepRandomEngine*) override;
57+
virtual bool readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& lumiID, CLHEP::HepRandomEngine*) override;
5458
virtual bool readOneSequential(EventPrincipal& cache);
5559
virtual bool readOneSequentialWithID(EventPrincipal& cache, LuminosityBlockID const& lumiID);
5660
virtual void readOneSpecified(EventPrincipal& cache, EventID const& id);

IOPool/Input/src/RootInputFileSequence.cc

+7-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "FWCore/ParameterSet/interface/ParameterSet.h"
1818
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
1919
#include "FWCore/ServiceRegistry/interface/Service.h"
20-
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
2120
#include "Utilities/StorageFactory/interface/StorageFactory.h"
2221

2322
#include "CLHEP/Random/RandFlat.h"
@@ -42,7 +41,6 @@ namespace edm {
4241
fileIterLastOpened_(fileIterEnd_),
4342
rootFile_(),
4443
branchesMustMatch_(BranchDescription::Permissive),
45-
flatDistribution_(),
4644
indexesIntoFiles_(fileCatalogItems().size()),
4745
orderedProcessHistoryIDs_(),
4846
nStreams_(nStreams),
@@ -701,20 +699,16 @@ namespace edm {
701699
}
702700

703701
void
704-
RootInputFileSequence::readOneRandom(EventPrincipal& cache) {
702+
RootInputFileSequence::readOneRandom(EventPrincipal& cache, CLHEP::HepRandomEngine* engine) {
705703
if(fileIterEnd_ == fileIterBegin_) {
706704
throw Exception(errors::Configuration) << "RootInputFileSequence::readOneRandom(): no input files specified.\n";
707705
}
708-
if(!flatDistribution_) {
709-
Service<RandomNumberGenerator> rng;
710-
CLHEP::HepRandomEngine& engine = rng->getEngine();
711-
flatDistribution_.reset(new CLHEP::RandFlat(engine));
712-
}
713706
assert(rootFile_);
714707
skipBadFiles_ = false;
715708
unsigned int currentSeqNumber = fileIter_ - fileIterBegin_;
716709
while(eventsRemainingInFile_ == 0) {
717-
fileIter_ = fileIterBegin_ + flatDistribution_->fireInt(fileCatalogItems().size());
710+
711+
fileIter_ = fileIterBegin_ + CLHEP::RandFlat::shootInt(engine, fileCatalogItems().size());
718712
unsigned int newSeqNumber = fileIter_ - fileIterBegin_;
719713
if(newSeqNumber != currentSeqNumber) {
720714
initFile(false);
@@ -725,7 +719,7 @@ namespace edm {
725719
throw Exception(errors::NotFound) <<
726720
"RootInputFileSequence::readOneRandom(): Secondary Input file " << fileIter_->fileName() << " contains no events.\n";
727721
}
728-
rootFile_->setAtEventEntry(flatDistribution_->fireInt(eventsRemainingInFile_) - 1);
722+
rootFile_->setAtEventEntry(CLHEP::RandFlat::shootInt(engine, eventsRemainingInFile_) - 1);
729723
}
730724
rootFile_->nextEventEntry();
731725

@@ -741,15 +735,10 @@ namespace edm {
741735
// bool RootFile::setEntryAtNextEventInLumi(RunNumber_t run, LuminosityBlockNumber_t lumi) {
742736

743737
bool
744-
RootInputFileSequence::readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id) {
738+
RootInputFileSequence::readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id, CLHEP::HepRandomEngine* engine) {
745739
if(fileIterEnd_ == fileIterBegin_) {
746740
throw Exception(errors::Configuration) << "RootInputFileSequence::readOneRandomWithID(): no input files specified.\n";
747741
}
748-
if(!flatDistribution_) {
749-
Service<RandomNumberGenerator> rng;
750-
CLHEP::HepRandomEngine& engine = rng->getEngine();
751-
flatDistribution_.reset(new CLHEP::RandFlat(engine));
752-
}
753742
skipBadFiles_ = false;
754743
if(fileIter_ == fileIterEnd_ || !rootFile_ ||
755744
rootFile_->indexIntoFileIter().run() != id.run() ||
@@ -763,7 +752,7 @@ namespace edm {
763752
while(rootFile_->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock())) ++eventsInLumi;
764753
found = skipToItem(id.run(), id.luminosityBlock(), 0);
765754
assert(found);
766-
int eventInLumi = flatDistribution_->fireInt(eventsInLumi);
755+
int eventInLumi = CLHEP::RandFlat::shootInt(engine, eventsInLumi);
767756
for(int i = 0; i < eventInLumi; ++i) {
768757
bool found = rootFile_->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
769758
assert(found);
@@ -779,7 +768,7 @@ namespace edm {
779768
if(!found) {
780769
return false;
781770
}
782-
return readOneRandomWithID(cache, id);
771+
return readOneRandomWithID(cache, id, engine);
783772
}
784773
return true;
785774
}

IOPool/Input/src/RootInputFileSequence.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ RootInputFileSequence: This is an InputSource
2222
#include <vector>
2323

2424
namespace CLHEP {
25-
class RandFlat;
25+
class HepRandomEngine;
2626
}
2727

2828
namespace edm {
@@ -62,8 +62,8 @@ namespace edm {
6262
bool skipToItem(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event, bool currentFileFirst = true);
6363
bool skipToItemInNewFile(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event);
6464
void rewind_();
65-
void readOneRandom(EventPrincipal& cache);
66-
bool readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id);
65+
void readOneRandom(EventPrincipal& cache, CLHEP::HepRandomEngine*);
66+
bool readOneRandomWithID(EventPrincipal& cache, LuminosityBlockID const& id, CLHEP::HepRandomEngine*);
6767
bool readOneSequential(EventPrincipal& cache);
6868
bool readOneSequentialWithID(EventPrincipal& cache, LuminosityBlockID const& id);
6969
void readOneSpecified(EventPrincipal& cache, EventID const& id);
@@ -101,7 +101,6 @@ namespace edm {
101101
RootFileSharedPtr rootFile_;
102102
BranchDescription::MatchMode branchesMustMatch_;
103103

104-
std::unique_ptr<CLHEP::RandFlat> flatDistribution_;
105104
std::vector<boost::shared_ptr<IndexIntoFile> > indexesIntoFiles_;
106105
std::vector<ProcessHistoryID> orderedProcessHistoryIDs_;
107106

IOPool/SecondaryInput/test/SecondaryProducer.cc

+23-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@
3131
#include "FWCore/Utilities/interface/TypeID.h"
3232
#include "FWCore/Version/interface/GetReleaseVersion.h"
3333

34+
#include "FWCore/ServiceRegistry/interface/Service.h"
35+
#include "FWCore/Utilities/interface/RandomNumberGenerator.h"
36+
#include "FWCore/Utilities/interface/Exception.h"
37+
3438
#include "boost/bind.hpp"
3539

3640
#include <memory>
3741
#include <string>
3842

43+
namespace CLHEP {
44+
class HepRandomEngine;
45+
}
46+
3947
namespace edm {
4048

4149
// Constructor
@@ -74,6 +82,7 @@ namespace edm {
7482

7583
// Functions that get called by framework every event
7684
void SecondaryProducer::produce(Event& e, EventSetup const&) {
85+
7786
if(sequential_) {
7887
if(lumiSpecified_) {
7988
// Just for simplicity, we use the luminosity block ID from the primary to read the secondary.
@@ -86,11 +95,23 @@ namespace edm {
8695
std::vector<EventID> events(1, e.id());
8796
secInput_->loopSpecified(*eventPrincipal_, events, boost::bind(&SecondaryProducer::processOneEvent, this, _1, boost::ref(e)));
8897
} else {
98+
99+
edm::Service<edm::RandomNumberGenerator> rng;
100+
if ( ! rng.isAvailable()) {
101+
throw cms::Exception("Configuration")
102+
<< "SecondaryProducer in its random mode requires the RandomNumberGeneratorService\n"
103+
"which is not present in the configuration file. You must add the service\n"
104+
"in the configuration file or remove the modules that require it.";
105+
}
106+
CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
107+
89108
if(lumiSpecified_) {
90109
// Just for simplicity, we use the luminosity block ID from the primary to read the secondary.
91-
secInput_->loopRandomWithID(*eventPrincipal_, LuminosityBlockID(e.id().run(), e.id().luminosityBlock()), 1, boost::bind(&SecondaryProducer::processOneEvent, this, _1, boost::ref(e)));
110+
secInput_->loopRandomWithID(*eventPrincipal_, LuminosityBlockID(e.id().run(), e.id().luminosityBlock()), 1,
111+
boost::bind(&SecondaryProducer::processOneEvent, this, _1, boost::ref(e)),
112+
engine);
92113
} else {
93-
secInput_->loopRandom(*eventPrincipal_, 1, boost::bind(&SecondaryProducer::processOneEvent, this, _1, boost::ref(e)));
114+
secInput_->loopRandom(*eventPrincipal_, 1, boost::bind(&SecondaryProducer::processOneEvent, this, _1, boost::ref(e)), engine);
94115
}
95116
}
96117
}

Mixing/Base/interface/PileUp.h

+16-13
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#include "FWCore/Framework/interface/EventPrincipal.h"
1212
#include "FWCore/MessageLogger/interface/MessageLogger.h"
1313

14-
#include "CLHEP/Random/RandPoissonQ.h"
15-
#include "CLHEP/Random/RandFlat.h"
16-
1714
#include "boost/shared_ptr.hpp"
1815

1916
#include "TRandom.h"
@@ -26,20 +23,20 @@ class TH1F;
2623
namespace CLHEP {
2724
class RandPoissonQ;
2825
class RandPoisson;
26+
class HepRandomEngine;
2927
}
3028

31-
32-
3329
namespace edm {
3430
class SecondaryEventProvider;
31+
class StreamID;
3532

3633
class PileUp {
3734
public:
3835
explicit PileUp(ParameterSet const& pset, double averageNumber, TH1F* const histo, const bool playback);
3936
~PileUp();
4037

4138
template<typename T>
42-
void readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator, const int NumPU );
39+
void readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator, const int NumPU, StreamID const&);
4340

4441
template<typename T>
4542
void playPileUp(const std::vector<edm::EventID> &ids, T eventOperator);
@@ -63,7 +60,7 @@ namespace edm {
6360

6461
void reload(const edm::EventSetup & setup);
6562

66-
void CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions);
63+
void CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions, StreamID const&);
6764

6865
//template<typename T>
6966
// void recordEventForPlayback(EventPrincipal const& eventPrincipal,
@@ -73,6 +70,11 @@ namespace edm {
7370
void input(unsigned int s){inputType_=s;}
7471

7572
private:
73+
74+
std::unique_ptr<CLHEP::RandPoissonQ> const& poissonDistribution(StreamID const& streamID);
75+
std::unique_ptr<CLHEP::RandPoisson> const& poissonDistr_OOT(StreamID const& streamID);
76+
CLHEP::HepRandomEngine* randomEngine(StreamID const& streamID);
77+
7678
unsigned int inputType_;
7779
std::string type_;
7880
double averageNumber_;
@@ -100,9 +102,9 @@ namespace edm {
100102
boost::shared_ptr<LuminosityBlockPrincipal> lumiPrincipal_;
101103
boost::shared_ptr<RunPrincipal> runPrincipal_;
102104
std::unique_ptr<SecondaryEventProvider> provider_;
103-
std::unique_ptr<CLHEP::RandPoissonQ> poissonDistribution_;
104-
std::unique_ptr<CLHEP::RandPoisson> poissonDistr_OOT_;
105-
105+
std::vector<std::unique_ptr<CLHEP::RandPoissonQ> > vPoissonDistribution_;
106+
std::vector<std::unique_ptr<CLHEP::RandPoisson> > vPoissonDistr_OOT_;
107+
std::vector<CLHEP::HepRandomEngine*> randomEngines_;
106108

107109
TH1F *h1f;
108110
TH1F *hprobFunction;
@@ -152,7 +154,8 @@ namespace edm {
152154
*/
153155
template<typename T>
154156
void
155-
PileUp::readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator, const int pileEventCnt) {
157+
PileUp::readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator,
158+
const int pileEventCnt, StreamID const& streamID) {
156159

157160
// One reason PileUp is responsible for recording event IDs is
158161
// that it is the one that knows how many events will be read.
@@ -164,7 +167,7 @@ namespace edm {
164167
if (sequential_)
165168
read = input_->loopSequentialWithID(*eventPrincipal_, lumi, pileEventCnt, recorder);
166169
else
167-
read = input_->loopRandomWithID(*eventPrincipal_, lumi, pileEventCnt, recorder);
170+
read = input_->loopRandomWithID(*eventPrincipal_, lumi, pileEventCnt, recorder, randomEngine(streamID));
168171
} else {
169172
if (sequential_) {
170173
// boost::bind creates a functor from recordEventForPlayback
@@ -178,7 +181,7 @@ namespace edm {
178181
// );
179182

180183
} else {
181-
read = input_->loopRandom(*eventPrincipal_, pileEventCnt, recorder);
184+
read = input_->loopRandom(*eventPrincipal_, pileEventCnt, recorder, randomEngine(streamID));
182185
// boost::bind(&PileUp::recordEventForPlayback<T>,
183186
// boost::ref(*this), _1, boost::ref(ids),
184187
// boost::ref(eventOperator))

0 commit comments

Comments
 (0)