RandomPolicy.h
Go to the documentation of this file.
1 /**
2  * @file RandomPolicy.h
3  * @brief Implementation of the random seed assignment policy "random"
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date 20150211
6  * @see SeedMaster.h
7  *
8  * No code in this files is directly serviceable.
9  * Documentation is up to date though.
10  */
11 
12 #ifndef NUTOOLS_RANDOMUTILS_PROVIDERS_RANDOMPOLICY_H
13 #define NUTOOLS_RANDOMUTILS_PROVIDERS_RANDOMPOLICY_H 1
14 
15 // C/C++ standard libraries
16 #include <ostream> // std::endl
17 #include <memory> // std::unique_ptr<>
18 #include <random> // std::uniform_int_distribution, std::default_random_engine
19 #include <chrono> // std::system_clock
20 
21 // From art and its tool chain
23 #include "fhiclcpp/ParameterSet.h"
24 
25 // Some helper classes
26 #include "nutools/RandomUtils/Providers/RandomSeedPolicyBase.h"
27 
28 
29 namespace rndm {
30 
31  namespace details {
32 
33  /** ************************************************************************
34  * @brief Implementation of the "random" policy
35  *
36  * This policy extracts seeds randomly. Each seed is between 1 and the
37  * maximum seed value specified at construction, both extremes included.
38  * The sequence of these seeds is initialized by a random seed.
39  * The random generator used is potentially low quality -- it does not
40  * matter for this application.
41  */
42  template <typename SEED>
43  class RandomPolicy: public RandomSeedPolicyBase<SEED> {
44  public:
47  using seed_t = typename base_t::seed_t;
48 
49  /// Configures from a parameter set
50  /// @see configure()
51  RandomPolicy(fhicl::ParameterSet const& pset): base_t("random")
52  { this_t::configure(pset); }
53 
54 
55  /**
56  * @brief Configure this policy
57  * @param pset the parameter set for the configuration
58  *
59  * Parameters:
60  * - *masterSeed* (unsigned integer, optional): the seed of the seed
61  * generator; by default, it's taken from the system clock
62  */
63  virtual void configure(fhicl::ParameterSet const& pset) override;
64 
65  /// Prints the details of the configuration of the random generator
66  virtual void print(std::ostream& out) const override;
67 
68 
69  private:
70  class RandomImpl {
71  public:
72  RandomImpl(seed_t master_seed, seed_t min_seed, seed_t max_seed):
73  seed(master_seed),
74  generator(master_seed), distribution(min_seed, max_seed)
75  {}
76 
77  seed_t master_seed() const { return seed; }
78  seed_t min() const { return distribution.min(); }
79  seed_t max() const { return distribution.max(); }
81 
82  private:
83  seed_t seed; ///< seed given at construction, for the record
84  std::default_random_engine generator; ///< random engine
85  std::uniform_int_distribution<seed_t> distribution; ///< flat
86  }; // RandomImpl
87 
88  std::unique_ptr<RandomImpl> random_seed;
89 
90  /// Extracts a random seed
91  virtual seed_t createSeed(SeedMasterHelper::EngineId const&) override
92  { return (*random_seed)(); }
93 
94  }; // class RandomPolicy<>
95 
96 
97  template <typename SEED>
99  constexpr seed_t MagicMaxSeed = 900000000;
100  seed_t master_seed = 0;
101  if (!pset.get_if_present("masterSeed", master_seed)) {
102  // get the base seed randomly too, from the clock,
103  // and within [1; MagicMaxSeed]
104  master_seed = 1 +
105  std::chrono::system_clock::now().time_since_epoch().count()
106  % MagicMaxSeed;
107  }
108  random_seed.reset(new RandomImpl(master_seed, 1, MagicMaxSeed));
109  } // RandomPolicy<SEED>::configure()
110 
111 
112  /// Prints the details of the configuration of the random generator
113  template <typename SEED>
114  void RandomPolicy<SEED>::print(std::ostream& out) const {
115  base_t::print(out);
116  out
117  << "\n master seed: " << random_seed->master_seed()
118  << "\n seed within: [ " << random_seed->min()
119  << " ; " << random_seed->max() << " ]"
120  ;
121  } // RandomPolicy<SEED>::print()
122 
123 
124  } // namespace details
125 
126 } // namespace rndm
127 
128 
129 #endif // NUTOOLS_RANDOMUTILS_PROVIDERS_RANDOMPOLICY_H
SEED seed_t
type of the random seed
Definition: BasePolicies.h:45
std::unique_ptr< RandomImpl > random_seed
Definition: BasePolicies.h:826
seed_t seed
seed given at construction, for the record
Definition: BasePolicies.h:821
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Extracts a random seed.
Definition: RandomPolicy.h:91
virtual void print(std::ostream &out) const
Prints information on the configuration of this policy.
Definition: BasePolicies.h:76
Implementation of the "random" policy.
Definition: BasePolicies.h:781
RandomImpl(seed_t master_seed, seed_t min_seed, seed_t max_seed)
Definition: RandomPolicy.h:72
Interface for a policy implementation.
Definition: BasePolicies.h:43
Identifier for a engine, made of module name and optional instance name.
Definition: EngineId.h:22
bool get_if_present(std::string const &key, T &value) const
Definition: ParameterSet.h:208
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
Definition: BasePolicies.h:836
std::uniform_int_distribution< seed_t > distribution
flat
Definition: BasePolicies.h:823
std::default_random_engine generator
random engine
Definition: BasePolicies.h:822
virtual void print(std::ostream &out) const override
Prints the details of the configuration of the random generator.
Definition: BasePolicies.h:852
RandomPolicy(fhicl::ParameterSet const &pset)
Definition: RandomPolicy.h:51