StandardPolicies.h
Go to the documentation of this file.
1 /**
2  * @file StandardPolicies.h
3  * @brief Implementation of some standard and historical random seed assignment
4  * policies
5  * @author Gianluca Petrillo (petrillo@fnal.gov)
6  * @date 20150211
7  * @see SeedMaster.h
8  *
9  * No code in this files is directly serviceable.
10  * Documentation is up to date though.
11  */
12 
13 #ifndef NUTOOLS_RANDOMUTILS_PROVIDERS_STANDARDPOLICIES_H
14 #define NUTOOLS_RANDOMUTILS_PROVIDERS_STANDARDPOLICIES_H 1
15 
16 // C/C++ standard libraries
17 #include <string>
18 #include <ostream> // std::endl
19 
20 // From art and its tool chain
22 #include "fhiclcpp/ParameterSet.h"
23 
24 // Some helper classes
25 #include "nutools/RandomUtils/Providers/RandomSeedPolicyBase.h"
26 #include "nutools/RandomUtils/Providers/EngineId.h"
27 
28 
29 namespace rndm {
30 
31  namespace details {
32 
33  /** ************************************************************************
34  * @brief Implementation of the "autoIncrement" policy
35  * @see CheckedRangePolicy
36  *
37  * This is heavily based on CheckedRangePolicy.
38  */
39  template <typename SEED>
40  class AutoIncrementPolicy: public CheckedRangePolicy<SEED> {
41  public:
44  using seed_t = typename base_t::seed_t;
45 
46  /// Configures from a parameter set
47  /// @see configure()
49  base_t("autoIncrement")
50  { this_t::configure(pset); }
51 
52 
53  /**
54  * @brief Configure this policy
55  * @param pset the parameter set for the configuration
56  *
57  * Parameters:
58  * - *baseSeed* (unsigned integer): the first seed to be delivered
59  * - *checkRange* (boolean, default: true): whether to verify that each
60  * seed is within the expected range
61  * - *maxUniqueEngines* (unsigned integer, mandatory if /checkRange/ is
62  * true) the maximum number on seeds we expect to create
63  */
64  virtual void configure(fhicl::ParameterSet const& pset) override
65  {
67  ("maxUniqueEngines", "", "checkRange");
68  base_t::configure(pset);
69  static_configure(pset);
70  } // configure()
71 
72  /// Prints the configuration of this policy
73  virtual void print(std::ostream& out) const override;
74 
75  protected:
77  seed_t next_seed; ///< next seed delivered
78 
79  /// Returns the next random number
80  virtual seed_t createSeed(SeedMasterHelper::EngineId const&) override
81  { return next_seed++; }
82 
84  }; // class AutoIncrementPolicy<>
85 
86 
87  template <typename SEED>
89  (fhicl::ParameterSet const& pset)
90  {
91  first_seed = pset.get<seed_t>("baseSeed");
95  } // AutoIncrementPolicy<SEED>::configure()
96 
97 
98  template <typename SEED>
99  void AutoIncrementPolicy<SEED>::print(std::ostream& out) const {
100  base_t::print(out);
101  out << "\n first seed: " << first_seed;
102  } // AutoIncrementPolicy<SEED>::print()
103 
104 
105  /** ************************************************************************
106  * @brief Implementation of the "linearMapping" policy
107  * @see CheckedRangePolicy
108  *
109  * This is heavily based on CheckedRangePolicy.
110  */
111  template <typename SEED>
112  class LinearMappingPolicy: public CheckedRangePolicy<SEED> {
113  public:
116  using seed_t = typename base_t::seed_t;
117 
118  /// Configures from a parameter set
119  /// @see configure()
121  base_t("linearMapping")
122  { this_t::configure(pset); }
123 
124 
125  /**
126  * @brief Configure this policy
127  * @param pset the parameter set for the configuration
128  *
129  * Parameters:
130  * - *nJob* (unsigned integer): the number of this job; the first seed
131  * - *checkRange* (boolean, default: true): whether to verify that each
132  * seed is within the expected range
133  * - *maxUniqueEngines* (unsigned integer, mandatory) the maximum number
134  * on seeds we expect to create
135  */
136  virtual void configure(fhicl::ParameterSet const& pset) override
137  {
138  base_t::range_check.SetConfigLabels("", "", "checkRange");
139  base_t::configure(pset);
140  static_configure(pset);
141  }
142 
143  /// Prints the configuration of this policy
144  virtual void print(std::ostream& out) const override;
145 
146  protected:
147  seed_t first_seed; ///< base seed
148  seed_t next_seed; ///< next seed delivered
149  unsigned int nSeedsPerJob;
150 
151  /// Returns the next random number
153  { return next_seed++; }
154 
155  void static_configure(fhicl::ParameterSet const& pset);
156 
157  }; // class LinearMappingPolicy<>
158 
159 
160  template <typename SEED>
162  (fhicl::ParameterSet const& pset)
163  {
164  // this code is for legacy support, and it could disappear in the future
165  if (!pset.get_if_present<seed_t>("nJob", first_seed)) {
166  if (!pset.get_if_present<seed_t>("baseSeed", first_seed)) {
167  // this is going to fail; I am doing this just to get
168  // the more appropriate error message possible
169  first_seed = pset.get<seed_t>("nJob");
170  }
171  else {
172  mf::LogWarning("SeedMaster") <<
173  std::string(80, '*') <<
174  "\nDEPRECATION WARNING: 'baseSeed' parameter has been deprecated"
175  " for linearMapping policy, in favour of 'nJob'."
176  "\nPlease update your configuration accordingly."
177  << "\n" << std::string(80, '*');
178  }
179  }
180  // first_seed = pset.get<seed_t>("nJob");
181  nSeedsPerJob = pset.get<seed_t>("maxUniqueEngines");
182  first_seed *= nSeedsPerJob;
183  ++first_seed; // we don't want 0 as a seed
186  base_t::range_check.SetNSeeds(nSeedsPerJob);
188  } // LinearMappingPolicy<SEED>::configure()
189 
190 
191  template <typename SEED>
192  void LinearMappingPolicy<SEED>::print(std::ostream& out) const {
193  base_t::print(out);
194  out
195  << "\n first seed: " << first_seed
196  << "\n seeds per job: " << nSeedsPerJob;
197  } // LinearMappingPolicy<SEED>::print()
198 
199 
200 
201  /** ************************************************************************
202  * @brief Implementation of the "preDefinedSeed" policy
203  *
204  */
205  template <typename SEED>
206  class PredefinedSeedPolicy: public PerInstancePolicy<SEED> {
207  public:
210  using seed_t = typename base_t::seed_t;
211 
212  /// Configures from a parameter set
213  /// @see configure()
215  base_t("preDefinedSeed")
216  { this_t::configure(pset); }
217 
218 
219  /**
220  * @brief Configure this policy
221  * @param pset the parameter set for the configuration
222  *
223  * Parameters: one entry per engine.
224  * The FHiCL grammar to specify the seeds takes two forms.
225  * If no instance name is given, the seed is given by:
226  *
227  * moduleLabel : seed
228  *
229  * When a module has multiple instances, the seeds are given by:
230  *
231  * moduleLabel : {
232  * instanceName1 : seed1
233  * instanceName2 : seed2
234  * }
235  *
236  */
237  virtual void configure(fhicl::ParameterSet const& pset) override
238  { base_t::configure(pset); static_configure(pset); }
239 
240  /// Prints the configuration of this policy
241  virtual void print(std::ostream& out) const override;
242 
243 
244  /// Returns whether the returned seed should be unique: for us it "no".
245  virtual bool yieldsUniqueSeeds() const override { return false; }
246 
247  protected:
248 
249  /// Returns the seed stored in the parameter set
250  virtual seed_t createSeed(SeedMasterHelper::EngineId const& id) override
251  { return base_t::getInstanceSeed(id); }
252 
254  { base_t::range_check.SetCheck(false); }
255 
256  }; // class PredefinedSeedPolicy<>
257 
258 
259  template <typename SEED>
260  void PredefinedSeedPolicy<SEED>::print(std::ostream& out) const {
261  base_t::print(out);
262  out << "\n seeds directly from the configuration";
263  } // PredefinedSeedPolicy<SEED>::print()
264 
265 
266 
267  /** ************************************************************************
268  * @brief Implementation of the "preDefinedOffset" policy
269  * @see CheckedRangePolicy
270  *
271  * This is heavily based on CheckedRangePolicy.
272  */
273  template <typename SEED>
274  class PredefinedOffsetPolicy: public PerInstancePolicy<SEED> {
275  public:
278  using seed_t = typename base_t::seed_t;
279 
280  /// Configures from a parameter set
281  /// @see configure()
283  base_t("preDefinedOffset")
284  { this_t::configure(pset); }
285 
286 
287  /**
288  * @brief Configure this policy
289  * @param pset the parameter set for the configuration
290  *
291  * Parameters:
292  * - *baseSeed* (unsigned integer): the base seed
293  * - *checkRange* (boolean, default: true): whether to verify that each
294  * seed is within the expected range
295  * - *maxUniqueEngines* (unsigned integer, mandatory if /checkRange/ is
296  * true) the maximum number on seeds we expect to create
297  * - in addition, one entry per engine (see below)
298  *
299  * The FHiCL grammar to specify the offsets takes two forms.
300  * If no instance name is given, the offset is given by:
301  *
302  * moduleLabel : offset
303  *
304  * When a module has multiple instances, the offsets are given by:
305  *
306  * moduleLabel : {
307  * instanceName1 : offset1
308  * instanceName2 : offset2
309  * }
310  */
311  virtual void configure(fhicl::ParameterSet const& pset) override
312  {
314  ("maxUniqueEngines", "", "checkRange");
315  base_t::configure(pset);
316  static_configure(pset);
317  }
318 
319  /// Prints the configuration of this policy
320  virtual void print(std::ostream& out) const override;
321 
322  protected:
323  seed_t base_seed;
324 
325  /// Returns the seed stored in the parameter set
326  virtual seed_t createSeed(SeedMasterHelper::EngineId const& id) override
327  { return base_seed + base_t::getInstanceSeed(id); }
328 
330 
331  }; // class PredefinedOffsetPolicy<>
332 
333 
334  template <typename SEED>
336  (fhicl::ParameterSet const& pset)
337  {
338  base_seed = pset.get<seed_t>("baseSeed");
339  base_t::range_check.SetBaseSeed(base_seed);
341  } // PredefinedOffsetPolicy<SEED>::configure()
342 
343 
344  template <typename SEED>
345  void PredefinedOffsetPolicy<SEED>::print(std::ostream& out) const {
346  base_t::print(out);
347  out << "\n base seed: " << base_seed;
348  } // PredefinedOffsetPolicy<SEED>::print()
349 
350 
351  } // namespace details
352 
353 } // namespace rndm
354 
355 
356 #endif // NUTOOLS_RANDOMUTILS_PROVIDERS_STANDARDPOLICIES_H
Base class for policies reacting at engine instance level.
Definition: BasePolicies.h:510
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Returns the next random number.
SEED seed_t
type of the random seed
Definition: BasePolicies.h:45
virtual bool yieldsUniqueSeeds() const override
Returns whether the returned seed should be unique: for us it "no".
virtual seed_t createSeed(SeedMasterHelper::EngineId const &id) override
Returns the seed stored in the parameter set.
std::string string
Definition: nybbler.cc:12
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
Definition: BasePolicies.h:679
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
Definition: BasePolicies.h:764
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
void static_configure(fhicl::ParameterSet const &pset)
Definition: BasePolicies.h:465
Implementation of the "preDefinedOffset" policy.
Definition: BasePolicies.h:693
typename base_t::seed_t seed_t
Definition: BasePolicies.h:258
void static_configure(fhicl::ParameterSet const &)
void SetCheck(bool doCheck=true)
Sets whether to perform the check or not.
Definition: BasePolicies.h:124
virtual void print(std::ostream &out) const override
Prints information on the configuration of this policy.
Definition: BasePolicies.h:289
void SetNSeeds(seed_t nSeeds)
Sets the number of seeds directly.
Definition: BasePolicies.h:132
Interface for a policy implementation.
Definition: BasePolicies.h:43
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
Definition: BasePolicies.h:367
Implementation of the "autoIncrement" policy.
Definition: BasePolicies.h:343
T get(std::string const &key) const
Definition: ParameterSet.h:231
PredefinedOffsetPolicy(fhicl::ParameterSet const &pset)
Implementation of the "preDefinedSeed" policy.
Definition: BasePolicies.h:625
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
Definition: BasePolicies.h:277
RangeCheckHelper< seed_t > range_check
Definition: BasePolicies.h:296
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
seed_t next_seed
next seed delivered
Definition: BasePolicies.h:380
Implementation of the "linearMapping" policy.
Definition: BasePolicies.h:415
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Returns the next random number.
PredefinedSeedPolicy(fhicl::ParameterSet const &pset)
Range-checked policy (abstract)
Definition: BasePolicies.h:254
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
LinearMappingPolicy(fhicl::ParameterSet const &pset)
virtual seed_t createSeed(SeedMasterHelper::EngineId const &id) override
Returns the seed stored in the parameter set.
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
Definition: BasePolicies.h:495
void static_configure(fhicl::ParameterSet const &)
Definition: BasePolicies.h:392
void SetConfigLabels(std::string maxSeedsLabel="maxUniqueEngines", std::string baseSeedLabel="baseSeed", std::string checkRangeLabel="checkRange")
Definition: BasePolicies.h:177
void SetBaseSeed(seed_t base_seed)
Sets the base seed directly.
Definition: BasePolicies.h:128
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
Definition: BasePolicies.h:402
void CheckRangeConfiguration() const
Check that the configuration is complete.
Definition: BasePolicies.h:323
AutoIncrementPolicy(fhicl::ParameterSet const &pset)
void static_configure(fhicl::ParameterSet const &)
Definition: BasePolicies.h:755