Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 23:30:13

0001 #ifndef IOMC_RandomEngine_RandomNumberGeneratorService_h
0002 #define IOMC_RandomEngine_RandomNumberGeneratorService_h
0003 
0004 /** \class edm::service::RandomNumberGeneratorService
0005 
0006  Description: Manages random number engines for modules
0007 
0008  Usage: See comments in base class, FWCore/Utilities/RandomNumberGenerator.h
0009 
0010 \author Chris Jones and W. David Dagenhart, created March 7, 2006
0011   (originally in FWCore/Services)
0012 */
0013 
0014 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
0015 #include "FWCore/Utilities/interface/InputTag.h"
0016 #include "FWCore/Utilities/interface/get_underlying_safe.h"
0017 
0018 #include <atomic>
0019 #include <cstdint>
0020 #include <fstream>
0021 #include <iosfwd>
0022 #include <istream>
0023 #include <limits>
0024 #include <map>
0025 #include <memory>
0026 #include <string>
0027 #include <vector>
0028 
0029 class RandomEngineState;
0030 
0031 namespace CLHEP {
0032   class HepRandomEngine;
0033 }
0034 
0035 namespace edm {
0036   class ActivityRegistry;
0037   class ConfigurationDescriptions;
0038   class ConsumesCollector;
0039   class Event;
0040   class LuminosityBlock;
0041   class LuminosityBlockIndex;
0042   class ModuleCallingContext;
0043   class ModuleDescription;
0044   class ParameterSet;
0045   class StreamContext;
0046   class StreamID;
0047 
0048   namespace service {
0049 
0050     class SystemBounds;
0051 
0052     class RandomNumberGeneratorService : public RandomNumberGenerator {
0053     public:
0054       RandomNumberGeneratorService(ParameterSet const& pset, ActivityRegistry& activityRegistry);
0055       ~RandomNumberGeneratorService() override;
0056 
0057       RandomNumberGeneratorService(RandomNumberGeneratorService const&) = delete;
0058       RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&) = delete;
0059 
0060       /// Use the next 2 functions to get the random number engine.
0061       /// These are the only functions most modules should call.
0062 
0063       /// Use this engine in event methods
0064       CLHEP::HepRandomEngine& getEngine(StreamID const& streamID) override;
0065 
0066       /// Use this engine in the global begin luminosity block method
0067       CLHEP::HepRandomEngine& getEngine(LuminosityBlockIndex const& luminosityBlockIndex) override;
0068 
0069       std::unique_ptr<CLHEP::HepRandomEngine> cloneEngine(LuminosityBlockIndex const&) override;
0070 
0071       // This returns the seed from the configuration. In the unusual case where an
0072       // an engine type takes multiple seeds to initialize a sequence, this function
0073       // only returns the first. As a general rule, this function should not be used,
0074       // but is available for backward compatibility and debugging. It might be useful
0075       // for some types of tests. Using this to seed engines constructed in modules is
0076       // not recommended because (unless done very carefully) it will create duplicate
0077       // sequences in different threads and/or data races. Also, if engines are created
0078       // by modules the replay mechanism will be broken.
0079       // Because it is dangerous and could be misused, this function might be deleted
0080       // someday if we ever find time to delete all uses of it in CMSSW. There are of
0081       // order 10 last time I checked ...
0082       std::uint32_t mySeed() const override;
0083 
0084       static void fillDescriptions(ConfigurationDescriptions& descriptions);
0085 
0086       void preModuleConstruction(ModuleDescription const& description);
0087       void preModuleDestruction(ModuleDescription const& description);
0088       void preallocate(SystemBounds const&);
0089 
0090       void preBeginLumi(LuminosityBlock const& lumi) override;
0091       void postEventRead(Event const& event) override;
0092       void setLumiCache(LuminosityBlockIndex, std::vector<RandomEngineState> const& iStates) override;
0093       void setEventCache(StreamID, std::vector<RandomEngineState> const& iStates) override;
0094 
0095       /// These next 12 functions are only used to check that random numbers are not
0096       /// being generated in these methods when enable checking is configured on.
0097       void preModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
0098       void postModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
0099 
0100       void preModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
0101       void postModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
0102 
0103       void preModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
0104       void postModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
0105 
0106       void preModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
0107       void postModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
0108 
0109       void preModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
0110       void postModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
0111 
0112       void preModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
0113       void postModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
0114 
0115       /// These two are used by the RandomEngineStateProducer
0116       std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const override;
0117       std::vector<RandomEngineState> const& getEventCache(StreamID const&) const override;
0118 
0119       void consumes(ConsumesCollector&& iC) const override;
0120 
0121       /// For debugging
0122       void print(std::ostream& os) const override;
0123 
0124     private:
0125       typedef std::vector<std::uint32_t> VUint32;
0126 
0127       class LabelAndEngine {
0128       public:
0129         LabelAndEngine(std::string const& theLabel,
0130                        VUint32 const& theSeeds,
0131                        std::shared_ptr<CLHEP::HepRandomEngine> const& theEngine)
0132             : label_(theLabel), seeds_(theSeeds), engine_(theEngine) {}
0133         std::string const& label() const { return label_; }
0134         VUint32 const& seeds() const { return seeds_; }
0135         std::shared_ptr<CLHEP::HepRandomEngine const> engine() const { return get_underlying_safe(engine_); }
0136         std::shared_ptr<CLHEP::HepRandomEngine>& engine() { return get_underlying_safe(engine_); }
0137         void setSeed(std::uint32_t v, unsigned int index) { seeds_.at(index) = v; }
0138 
0139       private:
0140         std::string label_;
0141         VUint32 seeds_;
0142         edm::propagate_const<std::shared_ptr<CLHEP::HepRandomEngine>> engine_;
0143       };
0144 
0145       // This class exists because it is faster to lookup a module using
0146       // the moduleID (an integer) than the label (a string). There is a
0147       // one to one association between LabelAndEngine objects and ModuleIDToEngine objects.
0148       class ModuleIDToEngine {
0149       public:
0150         ModuleIDToEngine(LabelAndEngine* theLabelAndEngine, unsigned int theModuleID)
0151             : engineState_(), labelAndEngine_(theLabelAndEngine), moduleID_(theModuleID) {}
0152 
0153         std::vector<unsigned long> const& engineState() const { return engineState_; }
0154         LabelAndEngine const* labelAndEngine() const { return get_underlying_safe(labelAndEngine_); }
0155         LabelAndEngine*& labelAndEngine() { return get_underlying_safe(labelAndEngine_); }
0156         unsigned int moduleID() const { return moduleID_; }
0157         void setEngineState(std::vector<unsigned long> const& v) { engineState_ = v; }
0158         // Used to sort so binary lookup can be used on a container of these.
0159         bool operator<(ModuleIDToEngine const& r) const { return moduleID() < r.moduleID(); }
0160 
0161       private:
0162         std::vector<unsigned long> engineState_;  // Used only for check in stream transitions
0163         edm::propagate_const<LabelAndEngine*> labelAndEngine_;
0164         unsigned int moduleID_;
0165       };
0166 
0167       void preModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
0168       void postModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
0169 
0170       void readFromLuminosityBlock(LuminosityBlock const& lumi);
0171       void readFromEvent(Event const& event);
0172 
0173       void snapShot(std::vector<LabelAndEngine> const& engines, std::vector<RandomEngineState>& cache);
0174       void restoreFromCache(std::vector<RandomEngineState> const& cache, std::vector<LabelAndEngine>& engines);
0175 
0176       void checkEngineType(std::string const& typeFromConfig,
0177                            std::string const& typeFromEvent,
0178                            std::string const& engineLabel) const;
0179 
0180       void saveStatesToFile(std::string const& fileName,
0181                             StreamID const& streamID,
0182                             LuminosityBlockIndex const& lumiIndex);
0183       void writeStates(std::vector<RandomEngineState> const& v, std::ofstream& outFile);
0184       void writeVector(VUint32 const& v, std::ofstream& outFile);
0185       std::string constructSaveFileName() const;
0186 
0187       void readEventStatesFromTextFile(std::string const& fileName, std::vector<RandomEngineState>& cache);
0188       void readLumiStatesFromTextFile(std::string const& fileName, std::vector<RandomEngineState>& cache);
0189       void readStatesFromFile(std::string const& fileName,
0190                               std::vector<RandomEngineState>& cache,
0191                               std::string const& whichStates);
0192       bool readEngineState(std::istream& is,
0193                            std::vector<RandomEngineState>& cache,
0194                            std::string const& whichStates,
0195                            bool& saveToCache);
0196       void readVector(std::istream& is, unsigned numItems, std::vector<std::uint32_t>& v);
0197 
0198       void createEnginesInVector(std::vector<LabelAndEngine>& engines,
0199                                  unsigned int seedOffset,
0200                                  unsigned int eventSeedOffset,
0201                                  std::vector<ModuleIDToEngine>& moduleIDVector);
0202 
0203       void resetEngineSeeds(LabelAndEngine& labelAndEngine,
0204                             std::string const& engineName,
0205                             VUint32 const& seeds,
0206                             std::uint32_t offset1,
0207                             std::uint32_t offset2);
0208 
0209       // ---------- member data --------------------------------
0210 
0211       unsigned int nStreams_;
0212 
0213       // This exists because we can look things up faster using the moduleID
0214       // than using string comparisons with the moduleLabel
0215       std::vector<std::vector<ModuleIDToEngine>> streamModuleIDToEngine_;  // streamID, sorted by moduleID
0216       std::vector<std::vector<ModuleIDToEngine>> lumiModuleIDToEngine_;    // luminosityBlockIndex, sortedByModuleID
0217 
0218       // Holds the engines, plus the seeds and module label also
0219       std::vector<std::vector<LabelAndEngine>> streamEngines_;  // streamID, sorted by label
0220       std::vector<std::vector<LabelAndEngine>> lumiEngines_;    // luminosityBlockIndex, sorted by label
0221 
0222       // These hold the input tags needed to retrieve the states
0223       // of the random number engines stored in a previous process.
0224       // If the label in the tag is the empty string (the default),
0225       // then the service does not try to restore the random numbers.
0226       edm::InputTag restoreStateTag_;
0227       edm::InputTag restoreStateBeginLumiTag_;
0228 
0229       std::vector<std::vector<RandomEngineState>> eventCache_;  // streamID, sorted by module label
0230       std::vector<std::vector<RandomEngineState>> lumiCache_;   // luminosityBlockIndex, sorted by module label
0231 
0232       // This is used to keep track of the seeds and engine name from
0233       // the configuration. The map key is the module label.
0234       // The module ID is filled in as modules are constructed.
0235       // It is left as max unsigned if the module is never constructed and not in the process
0236       class SeedsAndName {
0237       public:
0238         static constexpr unsigned int kInvalid = std::numeric_limits<unsigned int>::max();
0239 
0240         SeedsAndName(VUint32 const& theSeeds, std::string const& theEngineName)
0241             : seeds_(theSeeds), engineName_(theEngineName), moduleID_(kInvalid) {}
0242         VUint32 const& seeds() const { return seeds_; }
0243         std::string const& engineName() const { return engineName_; }
0244         unsigned int moduleID() const { return moduleID_; }
0245         void setModuleID(unsigned int v) { moduleID_ = v; }
0246 
0247       private:
0248         VUint32 seeds_;
0249         std::string engineName_;
0250         unsigned int moduleID_;
0251       };
0252       std::map<std::string, SeedsAndName> seedsAndNameMap_;
0253 
0254       // Keep the name of the file where we want to save the state
0255       // of all declared engines at the end of each event. A blank
0256       // name means don't bother.  Also, keep a record of whether
0257       // the save file name has been recorded in the job report.
0258       std::string saveFileName_;
0259       std::atomic<bool> saveFileNameRecorded_;
0260       std::vector<edm::propagate_const<std::shared_ptr<std::ofstream>>> outFiles_;  // streamID
0261 
0262       // Keep the name of the file from which we restore the state
0263       // of all declared engines at the beginning of a run. A
0264       // blank name means there isn't one.
0265       std::string restoreFileName_;
0266 
0267       // This turns on or off the checks that ensure no random
0268       // numbers are generated in a module during stream
0269       // beginStream, beginRun, endLuminosityBlock, or endRun.
0270       bool enableChecking_;
0271 
0272       std::uint32_t eventSeedOffset_;
0273 
0274       bool verbose_;
0275 
0276       static const std::vector<std::uint32_t>::size_type maxSeeds;
0277       static const std::vector<std::uint32_t>::size_type maxStates;
0278       static const std::uint32_t maxSeedRanecu;
0279       static const std::uint32_t maxSeedHepJames;
0280       static const std::uint32_t maxSeedTRandom3;
0281     };
0282   }  // namespace service
0283 }  // namespace edm
0284 #endif