CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RandomNumberGeneratorService.h
Go to the documentation of this file.
1 #ifndef IOMC_RandomEngine_RandomNumberGeneratorService_h
2 #define IOMC_RandomEngine_RandomNumberGeneratorService_h
3 
17 
18 #include <atomic>
19 #include <cstdint>
20 #include <fstream>
21 #include <iosfwd>
22 #include <istream>
23 #include <limits>
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 class RandomEngineState;
30 
31 namespace CLHEP {
32  class HepRandomEngine;
33 }
34 
35 namespace edm {
36  class ActivityRegistry;
38  class ConsumesCollector;
39  class Event;
40  class LuminosityBlock;
41  class LuminosityBlockIndex;
42  class ModuleCallingContext;
43  class ModuleDescription;
44  class ParameterSet;
45  class StreamContext;
46  class StreamID;
47 
48  namespace service {
49 
50  class SystemBounds;
51 
53  public:
56 
59 
62 
64  CLHEP::HepRandomEngine& getEngine(StreamID const& streamID) override;
65 
67  CLHEP::HepRandomEngine& getEngine(LuminosityBlockIndex const& luminosityBlockIndex) override;
68 
69  std::unique_ptr<CLHEP::HepRandomEngine> cloneEngine(LuminosityBlockIndex const&) override;
70 
71  // This returns the seed from the configuration. In the unusual case where an
72  // an engine type takes multiple seeds to initialize a sequence, this function
73  // only returns the first. As a general rule, this function should not be used,
74  // but is available for backward compatibility and debugging. It might be useful
75  // for some types of tests. Using this to seed engines constructed in modules is
76  // not recommended because (unless done very carefully) it will create duplicate
77  // sequences in different threads and/or data races. Also, if engines are created
78  // by modules the replay mechanism will be broken.
79  // Because it is dangerous and could be misused, this function might be deleted
80  // someday if we ever find time to delete all uses of it in CMSSW. There are of
81  // order 10 last time I checked ...
82  std::uint32_t mySeed() const override;
83 
84  static void fillDescriptions(ConfigurationDescriptions& descriptions);
85 
87  void preModuleDestruction(ModuleDescription const& description);
88  void preallocate(SystemBounds const&);
89 
90  void preBeginLumi(LuminosityBlock const& lumi) override;
91  void postEventRead(Event const& event) override;
92  void setLumiCache(LuminosityBlockIndex, std::vector<RandomEngineState> const& iStates) override;
93  void setEventCache(StreamID, std::vector<RandomEngineState> const& iStates) override;
94 
97  void preModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
98  void postModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
99 
100  void preModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
101  void postModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
102 
103  void preModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
104  void postModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
105 
106  void preModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
107  void postModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
108 
109  void preModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
111 
112  void preModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
113  void postModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
114 
116  std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const override;
117  std::vector<RandomEngineState> const& getEventCache(StreamID const&) const override;
118 
119  void consumes(ConsumesCollector&& iC) const override;
120 
122  void print(std::ostream& os) const override;
123 
124  private:
125  typedef std::vector<std::uint32_t> VUint32;
126 
128  public:
129  LabelAndEngine(std::string const& theLabel,
130  VUint32 const& theSeeds,
131  std::shared_ptr<CLHEP::HepRandomEngine> const& theEngine)
132  : label_(theLabel), seeds_(theSeeds), engine_(theEngine) {}
133  std::string const& label() const { return label_; }
134  VUint32 const& seeds() const { return seeds_; }
135  std::shared_ptr<CLHEP::HepRandomEngine const> engine() const { return get_underlying_safe(engine_); }
136  std::shared_ptr<CLHEP::HepRandomEngine>& engine() { return get_underlying_safe(engine_); }
137  void setSeed(std::uint32_t v, unsigned int index) { seeds_.at(index) = v; }
138 
139  private:
143  };
144 
145  // This class exists because it is faster to lookup a module using
146  // the moduleID (an integer) than the label (a string). There is a
147  // one to one association between LabelAndEngine objects and ModuleIDToEngine objects.
149  public:
150  ModuleIDToEngine(LabelAndEngine* theLabelAndEngine, unsigned int theModuleID)
151  : engineState_(), labelAndEngine_(theLabelAndEngine), moduleID_(theModuleID) {}
152 
153  std::vector<unsigned long> const& engineState() const { return engineState_; }
156  unsigned int moduleID() const { return moduleID_; }
157  void setEngineState(std::vector<unsigned long> const& v) { engineState_ = v; }
158  // Used to sort so binary lookup can be used on a container of these.
159  bool operator<(ModuleIDToEngine const& r) const { return moduleID() < r.moduleID(); }
160 
161  private:
162  std::vector<unsigned long> engineState_; // Used only for check in stream transitions
164  unsigned int moduleID_;
165  };
166 
167  void preModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
168  void postModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
169 
170  void readFromLuminosityBlock(LuminosityBlock const& lumi);
171  void readFromEvent(Event const& event);
172 
173  void snapShot(std::vector<LabelAndEngine> const& engines, std::vector<RandomEngineState>& cache);
174  void restoreFromCache(std::vector<RandomEngineState> const& cache, std::vector<LabelAndEngine>& engines);
175 
176  void checkEngineType(std::string const& typeFromConfig,
177  std::string const& typeFromEvent,
178  std::string const& engineLabel) const;
179 
181  StreamID const& streamID,
182  LuminosityBlockIndex const& lumiIndex);
183  void writeStates(std::vector<RandomEngineState> const& v, std::ofstream& outFile);
184  void writeVector(VUint32 const& v, std::ofstream& outFile);
186 
187  void readEventStatesFromTextFile(std::string const& fileName, std::vector<RandomEngineState>& cache);
188  void readLumiStatesFromTextFile(std::string const& fileName, std::vector<RandomEngineState>& cache);
190  std::vector<RandomEngineState>& cache,
191  std::string const& whichStates);
192  bool readEngineState(std::istream& is,
193  std::vector<RandomEngineState>& cache,
194  std::string const& whichStates,
195  bool& saveToCache);
196  void readVector(std::istream& is, unsigned numItems, std::vector<std::uint32_t>& v);
197 
198  void createEnginesInVector(std::vector<LabelAndEngine>& engines,
199  unsigned int seedOffset,
200  unsigned int eventSeedOffset,
201  std::vector<ModuleIDToEngine>& moduleIDVector);
202 
203  void resetEngineSeeds(LabelAndEngine& labelAndEngine,
204  std::string const& engineName,
205  VUint32 const& seeds,
206  std::uint32_t offset1,
207  std::uint32_t offset2);
208 
209  // ---------- member data --------------------------------
210 
211  unsigned int nStreams_;
212 
213  // This exists because we can look things up faster using the moduleID
214  // than using string comparisons with the moduleLabel
215  std::vector<std::vector<ModuleIDToEngine>> streamModuleIDToEngine_; // streamID, sorted by moduleID
216  std::vector<std::vector<ModuleIDToEngine>> lumiModuleIDToEngine_; // luminosityBlockIndex, sortedByModuleID
217 
218  // Holds the engines, plus the seeds and module label also
219  std::vector<std::vector<LabelAndEngine>> streamEngines_; // streamID, sorted by label
220  std::vector<std::vector<LabelAndEngine>> lumiEngines_; // luminosityBlockIndex, sorted by label
221 
222  // These hold the input tags needed to retrieve the states
223  // of the random number engines stored in a previous process.
224  // If the label in the tag is the empty string (the default),
225  // then the service does not try to restore the random numbers.
228 
229  std::vector<std::vector<RandomEngineState>> eventCache_; // streamID, sorted by module label
230  std::vector<std::vector<RandomEngineState>> lumiCache_; // luminosityBlockIndex, sorted by module label
231 
232  // This is used to keep track of the seeds and engine name from
233  // the configuration. The map key is the module label.
234  // The module ID is filled in as modules are constructed.
235  // It is left as max unsigned if the module is never constructed and not in the process
236  class SeedsAndName {
237  public:
238  static constexpr unsigned int kInvalid = std::numeric_limits<unsigned int>::max();
239 
240  SeedsAndName(VUint32 const& theSeeds, std::string const& theEngineName)
241  : seeds_(theSeeds), engineName_(theEngineName), moduleID_(kInvalid) {}
242  VUint32 const& seeds() const { return seeds_; }
243  std::string const& engineName() const { return engineName_; }
244  unsigned int moduleID() const { return moduleID_; }
245  void setModuleID(unsigned int v) { moduleID_ = v; }
246 
247  private:
250  unsigned int moduleID_;
251  };
252  std::map<std::string, SeedsAndName> seedsAndNameMap_;
253 
254  // Keep the name of the file where we want to save the state
255  // of all declared engines at the end of each event. A blank
256  // name means don't bother. Also, keep a record of whether
257  // the save file name has been recorded in the job report.
259  std::atomic<bool> saveFileNameRecorded_;
260  std::vector<edm::propagate_const<std::shared_ptr<std::ofstream>>> outFiles_; // streamID
261 
262  // Keep the name of the file from which we restore the state
263  // of all declared engines at the beginning of a run. A
264  // blank name means there isn't one.
266 
267  // This turns on or off the checks that ensure no random
268  // numbers are generated in a module during stream
269  // beginStream, beginRun, endLuminosityBlock, or endRun.
271 
272  std::uint32_t eventSeedOffset_;
273 
274  bool verbose_;
275 
278  static const std::uint32_t maxSeedRanecu;
279  static const std::uint32_t maxSeedHepJames;
280  static const std::uint32_t maxSeedTRandom3;
281  };
282  } // namespace service
283 } // namespace edm
284 #endif
void writeStates(std::vector< RandomEngineState > const &v, std::ofstream &outFile)
void preModuleStreamEndLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
RandomNumberGeneratorService const & operator=(RandomNumberGeneratorService const &)=delete
void readVector(std::istream &is, unsigned numItems, std::vector< std::uint32_t > &v)
CLHEP::HepRandomEngine & getEngine(StreamID const &streamID) override
Use this engine in event methods.
std::shared_ptr< CLHEP::HepRandomEngine const > engine() const
void preModuleEndStream(StreamContext const &sc, ModuleCallingContext const &mcc)
void preModuleConstruction(ModuleDescription const &description)
void postModuleStreamEndRun(StreamContext const &sc, ModuleCallingContext const &mcc)
void postModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
constexpr std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
void preModuleDestruction(ModuleDescription const &description)
ModuleIDToEngine(LabelAndEngine *theLabelAndEngine, unsigned int theModuleID)
void resetEngineSeeds(LabelAndEngine &labelAndEngine, std::string const &engineName, VUint32 const &seeds, std::uint32_t offset1, std::uint32_t offset2)
void readFromLuminosityBlock(LuminosityBlock const &lumi)
uint16_t size_type
void preModuleBeginStream(StreamContext const &sc, ModuleCallingContext const &mcc)
void postModuleStreamBeginRun(StreamContext const &sc, ModuleCallingContext const &mcc)
void snapShot(std::vector< LabelAndEngine > const &engines, std::vector< RandomEngineState > &cache)
void setLumiCache(LuminosityBlockIndex, std::vector< RandomEngineState > const &iStates) override
void postModuleEndStream(StreamContext const &sc, ModuleCallingContext const &mcc)
std::vector< std::vector< ModuleIDToEngine > > streamModuleIDToEngine_
void saveStatesToFile(std::string const &fileName, StreamID const &streamID, LuminosityBlockIndex const &lumiIndex)
RandomNumberGeneratorService(ParameterSet const &pset, ActivityRegistry &activityRegistry)
void preModuleStreamBeginLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
std::vector< edm::propagate_const< std::shared_ptr< std::ofstream > > > outFiles_
edm::propagate_const< std::shared_ptr< CLHEP::HepRandomEngine > > engine_
void createEnginesInVector(std::vector< LabelAndEngine > &engines, unsigned int seedOffset, unsigned int eventSeedOffset, std::vector< ModuleIDToEngine > &moduleIDVector)
bool readEngineState(std::istream &is, std::vector< RandomEngineState > &cache, std::string const &whichStates, bool &saveToCache)
void preModuleStreamBeginRun(StreamContext const &sc, ModuleCallingContext const &mcc)
void print(std::ostream &os) const override
For debugging.
std::vector< std::vector< LabelAndEngine > > streamEngines_
std::vector< std::vector< RandomEngineState > > eventCache_
void readStatesFromFile(std::string const &fileName, std::vector< RandomEngineState > &cache, std::string const &whichStates)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
list lumi
Definition: dqmdumpme.py:53
void readEventStatesFromTextFile(std::string const &fileName, std::vector< RandomEngineState > &cache)
std::vector< std::vector< RandomEngineState > > lumiCache_
static const std::vector< std::uint32_t >::size_type maxStates
void preModuleStreamEndRun(StreamContext const &sc, ModuleCallingContext const &mcc)
LabelAndEngine(std::string const &theLabel, VUint32 const &theSeeds, std::shared_ptr< CLHEP::HepRandomEngine > const &theEngine)
void checkEngineType(std::string const &typeFromConfig, std::string const &typeFromEvent, std::string const &engineLabel) const
void consumes(ConsumesCollector &&iC) const override
void postModuleStreamEndLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
void setEventCache(StreamID, std::vector< RandomEngineState > const &iStates) override
std::vector< RandomEngineState > const & getLumiCache(LuminosityBlockIndex const &) const override
These two are used by the RandomEngineStateProducer.
void writeVector(VUint32 const &v, std::ofstream &outFile)
static const std::vector< std::uint32_t >::size_type maxSeeds
void readLumiStatesFromTextFile(std::string const &fileName, std::vector< RandomEngineState > &cache)
std::unique_ptr< CLHEP::HepRandomEngine > cloneEngine(LuminosityBlockIndex const &) override
void restoreFromCache(std::vector< RandomEngineState > const &cache, std::vector< LabelAndEngine > &engines)
std::vector< std::vector< LabelAndEngine > > lumiEngines_
void postModuleBeginStream(StreamContext const &sc, ModuleCallingContext const &mcc)
std::map< std::string, SeedsAndName > seedsAndNameMap_
std::vector< std::vector< ModuleIDToEngine > > lumiModuleIDToEngine_
void preModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
SeedsAndName(VUint32 const &theSeeds, std::string const &theEngineName)
void preBeginLumi(LuminosityBlock const &lumi) override
std::vector< RandomEngineState > const & getEventCache(StreamID const &) const override
void postModuleStreamBeginLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
def cache
Definition: utilities.py:3